From 91befcd93c89421160570afec1f9da03a499bf75 Mon Sep 17 00:00:00 2001 From: cushon Date: Wed, 21 Sep 2016 19:01:35 -0700 Subject: [PATCH] Document that hasAnnotation expects binary names MOE_MIGRATED_REVID=133905450 --- .../google/errorprone/matchers/Matchers.java | 26 +++++++++--------- .../google/errorprone/util/ASTHelpers.java | 27 ++++++++++--------- 2 files changed, 28 insertions(+), 25 deletions(-) diff --git a/core/src/main/java/com/google/errorprone/matchers/Matchers.java b/core/src/main/java/com/google/errorprone/matchers/Matchers.java index bf04d565eb8..40966fd5e2c 100644 --- a/core/src/main/java/com/google/errorprone/matchers/Matchers.java +++ b/core/src/main/java/com/google/errorprone/matchers/Matchers.java @@ -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 Matcher hasAnnotation(final String annotationType) { + public static Matcher hasAnnotation(final String annotationClass) { return new Matcher() { @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); } }; } @@ -747,13 +748,14 @@ 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 hasAnnotationOnAnyOverriddenMethod( - final String annotationType) { + final String annotationClass) { return new Matcher() { @Override public boolean matches(MethodTree tree, VisitorState state) { @@ -761,11 +763,11 @@ public boolean matches(MethodTree tree, VisitorState state) { 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; } } diff --git a/core/src/main/java/com/google/errorprone/util/ASTHelpers.java b/core/src/main/java/com/google/errorprone/util/ASTHelpers.java index b2267d79af0..7b416dbcafc 100644 --- a/core/src/main/java/com/google/errorprone/util/ASTHelpers.java +++ b/core/src/main/java/com/google/errorprone/util/ASTHelpers.java @@ -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); @@ -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 annotationType, VisitorState state) { - return hasAnnotation(sym, annotationType.getName(), state); + Symbol sym, Class annotationClass, VisitorState state) { + return hasAnnotation(sym, annotationClass.getName(), state); } /** @@ -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 annotationType, VisitorState state) { + Tree tree, Class annotationClass, VisitorState state) { Symbol sym = getSymbol(tree); - return hasAnnotation(sym, annotationType.getName(), state); + return hasAnnotation(sym, annotationClass.getName(), state); } /** @@ -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 getAnnotation(Tree tree, Class annotationType) { + public static T getAnnotation(Tree tree, Class annotationClass) { Symbol sym = getSymbol(tree); - return sym == null ? null : getAnnotation(sym, annotationType); + return sym == null ? null : getAnnotation(sym, annotationClass); } /** @@ -584,8 +585,8 @@ public static T getAnnotation(Tree tree, Class 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 getAnnotation(Symbol sym, Class annotationType) { - return sym == null ? null : sym.getAnnotation(annotationType); + public static T getAnnotation(Symbol sym, Class annotationClass) { + return sym == null ? null : sym.getAnnotation(annotationClass); } /** @return all values of the given enum type, in declaration order. */