Search in sources :

Example 6 with VisitorState

use of com.google.errorprone.VisitorState in project error-prone by google.

the class ASTHelpersTest method testHasDirectAnnotationWithSimpleName.

@Test
public void testHasDirectAnnotationWithSimpleName() {
    writeFile(//
    "A.java", "public class A {", "  @Deprecated public void doIt() {}", "}");
    TestScanner scanner = new TestScanner() {

        @Override
        public Void visitMethod(MethodTree tree, VisitorState state) {
            if (tree.getName().contentEquals("doIt")) {
                setAssertionsComplete();
                Symbol sym = ASTHelpers.getSymbol(tree);
                assertThat(ASTHelpers.hasDirectAnnotationWithSimpleName(sym, "Deprecated")).isTrue();
                assertThat(ASTHelpers.hasDirectAnnotationWithSimpleName(sym, "Nullable")).isFalse();
            }
            return super.visitMethod(tree, state);
        }
    };
    tests.add(scanner);
    assertCompiles(scanner);
}
Also used : MethodTree(com.sun.source.tree.MethodTree) VisitorState(com.google.errorprone.VisitorState) Symbol(com.sun.tools.javac.code.Symbol) CompilerBasedAbstractTest(com.google.errorprone.matchers.CompilerBasedAbstractTest) Test(org.junit.Test)

Example 7 with VisitorState

use of com.google.errorprone.VisitorState in project error-prone by google.

the class ASTHelpersTest method getUpperBoundScanner.

/* Tests for ASTHelpers#getUpperBound */
private TestScanner getUpperBoundScanner(final String expectedBound) {
    return new TestScanner() {

        @Override
        public Void visitVariable(VariableTree tree, VisitorState state) {
            setAssertionsComplete();
            Type varType = ASTHelpers.getType(tree.getType());
            assertThat(ASTHelpers.getUpperBound(varType.getTypeArguments().get(0), state.getTypes()).toString()).isEqualTo(expectedBound);
            return super.visitVariable(tree, state);
        }
    };
}
Also used : Type(com.sun.tools.javac.code.Type) VisitorState(com.google.errorprone.VisitorState) VariableTree(com.sun.source.tree.VariableTree)

Example 8 with VisitorState

use of com.google.errorprone.VisitorState in project error-prone by google.

the class ASTHelpersTest method testGetUpperBoundCapturedTypeVariable.

@Test
public void testGetUpperBoundCapturedTypeVariable() {
    writeFile("A.java", "import java.lang.Number;", "import java.util.List;", "public class A {", "  public void doSomething(List<? extends Number> list) {", "    list.get(0);", "  }", "}");
    TestScanner scanner = new TestScanner() {

        @Override
        public Void visitMethodInvocation(MethodInvocationTree tree, VisitorState state) {
            if (!"super()".equals(tree.toString())) {
                // ignore synthetic super call
                setAssertionsComplete();
                Type type = ASTHelpers.getType(tree);
                assertThat(type instanceof TypeVar).isTrue();
                assertThat(((TypeVar) type).isCaptured()).isTrue();
                assertThat(ASTHelpers.getUpperBound(type, state.getTypes()).toString()).isEqualTo("java.lang.Number");
            }
            return super.visitMethodInvocation(tree, state);
        }
    };
    tests.add(scanner);
    assertCompiles(scanner);
}
Also used : Type(com.sun.tools.javac.code.Type) TypeVar(com.sun.tools.javac.code.Type.TypeVar) VisitorState(com.google.errorprone.VisitorState) MethodInvocationTree(com.sun.source.tree.MethodInvocationTree) CompilerBasedAbstractTest(com.google.errorprone.matchers.CompilerBasedAbstractTest) Test(org.junit.Test)

Example 9 with VisitorState

use of com.google.errorprone.VisitorState in project error-prone by google.

the class TypeParameterShadowing method renameTypeVariable.

private static SuggestedFix renameTypeVariable(Tree sourceTree, List<? extends TypeParameterTree> typeParameters, Name typeVariable, String typeVarReplacement, VisitorState state) {
    TypeParameterTree matchingTypeParam = typeParameters.stream().filter(t -> t.getName().contentEquals(typeVariable)).collect(MoreCollectors.onlyElement());
    Symbol typeVariableSymbol = ASTHelpers.getSymbol(matchingTypeParam);
    // replace only the type parameter name (and not any upper bounds)
    String name = matchingTypeParam.getName().toString();
    int pos = ((JCTree) matchingTypeParam).getStartPosition();
    SuggestedFix.Builder fixBuilder = SuggestedFix.builder().replace(pos, pos + name.length(), typeVarReplacement);
    ((JCTree) sourceTree).accept(new TreeScanner() {

        @Override
        public void visitIdent(JCTree.JCIdent tree) {
            Symbol identSym = ASTHelpers.getSymbol(tree);
            if (Objects.equal(identSym, typeVariableSymbol)) {
                // }
                if (Objects.equal(state.getSourceForNode(tree), name)) {
                    fixBuilder.replace(tree, typeVarReplacement);
                }
            }
        }
    });
    return fixBuilder.build();
}
Also used : ClassTreeMatcher(com.google.errorprone.bugpatterns.BugChecker.ClassTreeMatcher) MethodTreeMatcher(com.google.errorprone.bugpatterns.BugChecker.MethodTreeMatcher) MethodTree(com.sun.source.tree.MethodTree) TreeScanner(com.sun.tools.javac.tree.TreeScanner) TypeParameterTree(com.sun.source.tree.TypeParameterTree) ArrayList(java.util.ArrayList) VisitorState(com.google.errorprone.VisitorState) Matcher(java.util.regex.Matcher) ImmutableSet.toImmutableSet(com.google.common.collect.ImmutableSet.toImmutableSet) BugPattern(com.google.errorprone.BugPattern) JDK(com.google.errorprone.BugPattern.Category.JDK) Objects(com.google.common.base.Objects) Tree(com.sun.source.tree.Tree) ClassTree(com.sun.source.tree.ClassTree) Name(javax.lang.model.element.Name) MoreCollectors(com.google.common.collect.MoreCollectors) Symbol(com.sun.tools.javac.code.Symbol) Set(java.util.Set) Streams(com.google.common.collect.Streams) JCTree(com.sun.tools.javac.tree.JCTree) Collectors(java.util.stream.Collectors) TypeVariableSymbol(com.sun.tools.javac.code.Symbol.TypeVariableSymbol) List(java.util.List) Description(com.google.errorprone.matchers.Description) WARNING(com.google.errorprone.BugPattern.SeverityLevel.WARNING) SuggestedFix(com.google.errorprone.fixes.SuggestedFix) Pattern(java.util.regex.Pattern) ASTHelpers(com.google.errorprone.util.ASTHelpers) TypeParameterTree(com.sun.source.tree.TypeParameterTree) SuggestedFix(com.google.errorprone.fixes.SuggestedFix) TreeScanner(com.sun.tools.javac.tree.TreeScanner) Symbol(com.sun.tools.javac.code.Symbol) TypeVariableSymbol(com.sun.tools.javac.code.Symbol.TypeVariableSymbol) JCTree(com.sun.tools.javac.tree.JCTree)

Example 10 with VisitorState

use of com.google.errorprone.VisitorState in project error-prone by google.

the class UTemplater method visitMethodInvocation.

@Override
public UExpression visitMethodInvocation(MethodInvocationTree tree, Void v) {
    if (anyMatch(ANY_OF, tree.getMethodSelect(), new Unifier(context))) {
        return UAnyOf.create(templateExpressions(tree.getArguments()));
    } else if (anyMatch(IS_INSTANCE, tree.getMethodSelect(), new Unifier(context))) {
        return UInstanceOf.create(template(Iterables.getOnlyElement(tree.getArguments())), templateType(getSingleExplicitTypeArgument(tree)));
    } else if (anyMatch(CLAZZ, tree.getMethodSelect(), new Unifier(context))) {
        Tree typeArg = getSingleExplicitTypeArgument(tree);
        return UMemberSelect.create(templateType(typeArg), "class", UClassType.create("java.lang.Class", template(((JCTree) typeArg).type)));
    } else if (anyMatch(NEW_ARRAY, tree.getMethodSelect(), new Unifier(context))) {
        Tree typeArg = getSingleExplicitTypeArgument(tree);
        ExpressionTree lengthArg = Iterables.getOnlyElement(tree.getArguments());
        return UNewArray.create(templateType(typeArg), ImmutableList.of(template(lengthArg)), null);
    } else if (anyMatch(ENUM_VALUE_OF, tree.getMethodSelect(), new Unifier(context))) {
        Tree typeArg = getSingleExplicitTypeArgument(tree);
        ExpressionTree strArg = Iterables.getOnlyElement(tree.getArguments());
        return UMethodInvocation.create(UMemberSelect.create(templateType(typeArg), "valueOf", UMethodType.create(template(((JCTree) typeArg).type), UClassType.create("java.lang.String"))), template(strArg));
    } else if (anyMatch(AS_VARARGS, tree.getMethodSelect(), new Unifier(context))) {
        ExpressionTree arg = Iterables.getOnlyElement(tree.getArguments());
        checkArgument(ASTHelpers.hasAnnotation(arg, Repeated.class, new VisitorState(context)));
        return template(arg);
    }
    Map<MethodSymbol, PlaceholderMethod> placeholderMethods = context.get(RefasterRuleBuilderScanner.PLACEHOLDER_METHODS_KEY);
    if (placeholderMethods != null && placeholderMethods.containsKey(ASTHelpers.getSymbol(tree))) {
        return UPlaceholderExpression.create(placeholderMethods.get(ASTHelpers.getSymbol(tree)), templateExpressions(tree.getArguments()));
    } else {
        return UMethodInvocation.create(template(tree.getMethodSelect()), templateExpressions(tree.getArguments()));
    }
}
Also used : MethodSymbol(com.sun.tools.javac.code.Symbol.MethodSymbol) VisitorState(com.google.errorprone.VisitorState) CompoundAssignmentTree(com.sun.source.tree.CompoundAssignmentTree) LiteralTree(com.sun.source.tree.LiteralTree) MethodInvocationTree(com.sun.source.tree.MethodInvocationTree) AssignmentTree(com.sun.source.tree.AssignmentTree) TypeCastTree(com.sun.source.tree.TypeCastTree) LambdaExpressionTree(com.sun.source.tree.LambdaExpressionTree) ForLoopTree(com.sun.source.tree.ForLoopTree) InstanceOfTree(com.sun.source.tree.InstanceOfTree) ParenthesizedTree(com.sun.source.tree.ParenthesizedTree) ConditionalExpressionTree(com.sun.source.tree.ConditionalExpressionTree) MemberSelectTree(com.sun.source.tree.MemberSelectTree) ExpressionStatementTree(com.sun.source.tree.ExpressionStatementTree) ThrowTree(com.sun.source.tree.ThrowTree) BlockTree(com.sun.source.tree.BlockTree) EnhancedForLoopTree(com.sun.source.tree.EnhancedForLoopTree) ReturnTree(com.sun.source.tree.ReturnTree) ArrayTypeTree(com.sun.source.tree.ArrayTypeTree) LabeledStatementTree(com.sun.source.tree.LabeledStatementTree) UnaryTree(com.sun.source.tree.UnaryTree) VariableTree(com.sun.source.tree.VariableTree) TypeParameterTree(com.sun.source.tree.TypeParameterTree) NewClassTree(com.sun.source.tree.NewClassTree) ParameterizedTypeTree(com.sun.source.tree.ParameterizedTypeTree) BreakTree(com.sun.source.tree.BreakTree) Tree(com.sun.source.tree.Tree) ExpressionTree(com.sun.source.tree.ExpressionTree) IntersectionTypeTree(com.sun.source.tree.IntersectionTypeTree) WildcardTree(com.sun.source.tree.WildcardTree) UnionTypeTree(com.sun.source.tree.UnionTypeTree) ArrayAccessTree(com.sun.source.tree.ArrayAccessTree) AnnotatedTypeTree(com.sun.source.tree.AnnotatedTypeTree) IdentifierTree(com.sun.source.tree.IdentifierTree) CatchTree(com.sun.source.tree.CatchTree) NewArrayTree(com.sun.source.tree.NewArrayTree) JCPrimitiveTypeTree(com.sun.tools.javac.tree.JCTree.JCPrimitiveTypeTree) ContinueTree(com.sun.source.tree.ContinueTree) PrimitiveTypeTree(com.sun.source.tree.PrimitiveTypeTree) SynchronizedTree(com.sun.source.tree.SynchronizedTree) AssertTree(com.sun.source.tree.AssertTree) StatementTree(com.sun.source.tree.StatementTree) ModifiersTree(com.sun.source.tree.ModifiersTree) WhileLoopTree(com.sun.source.tree.WhileLoopTree) AnnotationTree(com.sun.source.tree.AnnotationTree) MethodTree(com.sun.source.tree.MethodTree) BinaryTree(com.sun.source.tree.BinaryTree) EmptyStatementTree(com.sun.source.tree.EmptyStatementTree) ClassTree(com.sun.source.tree.ClassTree) IfTree(com.sun.source.tree.IfTree) MemberReferenceTree(com.sun.source.tree.MemberReferenceTree) JCTree(com.sun.tools.javac.tree.JCTree) DoWhileLoopTree(com.sun.source.tree.DoWhileLoopTree) TryTree(com.sun.source.tree.TryTree) LambdaExpressionTree(com.sun.source.tree.LambdaExpressionTree) ConditionalExpressionTree(com.sun.source.tree.ConditionalExpressionTree) ExpressionTree(com.sun.source.tree.ExpressionTree) JCTree(com.sun.tools.javac.tree.JCTree)

Aggregations

VisitorState (com.google.errorprone.VisitorState)29 MethodTree (com.sun.source.tree.MethodTree)13 Tree (com.sun.source.tree.Tree)13 ExpressionTree (com.sun.source.tree.ExpressionTree)12 MethodInvocationTree (com.sun.source.tree.MethodInvocationTree)11 ClassTree (com.sun.source.tree.ClassTree)10 Symbol (com.sun.tools.javac.code.Symbol)9 Type (com.sun.tools.javac.code.Type)9 Description (com.google.errorprone.matchers.Description)8 IdentifierTree (com.sun.source.tree.IdentifierTree)8 VariableTree (com.sun.source.tree.VariableTree)8 JCTree (com.sun.tools.javac.tree.JCTree)8 List (java.util.List)8 BugPattern (com.google.errorprone.BugPattern)7 ASTHelpers (com.google.errorprone.util.ASTHelpers)7 TreePath (com.sun.source.util.TreePath)7 MethodSymbol (com.sun.tools.javac.code.Symbol.MethodSymbol)7 ArrayList (java.util.ArrayList)7 JDK (com.google.errorprone.BugPattern.Category.JDK)6 MemberSelectTree (com.sun.source.tree.MemberSelectTree)6