Skip to content

Commit

Permalink
Document that hasAnnotation expects binary names
Browse files Browse the repository at this point in the history
MOE_MIGRATED_REVID=133905450
  • Loading branch information
cushon committed Sep 23, 2016
1 parent 21cc48f commit 91befcd
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 25 deletions.
26 changes: 14 additions & 12 deletions core/src/main/java/com/google/errorprone/matchers/Matchers.java
Expand Up @@ -715,16 +715,17 @@ public boolean matches(MethodInvocationTree methodInvocationTree, VisitorState s
}

/**
* Determines whether an expression has an annotation of the given type.
* This includes annotations inherited from superclasses due to @Inherited.
* Determines whether an expression has an annotation of the given type. This includes annotations
* inherited from superclasses due to @Inherited.
*
* @param annotationType The type of the annotation to look for (e.g, "javax.annotation.Nullable")
* @param annotationClass the binary class name of the annotation (e.g.
* "javax.annotation.Nullable", or "some.package.OuterClassName$InnerClassName")
*/
public static <T extends Tree> Matcher<T> hasAnnotation(final String annotationType) {
public static <T extends Tree> Matcher<T> hasAnnotation(final String annotationClass) {
return new Matcher<T>() {
@Override
public boolean matches (T tree, VisitorState state) {
return ASTHelpers.hasAnnotation(ASTHelpers.getSymbol(tree), annotationType, state);
public boolean matches(T tree, VisitorState state) {
return ASTHelpers.hasAnnotation(ASTHelpers.getSymbol(tree), annotationClass, state);
}
};
}
Expand All @@ -747,25 +748,26 @@ public boolean matches(T tree, VisitorState state) {


/**
* Matches if a method or any method it overrides has an annotation of the given type.
* JUnit 4's {@code @Test}, {@code @Before}, and {@code @After} annotations behave this way.
* Matches if a method or any method it overrides has an annotation of the given type. JUnit 4's
* {@code @Test}, {@code @Before}, and {@code @After} annotations behave this way.
*
* @param annotationType The type of the annotation to look for (e.g, "org.junit.Test")
* @param annotationClass the binary class name of the annotation (e.g.
* "javax.annotation.Nullable", or "some.package.OuterClassName$InnerClassName")
*/
public static Matcher<MethodTree> hasAnnotationOnAnyOverriddenMethod(
final String annotationType) {
final String annotationClass) {
return new Matcher<MethodTree>() {
@Override
public boolean matches(MethodTree tree, VisitorState state) {
MethodSymbol methodSym = ASTHelpers.getSymbol(tree);
if (methodSym == null) {
return false;
}
if (ASTHelpers.hasAnnotation(methodSym, annotationType, state)) {
if (ASTHelpers.hasAnnotation(methodSym, annotationClass, state)) {
return true;
}
for (MethodSymbol method : ASTHelpers.findSuperMethods(methodSym, state.getTypes())) {
if (ASTHelpers.hasAnnotation(method, annotationType, state)) {
if (ASTHelpers.hasAnnotation(method, annotationClass, state)) {
return true;
}
}
Expand Down
27 changes: 14 additions & 13 deletions core/src/main/java/com/google/errorprone/util/ASTHelpers.java
Expand Up @@ -493,13 +493,14 @@ public static MethodSymbol findSuperMethod(MethodSymbol method, Types types) {
}

/**
* Determines whether a symbol has an annotation of the given type.
* This includes annotations inherited from superclasses due to @Inherited.
* Determines whether a symbol has an annotation of the given type. This includes annotations
* inherited from superclasses due to {@code @Inherited}.
*
* @param annotationType The type of the annotation to look for (e.g, "javax.annotation.Nullable")
* @param annotationClass the binary class name of the annotation (e.g.
* "javax.annotation.Nullable", or "some.package.OuterClassName$InnerClassName")
*/
public static boolean hasAnnotation(Symbol sym, String annotationType, VisitorState state) {
Name annotationName = state.getName(annotationType);
public static boolean hasAnnotation(Symbol sym, String annotationClass, VisitorState state) {
Name annotationName = state.getName(annotationClass);
Symbol annotationSym;
synchronized (state.context) {
annotationSym = state.getSymtab().enterClass(annotationName);
Expand Down Expand Up @@ -534,8 +535,8 @@ public static boolean hasAnnotation(Symbol sym, String annotationType, VisitorSt
* @return true if the symbol is annotated with given type.
*/
public static boolean hasAnnotation(
Symbol sym, Class<? extends Annotation> annotationType, VisitorState state) {
return hasAnnotation(sym, annotationType.getName(), state);
Symbol sym, Class<? extends Annotation> annotationClass, VisitorState state) {
return hasAnnotation(sym, annotationClass.getName(), state);
}

/**
Expand All @@ -544,9 +545,9 @@ public static boolean hasAnnotation(
* @return the annotation of given type on the tree's symbol, or null.
*/
public static boolean hasAnnotation(
Tree tree, Class<? extends Annotation> annotationType, VisitorState state) {
Tree tree, Class<? extends Annotation> annotationClass, VisitorState state) {
Symbol sym = getSymbol(tree);
return hasAnnotation(sym, annotationType.getName(), state);
return hasAnnotation(sym, annotationClass.getName(), state);
}

/**
Expand All @@ -571,9 +572,9 @@ public static boolean hasDirectAnnotationWithSimpleName(Symbol sym, String simpl
*
* @return the annotation of given type on the tree's symbol, or null.
*/
public static <T extends Annotation> T getAnnotation(Tree tree, Class<T> annotationType) {
public static <T extends Annotation> T getAnnotation(Tree tree, Class<T> annotationClass) {
Symbol sym = getSymbol(tree);
return sym == null ? null : getAnnotation(sym, annotationType);
return sym == null ? null : getAnnotation(sym, annotationClass);
}

/**
Expand All @@ -584,8 +585,8 @@ public static <T extends Annotation> T getAnnotation(Tree tree, Class<T> annotat
// Symbol#getAnnotation is not intended for internal javac use, but because error-prone is run
// after attribution it's safe to use here.
@SuppressWarnings("deprecation")
public static <T extends Annotation> T getAnnotation(Symbol sym, Class<T> annotationType) {
return sym == null ? null : sym.getAnnotation(annotationType);
public static <T extends Annotation> T getAnnotation(Symbol sym, Class<T> annotationClass) {
return sym == null ? null : sym.getAnnotation(annotationClass);
}

/** @return all values of the given enum type, in declaration order. */
Expand Down

0 comments on commit 91befcd

Please sign in to comment.