Search in sources :

Example 31 with ClassTree

use of com.sun.source.tree.ClassTree in project error-prone by google.

the class JdkObsolete method matchClass.

@Override
public Description matchClass(ClassTree tree, VisitorState state) {
    Tree parent = state.getPath().getParentPath().getLeaf();
    if (parent instanceof NewClassTree && tree.equals(((NewClassTree) parent).getClassBody())) {
        // don't double-report anonymous implementations of obsolete interfaces
        return NO_MATCH;
    }
    ClassSymbol symbol = ASTHelpers.getSymbol(tree);
    if (symbol == null) {
        return NO_MATCH;
    }
    return describeIfObsolete(null, state.getTypes().directSupertypes(symbol.asType()), state);
}
Also used : ClassSymbol(com.sun.tools.javac.code.Symbol.ClassSymbol) ReturnTree(com.sun.source.tree.ReturnTree) MethodTree(com.sun.source.tree.MethodTree) VariableTree(com.sun.source.tree.VariableTree) MethodInvocationTree(com.sun.source.tree.MethodInvocationTree) AssignmentTree(com.sun.source.tree.AssignmentTree) NewClassTree(com.sun.source.tree.NewClassTree) LambdaExpressionTree(com.sun.source.tree.LambdaExpressionTree) ParameterizedTypeTree(com.sun.source.tree.ParameterizedTypeTree) IdentifierTree(com.sun.source.tree.IdentifierTree) Tree(com.sun.source.tree.Tree) ClassTree(com.sun.source.tree.ClassTree) ExpressionTree(com.sun.source.tree.ExpressionTree) MemberReferenceTree(com.sun.source.tree.MemberReferenceTree) MemberSelectTree(com.sun.source.tree.MemberSelectTree) NewClassTree(com.sun.source.tree.NewClassTree)

Example 32 with ClassTree

use of com.sun.source.tree.ClassTree in project error-prone by google.

the class InputStreamSlowMultibyteRead method maybeMatchReadByte.

private Description maybeMatchReadByte(MethodTree readByteMethod, VisitorState state) {
    if (readByteMethod.getBody() != null) {
        // Null-check for native/abstract overrides of read()
        List<? extends StatementTree> statements = readByteMethod.getBody().getStatements();
        if (statements.size() == 1) {
            Tree tree = statements.get(0);
            if (tree.getKind() == Kind.RETURN && ASTHelpers.constValue(((ReturnTree) tree).getExpression()) != null) {
                return Description.NO_MATCH;
            }
        }
    }
    // Streams within JUnit test cases are likely to be OK as well.
    TreePath enclosingPath = ASTHelpers.findPathFromEnclosingNodeToTopLevel(state.getPath(), ClassTree.class);
    while (enclosingPath != null) {
        ClassTree klazz = (ClassTree) enclosingPath.getLeaf();
        if (JUnitMatchers.isTestCaseDescendant.matches(klazz, state) || hasAnnotation(JUnitMatchers.JUNIT4_RUN_WITH_ANNOTATION).matches(klazz, state)) {
            return Description.NO_MATCH;
        }
        enclosingPath = ASTHelpers.findPathFromEnclosingNodeToTopLevel(enclosingPath, ClassTree.class);
    }
    return describeMatch(readByteMethod);
}
Also used : TreePath(com.sun.source.util.TreePath) ClassTree(com.sun.source.tree.ClassTree) ReturnTree(com.sun.source.tree.ReturnTree) MethodTree(com.sun.source.tree.MethodTree) Tree(com.sun.source.tree.Tree) ClassTree(com.sun.source.tree.ClassTree) StatementTree(com.sun.source.tree.StatementTree)

Example 33 with ClassTree

use of com.sun.source.tree.ClassTree in project error-prone by google.

the class MultipleTopLevelClasses method matchCompilationUnit.

@Override
public Description matchCompilationUnit(CompilationUnitTree tree, VisitorState state) {
    if (tree.getTypeDecls().size() <= 1) {
        // else should have exactly one.
        return Description.NO_MATCH;
    }
    List<String> names = new ArrayList<>();
    for (Tree member : tree.getTypeDecls()) {
        if (member instanceof ClassTree) {
            ClassTree classMember = (ClassTree) member;
            switch(classMember.getKind()) {
                case CLASS:
                case INTERFACE:
                case ANNOTATION_TYPE:
                case ENUM:
                    if (isSuppressed(classMember)) {
                        // and @SuppressWarnings can't be applied to packages.
                        return Description.NO_MATCH;
                    }
                    names.add(classMember.getSimpleName().toString());
                    break;
                default:
                    break;
            }
        }
    }
    if (names.size() <= 1) {
        // empty (e.g. ";" at the top level counts as an empty type decl)
        return Description.NO_MATCH;
    }
    String message = String.format("Expected at most one top-level class declaration, instead found: %s", Joiner.on(", ").join(names));
    return buildDescription(firstNonNull(tree.getPackageName(), tree.getTypeDecls().get(0))).setMessage(message).build();
}
Also used : ArrayList(java.util.ArrayList) ClassTree(com.sun.source.tree.ClassTree) CompilationUnitTree(com.sun.source.tree.CompilationUnitTree) Tree(com.sun.source.tree.Tree) ClassTree(com.sun.source.tree.ClassTree)

Example 34 with ClassTree

use of com.sun.source.tree.ClassTree in project error-prone by google.

the class OverrideThrowableToString method matchClass.

@Override
public Description matchClass(ClassTree classTree, VisitorState visitorState) {
    Symbol throwableClass = visitorState.getSymbolFromString("java.lang.Throwable");
    if (Objects.equals(ASTHelpers.getSymbol(classTree.getExtendsClause()), throwableClass)) {
        Optional<? extends Tree> methodTree = classTree.getMembers().stream().filter(m -> m instanceof MethodTree && ((MethodTree) m).getName().contentEquals("toString")).findFirst();
        if (methodTree.isPresent()) {
            SuggestedFix.Builder builder = SuggestedFix.builder();
            MethodTree tree = (MethodTree) methodTree.get();
            if (!tree.getParameters().isEmpty()) {
                return Description.NO_MATCH;
            }
            String newTree = tree.getModifiers().toString().replaceAll("@Override[(][)]", "@Override") + "String getMessage()\n" + visitorState.getSourceForNode(tree.getBody());
            builder.replace(tree, newTree);
            return describeMatch(classTree, builder.build());
        }
    }
    return Description.NO_MATCH;
}
Also used : ClassTreeMatcher(com.google.errorprone.bugpatterns.BugChecker.ClassTreeMatcher) MethodTree(com.sun.source.tree.MethodTree) Symbol(com.sun.tools.javac.code.Symbol) Objects(java.util.Objects) VisitorState(com.google.errorprone.VisitorState) Description(com.google.errorprone.matchers.Description) BugPattern(com.google.errorprone.BugPattern) Optional(java.util.Optional) WARNING(com.google.errorprone.BugPattern.SeverityLevel.WARNING) ProvidesFix(com.google.errorprone.BugPattern.ProvidesFix) SuggestedFix(com.google.errorprone.fixes.SuggestedFix) JDK(com.google.errorprone.BugPattern.Category.JDK) Tree(com.sun.source.tree.Tree) ASTHelpers(com.google.errorprone.util.ASTHelpers) ClassTree(com.sun.source.tree.ClassTree) SuggestedFix(com.google.errorprone.fixes.SuggestedFix) MethodTree(com.sun.source.tree.MethodTree) Symbol(com.sun.tools.javac.code.Symbol)

Example 35 with ClassTree

use of com.sun.source.tree.ClassTree in project robolectric by robolectric.

the class DeprecatedMethodsCheck method matchClass.

@Override
public Description matchClass(ClassTree tree, VisitorState state) {
    if (isInShadowClass(state.getPath(), state)) {
        return NO_MATCH;
    }
    final SuggestedFix.Builder fixBuilder = SuggestedFix.builder();
    HashMap<Tree, Runnable> possibleFixes = new HashMap<>();
    new TreeScanner<Void, VisitorState>() {

        private boolean inShadowClass;

        @Override
        public Void visitClass(ClassTree classTree, VisitorState visitorState) {
            boolean priorInShadowClass = inShadowClass;
            inShadowClass = hasAnnotation(classTree, Implements.class, visitorState);
            try {
                return super.visitClass(classTree, visitorState);
            } finally {
                inShadowClass = priorInShadowClass;
            }
        }

        @Override
        public Void visitMethodInvocation(MethodInvocationTree tree, VisitorState state) {
            VisitorState nowState = state.withPath(TreePath.getPath(state.getPath(), tree));
            if (!inShadowClass) {
                for (MethodInvocationMatcher matcher : matchers) {
                    if (matcher.matcher().matches(tree, state)) {
                        matcher.replace(tree, nowState, fixBuilder, possibleFixes);
                        return null;
                    }
                }
            }
            return super.visitMethodInvocation(tree, nowState);
        }
    }.scan(tree, state);
    for (Runnable runnable : possibleFixes.values()) {
        runnable.run();
    }
    Fix fix = fixBuilder.build();
    return fix.isEmpty() ? NO_MATCH : describeMatch(tree, fix);
}
Also used : HashMap(java.util.HashMap) ClassTree(com.sun.source.tree.ClassTree) SuggestedFix(com.google.errorprone.fixes.SuggestedFix) Fix(com.google.errorprone.fixes.Fix) SuggestedFix(com.google.errorprone.fixes.SuggestedFix) VisitorState(com.google.errorprone.VisitorState) MethodInvocationTree(com.sun.source.tree.MethodInvocationTree) MethodInvocationTree(com.sun.source.tree.MethodInvocationTree) ImportTree(com.sun.source.tree.ImportTree) Tree(com.sun.source.tree.Tree) ClassTree(com.sun.source.tree.ClassTree)

Aggregations

ClassTree (com.sun.source.tree.ClassTree)119 Tree (com.sun.source.tree.Tree)76 MethodTree (com.sun.source.tree.MethodTree)66 VariableTree (com.sun.source.tree.VariableTree)59 NewClassTree (com.sun.source.tree.NewClassTree)48 ExpressionTree (com.sun.source.tree.ExpressionTree)45 MethodInvocationTree (com.sun.source.tree.MethodInvocationTree)40 CompilationUnitTree (com.sun.source.tree.CompilationUnitTree)31 AnnotationTree (com.sun.source.tree.AnnotationTree)29 BlockTree (com.sun.source.tree.BlockTree)28 IdentifierTree (com.sun.source.tree.IdentifierTree)27 NewArrayTree (com.sun.source.tree.NewArrayTree)26 AssignmentTree (com.sun.source.tree.AssignmentTree)25 MemberSelectTree (com.sun.source.tree.MemberSelectTree)25 LambdaExpressionTree (com.sun.source.tree.LambdaExpressionTree)24 TypeElement (javax.lang.model.element.TypeElement)24 ConditionalExpressionTree (com.sun.source.tree.ConditionalExpressionTree)23 ArrayList (java.util.ArrayList)23 ReturnTree (com.sun.source.tree.ReturnTree)22 TreePath (com.sun.source.util.TreePath)22