Search in sources :

Example 6 with StatementTree

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

the class TreeUtils method containsThisConstructorInvocation.

/**
 * @return true if the first statement in the body is a self constructor invocation within a
 *     constructor
 */
public static final boolean containsThisConstructorInvocation(MethodTree node) {
    if (!TreeUtils.isConstructor(node) || node.getBody().getStatements().isEmpty())
        return false;
    StatementTree st = node.getBody().getStatements().get(0);
    if (!(st instanceof ExpressionStatementTree) || !(((ExpressionStatementTree) st).getExpression() instanceof MethodInvocationTree)) {
        return false;
    }
    MethodInvocationTree invocation = (MethodInvocationTree) ((ExpressionStatementTree) st).getExpression();
    return "this".contentEquals(TreeUtils.methodName(invocation));
}
Also used : ExpressionStatementTree(com.sun.source.tree.ExpressionStatementTree) StatementTree(com.sun.source.tree.StatementTree) MethodInvocationTree(com.sun.source.tree.MethodInvocationTree) ExpressionStatementTree(com.sun.source.tree.ExpressionStatementTree)

Example 7 with StatementTree

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

the class OptionalVisitor method handleConditionalStatementIsPresentGet.

/**
 * Part of rule #3.
 *
 * <p>Pattern match for: {@code if (VAR.isPresent()) { METHOD(VAR.get()); }}
 *
 * <p>Prefer: {@code VAR.ifPresent(METHOD);}
 */
public void handleConditionalStatementIsPresentGet(IfTree node) {
    ExpressionTree condExpr = TreeUtils.skipParens(node.getCondition());
    StatementTree thenStmt = skipBlocks(node.getThenStatement());
    StatementTree elseStmt = skipBlocks(node.getElseStatement());
    if (!(elseStmt == null || (elseStmt.getKind() == Tree.Kind.BLOCK && ((BlockTree) elseStmt).getStatements().isEmpty()))) {
        // else block is missing or is an empty block: "{}"
        return;
    }
    if (!isCallToIsPresent(condExpr)) {
        return;
    }
    ExpressionTree receiver = TreeUtils.getReceiverTree(condExpr);
    if (thenStmt.getKind() != Kind.EXPRESSION_STATEMENT) {
        return;
    }
    ExpressionTree thenExpr = ((ExpressionStatementTree) thenStmt).getExpression();
    if (thenExpr.getKind() != Kind.METHOD_INVOCATION) {
        return;
    }
    MethodInvocationTree invok = (MethodInvocationTree) thenExpr;
    List<? extends ExpressionTree> args = invok.getArguments();
    if (args.size() != 1) {
        return;
    }
    ExpressionTree arg = TreeUtils.skipParens(args.get(0));
    if (!isCallToGet(arg)) {
        return;
    }
    ExpressionTree getReceiver = TreeUtils.getReceiverTree(arg);
    if (!receiver.toString().equals(getReceiver.toString())) {
        return;
    }
    ExpressionTree method = invok.getMethodSelect();
    String methodString = method.toString();
    int dotPos = methodString.lastIndexOf(".");
    if (dotPos != -1) {
        methodString = methodString.substring(0, dotPos) + "::" + methodString.substring(dotPos + 1);
    }
    checker.report(Result.warning("prefer.ifpresent", receiver, methodString), node);
}
Also used : ExpressionStatementTree(com.sun.source.tree.ExpressionStatementTree) StatementTree(com.sun.source.tree.StatementTree) MethodInvocationTree(com.sun.source.tree.MethodInvocationTree) ConditionalExpressionTree(com.sun.source.tree.ConditionalExpressionTree) ExpressionTree(com.sun.source.tree.ExpressionTree) BlockTree(com.sun.source.tree.BlockTree) ExpressionStatementTree(com.sun.source.tree.ExpressionStatementTree)

Example 8 with StatementTree

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

the class TreeUtils method containsThisConstructorInvocation.

/**
 * <p>
 * containsThisConstructorInvocation.
 * </p>
 *
 * @return true if the first statement in the body is a self constructor invocation within a constructor
 * @param node
 *            a {@link com.sun.source.tree.MethodTree} object.
 */
public static final boolean containsThisConstructorInvocation(MethodTree node) {
    if (!TreeUtils.isConstructor(node) || node.getBody().getStatements().isEmpty()) {
        return false;
    }
    StatementTree st = node.getBody().getStatements().get(0);
    if (!(st instanceof ExpressionStatementTree) || !(((ExpressionStatementTree) st).getExpression() instanceof MethodInvocationTree)) {
        return false;
    }
    MethodInvocationTree invocation = (MethodInvocationTree) ((ExpressionStatementTree) st).getExpression();
    return "this".contentEquals(TreeUtils.methodName(invocation));
}
Also used : ExpressionStatementTree(com.sun.source.tree.ExpressionStatementTree) StatementTree(com.sun.source.tree.StatementTree) MethodInvocationTree(com.sun.source.tree.MethodInvocationTree) ExpressionStatementTree(com.sun.source.tree.ExpressionStatementTree)

Example 9 with StatementTree

use of com.sun.source.tree.StatementTree 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 10 with StatementTree

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

the class NewClassObjectInitCheck method visit.

/**
 * {@inheritDoc}
 */
@Override
public Void visit(CheckVisitor visitor, NewClassTree tree, GenerationContext<Void> context) {
    BlockTree initBlock = NewClassWriter.getDoubleBracesBlock(tree);
    TreeWrapper<ClassTree, Void> tw = context.getCurrentWrapper();
    if (initBlock == null && !tw.child(tree.getIdentifier()).isSyntheticType()) {
        return null;
    }
    if (initBlock != null) {
        if (!tw.child(tree.getIdentifier()).isSyntheticType()) {
            context.addError(tree, "Object creation block (double braces {{}}) is only allowed for classes annotated with @SyntheticType");
        }
        for (StatementTree stmt : initBlock.getStatements()) {
            checkStatement(stmt, context);
        }
    }
    return null;
}
Also used : ExpressionStatementTree(com.sun.source.tree.ExpressionStatementTree) StatementTree(com.sun.source.tree.StatementTree) NewClassTree(com.sun.source.tree.NewClassTree) ClassTree(com.sun.source.tree.ClassTree) BlockTree(com.sun.source.tree.BlockTree)

Aggregations

StatementTree (com.sun.source.tree.StatementTree)37 MethodInvocationTree (com.sun.source.tree.MethodInvocationTree)19 ExpressionStatementTree (com.sun.source.tree.ExpressionStatementTree)18 BlockTree (com.sun.source.tree.BlockTree)16 Tree (com.sun.source.tree.Tree)16 ExpressionTree (com.sun.source.tree.ExpressionTree)12 TreePath (com.sun.source.util.TreePath)9 SuggestedFix (com.google.errorprone.fixes.SuggestedFix)8 MethodTree (com.sun.source.tree.MethodTree)8 MemberSelectTree (com.sun.source.tree.MemberSelectTree)7 TryTree (com.sun.source.tree.TryTree)7 VariableTree (com.sun.source.tree.VariableTree)7 CatchTree (com.sun.source.tree.CatchTree)6 JCTree (com.sun.tools.javac.tree.JCTree)6 ClassTree (com.sun.source.tree.ClassTree)5 IdentifierTree (com.sun.source.tree.IdentifierTree)5 MethodSymbol (com.sun.tools.javac.code.Symbol.MethodSymbol)5 VisitorState (com.google.errorprone.VisitorState)4 Description (com.google.errorprone.matchers.Description)4 EnhancedForLoopTree (com.sun.source.tree.EnhancedForLoopTree)4