Search in sources :

Example 1 with AssignmentTree

use of com.sun.source.tree.AssignmentTree in project bazel by bazelbuild.

the class TreeUtils method getAssignmentContext.

/**
     * Returns the tree with the assignment context for the treePath
     * leaf node.
     *
     * The assignment context for the treepath is the most enclosing
     * tree of type:
     * <ul>
     *   <li>AssignmentTree </li>
     *   <li>CompoundAssignmentTree </li>
     *   <li>MethodInvocationTree</li>
     *   <li>NewArrayTree</li>
     *   <li>NewClassTree</li>
     *   <li>ReturnTree</li>
     *   <li>VariableTree</li>
     * </ul>
     *
     * @param treePath
     * @return  the assignment context as described.
     */
public static Tree getAssignmentContext(final TreePath treePath) {
    TreePath path = treePath.getParentPath();
    if (path == null)
        return null;
    Tree node = path.getLeaf();
    if ((node instanceof AssignmentTree) || (node instanceof CompoundAssignmentTree) || (node instanceof MethodInvocationTree) || (node instanceof NewArrayTree) || (node instanceof NewClassTree) || (node instanceof ReturnTree) || (node instanceof VariableTree))
        return node;
    return null;
}
Also used : TreePath(com.sun.source.util.TreePath) NewArrayTree(com.sun.source.tree.NewArrayTree) MethodInvocationTree(com.sun.source.tree.MethodInvocationTree) VariableTree(com.sun.source.tree.VariableTree) ReturnTree(com.sun.source.tree.ReturnTree) ArrayAccessTree(com.sun.source.tree.ArrayAccessTree) CompoundAssignmentTree(com.sun.source.tree.CompoundAssignmentTree) LiteralTree(com.sun.source.tree.LiteralTree) MethodTree(com.sun.source.tree.MethodTree) BinaryTree(com.sun.source.tree.BinaryTree) VariableTree(com.sun.source.tree.VariableTree) AnnotatedTypeTree(com.sun.source.tree.AnnotatedTypeTree) MethodInvocationTree(com.sun.source.tree.MethodInvocationTree) AssignmentTree(com.sun.source.tree.AssignmentTree) TypeCastTree(com.sun.source.tree.TypeCastTree) NewClassTree(com.sun.source.tree.NewClassTree) ParameterizedTypeTree(com.sun.source.tree.ParameterizedTypeTree) IdentifierTree(com.sun.source.tree.IdentifierTree) NewArrayTree(com.sun.source.tree.NewArrayTree) Tree(com.sun.source.tree.Tree) ClassTree(com.sun.source.tree.ClassTree) ParenthesizedTree(com.sun.source.tree.ParenthesizedTree) ExpressionTree(com.sun.source.tree.ExpressionTree) MemberSelectTree(com.sun.source.tree.MemberSelectTree) JCTree(com.sun.tools.javac.tree.JCTree) ExpressionStatementTree(com.sun.source.tree.ExpressionStatementTree) BlockTree(com.sun.source.tree.BlockTree) PrimitiveTypeTree(com.sun.source.tree.PrimitiveTypeTree) StatementTree(com.sun.source.tree.StatementTree) NewClassTree(com.sun.source.tree.NewClassTree) CompoundAssignmentTree(com.sun.source.tree.CompoundAssignmentTree) AssignmentTree(com.sun.source.tree.AssignmentTree) ReturnTree(com.sun.source.tree.ReturnTree) CompoundAssignmentTree(com.sun.source.tree.CompoundAssignmentTree)

Example 2 with AssignmentTree

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

the class BoxedPrimitiveConstructor method maybeCast.

private String maybeCast(VisitorState state, Type type, Type argType) {
    if (doubleAndFloatStatus(state, type, argType) == DoubleAndFloatStatus.PRIMITIVE_DOUBLE_INTO_FLOAT) {
        // e.g.: new Float(3.0d) => (float) 3.0d
        return "(float) ";
    }
    // primitive widening conversions can't be combined with autoboxing, so add a
    // explicit widening cast unless we're sure the expression doesn't get autoboxed
    Tree parent = state.getPath().getParentPath().getLeaf();
    // TODO(cushon): de-dupe with UnnecessaryCast
    Type targetType = parent.accept(new TreeScanner<Type, Void>() {

        @Override
        public Type visitAssignment(AssignmentTree node, Void unused) {
            return getType(node.getVariable());
        }

        @Override
        public Type visitCompoundAssignment(CompoundAssignmentTree node, Void unused) {
            return getType(node.getVariable());
        }

        @Override
        public Type visitReturn(ReturnTree node, Void unused) {
            return getType(state.findEnclosing(MethodTree.class).getReturnType());
        }

        @Override
        public Type visitVariable(VariableTree node, Void unused) {
            return getType(node.getType());
        }
    }, null);
    if (!isSameType(type, argType, state) && !isSameType(targetType, type, state)) {
        return String.format("(%s) ", type);
    }
    return "";
}
Also used : Matchers.toType(com.google.errorprone.matchers.Matchers.toType) ASTHelpers.getType(com.google.errorprone.util.ASTHelpers.getType) ASTHelpers.isSameType(com.google.errorprone.util.ASTHelpers.isSameType) Type(com.sun.tools.javac.code.Type) MethodTree(com.sun.source.tree.MethodTree) VariableTree(com.sun.source.tree.VariableTree) ReturnTree(com.sun.source.tree.ReturnTree) CompoundAssignmentTree(com.sun.source.tree.CompoundAssignmentTree) LiteralTree(com.sun.source.tree.LiteralTree) MethodTree(com.sun.source.tree.MethodTree) VariableTree(com.sun.source.tree.VariableTree) AssignmentTree(com.sun.source.tree.AssignmentTree) NewClassTree(com.sun.source.tree.NewClassTree) Tree(com.sun.source.tree.Tree) ExpressionTree(com.sun.source.tree.ExpressionTree) JCTree(com.sun.tools.javac.tree.JCTree) CompoundAssignmentTree(com.sun.source.tree.CompoundAssignmentTree) AssignmentTree(com.sun.source.tree.AssignmentTree) ReturnTree(com.sun.source.tree.ReturnTree) CompoundAssignmentTree(com.sun.source.tree.CompoundAssignmentTree)

Example 3 with AssignmentTree

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

the class CompileTimeConstantExpressionMatcherTest method assertCompilerMatchesOnAssignment.

// Helper methods.
private void assertCompilerMatchesOnAssignment(final Map<String, Boolean> expectedMatches, String... lines) {
    final Matcher<ExpressionTree> matcher = new CompileTimeConstantExpressionMatcher();
    final Scanner scanner = new Scanner() {

        @Override
        public Void visitAssignment(AssignmentTree t, VisitorState state) {
            ExpressionTree lhs = t.getVariable();
            if (expectedMatches.containsKey(lhs.toString())) {
                boolean matches = matcher.matches(t.getExpression(), state);
                if (expectedMatches.get(lhs.toString())) {
                    assertTrue("Matcher should match expression" + t.getExpression(), matches);
                } else {
                    assertFalse("Matcher should not match expression" + t.getExpression(), matches);
                }
            }
            return super.visitAssignment(t, state);
        }
    };
    CompilationTestHelper.newInstance(ScannerSupplier.fromScanner(scanner), getClass()).expectResult(Result.OK).addSourceLines("test/CompileTimeConstantExpressionMatcherTestCase.java", lines).doTest();
}
Also used : Scanner(com.google.errorprone.scanner.Scanner) VisitorState(com.google.errorprone.VisitorState) ExpressionTree(com.sun.source.tree.ExpressionTree) AssignmentTree(com.sun.source.tree.AssignmentTree)

Example 4 with AssignmentTree

use of com.sun.source.tree.AssignmentTree in project checker-framework by typetools.

the class BaseTypeVisitor method visitAnnotation.

/* TODO: something similar to visitReturn should be done.
     * public Void visitThrow(ThrowTree node, Void p) {
     * return super.visitThrow(node, p);
     * }
     */
/**
 * Ensure that the annotation arguments comply to their declarations. This needs some special
 * casing, as annotation arguments form special trees.
 */
@Override
public Void visitAnnotation(AnnotationTree node, Void p) {
    List<? extends ExpressionTree> args = node.getArguments();
    if (args.isEmpty()) {
        // Nothing to do if there are no annotation arguments.
        return null;
    }
    TypeElement anno = (TypeElement) TreeInfo.symbol((JCTree) node.getAnnotationType());
    Name annoName = anno.getQualifiedName();
    if (annoName.contentEquals(DefaultQualifier.class.getName()) || annoName.contentEquals(SuppressWarnings.class.getName())) {
        // Skip these two annotations, as we don't care about the arguments to them.
        return null;
    }
    // Mapping from argument simple name to its annotated type.
    Map<String, AnnotatedTypeMirror> annoTypes = new HashMap<>();
    for (Element encl : ElementFilter.methodsIn(anno.getEnclosedElements())) {
        AnnotatedExecutableType exeatm = (AnnotatedExecutableType) atypeFactory.getAnnotatedType(encl);
        AnnotatedTypeMirror retty = exeatm.getReturnType();
        annoTypes.put(encl.getSimpleName().toString(), retty);
    }
    for (ExpressionTree arg : args) {
        if (!(arg instanceof AssignmentTree)) {
            // TODO: when can this happen?
            continue;
        }
        AssignmentTree at = (AssignmentTree) arg;
        // we don't have a type for annotations.
        if (at.getExpression().getKind() == Tree.Kind.ANNOTATION) {
            visitAnnotation((AnnotationTree) at.getExpression(), p);
            continue;
        }
        if (at.getExpression().getKind() == Tree.Kind.NEW_ARRAY) {
            NewArrayTree nat = (NewArrayTree) at.getExpression();
            boolean isAnno = false;
            for (ExpressionTree init : nat.getInitializers()) {
                if (init.getKind() == Tree.Kind.ANNOTATION) {
                    visitAnnotation((AnnotationTree) init, p);
                    isAnno = true;
                }
            }
            if (isAnno) {
                continue;
            }
        }
        AnnotatedTypeMirror expected = annoTypes.get(at.getVariable().toString());
        Pair<Tree, AnnotatedTypeMirror> preAssCtxt = visitorState.getAssignmentContext();
        {
            // Determine and set the new assignment context.
            ExpressionTree var = at.getVariable();
            assert var instanceof IdentifierTree : "Expected IdentifierTree as context. Found: " + var;
            AnnotatedTypeMirror meth = atypeFactory.getAnnotatedType(var);
            assert meth instanceof AnnotatedExecutableType : "Expected AnnotatedExecutableType as context. Found: " + meth;
            AnnotatedTypeMirror newctx = ((AnnotatedExecutableType) meth).getReturnType();
            visitorState.setAssignmentContext(Pair.of((Tree) null, newctx));
        }
        try {
            AnnotatedTypeMirror actual = atypeFactory.getAnnotatedType(at.getExpression());
            if (expected.getKind() != TypeKind.ARRAY) {
                // Expected is not an array -> direct comparison.
                commonAssignmentCheck(expected, actual, at.getExpression(), "annotation.type.incompatible");
            } else {
                if (actual.getKind() == TypeKind.ARRAY) {
                    // Both actual and expected are arrays.
                    commonAssignmentCheck(expected, actual, at.getExpression(), "annotation.type.incompatible");
                } else {
                    // The declaration is an array type, but just a single
                    // element is given.
                    commonAssignmentCheck(((AnnotatedArrayType) expected).getComponentType(), actual, at.getExpression(), "annotation.type.incompatible");
                }
            }
        } finally {
            visitorState.setAssignmentContext(preAssCtxt);
        }
    }
    return null;
}
Also used : HashMap(java.util.HashMap) TypeElement(javax.lang.model.element.TypeElement) TypeElement(javax.lang.model.element.TypeElement) Element(javax.lang.model.element.Element) VariableElement(javax.lang.model.element.VariableElement) ExecutableElement(javax.lang.model.element.ExecutableElement) JCTree(com.sun.tools.javac.tree.JCTree) IdentifierTree(com.sun.source.tree.IdentifierTree) AnnotatedTypeMirror(org.checkerframework.framework.type.AnnotatedTypeMirror) Name(javax.lang.model.element.Name) AnnotatedExecutableType(org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedExecutableType) NewArrayTree(com.sun.source.tree.NewArrayTree) LambdaExpressionTree(com.sun.source.tree.LambdaExpressionTree) ConditionalExpressionTree(com.sun.source.tree.ConditionalExpressionTree) ExpressionTree(com.sun.source.tree.ExpressionTree) CompoundAssignmentTree(com.sun.source.tree.CompoundAssignmentTree) MethodInvocationTree(com.sun.source.tree.MethodInvocationTree) AssignmentTree(com.sun.source.tree.AssignmentTree) TypeCastTree(com.sun.source.tree.TypeCastTree) LambdaExpressionTree(com.sun.source.tree.LambdaExpressionTree) InstanceOfTree(com.sun.source.tree.InstanceOfTree) ConditionalExpressionTree(com.sun.source.tree.ConditionalExpressionTree) MemberSelectTree(com.sun.source.tree.MemberSelectTree) ThrowTree(com.sun.source.tree.ThrowTree) EnhancedForLoopTree(com.sun.source.tree.EnhancedForLoopTree) ReturnTree(com.sun.source.tree.ReturnTree) 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) Tree(com.sun.source.tree.Tree) ExpressionTree(com.sun.source.tree.ExpressionTree) ArrayAccessTree(com.sun.source.tree.ArrayAccessTree) IdentifierTree(com.sun.source.tree.IdentifierTree) CatchTree(com.sun.source.tree.CatchTree) NewArrayTree(com.sun.source.tree.NewArrayTree) CompilationUnitTree(com.sun.source.tree.CompilationUnitTree) AnnotationTree(com.sun.source.tree.AnnotationTree) MethodTree(com.sun.source.tree.MethodTree) ClassTree(com.sun.source.tree.ClassTree) MemberReferenceTree(com.sun.source.tree.MemberReferenceTree) JCTree(com.sun.tools.javac.tree.JCTree) CompoundAssignmentTree(com.sun.source.tree.CompoundAssignmentTree) AssignmentTree(com.sun.source.tree.AssignmentTree)

Example 5 with AssignmentTree

use of com.sun.source.tree.AssignmentTree in project st-js by st-js.

the class TreeUtils method getAssignmentContext.

/**
 * Returns the tree with the assignment context for the treePath leaf node.
 *
 * The assignment context for the treepath is the most enclosing tree of type:
 * <ul>
 * <li>AssignmentTree</li>
 * <li>CompoundAssignmentTree</li>
 * <li>MethodInvocationTree</li>
 * <li>NewArrayTree</li>
 * <li>NewClassTree</li>
 * <li>ReturnTree</li>
 * <li>VariableTree</li>
 * </ul>
 *
 * @param treePath
 *            a {@link com.sun.source.util.TreePath} object.
 * @return the assignment context as described.
 */
public static Tree getAssignmentContext(final TreePath treePath) {
    TreePath path = treePath.getParentPath();
    if (path == null) {
        return null;
    }
    Tree node = path.getLeaf();
    if ((node instanceof AssignmentTree) || (node instanceof CompoundAssignmentTree) || (node instanceof MethodInvocationTree) || (node instanceof NewArrayTree) || (node instanceof NewClassTree) || (node instanceof ReturnTree) || (node instanceof VariableTree)) {
        return node;
    }
    return null;
}
Also used : TreePath(com.sun.source.util.TreePath) NewArrayTree(com.sun.source.tree.NewArrayTree) MethodInvocationTree(com.sun.source.tree.MethodInvocationTree) VariableTree(com.sun.source.tree.VariableTree) ReturnTree(com.sun.source.tree.ReturnTree) ArrayAccessTree(com.sun.source.tree.ArrayAccessTree) CompoundAssignmentTree(com.sun.source.tree.CompoundAssignmentTree) LiteralTree(com.sun.source.tree.LiteralTree) MethodTree(com.sun.source.tree.MethodTree) BinaryTree(com.sun.source.tree.BinaryTree) VariableTree(com.sun.source.tree.VariableTree) MethodInvocationTree(com.sun.source.tree.MethodInvocationTree) AssignmentTree(com.sun.source.tree.AssignmentTree) TypeCastTree(com.sun.source.tree.TypeCastTree) NewClassTree(com.sun.source.tree.NewClassTree) ParameterizedTypeTree(com.sun.source.tree.ParameterizedTypeTree) IdentifierTree(com.sun.source.tree.IdentifierTree) NewArrayTree(com.sun.source.tree.NewArrayTree) Tree(com.sun.source.tree.Tree) ClassTree(com.sun.source.tree.ClassTree) ParenthesizedTree(com.sun.source.tree.ParenthesizedTree) ExpressionTree(com.sun.source.tree.ExpressionTree) MemberSelectTree(com.sun.source.tree.MemberSelectTree) JCTree(com.sun.tools.javac.tree.JCTree) ExpressionStatementTree(com.sun.source.tree.ExpressionStatementTree) BlockTree(com.sun.source.tree.BlockTree) PrimitiveTypeTree(com.sun.source.tree.PrimitiveTypeTree) StatementTree(com.sun.source.tree.StatementTree) NewClassTree(com.sun.source.tree.NewClassTree) CompoundAssignmentTree(com.sun.source.tree.CompoundAssignmentTree) AssignmentTree(com.sun.source.tree.AssignmentTree) ReturnTree(com.sun.source.tree.ReturnTree) CompoundAssignmentTree(com.sun.source.tree.CompoundAssignmentTree)

Aggregations

AssignmentTree (com.sun.source.tree.AssignmentTree)20 ExpressionTree (com.sun.source.tree.ExpressionTree)20 MethodInvocationTree (com.sun.source.tree.MethodInvocationTree)13 Tree (com.sun.source.tree.Tree)13 VariableTree (com.sun.source.tree.VariableTree)13 NewClassTree (com.sun.source.tree.NewClassTree)12 MemberSelectTree (com.sun.source.tree.MemberSelectTree)11 IdentifierTree (com.sun.source.tree.IdentifierTree)10 JCTree (com.sun.tools.javac.tree.JCTree)10 ClassTree (com.sun.source.tree.ClassTree)9 CompoundAssignmentTree (com.sun.source.tree.CompoundAssignmentTree)9 MethodTree (com.sun.source.tree.MethodTree)9 NewArrayTree (com.sun.source.tree.NewArrayTree)9 ReturnTree (com.sun.source.tree.ReturnTree)9 ArrayAccessTree (com.sun.source.tree.ArrayAccessTree)7 ConditionalExpressionTree (com.sun.source.tree.ConditionalExpressionTree)7 ParameterizedTypeTree (com.sun.source.tree.ParameterizedTypeTree)7 TypeCastTree (com.sun.source.tree.TypeCastTree)7 LambdaExpressionTree (com.sun.source.tree.LambdaExpressionTree)6 ArrayList (java.util.ArrayList)6