Skip to content

Commit

Permalink
Rewrite MemoizeExtension so that it functions even if javax.annotatio…
Browse files Browse the repository at this point in the history
…n.Generated is unavailable.

Fixes #503.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=168879489
  • Loading branch information
eamonnmcmanus authored and ronshapiro committed Sep 18, 2017
1 parent 021bf05 commit e478e72
Showing 1 changed file with 16 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
import com.google.auto.service.AutoService;
import com.google.auto.value.extension.AutoValueExtension;
import com.google.common.base.Joiner;
import com.google.common.base.Optional;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import com.squareup.javapoet.AnnotationSpec;
Expand All @@ -52,7 +51,7 @@
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import javax.annotation.Generated;
import java.util.Optional;
import javax.annotation.processing.Messager;
import javax.lang.model.element.AnnotationMirror;
import javax.lang.model.element.ExecutableElement;
Expand All @@ -68,11 +67,6 @@ public final class MemoizeExtension extends AutoValueExtension {
private static final ImmutableSet<String> DO_NOT_PULL_DOWN_ANNOTATIONS =
ImmutableSet.of(Override.class.getCanonicalName(), Memoized.class.getCanonicalName());

private static final AnnotationSpec GENERATED =
AnnotationSpec.builder(Generated.class)
.addMember("value", "$S", MemoizeExtension.class.getCanonicalName())
.build();

private static final ClassName LAZY_INIT =
ClassName.get("com.google.errorprone.annotations.concurrent", "LazyInit");

Expand Down Expand Up @@ -123,9 +117,8 @@ String generate() {
.superclass(superType())
.addTypeVariables(typeVariableNames())
.addModifiers(isFinal ? FINAL : ABSTRACT)
.addAnnotation(GENERATED)
.addMethod(constructor());

generatedAnnotation().ifPresent(generated::addAnnotation);
for (ExecutableElement method : memoizedMethods(context)) {
MethodOverrider methodOverrider = new MethodOverrider(method);
generated.addFields(methodOverrider.fields());
Expand All @@ -137,6 +130,19 @@ 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 Expand Up @@ -360,7 +366,7 @@ CodeBlock setMemoized() {
/** Returns the errorprone {@code @LazyInit} annotation if it is found on the classpath. */
private static Optional<AnnotationSpec> getLazyInitAnnotation(Elements elements) {
if (elements.getTypeElement(LAZY_INIT.toString()) == null) {
return Optional.absent();
return Optional.empty();
}
return Optional.of(AnnotationSpec.builder(LAZY_INIT).build());
}
Expand Down

0 comments on commit e478e72

Please sign in to comment.