Skip to content

Commit

Permalink
Write the correct @generated annotation within auto-value.
Browse files Browse the repository at this point in the history
RELNOTES=Use `@javax.annotation.processing.Generated` for Java 9.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=179187847
  • Loading branch information
netdpb authored and ronshapiro committed Dec 15, 2017
1 parent e7748be commit f04406c
Show file tree
Hide file tree
Showing 10 changed files with 999 additions and 918 deletions.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package com.google.auto.value.extension.memoized;

import static com.google.auto.common.GeneratedAnnotationSpecs.generatedAnnotationSpec;
import static com.google.auto.common.MoreElements.isAnnotationPresent;
import static com.google.common.base.Predicates.equalTo;
import static com.google.common.base.Predicates.not;
Expand Down Expand Up @@ -121,7 +122,7 @@ String generate() {
.addTypeVariables(typeVariableNames())
.addModifiers(isFinal ? FINAL : ABSTRACT)
.addMethod(constructor());
generatedAnnotation().ifPresent(generated::addAnnotation);
generatedAnnotationSpec(elements, MemoizeExtension.class).ifPresent(generated::addAnnotation);
for (ExecutableElement method : memoizedMethods(context)) {
MethodOverrider methodOverrider = new MethodOverrider(method);
generated.addFields(methodOverrider.fields());
Expand All @@ -133,19 +134,6 @@ String generate() {
return JavaFile.builder(context.packageName(), generated.build()).build().toString();
}

private Optional<AnnotationSpec> generatedAnnotation() {
TypeElement generatedAnnotationElement =
elements.getTypeElement("javax.annotation.Generated");
if (generatedAnnotationElement == null) {
return Optional.empty();
}
ClassName generatedName = ClassName.get(generatedAnnotationElement);
return Optional.of(
AnnotationSpec.builder(generatedName)
.addMember("value", "$S", MemoizeExtension.class.getCanonicalName())
.build());
}

private TypeName superType() {
ClassName superType = ClassName.get(context.packageName(), classToExtend);
ImmutableList<TypeVariableName> typeVariableNames = typeVariableNames();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
*/
package com.google.auto.value.processor;

import static com.google.auto.common.GeneratedAnnotations.generatedAnnotation;

import com.google.auto.common.MoreElements;
import com.google.auto.common.SuperficialValidation;
import com.google.auto.service.AutoService;
Expand Down Expand Up @@ -177,12 +179,9 @@ private void processMethod(ExecutableElement method) {
}

private String getGeneratedTypeName() {
TypeElement generatedTypeElement =
processingEnv.getElementUtils().getTypeElement("javax.annotation.Generated");
if (generatedTypeElement == null) {
return "";
}
return TypeEncoder.encode(generatedTypeElement.asType());
return generatedAnnotation(processingEnv.getElementUtils())
.map(generatedAnnotation -> TypeEncoder.encode(generatedAnnotation.asType()))
.orElse(null);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package com.google.auto.value.processor;

import static com.google.auto.common.AnnotationMirrors.getAnnotationValue;
import static com.google.auto.common.GeneratedAnnotations.generatedAnnotation;
import static com.google.auto.common.MoreElements.getAnnotationMirror;
import static com.google.auto.common.MoreElements.getLocalAndInheritedMethods;
import static com.google.auto.common.MoreElements.getPackage;
Expand Down Expand Up @@ -819,14 +820,13 @@ private void defineVarsForType(
ImmutableSet<ExecutableElement> toBuilderMethods,
ImmutableSet<ExecutableElement> propertyMethods,
Optional<BuilderSpec.Builder> builder) {
TypeElement generatedTypeElement =
processingEnv.getElementUtils().getTypeElement("javax.annotation.Generated");
// We can't use ImmutableList.toImmutableList() for obscure Google-internal reasons.
vars.toBuilderMethods =
ImmutableList.copyOf(toBuilderMethods.stream().map(SimpleMethod::new).collect(toList()));
vars.generated = generatedTypeElement == null
? ""
: TypeEncoder.encode(generatedTypeElement.asType());
vars.generated =
generatedAnnotation(processingEnv.getElementUtils())
.map(annotation -> TypeEncoder.encode(annotation.asType()))
.orElse("");
ImmutableBiMap<ExecutableElement, String> methodToPropertyName =
propertyNameToMethodMap(propertyMethods).inverse();
vars.props = propertySet(type, propertyMethods);
Expand Down
Loading

0 comments on commit f04406c

Please sign in to comment.