Skip to content

Commit

Permalink
Improve hasAnnotation check for missing annotation classes
Browse files Browse the repository at this point in the history
getSymbolForName returns null if the class doesn't exist,
Symtab.enterClass speculatively enters it in the symbol table even if it
isn't on the classpath.

MOE_MIGRATED_REVID=125219502
  • Loading branch information
cushon committed Jun 21, 2016
1 parent 95792c1 commit 750321c
Showing 1 changed file with 8 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
import com.sun.tools.javac.code.Scope;
import com.sun.tools.javac.code.Symbol;
import com.sun.tools.javac.code.Symbol.ClassSymbol;
import com.sun.tools.javac.code.Symbol.CompletionFailure;
import com.sun.tools.javac.code.Symbol.MethodSymbol;
import com.sun.tools.javac.code.Symbol.PackageSymbol;
import com.sun.tools.javac.code.Symbol.TypeSymbol;
Expand Down Expand Up @@ -485,7 +486,13 @@ public static MethodSymbol findSuperMethod(MethodSymbol method, Types types) {
* @param annotationType The type of the annotation to look for (e.g, "javax.annotation.Nullable")
*/
public static boolean hasAnnotation(Symbol sym, String annotationType, VisitorState state) {
Symbol annotationSym = state.getSymbolFromString(annotationType);
Symbol annotationSym = state.getSymtab().enterClass(state.getName(annotationType));
try {
annotationSym.complete();
} catch (CompletionFailure e) {
// @Inherited won't work if the annotation isn't on the classpath, but we can still check
// if it's present directly
}
Symbol inheritedSym = state.getSymtab().inheritedType.tsym;

if ((sym == null) || (annotationSym == null)) {
Expand Down

0 comments on commit 750321c

Please sign in to comment.