Skip to content

Fix: properly detect the @Generated annotation on JDK9 to avoid package splits #882

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion java/dagger/internal/codegen/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ CODEGEN_SHARED_DEPS = [
"//third_party:google_java_format",
"//third_party:javapoet",
"@local_jdk//:lib/tools.jar",
"//third_party:jsr250_annotations",
"//third_party:jsr305_annotations",
"//third_party:jsr330_inject",
"//java/dagger:core",
Expand Down
47 changes: 37 additions & 10 deletions java/dagger/internal/codegen/SourceFileGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,34 +24,61 @@
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;
import javax.lang.model.element.TypeElement;
import javax.lang.model.util.Elements;

/**
* A template class that provides a framework for properly handling IO while generating source files
* from an annotation processor. Particularly, it makes a best effort to ensure that files that
* fail to write successfully are deleted.
*
* <p>In Java 9 and above, the decision, which 'Generated' annotation to use, depends on the used
* modules:
* <ul>
* <li>if <tt>java.compiler</tt> is present, the new official annotation is used,
* <li>if legacy <tt>java.xml.ws.annotation</tt> or <tt>jsr250</tt> are present (or we have JDK8), the legacy annotation is used,
* <li>otherwise, no annotation is used.
* </ul>
*
* @param <T> The input type from which source is to be generated.
*/
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", ComponentProcessor.class.getName())
.addMember("comments", "$S", GENERATED_COMMENTS)
.build();
private static final String GENERATED_ANNOTATION_JDK9 = "javax.annotation.processing.Generated";
private static final String GENERATED_ANNOTATION_LEGACY = "javax.annotation.Generated";

private final Filer filer;
private final boolean generatedAnnotationAvailable;

private final AnnotationSpec generated;

SourceFileGenerator(Filer filer, Elements elements) {
this.filer = checkNotNull(filer);
generatedAnnotationAvailable = elements.getTypeElement("javax.annotation.Generated") != null;
TypeElement generatedAnnotation = chooseAnnotation(elements);
if (null != generatedAnnotation) {
generated = createAnnotationSpec(generatedAnnotation);
generatedAnnotationAvailable = true;
} else {
generatedAnnotationAvailable = false;
generated = null;
}
}

TypeElement chooseAnnotation(Elements elements) {
TypeElement element = elements.getTypeElement(GENERATED_ANNOTATION_JDK9);
if (null == element) {
element = elements.getTypeElement(GENERATED_ANNOTATION_LEGACY);
}
return element;
}

AnnotationSpec createAnnotationSpec(TypeElement generatedAnnotation) {
return AnnotationSpec.builder(ClassName.get(generatedAnnotation))
.addMember("value", "$S", ComponentProcessor.class.getName())
.addMember("comments", "$S", GENERATED_COMMENTS)
.build();
}

/**
Expand Down Expand Up @@ -87,7 +114,7 @@ void generate(T input) throws SourceFileGenerationException {
private JavaFile buildJavaFile(
ClassName generatedTypeName, TypeSpec.Builder typeSpecBuilder) {
if (generatedAnnotationAvailable) {
typeSpecBuilder.addAnnotation(GENERATED);
typeSpecBuilder.addAnnotation(generated);
}
JavaFile.Builder javaFileBuilder =
JavaFile.builder(generatedTypeName.packageName(), typeSpecBuilder.build())
Expand Down
1 change: 0 additions & 1 deletion javatests/dagger/internal/codegen/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ GenJavaTests(
"//third_party:compile_testing",
"//third_party:guava",
"//third_party:javapoet",
"//third_party:jsr250_annotations",
"//third_party:jsr330_inject",
"//third_party:junit",
"//third_party:mockito",
Expand Down
1 change: 0 additions & 1 deletion tools/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ java_binary(
name = "jarjar",
main_class = "org.pantsbuild.jarjar.Main",
runtime_deps = [
"@javax_annotation_jsr250_api//jar",
"@javax_enterprise_cdi_api//jar",
"@javax_inject_javax_inject//jar",
"@org_apache_ant_ant//jar",
Expand Down