Add an implementation of generatedAnnotation that uses the source version instead of classpath introspection.
RELNOTES=N/A
-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=181173931
diff --git a/common/src/main/java/com/google/auto/common/GeneratedAnnotationSpecs.java b/common/src/main/java/com/google/auto/common/GeneratedAnnotationSpecs.java
index a4694ef..024f9af 100644
--- a/common/src/main/java/com/google/auto/common/GeneratedAnnotationSpecs.java
+++ b/common/src/main/java/com/google/auto/common/GeneratedAnnotationSpecs.java
@@ -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. */
@@ -29,7 +30,10 @@
* 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)
@@ -40,7 +44,10 @@
* 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)
@@ -55,4 +62,40 @@
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()));
+ }
}
diff --git a/common/src/main/java/com/google/auto/common/GeneratedAnnotations.java b/common/src/main/java/com/google/auto/common/GeneratedAnnotations.java
index 1a5f089..fd6e9bb 100644
--- a/common/src/main/java/com/google/auto/common/GeneratedAnnotations.java
+++ b/common/src/main/java/com/google/auto/common/GeneratedAnnotations.java
@@ -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;
@@ -30,7 +31,10 @@
* <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) {
@@ -38,4 +42,21 @@
}
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"));
+ }
}