-
Notifications
You must be signed in to change notification settings - Fork 750
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce
com.google.errorprone.annotations.Modifier
for use in `@IncompatibleModifiers` and `@RequiredModifiers`, as an alternative to `javax.lang.model.element.Modifier` which is not available at compile-time on Android. #2122 PiperOrigin-RevId: 375137408
- Loading branch information
Showing
8 changed files
with
148 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
annotations/src/main/java/com/google/errorprone/annotations/Modifier.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/* | ||
* Copyright 2021 The Error Prone Authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.google.errorprone.annotations; | ||
|
||
/** | ||
* Modifiers in the Java language, as specified in: | ||
* | ||
* <ul> | ||
* <li>https://docs.oracle.com/javase/specs/jls/se11/html/jls-8.html#jls-8.1.1 | ||
* <li>https://docs.oracle.com/javase/specs/jls/se11/html/jls-8.html#jls-8.3.1 | ||
* <li>https://docs.oracle.com/javase/specs/jls/se11/html/jls-8.html#jls-8.4.3 | ||
* <li>https://docs.oracle.com/javase/specs/jls/se11/html/jls-9.html#jls-9.4 | ||
* </ul> | ||
*/ | ||
public enum Modifier { | ||
PUBLIC, | ||
PROTECTED, | ||
PRIVATE, | ||
ABSTRACT, | ||
DEFAULT, | ||
STATIC, | ||
FINAL, | ||
TRANSIENT, | ||
VOLATILE, | ||
SYNCHRONIZED, | ||
NATIVE, | ||
STRICTFP | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,21 +19,28 @@ | |
import static com.google.errorprone.BugPattern.LinkType.NONE; | ||
import static com.google.errorprone.BugPattern.SeverityLevel.ERROR; | ||
import static com.google.errorprone.matchers.Description.NO_MATCH; | ||
import static com.google.errorprone.util.MoreAnnotations.getValue; | ||
|
||
import com.google.common.collect.ImmutableSet; | ||
import com.google.common.collect.Sets; | ||
import com.google.errorprone.BugPattern; | ||
import com.google.errorprone.VisitorState; | ||
import com.google.errorprone.annotations.IncompatibleModifiers; | ||
import com.google.errorprone.bugpatterns.BugChecker.AnnotationTreeMatcher; | ||
import com.google.errorprone.fixes.SuggestedFixes; | ||
import com.google.errorprone.matchers.Description; | ||
import com.google.errorprone.util.ASTHelpers; | ||
import com.sun.source.tree.AnnotationTree; | ||
import com.sun.source.tree.ModifiersTree; | ||
import com.sun.source.tree.Tree; | ||
import com.sun.tools.javac.code.Attribute; | ||
import com.sun.tools.javac.code.Symbol; | ||
import java.util.Collection; | ||
import java.util.LinkedHashSet; | ||
import java.util.List; | ||
import java.util.Set; | ||
import javax.lang.model.element.AnnotationValue; | ||
import javax.lang.model.element.Modifier; | ||
import javax.lang.model.element.VariableElement; | ||
import javax.lang.model.util.SimpleAnnotationValueVisitor8; | ||
|
||
/** @author [email protected] (Steven Goldfeder) */ | ||
@BugPattern( | ||
|
@@ -45,15 +52,28 @@ | |
severity = ERROR) | ||
public class IncompatibleModifiersChecker extends BugChecker implements AnnotationTreeMatcher { | ||
|
||
private static final String INCOMPATIBLE_MODIFIERS = | ||
"com.google.errorprone.annotations.IncompatibleModifiers"; | ||
|
||
@Override | ||
public Description matchAnnotation(AnnotationTree tree, VisitorState state) { | ||
IncompatibleModifiers annotation = ASTHelpers.getAnnotation(tree, IncompatibleModifiers.class); | ||
Symbol sym = ASTHelpers.getSymbol(tree); | ||
if (sym == null) { | ||
return NO_MATCH; | ||
} | ||
Attribute.Compound annotation = | ||
sym.getRawAttributes().stream() | ||
.filter(a -> a.type.tsym.getQualifiedName().contentEquals(INCOMPATIBLE_MODIFIERS)) | ||
.findAny() | ||
.orElse(null); | ||
if (annotation == null) { | ||
return NO_MATCH; | ||
} | ||
ImmutableSet<Modifier> incompatibleModifiers = ImmutableSet.copyOf(annotation.value()); | ||
Set<Modifier> incompatibleModifiers = new LinkedHashSet<>(); | ||
getValue(annotation, "value").ifPresent(a -> getModifiers(incompatibleModifiers, a)); | ||
getValue(annotation, "modifier").ifPresent(a -> getModifiers(incompatibleModifiers, a)); | ||
if (incompatibleModifiers.isEmpty()) { | ||
return Description.NO_MATCH; | ||
return NO_MATCH; | ||
} | ||
|
||
Tree parent = state.getPath().getParentPath().getLeaf(); | ||
|
@@ -83,4 +103,21 @@ public Description matchAnnotation(AnnotationTree tree, VisitorState state) { | |
.setMessage(message) | ||
.build(); | ||
} | ||
|
||
private static void getModifiers(Collection<Modifier> modifiers, Attribute attribute) { | ||
class Visitor extends SimpleAnnotationValueVisitor8<Void, Void> { | ||
@Override | ||
public Void visitEnumConstant(VariableElement c, Void unused) { | ||
modifiers.add(Modifier.valueOf(c.getSimpleName().toString())); | ||
return null; | ||
} | ||
|
||
@Override | ||
public Void visitArray(List<? extends AnnotationValue> vals, Void unused) { | ||
vals.forEach(val -> val.accept(this, null)); | ||
return null; | ||
} | ||
} | ||
attribute.accept(new Visitor(), null); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,21 +18,29 @@ | |
|
||
import static com.google.errorprone.BugPattern.LinkType.NONE; | ||
import static com.google.errorprone.BugPattern.SeverityLevel.ERROR; | ||
import static com.google.errorprone.matchers.Description.NO_MATCH; | ||
import static com.google.errorprone.util.MoreAnnotations.getValue; | ||
|
||
import com.google.common.collect.ImmutableSet; | ||
import com.google.common.collect.Sets; | ||
import com.google.errorprone.BugPattern; | ||
import com.google.errorprone.VisitorState; | ||
import com.google.errorprone.annotations.RequiredModifiers; | ||
import com.google.errorprone.bugpatterns.BugChecker.AnnotationTreeMatcher; | ||
import com.google.errorprone.fixes.SuggestedFixes; | ||
import com.google.errorprone.matchers.Description; | ||
import com.google.errorprone.util.ASTHelpers; | ||
import com.sun.source.tree.AnnotationTree; | ||
import com.sun.source.tree.ModifiersTree; | ||
import com.sun.source.tree.Tree; | ||
import com.sun.tools.javac.code.Attribute; | ||
import com.sun.tools.javac.code.Symbol; | ||
import java.util.Collection; | ||
import java.util.LinkedHashSet; | ||
import java.util.List; | ||
import java.util.Set; | ||
import javax.lang.model.element.AnnotationValue; | ||
import javax.lang.model.element.Modifier; | ||
import javax.lang.model.element.VariableElement; | ||
import javax.lang.model.util.SimpleAnnotationValueVisitor8; | ||
|
||
/** @author [email protected] (Steven Goldfeder) */ | ||
@BugPattern( | ||
|
@@ -46,28 +54,40 @@ public class RequiredModifiersChecker extends BugChecker implements AnnotationTr | |
|
||
private static final String MESSAGE_TEMPLATE = | ||
"%s has specified that it must be used together with the following modifiers: %s"; | ||
private static final String REQUIRED_MODIFIERS = | ||
"com.google.errorprone.annotations.RequiredModifiers"; | ||
|
||
@Override | ||
public Description matchAnnotation(AnnotationTree tree, VisitorState state) { | ||
RequiredModifiers annotation = ASTHelpers.getAnnotation(tree, RequiredModifiers.class); | ||
Symbol sym = ASTHelpers.getSymbol(tree); | ||
if (sym == null) { | ||
return NO_MATCH; | ||
} | ||
Attribute.Compound annotation = | ||
sym.getRawAttributes().stream() | ||
.filter(a -> a.type.tsym.getQualifiedName().contentEquals(REQUIRED_MODIFIERS)) | ||
.findAny() | ||
.orElse(null); | ||
if (annotation == null) { | ||
return Description.NO_MATCH; | ||
return NO_MATCH; | ||
} | ||
Set<Modifier> requiredModifiers = ImmutableSet.copyOf(annotation.value()); | ||
Set<Modifier> requiredModifiers = new LinkedHashSet<>(); | ||
getValue(annotation, "value").ifPresent(a -> getModifiers(requiredModifiers, a)); | ||
getValue(annotation, "modifier").ifPresent(a -> getModifiers(requiredModifiers, a)); | ||
if (requiredModifiers.isEmpty()) { | ||
return Description.NO_MATCH; | ||
return NO_MATCH; | ||
} | ||
|
||
Tree parent = state.getPath().getParentPath().getLeaf(); | ||
if (!(parent instanceof ModifiersTree)) { | ||
// e.g. An annotated package name | ||
return Description.NO_MATCH; | ||
return NO_MATCH; | ||
} | ||
|
||
Set<Modifier> missing = Sets.difference(requiredModifiers, ((ModifiersTree) parent).getFlags()); | ||
|
||
if (missing.isEmpty()) { | ||
return Description.NO_MATCH; | ||
return NO_MATCH; | ||
} | ||
|
||
String annotationName = ASTHelpers.getAnnotationName(tree); | ||
|
@@ -86,4 +106,21 @@ public Description matchAnnotation(AnnotationTree tree, VisitorState state) { | |
.setMessage(customMessage) | ||
.build(); | ||
} | ||
|
||
private static void getModifiers(Collection<Modifier> modifiers, Attribute attribute) { | ||
class Visitor extends SimpleAnnotationValueVisitor8<Void, Void> { | ||
@Override | ||
public Void visitEnumConstant(VariableElement c, Void unused) { | ||
modifiers.add(Modifier.valueOf(c.getSimpleName().toString())); | ||
return null; | ||
} | ||
|
||
@Override | ||
public Void visitArray(List<? extends AnnotationValue> vals, Void unused) { | ||
vals.forEach(val -> val.accept(this, null)); | ||
return null; | ||
} | ||
} | ||
attribute.accept(new Visitor(), null); | ||
} | ||
} |