Skip to content

Commit

Permalink
Don't allow scope annotations on modules. It has no function and lead…
Browse files Browse the repository at this point in the history
…s to user confusion.

Fixes #932

RELNOTES=Scopes are no longer allowed on @module elements. They never had a function and are now disallowed.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=182214078
  • Loading branch information
ronshapiro committed Jan 18, 2018
1 parent 3bff29f commit beeb29d
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
17 changes: 17 additions & 0 deletions java/dagger/internal/codegen/ModuleValidator.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package dagger.internal.codegen;

import static com.google.auto.common.AnnotationMirrors.getAnnotatedAnnotations;
import static com.google.auto.common.MoreElements.isAnnotationPresent;
import static com.google.auto.common.Visibility.PRIVATE;
import static com.google.auto.common.Visibility.PUBLIC;
Expand Down Expand Up @@ -71,6 +72,7 @@
import java.util.Optional;
import java.util.Set;
import javax.inject.Inject;
import javax.inject.Scope;
import javax.inject.Singleton;
import javax.lang.model.element.AnnotationMirror;
import javax.lang.model.element.AnnotationValue;
Expand Down Expand Up @@ -211,6 +213,7 @@ private ValidationReport<TypeElement> validateUncached(TypeElement module) {
validateModifiers(module, builder);
validateReferencedModules(module, moduleKind, builder);
validateReferencedSubcomponents(module, moduleKind, builder);
validateNoScopeAnnotationsOnModuleElement(module, moduleKind, builder);

return builder.build();
}
Expand Down Expand Up @@ -490,6 +493,20 @@ private void validateModuleVisibility(
}
}

private void validateNoScopeAnnotationsOnModuleElement(
TypeElement module,
ModuleDescriptor.Kind moduleKind,
ValidationReport.Builder<TypeElement> report) {
for (AnnotationMirror scope : getAnnotatedAnnotations(module, Scope.class)) {
report.addError(
String.format(
"@%ss cannot be scoped. Did you mean to scope a method instead?",
moduleKind.moduleAnnotation().getSimpleName()),
module,
scope);
}
}

private static String formatListForErrorMessage(List<?> things) {
switch (things.size()) {
case 0:
Expand Down
22 changes: 22 additions & 0 deletions javatests/dagger/internal/codegen/ModuleValidatorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import static dagger.internal.codegen.Compilers.daggerCompiler;
import static dagger.internal.codegen.DaggerModuleMethodSubject.Factory.assertThatModuleMethod;

import com.google.testing.compile.Compilation;
import com.google.testing.compile.JavaFileObjects;
import dagger.Module;
import dagger.producers.ProducerModule;
Expand Down Expand Up @@ -279,4 +280,25 @@ public void invalidIncludedModule() {
.inFile(module)
.onLine(5);
}

@Test
public void scopeOnModule() {
JavaFileObject badModule =
JavaFileObjects.forSourceLines(
"test.BadModule",
"package test;",
"",
"import dagger.Module;",
"import javax.inject.Singleton;",
"",
"@Singleton",
"@Module",
"interface BadModule {}");
Compilation compilation = daggerCompiler().compile(badModule);
assertThat(compilation).failed();
assertThat(compilation)
.hadErrorContaining("@Modules cannot be scoped")
.inFile(badModule)
.onLineContaining("@Singleton");
}
}

0 comments on commit beeb29d

Please sign in to comment.