@Override public Void visitAnnotation(AnnotationTree at, Void p) { String annType = at.getAnnotationType().toString(); Integer i = annotationHisto.get(annType); if (i == null) { annotationHisto.put(annType, 0); } else { annotationHisto.put(annType, i + 1); } return super.visitAnnotation(at, p); } }, null);
private static boolean hasSimpleName(AnnotationTree annotation, String name) { Tree annotationType = annotation.getAnnotationType(); javax.lang.model.element.Name simpleName; if (annotationType instanceof IdentifierTree) { simpleName = ((IdentifierTree) annotationType).getName(); } else if (annotationType instanceof MemberSelectTree) { simpleName = ((MemberSelectTree) annotationType).getIdentifier(); } else { return false; } return simpleName.contentEquals(name); }
private static boolean isTypeAnnotation(AnnotationTree t) { Symbol annotationSymbol = ASTHelpers.getSymbol(t.getAnnotationType()); if (annotationSymbol == null) { return false; } Target target = annotationSymbol.getAnnotation(Target.class); if (target == null) { return false; } List<ElementType> value = Arrays.asList(target.value()); return value.contains(ElementType.TYPE_USE) || value.contains(ElementType.TYPE_PARAMETER); } }
@Override public boolean matches(AnnotationTree annotationTree, VisitorState state) { return symbolHasAnnotation(GUICE_BINDING_ANNOTATION) .matches(annotationTree.getAnnotationType(), state); } };
/** Returns the annotation on {@code classTree} whose type's FQCN is {@code annotationName}. */ static Optional<AnnotationTree> findAnnotation(String annotationName, ClassTree classTree) { for (AnnotationTree annotationTree : classTree.getModifiers().getAnnotations()) { ClassSymbol annotationClass = (ClassSymbol) getSymbol(annotationTree.getAnnotationType()); if (annotationClass.fullname.contentEquals(annotationName)) { return Optional.of(annotationTree); } } return Optional.absent(); }
private boolean isErrorHandler(MethodTree node) { ModifiersTree mt = node.getModifiers(); List<? extends AnnotationTree> annos = mt.getAnnotations(); for(AnnotationTree at : annos) { String annFqn = ((JCTree)at.getAnnotationType()).type.tsym.getQualifiedName().toString(); if (annFqn.equals(ON_ERROR_TYPE)) { return true; } } return false; }
private boolean isExitHandler(MethodTree node) { ModifiersTree mt = node.getModifiers(); List<? extends AnnotationTree> annos = mt.getAnnotations(); for(AnnotationTree at : annos) { String annFqn = ((JCTree)at.getAnnotationType()).type.tsym.getQualifiedName().toString(); if (annFqn.equals(ON_EXIT_TYPE)) { return true; } } return false; }
private boolean isAnnotated(MethodTree node) { ModifiersTree mt = node.getModifiers(); List<? extends AnnotationTree> annos = mt.getAnnotations(); for(AnnotationTree at : annos) { String annFqn = ((JCTree)at.getAnnotationType()).type.tsym.getQualifiedName().toString(); if (annFqn.startsWith("com.sun.btrace.annotations")) { return true; } } return false; }
@Override public UAnnotation visitAnnotation(AnnotationTree tree, Void v) { return UAnnotation.create( templateType(tree.getAnnotationType()), templateExpressions(tree.getArguments())); }
JCIdent ident = (JCIdent) annotationTree.getAnnotationType(); String annotationClassName = ident.sym.getQualifiedName().toString(); if ("java.lang.Override".equals(annotationClassName)) {
/** Detects if the class is annotated as @BTrace(trusted=true). */ private boolean hasTrustedAnnotation(ClassTree ct, Element topElement) { for (AnnotationTree at : ct.getModifiers().getAnnotations()) { String annFqn = ((JCTree)at.getAnnotationType()).type.tsym.getQualifiedName().toString(); if (!annFqn.equals(BTrace.class.getName())) { continue; } // now we have @BTrace, look for unsafe = xxx or trusted = xxx for (ExpressionTree ext : at.getArguments()) { if (!(ext instanceof JCAssign)) { continue; } JCAssign assign = (JCAssign) ext; String name = ((JCIdent)assign.lhs).name.toString(); if (!"unsafe".equals(name) && !"trusted".equals(name)) { continue; } // now rhs is the value of @BTrace.unsafe. // The value can be complex (!!true, 1 == 2, etc.) - we support only booleans String val = assign.rhs.toString(); if ("true".equals(val)) { return true; // bingo! } else if (!"false".equals(val)) { processingEnv.getMessager().printMessage(Kind.WARNING, Messages.get("no.complex.unsafe.value"), topElement); } } } return false; }
@Override @Nullable public Choice<Unifier> visitAnnotation(AnnotationTree annotation, Unifier unifier) { return getAnnotationType() .unify(annotation.getAnnotationType(), unifier) .thenChoose(unifications(getArguments(), annotation.getArguments())); }
Iterables.tryFind( annotations, annot -> annot.getAnnotationType().toString().endsWith("SuppressWarnings")); if (!suppressWarningsAnnot.isPresent()) { throw new AssertionError("something went horribly wrong");
/** * Gets the symbol declared by a tree. Returns null if {@code tree} does not declare a symbol or * is null. */ @Nullable public static Symbol getDeclaredSymbol(Tree tree) { if (tree instanceof AnnotationTree) { return getSymbol(((AnnotationTree) tree).getAnnotationType()); } if (tree instanceof PackageTree) { return getSymbol((PackageTree) tree); } if (tree instanceof TypeParameterTree) { Type type = ((JCTypeParameter) tree).type; return type == null ? null : type.tsym; } if (tree instanceof ClassTree) { return getSymbol((ClassTree) tree); } if (tree instanceof MethodTree) { return getSymbol((MethodTree) tree); } if (tree instanceof VariableTree) { return getSymbol((VariableTree) tree); } return null; }
@SuppressWarnings("unused") private Description.Builder changeReturnNullabilityFix( Tree suggestTree, Description.Builder builder) { if (suggestTree.getKind() != Tree.Kind.METHOD) { throw new RuntimeException("This should be a MethodTree"); } SuggestedFix.Builder fixBuilder = SuggestedFix.builder(); MethodTree methodTree = (MethodTree) suggestTree; int countNullableAnnotations = 0; for (AnnotationTree annotationTree : methodTree.getModifiers().getAnnotations()) { if (annotationTree.getAnnotationType().toString().endsWith("Nullable")) { fixBuilder.delete(annotationTree); countNullableAnnotations += 1; } } assert countNullableAnnotations > 1; return builder.addFix(fixBuilder.build()); }
@Override public Description matchClass(ClassTree classTree, VisitorState state) { if (!DaggerAnnotations.isAnyModule().matches(classTree, state)) { return Description.NO_MATCH; } List<SuggestedFix> fixes = new ArrayList<>(); for (AnnotationTree annotation : classTree.getModifiers().getAnnotations()) { Symbol annotationType = getSymbol(annotation.getAnnotationType()); if (hasAnnotation(annotationType, "javax.inject.Scope", state)) { fixes.add(SuggestedFix.delete(annotation)); } } if (fixes.isEmpty()) { return Description.NO_MATCH; } return buildDescription(classTree).addAllFixes(fixes).build(); } }
@Override public boolean matches(AnnotationTree annotationTree, VisitorState state) { Tree type = annotationTree.getAnnotationType(); if (type.getKind() == Tree.Kind.IDENTIFIER && type instanceof JCTree.JCIdent) { JCTree.JCIdent jcIdent = (JCTree.JCIdent) type; return jcIdent.sym.getQualifiedName().contentEquals(annotationClassName); } else if (type.getKind() == Tree.Kind.MEMBER_SELECT && type instanceof JCTree.JCFieldAccess) { JCTree.JCFieldAccess jcFieldAccess = (JCTree.JCFieldAccess) type; return jcFieldAccess.sym.getQualifiedName().contentEquals(annotationClassName); } else { return false; } } }
JCIdent ident = (JCIdent) annotationTree.getAnnotationType(); String annotationClassName = ident.sym.getQualifiedName().toString(); if ("org.robolectric.annotation.Implements".equals(annotationClassName)) {
String btrace = BTrace.class.getName(); for (AnnotationTree at : anno) { String name = at.getAnnotationType().toString(); if (name.equals(btrace) || name.equals("BTrace")) {
@Override public Void visitAnnotation(AnnotationTree expected, Tree actual) { Optional<AnnotationTree> other = checkTypeAndCast(expected, actual); if (!other.isPresent()) { addTypeMismatch(expected, actual); return null; } scan(expected.getAnnotationType(), other.get().getAnnotationType()); parallelScan(expected.getArguments(), other.get().getArguments()); return null; }