Skip to content
This repository has been archived by the owner on Oct 17, 2023. It is now read-only.

Commit

Permalink
Warn when assisted/provided params are not mixed
Browse files Browse the repository at this point in the history
In these cases, you can almost always use either direct construction or normal @Inject instead.
  • Loading branch information
JakeWharton committed Nov 25, 2018
1 parent 8fd343d commit 7240d1d
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ import javax.lang.model.type.ExecutableType
import javax.lang.model.util.Elements
import javax.lang.model.util.Types
import javax.tools.Diagnostic.Kind.ERROR
import javax.tools.Diagnostic.Kind.WARNING

@AutoService(Processor::class)
class AssistedInjectProcessor : AbstractProcessor() {
Expand Down Expand Up @@ -217,12 +218,12 @@ class AssistedInjectProcessor : AbstractProcessor() {
val requests = targetConstructor.parameters.map { it.asDependencyRequest() }
val (assistedRequests, providedRequests) = requests.partition { it.isAssisted }
if (assistedRequests.isEmpty()) {
error("Assisted injection requires at least one @Assisted parameter.", targetConstructor)
valid = false
warn("Assisted injection without at least one @Assisted parameter can use @Inject",
targetConstructor)
}
if (providedRequests.isEmpty()) {
error("Assisted injection requires at least one non-@Assisted parameter.", targetConstructor)
valid = false
warn("Assisted injection without at least one non-@Assisted parameter doesn't need a factory",
targetConstructor)
} else {
val providedDuplicates = providedRequests.groupBy { it.key }.filterValues { it.size > 1 }
if (providedDuplicates.isNotEmpty()) {
Expand Down Expand Up @@ -282,6 +283,10 @@ class AssistedInjectProcessor : AbstractProcessor() {
.writeTo(filer)
}

private fun warn(message: String, element: Element? = null) {
messager.printMessage(WARNING, message, element)
}

private fun error(message: String, element: Element? = null) {
messager.printMessage(ERROR, message, element)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -634,7 +634,7 @@ class AssistedInjectProcessorTest {
.generatesSources(expected)
}

@Test fun noAssistedParametersFails() {
@Test fun noAssistedParametersWarns() {
val input = JavaFileObjects.forSourceString("test.Test", """
package test;
Expand All @@ -652,15 +652,41 @@ class AssistedInjectProcessorTest {
}
""")

val expected = JavaFileObjects.forSourceString("test.Test_AssistedFactory", """
package test;
import java.lang.Override;
import java.lang.String;
import $GENERATED_TYPE;
import javax.inject.Inject;
import javax.inject.Provider;
$GENERATED_ANNOTATION
public final class Test_AssistedFactory implements Test.Factory {
private final Provider<String> foo;
@Inject public Test_AssistedFactory(Provider<String> foo) {
this.foo = foo;
}
@Override public Test create() {
return new Test(foo.get());
}
}
""")

assertAbout(javaSource())
.that(input)
.processedWith(AssistedInjectProcessor())
.failsToCompile()
.withErrorContaining("Assisted injection requires at least one @Assisted parameter")
.compilesWithoutError()
.and()
.generatesSources(expected)
.withWarningContaining(
"Assisted injection without at least one @Assisted parameter can use @Inject")
.`in`(input).onLine(9)
}

@Test fun allAssistedParametersFails() {
@Test fun allAssistedParametersWarns() {
val input = JavaFileObjects.forSourceString("test.Test", """
package test;
Expand All @@ -678,11 +704,33 @@ class AssistedInjectProcessorTest {
}
""")

val expected = JavaFileObjects.forSourceString("test.Test_AssistedFactory", """
package test;
import java.lang.Override;
import java.lang.String;
import $GENERATED_TYPE;
import javax.inject.Inject;
$GENERATED_ANNOTATION
public final class Test_AssistedFactory implements Test.Factory {
@Inject public Test_AssistedFactory() {
}
@Override public Test create(String foo) {
return new Test(foo);
}
}
""")

assertAbout(javaSource())
.that(input)
.processedWith(AssistedInjectProcessor())
.failsToCompile()
.withErrorContaining("Assisted injection requires at least one non-@Assisted parameter.")
.compilesWithoutError()
.and()
.generatesSources(expected)
.withWarningContaining(
"Assisted injection without at least one non-@Assisted parameter doesn't need a factory")
.`in`(input).onLine(9)
}

Expand Down

0 comments on commit 7240d1d

Please sign in to comment.