Skip to content

Commit

Permalink
Fix issue with Dagger 2.29 which generates methods with variant names…
Browse files Browse the repository at this point in the history
… in Android projects
  • Loading branch information
thsaravana committed Feb 15, 2021
1 parent bb3d845 commit 2040ba6
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 29 deletions.
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ plugins {
}

group 'com.madrapps'
version '0.3.2020.2.1'
version '0.3.2020.2.2'

jacoco {
toolVersion '0.8.6'
Expand Down Expand Up @@ -59,7 +59,7 @@ compileTestKotlin {

patchPluginXml {
changeNotes """
- Show basic dagger validation errors in the editor for @Inject, @Binds and @Provides <br/>
- Fix issue with Dagger 2.29 which generates methods with variant names in Android projects<br/>
"""
}

Expand Down
54 changes: 29 additions & 25 deletions src/main/kotlin/com/madrapps/dagger/core/SpiPlugin.kt
Original file line number Diff line number Diff line change
Expand Up @@ -127,34 +127,38 @@ class SpiPlugin(private val project: Project) : BindingGraphPlugin {
name = requestElement().orNull()?.name() ?: (psiElement as PsiParameter).type.presentableText
}
is ExecutableElement -> {
psiElement = element.toPsiMethod(project)!!
var temp = requestElement().orNull()?.name() ?: if ((psiElement as PsiMethod).isConstructor) {
psiElement.name
} else {
psiElement.returnType?.presentableText ?: "NULL"
}
if (parentBinding?.kind() == BindingKind.MULTIBOUND_MAP) {
val find = element.annotationMirrors.find {
it.isStandardKey || (it.annotationType as DeclaredType).asElement().annotationMirrors.find { it.isMapKey } != null
psiElement = element.toPsiMethod(project) ?: element.getClass().toPsiClass(project)!!
if (psiElement is PsiMethod) {
var temp = requestElement().orNull()?.name() ?: if ((psiElement as PsiMethod).isConstructor) {
psiElement.name
} else {
psiElement.returnType?.presentableText ?: "NULL"
}
val snd = find?.elementValues?.values?.first()
val sndText = if (snd != null) {
val value = snd.value
when (value) {
is VariableElement -> "${(value.asType() as DeclaredType).asElement().simpleName}.${value.simpleName}"
is DeclaredType -> value.asElement().simpleName.toString()
else -> value.toString()
if (parentBinding?.kind() == BindingKind.MULTIBOUND_MAP) {
val find = element.annotationMirrors.find {
it.isStandardKey || (it.annotationType as DeclaredType).asElement().annotationMirrors.find { it.isMapKey } != null
}
} else "Object"
temp += " [$sndText]"
}
val snd = element.annotationMirrors.find {
(it.annotationType as DeclaredType).asElement().annotationMirrors.find { it.isQualifier } != null
}
if (snd != null) {
temp += "[${snd.annotationType.asElement().simpleName}]"
val snd = find?.elementValues?.values?.first()
val sndText = if (snd != null) {
val value = snd.value
when (value) {
is VariableElement -> "${(value.asType() as DeclaredType).asElement().simpleName}.${value.simpleName}"
is DeclaredType -> value.asElement().simpleName.toString()
else -> value.toString()
}
} else "Object"
temp += " [$sndText]"
}
val snd = element.annotationMirrors.find {
(it.annotationType as DeclaredType).asElement().annotationMirrors.find { it.isQualifier } != null
}
if (snd != null) {
temp += "[${snd.annotationType.asElement().simpleName}]"
}
name = temp
} else {
name = "[UNKNOWN]"
}
name = temp
}
else -> return null
}
Expand Down
18 changes: 16 additions & 2 deletions src/main/kotlin/com/madrapps/dagger/utils/Utils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import com.intellij.openapi.vfs.VirtualFile
import com.intellij.psi.*
import com.intellij.psi.impl.light.LightMethodBuilder
import com.intellij.psi.util.ClassUtil
import com.madrapps.dagger.services.log
import dagger.MapKey
import dagger.model.BindingGraph.ComponentNode
import dagger.model.DependencyRequest
Expand Down Expand Up @@ -102,15 +103,28 @@ fun ExecutableElement.toPsiMethod(project: Project): PsiMethod? {
isConstructor = true
psiClass.name
} else {
simpleName.toString()
val name = simpleName.toString()
if (name.contains("$")) {
val firstName = name.substringBefore("$")
psiClass.methods.find { it.name.startsWith("$firstName$") }?.name ?: firstName
} else {
name
}
}
val patternMethod = LightMethodBuilder(psiManager, methodName)
.setMethodReturnType(returnType.toString())
.setConstructor(isConstructor)
parameters.forEach {
patternMethod.addParameter(it.simpleName.toString(), it.asType().toString())
}
return psiClass.findMethodBySignature(patternMethod, false)
val psiMethod = psiClass.findMethodBySignature(patternMethod, false)
if (psiMethod == null) {
project.log("Method not found: $patternMethod")
psiClass.methods.forEach {
project.log("-- All Methods: ${it.name}")
}
}
return psiMethod
}
return null
}
Expand Down

0 comments on commit 2040ba6

Please sign in to comment.