Skip to content

Commit

Permalink
Add an implementation of generatedAnnotation that uses the source ver…
Browse files Browse the repository at this point in the history
…sion instead of classpath introspection.

RELNOTES=N/A

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=181173931
  • Loading branch information
cushon authored and ronshapiro committed Jan 8, 2018
1 parent d5ca4ab commit 0383c1c
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import com.squareup.javapoet.AnnotationSpec;
import com.squareup.javapoet.ClassName;
import java.util.Optional;
import javax.lang.model.SourceVersion;
import javax.lang.model.util.Elements;

/** Utility methods for writing {@code @Generated} annotations using JavaPoet. */
Expand All @@ -29,7 +30,10 @@ private GeneratedAnnotationSpecs() {}
* Returns {@code @Generated("processorClass"} if either {@code
* javax.annotation.processing.Generated} or {@code javax.annotation.Generated} is {@linkplain
* GeneratedAnnotations#generatedAnnotation(Elements) available at compile time}.
*
* @deprecated prefer {@link #generatedAnnotationSpec(Elements, SourceVersion, Class<?>)}
*/
@Deprecated
public static Optional<AnnotationSpec> generatedAnnotationSpec(
Elements elements, Class<?> processorClass) {
return generatedAnnotationSpecBuilder(elements, processorClass)
Expand All @@ -40,7 +44,10 @@ public static Optional<AnnotationSpec> generatedAnnotationSpec(
* Returns {@code @Generated(value = "processorClass", comments = "comments"} if either {@code
* javax.annotation.processing.Generated} or {@code javax.annotation.Generated} is {@linkplain
* GeneratedAnnotations#generatedAnnotation(Elements) available at compile time}.
*
* @deprecated prefer {@link #generatedAnnotationSpec(Elements, SourceVersion, Class<?>, String)}
*/
@Deprecated
public static Optional<AnnotationSpec> generatedAnnotationSpec(
Elements elements, Class<?> processorClass, String comments) {
return generatedAnnotationSpecBuilder(elements, processorClass)
Expand All @@ -55,4 +62,40 @@ private static Optional<AnnotationSpec.Builder> generatedAnnotationSpecBuilder(
AnnotationSpec.builder(ClassName.get(generated))
.addMember("value", "$S", processorClass.getCanonicalName()));
}

/**
* Returns {@code @Generated("processorClass"} for the target {@code SourceVersion}.
*
* <p>Returns {@code javax.annotation.processing.Generated} for JDK 9 and newer, {@code
* javax.annotation.Generated} for earlier releases, and Optional#empty()} if the annotation is
* not available.
*/
public static Optional<AnnotationSpec> generatedAnnotationSpec(
Elements elements, SourceVersion sourceVersion, Class<?> processorClass) {
return generatedAnnotationSpecBuilder(elements, sourceVersion, processorClass)
.map(AnnotationSpec.Builder::build);
}

/**
* Returns {@code @Generated(value = "processorClass", comments = "comments"} for the target
* {@code SourceVersion}.
*
* <p>Returns {@code javax.annotation.processing.Generated} for JDK 9 and newer, {@code
* javax.annotation.Generated} for earlier releases, and Optional#empty()} if the annotation is
* not available.
*/
public static Optional<AnnotationSpec> generatedAnnotationSpec(
Elements elements, SourceVersion sourceVersion, Class<?> processorClass, String comments) {
return generatedAnnotationSpecBuilder(elements, sourceVersion, processorClass)
.map(annotation -> annotation.addMember("comments", "$S", comments).build());
}

private static Optional<AnnotationSpec.Builder> generatedAnnotationSpecBuilder(
Elements elements, SourceVersion sourceVersion, Class<?> processorClass) {
return GeneratedAnnotations.generatedAnnotation(elements, sourceVersion)
.map(
generated ->
AnnotationSpec.builder(ClassName.get(generated))
.addMember("value", "$S", processorClass.getCanonicalName()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package com.google.auto.common;

import java.util.Optional;
import javax.lang.model.SourceVersion;
import javax.lang.model.element.TypeElement;
import javax.lang.model.util.Elements;

Expand All @@ -30,12 +31,32 @@ private GeneratedAnnotations() {}
* <p>First looks for {@code javax.annotation.processing.Generated}, and then {@code
* javax.annotation.Generated}. Returns whichever is in the classpath (or modulepath), or {@link
* Optional#empty()} if neither is.
*
* @deprecated prefer {@link #generatedAnnotation(Elements, SourceVersion)}
*/
@Deprecated
public static Optional<TypeElement> generatedAnnotation(Elements elements) {
TypeElement jdk9Generated = elements.getTypeElement("javax.annotation.processing.Generated");
if (jdk9Generated != null) {
return Optional.of(jdk9Generated);
}
return Optional.ofNullable(elements.getTypeElement("javax.annotation.Generated"));
}

/**
* Returns the element corresponding to the {@code @Generated} annotation present at the target
* {@code SourceVersion}.
*
* <p>Returns {@code javax.annotation.processing.Generated} for JDK 9 and newer, {@code
* javax.annotation.Generated} for earlier releases, and Optional#empty()} if the annotation is
* not available.
*/
public static Optional<TypeElement> generatedAnnotation(
Elements elements, SourceVersion sourceVersion) {
return Optional.ofNullable(
elements.getTypeElement(
sourceVersion.compareTo(SourceVersion.RELEASE_8) > 0
? "javax.annotation.processing.Generated"
: "javax.annotation.Generated"));
}
}

0 comments on commit 0383c1c

Please sign in to comment.