Java Code Examples for org.codehaus.groovy.ast.stmt.ExpressionStatement#getExpression()

The following examples show how to use org.codehaus.groovy.ast.stmt.ExpressionStatement#getExpression() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AstUtils.java    From pushfish-android with BSD 2-Clause "Simplified" License 6 votes vote down vote up
@Nullable
public static MethodCallExpression extractBareMethodCall(Statement statement) {
    if (!(statement instanceof ExpressionStatement)) {
        return null;
    }

    ExpressionStatement expressionStatement = (ExpressionStatement) statement;
    if (!(expressionStatement.getExpression() instanceof MethodCallExpression)) {
        return null;
    }

    MethodCallExpression methodCall = (MethodCallExpression) expressionStatement.getExpression();
    if (!targetIsThis(methodCall)) {
        return null;
    }

    return methodCall;
}
 
Example 2
Source File: TryWithResourcesASTTransformation.java    From groovy with Apache License 2.0 6 votes vote down vote up
private ExpressionStatement makeVariableDeclarationFinal(ExpressionStatement variableDeclaration) {
    if (!asBoolean(variableDeclaration)) {
        return variableDeclaration;
    }

    if (!(variableDeclaration.getExpression() instanceof DeclarationExpression)) {
        throw new IllegalArgumentException("variableDeclaration is not a declaration statement");
    }

    DeclarationExpression declarationExpression = (DeclarationExpression) variableDeclaration.getExpression();
    if (!(declarationExpression.getLeftExpression() instanceof VariableExpression)) {
        throw astBuilder.createParsingFailedException("The expression statement is not a variable delcaration statement", variableDeclaration);
    }

    VariableExpression variableExpression = (VariableExpression) declarationExpression.getLeftExpression();
    variableExpression.setModifiers(variableExpression.getModifiers() | Opcodes.ACC_FINAL);

    return variableDeclaration;
}
 
Example 3
Source File: GeneralUtils.java    From groovy with Apache License 2.0 6 votes vote down vote up
public static boolean copyStatementsWithSuperAdjustment(final ClosureExpression pre, final BlockStatement body) {
    Statement preCode = pre.getCode();
    boolean changed = false;
    if (preCode instanceof BlockStatement) {
        BlockStatement block = (BlockStatement) preCode;
        List<Statement> statements = block.getStatements();
        for (int i = 0, n = statements.size(); i < n; i += 1) {
            Statement statement = statements.get(i);
            // adjust the first statement if it's a super call
            if (i == 0 && statement instanceof ExpressionStatement) {
                ExpressionStatement es = (ExpressionStatement) statement;
                Expression preExp = es.getExpression();
                if (preExp instanceof MethodCallExpression) {
                    MethodCallExpression mce = (MethodCallExpression) preExp;
                    String name = mce.getMethodAsString();
                    if ("super".equals(name)) {
                        es.setExpression(new ConstructorCallExpression(ClassNode.SUPER, mce.getArguments()));
                        changed = true;
                    }
                }
            }
            body.addStatement(statement);
        }
    }
    return changed;
}
 
Example 4
Source File: AstUtils.java    From Pushjet-Android with BSD 2-Clause "Simplified" License 6 votes vote down vote up
@Nullable
public static MethodCallExpression extractBareMethodCall(Statement statement) {
    if (!(statement instanceof ExpressionStatement)) {
        return null;
    }

    ExpressionStatement expressionStatement = (ExpressionStatement) statement;
    if (!(expressionStatement.getExpression() instanceof MethodCallExpression)) {
        return null;
    }

    MethodCallExpression methodCall = (MethodCallExpression) expressionStatement.getExpression();
    if (!targetIsThis(methodCall)) {
        return null;
    }

    return methodCall;
}
 
Example 5
Source File: ReferenceExtractor.java    From pushfish-android with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Override
public void visitExpressionStatement(ExpressionStatement statement) {
    super.visitExpressionStatement(statement);
    Expression expression = statement.getExpression();
    Boolean shouldRemoveExpression = expression.getNodeMetaData(AST_NODE_REMOVE_KEY);
    if (shouldRemoveExpression != null && shouldRemoveExpression) {
        statement.setExpression(new EmptyExpression());
    } else {
        statement.setExpression(rewrittenOrOriginal(expression));
    }
}
 
Example 6
Source File: PluginsAndBuildscriptTransformer.java    From pushfish-android with BSD 2-Clause "Simplified" License 5 votes vote down vote up
private ScriptBlock detectScriptBlock(Statement statement) {
    if (!(statement instanceof ExpressionStatement)) {
        return null;
    }

    ExpressionStatement expressionStatement = (ExpressionStatement) statement;
    if (!(expressionStatement.getExpression() instanceof MethodCallExpression)) {
        return null;
    }

    MethodCallExpression methodCall = (MethodCallExpression) expressionStatement.getExpression();
    if (!AstUtils.targetIsThis(methodCall)) {
        return null;
    }

    if (!(methodCall.getMethod() instanceof ConstantExpression)) {
        return null;
    }

    String methodName = methodCall.getMethod().getText();

    if (methodName.equals(PLUGINS) || methodName.equals(classpathBlockName)) {
        if (!(methodCall.getArguments() instanceof ArgumentListExpression)) {
            return null;
        }

        ArgumentListExpression args = (ArgumentListExpression) methodCall.getArguments();
        if (args.getExpressions().size() == 1 && args.getExpression(0) instanceof ClosureExpression) {
            return new ScriptBlock(methodName, (ClosureExpression) args.getExpression(0));
        } else {
            return null;
        }
    } else {
        return null;
    }
}
 
Example 7
Source File: ASTUtils.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private static ASTNode getVariableInBlockStatement(BlockStatement block, String variable) {
    for (Object object : block.getStatements()) {
        if (object instanceof ExpressionStatement) {
            ExpressionStatement expressionStatement = (ExpressionStatement) object;
            Expression expression = expressionStatement.getExpression();
            if (expression instanceof DeclarationExpression) {
                DeclarationExpression declaration = (DeclarationExpression) expression;
                if (variable.equals(declaration.getVariableExpression().getName())) {
                    return declaration.getVariableExpression();
                }
            }
        }
    }
    return null;
}
 
Example 8
Source File: StatementWriter.java    From groovy with Apache License 2.0 5 votes vote down vote up
public void writeExpressionStatement(final ExpressionStatement statement) {
    controller.getAcg().onLineNumber(statement, "visitExpressionStatement: " + statement.getExpression().getClass().getName());
    writeStatementLabel(statement);

    int mark = controller.getOperandStack().getStackLength();
    Expression expression = statement.getExpression();
    expression.visit(controller.getAcg());
    controller.getOperandStack().popDownTo(mark);
}
 
Example 9
Source File: ReferenceExtractor.java    From Pushjet-Android with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Override
public void visitExpressionStatement(ExpressionStatement statement) {
    super.visitExpressionStatement(statement);
    Expression expression = statement.getExpression();
    Boolean shouldRemoveExpression = expression.getNodeMetaData(AST_NODE_REMOVE_KEY);
    if (shouldRemoveExpression != null && shouldRemoveExpression) {
        statement.setExpression(new EmptyExpression());
    } else {
        statement.setExpression(rewrittenOrOriginal(expression));
    }
}
 
Example 10
Source File: PluginsAndBuildscriptTransformer.java    From Pushjet-Android with BSD 2-Clause "Simplified" License 5 votes vote down vote up
private ScriptBlock detectScriptBlock(Statement statement) {
    if (!(statement instanceof ExpressionStatement)) {
        return null;
    }

    ExpressionStatement expressionStatement = (ExpressionStatement) statement;
    if (!(expressionStatement.getExpression() instanceof MethodCallExpression)) {
        return null;
    }

    MethodCallExpression methodCall = (MethodCallExpression) expressionStatement.getExpression();
    if (!AstUtils.targetIsThis(methodCall)) {
        return null;
    }

    if (!(methodCall.getMethod() instanceof ConstantExpression)) {
        return null;
    }

    String methodName = methodCall.getMethod().getText();

    if (methodName.equals(PLUGINS) || methodName.equals(classpathBlockName)) {
        if (!(methodCall.getArguments() instanceof ArgumentListExpression)) {
            return null;
        }

        ArgumentListExpression args = (ArgumentListExpression) methodCall.getArguments();
        if (args.getExpressions().size() == 1 && args.getExpression(0) instanceof ClosureExpression) {
            return new ScriptBlock(methodName, (ClosureExpression) args.getExpression(0));
        } else {
            return null;
        }
    } else {
        return null;
    }
}
 
Example 11
Source File: GroovyASTUtils.java    From groovy-language-server with Apache License 2.0 4 votes vote down vote up
public static ASTNode getDefinition(ASTNode node, boolean strict, ASTNodeVisitor astVisitor) {
    if (node == null) {
        return null;
    }
    ASTNode parentNode = astVisitor.getParent(node);
    if (node instanceof ExpressionStatement) {
        ExpressionStatement statement = (ExpressionStatement) node;
        node = statement.getExpression();
    }
    if (node instanceof ClassNode) {
        return tryToResolveOriginalClassNode((ClassNode) node, strict, astVisitor);
    } else if (node instanceof ConstructorCallExpression) {
        ConstructorCallExpression callExpression = (ConstructorCallExpression) node;
        return GroovyASTUtils.getMethodFromCallExpression(callExpression, astVisitor);
    } else if (node instanceof DeclarationExpression) {
        DeclarationExpression declExpression = (DeclarationExpression) node;
        if (!declExpression.isMultipleAssignmentDeclaration()) {
            ClassNode originType = declExpression.getVariableExpression().getOriginType();
            return tryToResolveOriginalClassNode(originType, strict, astVisitor);
        }
    } else if (node instanceof ClassExpression) {
        ClassExpression classExpression = (ClassExpression) node;
        return tryToResolveOriginalClassNode(classExpression.getType(), strict, astVisitor);
    } else if (node instanceof ImportNode) {
        ImportNode importNode = (ImportNode) node;
        return tryToResolveOriginalClassNode(importNode.getType(), strict, astVisitor);
    } else if (node instanceof MethodNode) {
        return node;
    } else if (node instanceof ConstantExpression && parentNode != null) {
        if (parentNode instanceof MethodCallExpression) {
            MethodCallExpression methodCallExpression = (MethodCallExpression) parentNode;
            return GroovyASTUtils.getMethodFromCallExpression(methodCallExpression, astVisitor);
        } else if (parentNode instanceof PropertyExpression) {
            PropertyExpression propertyExpression = (PropertyExpression) parentNode;
            PropertyNode propNode = GroovyASTUtils.getPropertyFromExpression(propertyExpression, astVisitor);
            if (propNode != null) {
                return propNode;
            }
            return GroovyASTUtils.getFieldFromExpression(propertyExpression, astVisitor);
        }
    } else if (node instanceof VariableExpression) {
        VariableExpression variableExpression = (VariableExpression) node;
        Variable accessedVariable = variableExpression.getAccessedVariable();
        if (accessedVariable instanceof ASTNode) {
            return (ASTNode) accessedVariable;
        }
        // DynamicVariable is not an ASTNode, so skip it
        return null;
    } else if (node instanceof Variable) {
        return node;
    }
    return null;
}
 
Example 12
Source File: FieldASTTransformation.java    From groovy with Apache License 2.0 4 votes vote down vote up
@Override
public void visitExpressionStatement(ExpressionStatement es) {
    Expression exp = es.getExpression();
    exp.visit(this);
    super.visitExpressionStatement(es);
}
 
Example 13
Source File: JavaStubGenerator.java    From groovy with Apache License 2.0 4 votes vote down vote up
private void printMethod(PrintWriter out, ClassNode clazz, MethodNode methodNode) {
    if (methodNode.getName().equals("<clinit>")) return;
    if (methodNode.isPrivate() || !Utilities.isJavaIdentifier(methodNode.getName())) return;
    if (methodNode.isSynthetic() && methodNode.getName().equals("$getStaticMetaClass")) return;

    printAnnotations(out, methodNode);
    if (!isInterfaceOrTrait(clazz)) {
        int modifiers = methodNode.getModifiers();
        if (isDefaultTraitImpl(methodNode)) {
            modifiers ^= Opcodes.ACC_ABSTRACT;
        }
        printModifiers(out, modifiers & ~(clazz.isEnum() ? Opcodes.ACC_ABSTRACT : 0));
    }

    printGenericsBounds(out, methodNode.getGenericsTypes());
    out.print(" ");
    printType(out, methodNode.getReturnType());
    out.print(" ");
    out.print(methodNode.getName());

    printParams(out, methodNode);

    ClassNode[] exceptions = methodNode.getExceptions();
    printExceptions(out, exceptions);

    if (Traits.isTrait(clazz)) {
        out.println(";");
    } else if (isAbstract(methodNode) && !clazz.isEnum()) {
        if (clazz.isAnnotationDefinition() && methodNode.hasAnnotationDefault()) {
            Statement fs = methodNode.getFirstStatement();
            if (fs instanceof ExpressionStatement) {
                ExpressionStatement es = (ExpressionStatement) fs;
                Expression re = es.getExpression();
                out.print(" default ");
                ClassNode rt = methodNode.getReturnType();
                boolean classReturn = ClassHelper.CLASS_Type.equals(rt) || (rt.isArray() && ClassHelper.CLASS_Type.equals(rt.getComponentType()));
                if (re instanceof ListExpression) {
                    out.print("{ ");
                    ListExpression le = (ListExpression) re;
                    boolean first = true;
                    for (Expression expression : le.getExpressions()) {
                        if (first) first = false;
                        else out.print(", ");
                        printValue(out, expression, classReturn);
                    }
                    out.print(" }");
                } else {
                    printValue(out, re, classReturn);
                }
            }
        }
        out.println(";");
    } else {
        out.print(" { ");
        ClassNode retType = methodNode.getReturnType();
        printReturn(out, retType);
        out.println("}");
    }
}