@Override public Void visitMethod(MethodTree tree, Void unused) { if (tree.getModifiers().getFlags().contains(Modifier.NATIVE)) { hasAnyNativeMethods.set(true); } return null; } }.scan(tree, null);
@Override public Description report( Set<MethodTree> affectedTrees, SuggestedFix fix, VisitorState state, BugChecker checker) { return affectedTrees.stream() .min(Comparator.comparingInt(t -> ((JCTree) t).getStartPosition())) .map(t -> checker.describeMatch(t.getModifiers(), fix)) .orElse(NO_MATCH); } },
@Override public int compare(MethodTree l, MethodTree r) { return Boolean.compare( l.getModifiers().getFlags().contains(Modifier.ABSTRACT), r.getModifiers().getFlags().contains(Modifier.ABSTRACT)); } }.reverse().immutableSortedCopy(Iterables.filter(tree.getMembers(), MethodTree.class));
/** Returns the modifiers tree of the given class, method, or variable declaration. */ @Nullable public static ModifiersTree getModifiers(Tree tree) { if (tree instanceof ClassTree) { return ((ClassTree) tree).getModifiers(); } else if (tree instanceof MethodTree) { return ((MethodTree) tree).getModifiers(); } else if (tree instanceof VariableTree) { return ((VariableTree) tree).getModifiers(); } else { return null; } }
@Override public Description report( Set<MethodTree> affectedTrees, SuggestedFix fix, VisitorState state, BugChecker checker) { for (MethodTree tree : affectedTrees) { state.reportMatch(checker.describeMatch(tree.getModifiers(), fix)); } return NO_MATCH; } };
@Override public Description matchMethod(MethodTree tree, VisitorState state) { return handle(tree, tree.getName(), tree.getModifiers(), state); }
@Override public boolean matches(MethodTree t, VisitorState state) { Set<Modifier> modifiers = t.getModifiers().getFlags(); if (visibility == Visibility.DEFAULT) { return !(modifiers.contains(Visibility.PUBLIC.toModifier()) || modifiers.contains(Visibility.PROTECTED.toModifier()) || modifiers.contains(Visibility.PRIVATE.toModifier())); } else { return modifiers.contains(visibility.toModifier()); } }
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; }
private void makeProtectedPublic( MethodTree methodTree, VisitorState state, SuggestedFix.Builder suggestedFix) { if (Matchers.<MethodTree>hasModifier(Modifier.PROTECTED).matches(methodTree, state)) { ModifiersTree modifiers = methodTree.getModifiers(); CharSequence modifiersSource = state.getSourceForNode(modifiers); suggestedFix.replace( modifiers, modifiersSource.toString().replaceFirst("protected", "public")); } }
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 boolean matches(T tree, VisitorState state) { SynchronizedTree synchronizedTree = ASTHelpers.findEnclosingNode(state.getPath(), SynchronizedTree.class); if (synchronizedTree != null) { return true; } MethodTree methodTree = ASTHelpers.findEnclosingNode(state.getPath(), MethodTree.class); return methodTree != null && methodTree.getModifiers().getFlags().contains(Modifier.SYNCHRONIZED); } };
@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()); }
@Override public boolean apply(Tree tree) { switch (tree.getKind()) { case CLASS: return ((ClassTree) tree).getModifiers().getFlags().contains(STATIC); case METHOD: return ((MethodTree) tree).getModifiers().getFlags().contains(STATIC); case VARIABLE: return ((VariableTree) tree).getModifiers().getFlags().contains(STATIC); case BLOCK: return ((BlockTree) tree).isStatic(); case ENUM: case ANNOTATION_TYPE: case INTERFACE: return true; default: throw new AssertionError("unknown member type:" + tree.getKind()); } } });
@Override public Void visitMethod(MethodTree tree, HeldLockSet locks) { // Synchronized instance methods hold the 'this' lock; synchronized static methods // hold the Class lock for the enclosing class. Set<Modifier> mods = tree.getModifiers().getFlags(); if (mods.contains(Modifier.SYNCHRONIZED)) { Symbol owner = (((JCTree.JCMethodDecl) tree).sym.owner); GuardedByExpression lock = mods.contains(Modifier.STATIC) ? F.classLiteral(owner) : F.thisliteral(); locks = locks.plus(lock); } // @GuardedBy annotations on methods are trusted for declarations, and checked // for invocations. for (String guard : GuardedByUtils.getGuardValues(tree, visitorState)) { Optional<GuardedByExpression> bound = GuardedByBinder.bindString(guard, GuardedBySymbolResolver.from(tree, visitorState)); if (bound.isPresent()) { locks = locks.plus(bound.get()); } } return super.visitMethod(tree, locks); }
@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()); } } }
@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)); } }
@Override public UMethodDecl visitMethod(MethodTree decl, Void v) { return UMethodDecl.create( visitModifiers(decl.getModifiers(), null), decl.getName(), templateType(decl.getReturnType()), cast(templateStatements(decl.getParameters()), UVariableDecl.class), templateExpressions(decl.getThrows()), (UBlock) template(decl.getBody())); }