Skip to content

Commit 73e8eff

Browse files
Merge pull request #605 from google/sync-master-2018/03/08
Moe Sync
2 parents 1561e2c + b3abffe commit 73e8eff

File tree

7 files changed

+112
-94
lines changed

7 files changed

+112
-94
lines changed

value/src/main/java/com/google/auto/value/extension/memoized/MemoizedValidator.java

Lines changed: 0 additions & 89 deletions
This file was deleted.
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/*
2+
* Copyright (C) 2017 Google, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.google.auto.value.extension.memoized.processor;
17+
18+
/** Names of classes that are referenced in the processor/extension. */
19+
final class ClassNames {
20+
static final String MEMOIZED_NAME = "com.google.auto.value.extension.memoized.Memoized";
21+
}

value/src/main/java/com/google/auto/value/extension/memoized/MemoizeExtension.java renamed to value/src/main/java/com/google/auto/value/extension/memoized/processor/MemoizeExtension.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,11 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
package com.google.auto.value.extension.memoized;
16+
package com.google.auto.value.extension.memoized.processor;
1717

1818
import static com.google.auto.common.GeneratedAnnotationSpecs.generatedAnnotationSpec;
19-
import static com.google.auto.common.MoreElements.isAnnotationPresent;
19+
import static com.google.auto.value.extension.memoized.processor.ClassNames.MEMOIZED_NAME;
20+
import static com.google.auto.value.extension.memoized.processor.MemoizedValidator.getAnnotationMirror;
2021
import static com.google.common.base.Predicates.equalTo;
2122
import static com.google.common.base.Predicates.not;
2223
import static com.google.common.collect.Iterables.filter;
@@ -67,7 +68,7 @@
6768
@AutoService(AutoValueExtension.class)
6869
public final class MemoizeExtension extends AutoValueExtension {
6970
private static final ImmutableSet<String> DO_NOT_PULL_DOWN_ANNOTATIONS =
70-
ImmutableSet.of(Override.class.getCanonicalName(), Memoized.class.getCanonicalName());
71+
ImmutableSet.of(Override.class.getCanonicalName(), MEMOIZED_NAME);
7172

7273
private static final ClassName LAZY_INIT =
7374
ClassName.get("com.google.errorprone.annotations.concurrent", "LazyInit");
@@ -89,7 +90,7 @@ public String generateClass(
8990
private static ImmutableSet<ExecutableElement> memoizedMethods(Context context) {
9091
ImmutableSet.Builder<ExecutableElement> memoizedMethods = ImmutableSet.builder();
9192
for (ExecutableElement method : methodsIn(context.autoValueClass().getEnclosedElements())) {
92-
if (isAnnotationPresent(method, Memoized.class)) {
93+
if (getAnnotationMirror(method, MEMOIZED_NAME).isPresent()) {
9394
memoizedMethods.add(method);
9495
}
9596
}
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/*
2+
* Copyright (C) 2017 Google, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.google.auto.value.extension.memoized.processor;
17+
18+
import static com.google.auto.value.extension.memoized.processor.ClassNames.MEMOIZED_NAME;
19+
import static javax.lang.model.util.ElementFilter.methodsIn;
20+
import static javax.tools.Diagnostic.Kind.ERROR;
21+
22+
import com.google.auto.common.MoreTypes;
23+
import com.google.auto.service.AutoService;
24+
import java.util.Optional;
25+
import java.util.Set;
26+
import javax.annotation.processing.AbstractProcessor;
27+
import javax.annotation.processing.Messager;
28+
import javax.annotation.processing.Processor;
29+
import javax.annotation.processing.RoundEnvironment;
30+
import javax.annotation.processing.SupportedAnnotationTypes;
31+
import javax.lang.model.SourceVersion;
32+
import javax.lang.model.element.AnnotationMirror;
33+
import javax.lang.model.element.Element;
34+
import javax.lang.model.element.ExecutableElement;
35+
import javax.lang.model.element.TypeElement;
36+
37+
/**
38+
* An annotation {@link Processor} that reports errors for {@link Memoized @Memoized} methods that
39+
* are not inside {@code AutoValue}-annotated classes.
40+
*/
41+
@AutoService(Processor.class)
42+
@SupportedAnnotationTypes(MEMOIZED_NAME)
43+
public final class MemoizedValidator extends AbstractProcessor {
44+
@Override
45+
public final boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
46+
Messager messager = processingEnv.getMessager();
47+
TypeElement memoized = processingEnv.getElementUtils().getTypeElement(MEMOIZED_NAME);
48+
for (ExecutableElement method : methodsIn(roundEnv.getElementsAnnotatedWith(memoized))) {
49+
if (!isAutoValue(method.getEnclosingElement())) {
50+
messager.printMessage(
51+
ERROR,
52+
"@Memoized methods must be declared only in @AutoValue classes",
53+
method,
54+
getAnnotationMirror(method, MEMOIZED_NAME).get());
55+
}
56+
}
57+
return false;
58+
}
59+
60+
@Override
61+
public SourceVersion getSupportedSourceVersion() {
62+
return SourceVersion.latestSupported();
63+
}
64+
65+
private static boolean isAutoValue(Element element) {
66+
return element
67+
.getAnnotationMirrors()
68+
.stream()
69+
.map(annotation -> MoreTypes.asTypeElement(annotation.getAnnotationType()))
70+
.anyMatch(type -> type.getQualifiedName().contentEquals("com.google.auto.value.AutoValue"));
71+
}
72+
73+
static Optional<AnnotationMirror> getAnnotationMirror(Element element, String annotationName) {
74+
for (AnnotationMirror annotation : element.getAnnotationMirrors()) {
75+
TypeElement annotationElement = MoreTypes.asTypeElement(annotation.getAnnotationType());
76+
if (annotationElement.getQualifiedName().contentEquals(annotationName)) {
77+
return Optional.of(annotation);
78+
}
79+
}
80+
return Optional.empty();
81+
}
82+
}

value/src/main/java/com/google/auto/value/processor/AutoValueOrOneOfProcessor.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -725,6 +725,7 @@ static String wildcardTypeParametersString(TypeElement type) {
725725
}
726726
}
727727

728+
// TODO(emcmanus,ronshapiro): move to auto-common
728729
static Optional<AnnotationMirror> getAnnotationMirror(Element element, String annotationName) {
729730
for (AnnotationMirror annotation : element.getAnnotationMirrors()) {
730731
TypeElement annotationElement = MoreTypes.asTypeElement(annotation.getAnnotationType());

value/src/test/java/com/google/auto/value/extension/memoized/MemoizedMethodSubject.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import static com.google.common.truth.Truth.assertAbout;
2020
import static com.google.testing.compile.JavaSourceSubjectFactory.javaSource;
2121

22+
import com.google.auto.value.extension.memoized.processor.MemoizeExtension;
2223
import com.google.auto.value.processor.AutoValueProcessor;
2324
import com.google.common.collect.ImmutableList;
2425
import com.google.common.truth.FailureMetadata;
@@ -51,4 +52,4 @@ void hasError(String error) {
5152
.in(file)
5253
.onLine(6);
5354
}
54-
}
55+
}

value/src/test/java/com/google/auto/value/extension/memoized/MemoizedValidationTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import static com.google.testing.compile.CompilationSubject.assertThat;
2020
import static com.google.testing.compile.Compiler.javac;
2121

22+
import com.google.auto.value.extension.memoized.processor.MemoizedValidator;
2223
import com.google.testing.compile.Compilation;
2324
import com.google.testing.compile.JavaFileObjects;
2425
import javax.tools.JavaFileObject;

0 commit comments

Comments
 (0)