private static List<? extends AnnotationTree> findAnnotationsTree(Tree tree) { ModifiersTree maybeModifiers = getModifiers(tree); return maybeModifiers == null ? ImmutableList.of() : maybeModifiers.getAnnotations(); }
private static Optional<SuggestedFix> removeInjectAnnotationFromVariable( VariableTree variableTree, VisitorState state) { for (AnnotationTree annotation : variableTree.getModifiers().getAnnotations()) { if (InjectMatchers.IS_APPLICATION_OF_AT_INJECT.matches(annotation, state)) { return Optional.of(SuggestedFix.replace(annotation, "")); } } return Optional.empty(); } }
private AnnotationTree findAnnotation( MethodTree methodTree, VisitorState state, String annotationName) { AnnotationTree annotationNode = null; for (AnnotationTree annotation : methodTree.getModifiers().getAnnotations()) { if (ASTHelpers.getSymbol(annotation).equals(state.getSymbolFromString(annotationName))) { annotationNode = annotation; } } return annotationNode; }
@Override protected Iterable<? extends AnnotationTree> getChildNodes(T tree, VisitorState state) { if (tree instanceof ClassTree) { return ((ClassTree) tree).getModifiers().getAnnotations(); } else if (tree instanceof VariableTree) { return ((VariableTree) tree).getModifiers().getAnnotations(); } else if (tree instanceof MethodTree) { return ((MethodTree) tree).getModifiers().getAnnotations(); } else if (tree instanceof CompilationUnitTree) { return ((CompilationUnitTree) tree).getPackageAnnotations(); } else if (tree instanceof AnnotatedTypeTree) { return ((AnnotatedTypeTree) tree).getAnnotations(); } else if (tree instanceof PackageTree) { return ((PackageTree) tree).getAnnotations(); } else { throw new IllegalArgumentException( "Cannot access annotations from tree of type " + tree.getClass()); } } }
/** 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(); }
@Override public Description matchVariable(VariableTree tree, VisitorState state) { Symbol.VarSymbol sym = ASTHelpers.getSymbol(tree); if (sym == null) { return NO_MATCH; } return check(sym.type, tree.getModifiers().getAnnotations()); }
@Override public UModifiers visitModifiers(ModifiersTree modifiers, Void v) { return UModifiers.create( ((JCModifiers) modifiers).flags, cast(templateExpressions(modifiers.getAnnotations()), UAnnotation.class)); }
private List<? extends AnnotationTree> getSiblingAnnotations(VisitorState state) { return ((ModifiersTree) state.getPath().getParentPath().getLeaf()).getAnnotations(); } }
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 Description matchMethod(MethodTree tree, VisitorState state) { MethodSymbol sym = ASTHelpers.getSymbol(tree); if (sym == null) { return NO_MATCH; } return check(sym.getReturnType(), tree.getModifiers().getAnnotations()); }
for (AnnotationTree annotation : classTree.getModifiers().getAnnotations()) { modifiers.add(state.getSourceForNode(annotation));
@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 Description matchMethod(MethodTree tree, VisitorState state) { Symbol sym = ASTHelpers.getSymbol(tree); if (sym == null) { return NO_MATCH; } if (!sym.isConstructor()) { return NO_MATCH; } AnnotationTree annotation = ASTHelpers.getAnnotationWithSimpleName(tree.getModifiers().getAnnotations(), "Nullable"); if (annotation == null) { return NO_MATCH; } return describeMatch(annotation, SuggestedFix.delete(annotation)); } }
@Override public Description matchMethod(MethodTree tree, VisitorState state) { MethodSymbol sym = ASTHelpers.getSymbol(tree); if (sym == null) { return NO_MATCH; } if (sym.getReturnType().getKind() != TypeKind.VOID) { return NO_MATCH; } AnnotationTree annotation = ASTHelpers.getAnnotationWithSimpleName(tree.getModifiers().getAnnotations(), "Nullable"); if (annotation == null) { return NO_MATCH; } return describeMatch(annotation, SuggestedFix.delete(annotation)); } }
private Description describe( ClassTree classTree, VisitorState state, @Nullable Retention retention) { if (retention == null) { return describeMatch( classTree, SuggestedFix.builder() .addImport("java.lang.annotation.Retention") .addStaticImport("java.lang.annotation.RetentionPolicy.RUNTIME") .prefixWith(classTree, "@Retention(RUNTIME)\n") .build()); } AnnotationTree retentionNode = null; for (AnnotationTree annotation : classTree.getModifiers().getAnnotations()) { if (ASTHelpers.getSymbol(annotation) .equals(state.getSymbolFromString(RETENTION_ANNOTATION))) { retentionNode = annotation; } } return describeMatch( retentionNode, SuggestedFix.builder() .addImport("java.lang.annotation.Retention") .addStaticImport("java.lang.annotation.RetentionPolicy.RUNTIME") .replace(retentionNode, "@Retention(RUNTIME)") .build()); } }
@Override public Description matchClass(ClassTree tree, VisitorState state) { ClassSymbol symbol = getSymbol(tree); if (symbol == null || !symbol.isEnum()) { return NO_MATCH; } if (ASTHelpers.hasAnnotation(symbol, Immutable.class, state) && !implementsImmutableInterface(symbol)) { AnnotationTree annotation = ASTHelpers.getAnnotationWithSimpleName(tree.getModifiers().getAnnotations(), "Immutable"); if (annotation != null) { state.reportMatch( buildDescription(annotation) .setMessage(ANNOTATED_ENUM_MESSAGE) .addFix(SuggestedFix.delete(annotation)) .build()); } else { state.reportMatch(buildDescription(tree).setMessage(ANNOTATED_ENUM_MESSAGE).build()); } } Violation info = new ImmutableAnalysis( this, state, wellKnownMutability, ImmutableSet.of(Immutable.class.getName())) .checkForImmutability( Optional.of(tree), ImmutableSet.of(), getType(tree), this::describe); if (!info.isPresent()) { return NO_MATCH; } return describe(tree, info).build(); }
/** * Matches injected constructors annotated with @Inject(optional=true) or binding annotations. * Suggests fixes to remove the argument {@code optional=true} or binding annotations. */ @Override public Description matchMethod(MethodTree methodTree, VisitorState state) { SuggestedFix.Builder fix = null; if (isInjectedConstructor(methodTree, state)) { for (AnnotationTree annotationTree : methodTree.getModifiers().getAnnotations()) { if (OPTIONAL_INJECTION_MATCHER.matches(annotationTree, state)) { // Replace the annotation with "@Inject" if (fix == null) { fix = SuggestedFix.builder(); } fix = fix.replace(annotationTree, "@Inject"); } else if (BINDING_ANNOTATION_MATCHER.matches(annotationTree, state)) { // Remove the binding annotation if (fix == null) { fix = SuggestedFix.builder(); } fix = fix.delete(annotationTree); } } } if (fix == null) { return Description.NO_MATCH; } else { return describeMatch(methodTree, fix.build()); } }