Search in sources :

Example 6 with AssignmentTree

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

the class ClassWriter method getAnnotationForElement.

private JS getAnnotationForElement(AnnotationTree ann, WriterVisitor<JS> visitor, GenerationContext<JS> context) {
    // XXX: should use here type names?
    if (AnnotationHelper.getRetentionType(ann.getAnnotationType()) == RetentionPolicy.SOURCE) {
        return null;
    }
    String annEntryKey = ann.getAnnotationType().toString();
    if (!context.getConfiguration().getAnnotations().contains(annEntryKey)) {
        return null;
    }
    List<NameValue<JS>> annotationArgsDesc = new ArrayList<NameValue<JS>>();
    for (ExpressionTree arg : ann.getArguments()) {
        AssignmentTree assign = (AssignmentTree) arg;
        annotationArgsDesc.add(NameValue.of(assign.getVariable().toString(), writeAnnotationValue(visitor, assign.getExpression(), context)));
    }
    return context.js().object(annotationArgsDesc);
}
Also used : NameValue(org.stjs.generator.javascript.NameValue) ArrayList(java.util.ArrayList) ExpressionTree(com.sun.source.tree.ExpressionTree) AssignmentTree(com.sun.source.tree.AssignmentTree)

Example 7 with AssignmentTree

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

the class NewClassWriter method getObjectInitializer.

/**
 * special construction for object initialization new Object(){{x = 1; y = 2; }};
 */
private JS getObjectInitializer(WriterVisitor<JS> visitor, TreeWrapper<NewClassTree, JS> tw) {
    NewClassTree tree = tw.getTree();
    BlockTree initBlock = getDoubleBracesBlock(tree);
    if (initBlock == null) {
        if (tw.child(tree.getIdentifier()).isSyntheticType()) {
            // the syntethic type will generate {} constructor even without the initBlock
            return tw.getContext().js().object(new ArrayList<NameValue<JS>>());
        }
        return null;
    }
    if (!tw.child(tree.getIdentifier()).isSyntheticType()) {
        // this is already checked and not allowed
        return null;
    }
    List<NameValue<JS>> props = new ArrayList<NameValue<JS>>();
    for (StatementTree stmt : initBlock.getStatements()) {
        // check the right type of statements x=y is done in NewClassObjectInitCheck
        ExpressionTree expr = ((ExpressionStatementTree) stmt).getExpression();
        if (expr instanceof AssignmentTree) {
            AssignmentTree assign = (AssignmentTree) expr;
            props.add(NameValue.of(getPropertyName(assign.getVariable()), visitor.scan(assign.getExpression(), tw.getContext())));
        } else {
            MethodInvocationTree meth = (MethodInvocationTree) expr;
            String propertyName = MethodToPropertyTemplate.getPropertyName(meth);
            JS value = visitor.scan(meth.getArguments().get(0), tw.getContext());
            props.add(NameValue.of(propertyName, value));
        }
    }
    return tw.getContext().js().object(props);
}
Also used : ArrayList(java.util.ArrayList) ExpressionStatementTree(com.sun.source.tree.ExpressionStatementTree) NewClassTree(com.sun.source.tree.NewClassTree) NameValue(org.stjs.generator.javascript.NameValue) ExpressionStatementTree(com.sun.source.tree.ExpressionStatementTree) StatementTree(com.sun.source.tree.StatementTree) MethodInvocationTree(com.sun.source.tree.MethodInvocationTree) BlockTree(com.sun.source.tree.BlockTree) ExpressionTree(com.sun.source.tree.ExpressionTree) AssignmentTree(com.sun.source.tree.AssignmentTree)

Example 8 with AssignmentTree

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

the class AbstractSuppressWarningsMatcher method getSuggestedFix.

protected final Fix getSuggestedFix(AnnotationTree annotationTree) {
    List<String> values = new ArrayList<>();
    for (ExpressionTree argumentTree : annotationTree.getArguments()) {
        AssignmentTree assignmentTree = (AssignmentTree) argumentTree;
        if (assignmentTree.getVariable().toString().equals("value")) {
            ExpressionTree expressionTree = assignmentTree.getExpression();
            switch(expressionTree.getKind()) {
                case STRING_LITERAL:
                    values.add(((String) ((JCTree.JCLiteral) expressionTree).value));
                    break;
                case NEW_ARRAY:
                    NewArrayTree newArrayTree = (NewArrayTree) expressionTree;
                    for (ExpressionTree elementTree : newArrayTree.getInitializers()) {
                        values.add((String) ((JCTree.JCLiteral) elementTree).value);
                    }
                    break;
                default:
                    throw new AssertionError("Unknown kind: " + expressionTree.getKind());
            }
            processSuppressWarningsValues(values);
        } else {
            throw new AssertionError("SuppressWarnings has an element other than value=");
        }
    }
    if (values.isEmpty()) {
        return SuggestedFix.delete(annotationTree);
    } else if (values.size() == 1) {
        return SuggestedFix.replace(annotationTree, "@SuppressWarnings(\"" + values.get(0) + "\")");
    } else {
        return SuggestedFix.replace(annotationTree, "@SuppressWarnings({\"" + Joiner.on("\", \"").join(values) + "\"})");
    }
}
Also used : NewArrayTree(com.sun.source.tree.NewArrayTree) ArrayList(java.util.ArrayList) ExpressionTree(com.sun.source.tree.ExpressionTree) JCTree(com.sun.tools.javac.tree.JCTree) AssignmentTree(com.sun.source.tree.AssignmentTree)

Example 9 with AssignmentTree

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

the class AnnotationMatcherUtils method getArgument.

/**
 * Gets the value for an argument, or null if the argument does not exist.
 *
 * @param annotationTree the AST node for the annotation
 * @param name the name of the argument whose value to get
 * @return the value of the argument, or null if the argument does not exist
 */
public static ExpressionTree getArgument(AnnotationTree annotationTree, String name) {
    for (ExpressionTree argumentTree : annotationTree.getArguments()) {
        if (argumentTree.getKind() != Tree.Kind.ASSIGNMENT) {
            continue;
        }
        AssignmentTree assignmentTree = (AssignmentTree) argumentTree;
        if (!assignmentTree.getVariable().toString().equals(name)) {
            continue;
        }
        ExpressionTree expressionTree = assignmentTree.getExpression();
        return expressionTree;
    }
    return null;
}
Also used : ExpressionTree(com.sun.source.tree.ExpressionTree) AssignmentTree(com.sun.source.tree.AssignmentTree)

Example 10 with AssignmentTree

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

the class JdkObsolete method getTargetType.

static Type getTargetType(VisitorState state) {
    Tree parent = state.getPath().getParentPath().getLeaf();
    Type type;
    if (parent instanceof VariableTree || parent instanceof AssignmentTree) {
        type = ASTHelpers.getType(parent);
    } else if (parent instanceof ReturnTree || parent instanceof LambdaExpressionTree) {
        type = getMethodOrLambdaReturnType(state);
    } else if (parent instanceof MethodInvocationTree) {
        MethodInvocationTree tree = (MethodInvocationTree) parent;
        int idx = tree.getArguments().indexOf(state.getPath().getLeaf());
        if (idx == -1) {
            return null;
        }
        Type methodType = ASTHelpers.getType(tree.getMethodSelect());
        if (idx >= methodType.getParameterTypes().size()) {
            return null;
        }
        return methodType.getParameterTypes().get(idx);
    } else {
        return null;
    }
    Tree tree = state.getPath().getLeaf();
    if (tree instanceof MemberReferenceTree) {
        type = state.getTypes().findDescriptorType(ASTHelpers.getType(tree)).getReturnType();
    }
    return type;
}
Also used : LambdaExpressionTree(com.sun.source.tree.LambdaExpressionTree) Type(com.sun.tools.javac.code.Type) MethodInvocationTree(com.sun.source.tree.MethodInvocationTree) MemberReferenceTree(com.sun.source.tree.MemberReferenceTree) VariableTree(com.sun.source.tree.VariableTree) 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) AssignmentTree(com.sun.source.tree.AssignmentTree) ReturnTree(com.sun.source.tree.ReturnTree)

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