Skip to content

Commit

Permalink
Remove dependency on JSR 305 annotations
Browse files Browse the repository at this point in the history
Fixes #880
Closes #882

RELNOTES=Remove dependency on JSR 305 annotations

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=178784068
  • Loading branch information
ronshapiro committed Dec 14, 2017
1 parent 4e74fce commit 02de197
Show file tree
Hide file tree
Showing 25 changed files with 130 additions and 94 deletions.
4 changes: 2 additions & 2 deletions WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,8 @@ maven_jar(

maven_jar(
name = "com_google_auto_auto_common",
artifact = "com.google.auto:auto-common:0.8",
sha1 = "c6f7af0e57b9d69d81b05434ef9f3c5610d498c4",
artifact = "com.google.auto:auto-common:0.9",
sha1 = "766dd79e7e81cfefec890ffd6d63aa2807538def",
)

maven_jar(
Expand Down
1 change: 0 additions & 1 deletion java/dagger/android/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ android_library(
"//:dagger_with_compiler",
"//third_party:auto_value",
"//third_party:error_prone_annotations",
"//third_party:jsr305_annotations",
"@androidsdk//com.android.support:support-annotations-25.0.0",
],
)
Expand Down
1 change: 0 additions & 1 deletion java/dagger/android/support/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ android_library(
"//:dagger_with_compiler",
"//java/dagger/android",
"//third_party:error_prone_annotations",
"//third_party:jsr305_annotations",
"@androidsdk//com.android.support:appcompat-v7-25.0.0",
"@androidsdk//com.android.support:support-annotations-25.0.0",
"@androidsdk//com.android.support:support-fragment-25.0.0",
Expand Down
1 change: 0 additions & 1 deletion java/dagger/grpc/server/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ java_library(
"//third_party:grpc_netty",
"//third_party:grpc_protobuf",
"//third_party:guava",
"//third_party:jsr305_annotations",
"//third_party:jsr330_inject",
"//third_party:protobuf",
],
Expand Down
1 change: 0 additions & 1 deletion java/dagger/internal/codegen/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ CODEGEN_SHARED_DEPS = [
"//third_party:javapoet",
"@local_jdk//:lib/tools.jar",
"//third_party:jsr250_annotations",
"//third_party:jsr305_annotations",
"//third_party:jsr330_inject",
"//java/dagger:core",
"//java/dagger/producers",
Expand Down
3 changes: 1 addition & 2 deletions java/dagger/internal/codegen/ForwardingNetwork.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import com.google.common.graph.Network;
import java.util.Optional;
import java.util.Set;
import javax.annotation.Nullable;

/** A {@link Network} that delegates all methods to another instance. */
// TODO(dpb): Move to com.google.common.graph.
Expand Down Expand Up @@ -148,7 +147,7 @@ public Optional<E> edgeConnecting(N nodeU, N nodeV) {
}

@SuppressWarnings("MissingOverride") // Until Guava 23.0
@Nullable
// @Nullable // TODO(ronshapiro): replace with the checker framework?
public E edgeConnectingOrNull(N nodeU, N nodeV) {
return delegate().edgeConnectingOrNull(nodeU, nodeV);
}
Expand Down
26 changes: 13 additions & 13 deletions java/dagger/internal/codegen/SourceFileGenerator.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.GeneratedAnnotations.generatedAnnotation;
import static com.google.common.base.Preconditions.checkNotNull;

import com.google.common.base.Throwables;
Expand All @@ -24,7 +25,6 @@
import com.squareup.javapoet.JavaFile;
import com.squareup.javapoet.TypeSpec;
import java.util.Optional;
import javax.annotation.Generated;
import javax.annotation.processing.Filer;
import javax.annotation.processing.Messager;
import javax.lang.model.element.Element;
Expand All @@ -40,18 +40,12 @@
abstract class SourceFileGenerator<T> {
private static final String GENERATED_COMMENTS = "https://google.github.io/dagger";

private static final AnnotationSpec GENERATED =
AnnotationSpec.builder(Generated.class)
.addMember("value", "$S", "dagger.internal.codegen.ComponentProcessor")
.addMember("comments", "$S", GENERATED_COMMENTS)
.build();

private final Filer filer;
private final boolean generatedAnnotationAvailable;
private final Elements elements;

SourceFileGenerator(Filer filer, Elements elements) {
this.filer = checkNotNull(filer);
generatedAnnotationAvailable = elements.getTypeElement("javax.annotation.Generated") != null;
this.elements = checkNotNull(elements);
}

/**
Expand Down Expand Up @@ -86,13 +80,19 @@ void generate(T input) throws SourceFileGenerationException {

private JavaFile buildJavaFile(
ClassName generatedTypeName, TypeSpec.Builder typeSpecBuilder) {
if (generatedAnnotationAvailable) {
typeSpecBuilder.addAnnotation(GENERATED);
}
Optional<AnnotationSpec> generatedAnnotation =
generatedAnnotation(elements)
.map(
annotation ->
AnnotationSpec.builder(ClassName.get(annotation))
.addMember("value", "$S", "dagger.internal.codegen.ComponentProcessor")
.addMember("comments", "$S", GENERATED_COMMENTS)
.build());
generatedAnnotation.ifPresent(typeSpecBuilder::addAnnotation);
JavaFile.Builder javaFileBuilder =
JavaFile.builder(generatedTypeName.packageName(), typeSpecBuilder.build())
.skipJavaLangImports(true);
if (!generatedAnnotationAvailable) {
if (!generatedAnnotation.isPresent()) {
javaFileBuilder.addFileComment("Generated by Dagger ($L).", GENERATED_COMMENTS);
}
return javaFileBuilder.build();
Expand Down
5 changes: 3 additions & 2 deletions javatests/dagger/internal/codegen/ComponentBuilderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import static com.google.testing.compile.CompilationSubject.assertThat;
import static dagger.internal.codegen.Compilers.daggerCompiler;
import static dagger.internal.codegen.GeneratedLines.GENERATED_ANNOTATION;
import static dagger.internal.codegen.GeneratedLines.IMPORT_GENERATED_ANNOTATION;

import com.google.testing.compile.Compilation;
import com.google.testing.compile.JavaFileObjects;
Expand Down Expand Up @@ -217,7 +218,7 @@ public void testIgnoresModulesNotInApi() {
"package test;",
"",
"import dagger.internal.Preconditions;",
"import javax.annotation.Generated;",
IMPORT_GENERATED_ANNOTATION,
"",
GENERATED_ANNOTATION,
"public final class DaggerTestComponent implements TestComponent {",
Expand Down Expand Up @@ -406,7 +407,7 @@ public void testBuilderBindsInstanceNoCreateGenerated() {
"package test;",
"",
"import dagger.internal.Preconditions;",
"import javax.annotation.Generated;",
IMPORT_GENERATED_ANNOTATION,
"",
GENERATED_ANNOTATION,
"public final class DaggerSimpleComponent implements SimpleComponent {",
Expand Down
17 changes: 9 additions & 8 deletions javatests/dagger/internal/codegen/ComponentProcessorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import static dagger.internal.codegen.CompilerMode.DEFAULT_MODE;
import static dagger.internal.codegen.CompilerMode.EXPERIMENTAL_ANDROID_MODE;
import static dagger.internal.codegen.GeneratedLines.GENERATED_ANNOTATION;
import static dagger.internal.codegen.GeneratedLines.IMPORT_GENERATED_ANNOTATION;

import com.google.auto.common.MoreElements;
import com.google.common.base.Joiner;
Expand Down Expand Up @@ -284,7 +285,7 @@ public void componentWithInvalidModule() {
"",
"import dagger.Lazy;",
"import dagger.internal.DoubleCheck;",
"import javax.annotation.Generated;",
IMPORT_GENERATED_ANNOTATION,
"import javax.inject.Provider;",
"",
GENERATED_ANNOTATION,
Expand Down Expand Up @@ -548,7 +549,7 @@ public void componentWithInvalidModule() {
"package test;",
"",
"import dagger.internal.Preconditions;",
"import javax.annotation.Generated;",
IMPORT_GENERATED_ANNOTATION,
"",
GENERATED_ANNOTATION,
"public final class DaggerTestComponent implements TestComponent {",
Expand Down Expand Up @@ -751,7 +752,7 @@ public void componentWithAbstractModule() {
"package test;",
"",
"import dagger.internal.Preconditions;",
"import javax.annotation.Generated;",
IMPORT_GENERATED_ANNOTATION,
"",
GENERATED_ANNOTATION,
"public final class DaggerTestComponent implements TestComponent {",
Expand Down Expand Up @@ -935,7 +936,7 @@ public void subcomponentNotGeneratedIfNotUsedInGraph() {
"package test;",
"",
"import dagger.internal.Preconditions;",
"import javax.annotation.Generated;",
IMPORT_GENERATED_ANNOTATION,
"",
GENERATED_ANNOTATION,
"public final class DaggerParent implements Parent {",
Expand Down Expand Up @@ -1466,7 +1467,7 @@ public void testDefaultPackage() {
"package test;",
"",
"import dagger.internal.Preconditions;",
"import javax.annotation.Generated;",
IMPORT_GENERATED_ANNOTATION,
"",
GENERATED_ANNOTATION,
"public final class DaggerBComponent implements BComponent {",
Expand Down Expand Up @@ -1554,7 +1555,7 @@ public void testDefaultPackage() {
.addLines(
"package test;",
"",
"import javax.annotation.Generated;",
IMPORT_GENERATED_ANNOTATION,
"",
GENERATED_ANNOTATION,
"public final class DaggerTestComponent implements TestComponent {",
Expand Down Expand Up @@ -1635,7 +1636,7 @@ public void testDefaultPackage() {
"test.DaggerSimpleComponent",
"package test;",
"",
"import javax.annotation.Generated;",
IMPORT_GENERATED_ANNOTATION,
"",
GENERATED_ANNOTATION,
"public final class DaggerSimpleComponent implements SimpleComponent {",
Expand Down Expand Up @@ -2103,7 +2104,7 @@ public void unusedSubcomponents_dontResolveExtraBindingsInParentComponents() {
"package test;",
"",
"import dagger.internal.Preconditions;",
"import javax.annotation.Generated;",
IMPORT_GENERATED_ANNOTATION,
"",
GENERATED_ANNOTATION,
"public final class DaggerParent implements Parent {",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import static dagger.internal.codegen.CompilerMode.EXPERIMENTAL_ANDROID_MODE;
import static dagger.internal.codegen.Compilers.daggerCompiler;
import static dagger.internal.codegen.GeneratedLines.GENERATED_ANNOTATION;
import static dagger.internal.codegen.GeneratedLines.IMPORT_GENERATED_ANNOTATION;

import com.google.testing.compile.Compilation;
import com.google.testing.compile.CompilationSubject;
Expand Down Expand Up @@ -553,7 +554,7 @@ public void castNeeded_rawTypes_Provider_get() {
"package test;",
"",
"import dagger.internal.DoubleCheck;",
"import javax.annotation.Generated;",
IMPORT_GENERATED_ANNOTATION,
"import javax.inject.Provider;",
"import other.Subtype_Factory;",
"import other.Supertype;",
Expand Down
11 changes: 6 additions & 5 deletions javatests/dagger/internal/codegen/ElidedFactoriesTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import static com.google.testing.compile.CompilationSubject.assertThat;
import static dagger.internal.codegen.Compilers.daggerCompiler;
import static dagger.internal.codegen.GeneratedLines.GENERATED_ANNOTATION;
import static dagger.internal.codegen.GeneratedLines.IMPORT_GENERATED_ANNOTATION;

import com.google.testing.compile.Compilation;
import com.google.testing.compile.JavaFileObjects;
Expand Down Expand Up @@ -81,7 +82,7 @@ public void simpleComponent() {
"test.DaggerSimpleComponent",
"package test;",
"",
"import javax.annotation.Generated;",
IMPORT_GENERATED_ANNOTATION,
"",
GENERATED_ANNOTATION,
"public final class DaggerSimpleComponent implements SimpleComponent {",
Expand Down Expand Up @@ -180,7 +181,7 @@ public void simpleComponent_injectsProviderOf_dependsOnScoped() {
"package test;",
"",
"import dagger.internal.MemoizedSentinel;",
"import javax.annotation.Generated;",
IMPORT_GENERATED_ANNOTATION,
"import javax.inject.Provider;",
"",
GENERATED_ANNOTATION,
Expand Down Expand Up @@ -244,7 +245,7 @@ public void simpleComponent_injectsProviderOf_dependsOnScoped() {
"package test;",
"",
"import dagger.internal.DoubleCheck;",
"import javax.annotation.Generated;",
IMPORT_GENERATED_ANNOTATION,
"import javax.inject.Provider;",
"",
GENERATED_ANNOTATION,
Expand Down Expand Up @@ -355,7 +356,7 @@ public void scopedBinding_onlyUsedInSubcomponent() {
"package test;",
"",
"import dagger.internal.MemoizedSentinel;",
"import javax.annotation.Generated;",
IMPORT_GENERATED_ANNOTATION,
"",
GENERATED_ANNOTATION,
"public final class DaggerSimpleComponent implements SimpleComponent {",
Expand Down Expand Up @@ -414,7 +415,7 @@ public void scopedBinding_onlyUsedInSubcomponent() {
"package test;",
"",
"import dagger.internal.DoubleCheck;",
"import javax.annotation.Generated;",
IMPORT_GENERATED_ANNOTATION,
"import javax.inject.Provider;",
"",
GENERATED_ANNOTATION,
Expand Down
20 changes: 17 additions & 3 deletions javatests/dagger/internal/codegen/GeneratedLines.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,23 @@
*/
public final class GeneratedLines {
public static final String GENERATED_ANNOTATION =
"@Generated("
+ "value = \"dagger.internal.codegen.ComponentProcessor\", "
+ "comments = \"https://google.github.io/dagger\")";
"@Generated("
+ "value = \"dagger.internal.codegen.ComponentProcessor\", "
+ "comments = \"https://google.github.io/dagger\")";

public static final String IMPORT_GENERATED_ANNOTATION =
isBeforeJava9()
? "import javax.annotation.Generated;"
: "import javax.annotation.processing.Generated;";

private static boolean isBeforeJava9() {
try {
Class.forName("java.lang.Module");
return false;
} catch (ClassNotFoundException e) {
return true;
}
}

public static final CodeBlock NPE_FROM_PROVIDES_METHOD =
stringLiteral(ErrorMessages.CANNOT_RETURN_NULL_FROM_NON_NULLABLE_PROVIDES_METHOD);
Expand Down
Loading

0 comments on commit 02de197

Please sign in to comment.