Search in sources :

Example 1 with ClassNodeUtils

use of org.apache.groovy.ast.tools.ClassNodeUtils in project groovy by apache.

the class Verifier method addDefaultParameterConstructors.

/**
 * Creates a new constructor for each combination of default parameter expressions.
 */
protected void addDefaultParameterConstructors(final ClassNode type) {
    List<ConstructorNode> constructors = new ArrayList<>(type.getDeclaredConstructors());
    addDefaultParameters(constructors, (arguments, params, method) -> {
        // GROOVY-9151: check for references to parameters that have been removed
        for (ListIterator<Expression> it = arguments.getExpressions().listIterator(); it.hasNext(); ) {
            Expression argument = it.next();
            if (argument instanceof CastExpression) {
                argument = ((CastExpression) argument).getExpression();
            }
            if (argument instanceof VariableExpression) {
                VariableExpression v = (VariableExpression) argument;
                if (v.getAccessedVariable() instanceof Parameter) {
                    Parameter p = (Parameter) v.getAccessedVariable();
                    if (p.hasInitialExpression() && !Arrays.asList(params).contains(p) && p.getInitialExpression() instanceof ConstantExpression) {
                        // replace argument "(Type) param" with "(Type) <param's default>" for simple default value
                        it.set(castX(method.getParameters()[it.nextIndex() - 1].getType(), p.getInitialExpression()));
                    }
                }
            }
        }
        GroovyCodeVisitor visitor = new CodeVisitorSupport() {

            @Override
            public void visitVariableExpression(final VariableExpression e) {
                if (e.getAccessedVariable() instanceof Parameter) {
                    Parameter p = (Parameter) e.getAccessedVariable();
                    if (p.hasInitialExpression() && !Arrays.asList(params).contains(p)) {
                        String error = String.format("The generated constructor \"%s(%s)\" references parameter '%s' which has been replaced by a default value expression.", type.getNameWithoutPackage(), Arrays.stream(params).map(Parameter::getType).map(ClassNodeUtils::formatTypeName).collect(joining(",")), p.getName());
                        throw new RuntimeParserException(error, sourceOf(method));
                    }
                }
            }
        };
        visitor.visitArgumentlistExpression(arguments);
        // delegate to original constructor using arguments derived from defaults
        Statement code = new ExpressionStatement(new ConstructorCallExpression(ClassNode.THIS, arguments));
        addConstructor(params, (ConstructorNode) method, code, type);
    });
}
Also used : ExpressionStatement(org.codehaus.groovy.ast.stmt.ExpressionStatement) ReturnStatement(org.codehaus.groovy.ast.stmt.ReturnStatement) BlockStatement(org.codehaus.groovy.ast.stmt.BlockStatement) Statement(org.codehaus.groovy.ast.stmt.Statement) ConstructorCallExpression(org.codehaus.groovy.ast.expr.ConstructorCallExpression) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) ArrayList(java.util.ArrayList) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression) CodeVisitorSupport(org.codehaus.groovy.ast.CodeVisitorSupport) ClassNodeUtils(org.apache.groovy.ast.tools.ClassNodeUtils) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression) ArgumentListExpression(org.codehaus.groovy.ast.expr.ArgumentListExpression) CastExpression(org.codehaus.groovy.ast.expr.CastExpression) Expression(org.codehaus.groovy.ast.expr.Expression) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) BinaryExpression(org.codehaus.groovy.ast.expr.BinaryExpression) FieldExpression(org.codehaus.groovy.ast.expr.FieldExpression) MethodCallExpression(org.codehaus.groovy.ast.expr.MethodCallExpression) ConstructorCallExpression(org.codehaus.groovy.ast.expr.ConstructorCallExpression) ClosureExpression(org.codehaus.groovy.ast.expr.ClosureExpression) ConstructorNode(org.codehaus.groovy.ast.ConstructorNode) ExpressionStatement(org.codehaus.groovy.ast.stmt.ExpressionStatement) Parameter(org.codehaus.groovy.ast.Parameter) GroovyCodeVisitor(org.codehaus.groovy.ast.GroovyCodeVisitor) CastExpression(org.codehaus.groovy.ast.expr.CastExpression) RuntimeParserException(org.codehaus.groovy.syntax.RuntimeParserException)

Aggregations

ArrayList (java.util.ArrayList)1 ClassNodeUtils (org.apache.groovy.ast.tools.ClassNodeUtils)1 CodeVisitorSupport (org.codehaus.groovy.ast.CodeVisitorSupport)1 ConstructorNode (org.codehaus.groovy.ast.ConstructorNode)1 GroovyCodeVisitor (org.codehaus.groovy.ast.GroovyCodeVisitor)1 Parameter (org.codehaus.groovy.ast.Parameter)1 ArgumentListExpression (org.codehaus.groovy.ast.expr.ArgumentListExpression)1 BinaryExpression (org.codehaus.groovy.ast.expr.BinaryExpression)1 CastExpression (org.codehaus.groovy.ast.expr.CastExpression)1 ClosureExpression (org.codehaus.groovy.ast.expr.ClosureExpression)1 ConstantExpression (org.codehaus.groovy.ast.expr.ConstantExpression)1 ConstructorCallExpression (org.codehaus.groovy.ast.expr.ConstructorCallExpression)1 Expression (org.codehaus.groovy.ast.expr.Expression)1 FieldExpression (org.codehaus.groovy.ast.expr.FieldExpression)1 MethodCallExpression (org.codehaus.groovy.ast.expr.MethodCallExpression)1 VariableExpression (org.codehaus.groovy.ast.expr.VariableExpression)1 BlockStatement (org.codehaus.groovy.ast.stmt.BlockStatement)1 ExpressionStatement (org.codehaus.groovy.ast.stmt.ExpressionStatement)1 ReturnStatement (org.codehaus.groovy.ast.stmt.ReturnStatement)1 Statement (org.codehaus.groovy.ast.stmt.Statement)1