Skip to content

Commit

Permalink
Ignore abstract methods when transforming @AndroidEntryPoint-annotate…
Browse files Browse the repository at this point in the history
…d classes.

Abstract methods contain no code attribute and therefore don't have any
'super.something()' calls that need to be transformed.

Fixes #1955

RELNOTES=Fix a bug where Hilt would incorrectly try to transform abstract methods on abstract classes annnotated with @androidentrypoint.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=320233798
  • Loading branch information
danysantiago authored and kevinb9n committed Jul 9, 2020
1 parent 9c9d3a3 commit 884fee5
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,11 @@ internal class AndroidEntryPointClassTransformer(
) {
val constantPool = clazz.classFile.constPool
clazz.declaredMethods
.filter { it.methodInfo.isMethod && !Modifier.isStatic(it.modifiers) }
.filter {
it.methodInfo.isMethod &&
!Modifier.isStatic(it.modifiers) &&
!Modifier.isAbstract(it.modifiers)
}
.forEach { method ->
val codeAttr = method.methodInfo.codeAttribute
val code = codeAttr.code
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,42 @@ class HiltGradlePluginTest {
}
}

// Verify transformation ignores abstract methods.
@Test
fun testAssemble_abstractMethod() {
gradleTransformRunner.addDependencies(
"implementation 'androidx.appcompat:appcompat:1.1.0'",
"implementation 'com.google.dagger:hilt-android:LOCAL-SNAPSHOT'",
"annotationProcessor 'com.google.dagger:hilt-android-compiler:LOCAL-SNAPSHOT'"
)

gradleTransformRunner.addSrc(
srcPath = "minimal/AbstractActivity.java",
srcContent =
"""
package minimal;
import androidx.appcompat.app.AppCompatActivity;
@dagger.hilt.android.AndroidEntryPoint
public abstract class AbstractActivity extends AppCompatActivity {
public abstract void method();
}
""".trimIndent()
)

val result = gradleTransformRunner.build()
val assembleTask = result.getTask(":assembleDebug")
assertEquals(TaskOutcome.SUCCESS, assembleTask.outcome)

val transformedClass = result.getTransformedFile("minimal/AbstractActivity.class")
FileInputStream(transformedClass).use { fileInput ->
ClassFile(DataInputStream(fileInput)).let { classFile ->
assertEquals("minimal.Hilt_AbstractActivity", classFile.superclass)
}
}
}

// Verify plugin configuration fails when runtime dependency is missing but plugin is applied.
@Test
fun testAssemble_missingLibraryDep() {
Expand Down

0 comments on commit 884fee5

Please sign in to comment.