Skip to content

Commit

Permalink
Ban usage of var in Component.
Browse files Browse the repository at this point in the history
RELNOTES=n/a
PiperOrigin-RevId: 560887022
  • Loading branch information
wanyingd1996 authored and Dagger Team committed Aug 29, 2023
1 parent 232e524 commit f5d686d
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 0 deletions.
13 changes: 13 additions & 0 deletions java/dagger/internal/codegen/validation/ComponentValidator.java
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ ValidationReport validateElement() {
// the remaining checks will likely just output unhelpful noise in such cases.
return report.addError(invalidTypeError(), component).build();
}
validateFields();
validateUseOfCancellationPolicy();
validateIsAbstractType();
validateCreators();
Expand Down Expand Up @@ -208,6 +209,18 @@ private String invalidTypeError() {
componentKind().annotation().simpleName());
}

private void validateFields() {
component.getDeclaredMethods().stream()
.filter(method -> method.isKotlinPropertySetter() && method.isAbstract())
.forEach(
method ->
report.addError(
String.format(
"Cannot use 'abstract var' property in a component declaration to get a"
+ " binding. Use 'val' or 'fun' instead: %s",
method.getPropertyName())));
}

private void validateCreators() {
ImmutableSet<XTypeElement> creators =
enclosedAnnotatedTypes(component, creatorAnnotationsFor(componentAnnotation()));
Expand Down
72 changes: 72 additions & 0 deletions javatests/dagger/internal/codegen/ComponentProcessorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2045,4 +2045,76 @@ public void injectedTypeHasGeneratedParam() {
subject.hasWarningCount(0);
});
}

@Test
public void abstractVarFieldInComponent_failsValidation() {
Source component =
CompilerTests.kotlinSource(
"test.TestComponent.kt",
"package test",
"",
"import dagger.Component",
"",
"@Component(modules = [TestModule::class])",
"interface TestComponent {",
" var foo: String",
"}");

Source module =
CompilerTests.javaSource(
"test.TestModule",
"package test;",
"",
"import dagger.Module;",
"import dagger.Provides;",
"",
"@Module",
"abstract class TestModule {",
" @Provides",
" static String provideString() { return \"hello\"; }",
"}");

CompilerTests.daggerCompiler(component, module)
.withProcessingOptions(compilerMode.processorOptions())
.compile(
subject -> {
subject.hasErrorCount(1);
subject.hasErrorContaining(
"Cannot use 'abstract var' property in a component declaration to get a binding."
+ " Use 'val' or 'fun' instead: foo");
});
}

@Test
public void nonAbstractVarFieldInComponent_passesValidation() {
Source component =
CompilerTests.kotlinSource(
"test.TestComponent.kt",
"package test",
"",
"import dagger.Component",
"",
"@Component(modules = [TestModule::class])",
"abstract class TestComponent {",
" var foo: String = \"hello\"",
"}");

Source module =
CompilerTests.javaSource(
"test.TestModule",
"package test;",
"",
"import dagger.Module;",
"import dagger.Provides;",
"",
"@Module",
"abstract class TestModule {",
" @Provides",
" static String provideString() { return \"hello\"; }",
"}");

CompilerTests.daggerCompiler(component, module)
.withProcessingOptions(compilerMode.processorOptions())
.compile(subject -> subject.hasErrorCount(0));
}
}

0 comments on commit f5d686d

Please sign in to comment.