Search in sources :

Example 36 with ConstructorNode

use of org.codehaus.groovy.ast.ConstructorNode in project groovy-core by groovy.

the class TemplateASTTransformer method createConstructor.

private void createConstructor(final ClassNode classNode) {
    Parameter[] params = new Parameter[] { new Parameter(MarkupTemplateEngine.MARKUPTEMPLATEENGINE_CLASSNODE, "engine"), new Parameter(ClassHelper.MAP_TYPE.getPlainNodeReference(), "model"), new Parameter(ClassHelper.MAP_TYPE.getPlainNodeReference(), "modelTypes"), new Parameter(TEMPLATECONFIG_CLASSNODE, "tplConfig") };
    List<Expression> vars = new LinkedList<Expression>();
    for (Parameter param : params) {
        vars.add(new VariableExpression(param));
    }
    ExpressionStatement body = new ExpressionStatement(new ConstructorCallExpression(ClassNode.SUPER, new ArgumentListExpression(vars)));
    ConstructorNode ctor = new ConstructorNode(Opcodes.ACC_PUBLIC, params, ClassNode.EMPTY_ARRAY, body);
    classNode.addConstructor(ctor);
}
Also used : ConstructorCallExpression(org.codehaus.groovy.ast.expr.ConstructorCallExpression) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression) ArgumentListExpression(org.codehaus.groovy.ast.expr.ArgumentListExpression) Expression(org.codehaus.groovy.ast.expr.Expression) ConstructorCallExpression(org.codehaus.groovy.ast.expr.ConstructorCallExpression) ConstructorNode(org.codehaus.groovy.ast.ConstructorNode) ExpressionStatement(org.codehaus.groovy.ast.stmt.ExpressionStatement) Parameter(org.codehaus.groovy.ast.Parameter) ArgumentListExpression(org.codehaus.groovy.ast.expr.ArgumentListExpression) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression) LinkedList(java.util.LinkedList)

Example 37 with ConstructorNode

use of org.codehaus.groovy.ast.ConstructorNode in project groovy-core by groovy.

the class BaseScriptASTTransformation method changeBaseScriptType.

private void changeBaseScriptType(final AnnotatedNode parent, final ClassNode cNode, final ClassNode baseScriptType) {
    if (!cNode.isScriptBody()) {
        addError("Annotation " + MY_TYPE_NAME + " can only be used within a Script.", parent);
        return;
    }
    if (!baseScriptType.isScript()) {
        addError("Declared type " + baseScriptType + " does not extend groovy.lang.Script class!", parent);
        return;
    }
    cNode.setSuperClass(baseScriptType);
    // Method in base script that will contain the script body code.
    MethodNode runScriptMethod = ClassHelper.findSAM(baseScriptType);
    // If they want to use a name other than than "run", then make the change.
    if (isCustomScriptBodyMethod(runScriptMethod)) {
        MethodNode defaultMethod = cNode.getDeclaredMethod("run", Parameter.EMPTY_ARRAY);
        // The reason is that our transform is getting called more than once sometimes.  
        if (defaultMethod != null) {
            cNode.removeMethod(defaultMethod);
            MethodNode methodNode = new MethodNode(runScriptMethod.getName(), runScriptMethod.getModifiers() & ~ACC_ABSTRACT, runScriptMethod.getReturnType(), runScriptMethod.getParameters(), runScriptMethod.getExceptions(), defaultMethod.getCode());
            // The AST node metadata has the flag that indicates that this method is a script body.
            // It may also be carrying data for other AST transforms.
            methodNode.copyNodeMetaData(defaultMethod);
            cNode.addMethod(methodNode);
        }
    }
    // the constructors for our script class have already run.
    if (cNode.getSuperClass().getDeclaredConstructor(CONTEXT_CTOR_PARAMETERS) == null) {
        ConstructorNode orphanedConstructor = cNode.getDeclaredConstructor(CONTEXT_CTOR_PARAMETERS);
        cNode.removeConstructor(orphanedConstructor);
    }
}
Also used : MethodNode(org.codehaus.groovy.ast.MethodNode) ConstructorNode(org.codehaus.groovy.ast.ConstructorNode)

Example 38 with ConstructorNode

use of org.codehaus.groovy.ast.ConstructorNode in project groovy-core by groovy.

the class TupleConstructorASTTransformation method addMapConstructors.

public static void addMapConstructors(ClassNode cNode, boolean hasNoArg, String message) {
    Parameter[] parameters = params(new Parameter(LHMAP_TYPE, "__namedArgs"));
    BlockStatement code = new BlockStatement();
    VariableExpression namedArgs = varX("__namedArgs");
    namedArgs.setAccessedVariable(parameters[0]);
    code.addStatement(ifElseS(equalsNullX(namedArgs), illegalArgumentBlock(message), processArgsBlock(cNode, namedArgs)));
    ConstructorNode init = new ConstructorNode(ACC_PUBLIC, parameters, ClassNode.EMPTY_ARRAY, code);
    cNode.addConstructor(init);
    // add a no-arg constructor too
    if (!hasNoArg) {
        code = new BlockStatement();
        code.addStatement(stmt(ctorX(ClassNode.THIS, ctorX(LHMAP_TYPE))));
        init = new ConstructorNode(ACC_PUBLIC, Parameter.EMPTY_ARRAY, ClassNode.EMPTY_ARRAY, code);
        cNode.addConstructor(init);
    }
}
Also used : ConstructorNode(org.codehaus.groovy.ast.ConstructorNode) Parameter(org.codehaus.groovy.ast.Parameter) BlockStatement(org.codehaus.groovy.ast.stmt.BlockStatement) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression)

Example 39 with ConstructorNode

use of org.codehaus.groovy.ast.ConstructorNode in project groovy-core by groovy.

the class InheritConstructorsASTTransformation method addConstructorUnlessAlreadyExisting.

private void addConstructorUnlessAlreadyExisting(ClassNode classNode, ConstructorNode consNode, boolean copyConstructorAnnotations, boolean copyParameterAnnotations) {
    Parameter[] origParams = consNode.getParameters();
    if (consNode.isPrivate())
        return;
    Parameter[] params = new Parameter[origParams.length];
    Map<String, ClassNode> genericsSpec = createGenericsSpec(classNode);
    extractSuperClassGenerics(classNode, classNode.getSuperClass(), genericsSpec);
    List<Expression> theArgs = buildParams(origParams, params, genericsSpec, copyParameterAnnotations);
    if (isExisting(classNode, params))
        return;
    ConstructorNode added = classNode.addConstructor(consNode.getModifiers(), params, consNode.getExceptions(), block(ctorSuperS(args(theArgs))));
    if (copyConstructorAnnotations) {
        added.addAnnotations(copyAnnotatedNodeAnnotations(consNode, MY_TYPE_NAME));
    }
}
Also used : ClassNode(org.codehaus.groovy.ast.ClassNode) Expression(org.codehaus.groovy.ast.expr.Expression) ConstructorNode(org.codehaus.groovy.ast.ConstructorNode) Parameter(org.codehaus.groovy.ast.Parameter)

Example 40 with ConstructorNode

use of org.codehaus.groovy.ast.ConstructorNode in project groovy-core by groovy.

the class ImmutableASTTransformation method createConstructorMapCommon.

private void createConstructorMapCommon(ClassNode cNode, BlockStatement body) {
    final List<FieldNode> fList = cNode.getFields();
    for (FieldNode fNode : fList) {
        // public fields will be rejected elsewhere
        if (fNode.isPublic())
            continue;
        // a property
        if (cNode.getProperty(fNode.getName()) != null)
            continue;
        if (fNode.isFinal() && fNode.isStatic())
            continue;
        // internal field
        if (fNode.getName().contains("$") || fNode.isSynthetic())
            continue;
        if (fNode.isFinal() && fNode.getInitialExpression() != null)
            body.addStatement(checkFinalArgNotOverridden(cNode, fNode));
        body.addStatement(createConstructorStatementDefault(fNode));
    }
    doAddConstructor(cNode, new ConstructorNode(ACC_PUBLIC, params(new Parameter(HASHMAP_TYPE, "args")), ClassNode.EMPTY_ARRAY, body));
}
Also used : FieldNode(org.codehaus.groovy.ast.FieldNode) ConstructorNode(org.codehaus.groovy.ast.ConstructorNode) Parameter(org.codehaus.groovy.ast.Parameter)

Aggregations

ConstructorNode (org.codehaus.groovy.ast.ConstructorNode)59 Parameter (org.codehaus.groovy.ast.Parameter)30 ClassNode (org.codehaus.groovy.ast.ClassNode)28 MethodNode (org.codehaus.groovy.ast.MethodNode)21 InnerClassNode (org.codehaus.groovy.ast.InnerClassNode)19 BlockStatement (org.codehaus.groovy.ast.stmt.BlockStatement)19 FieldNode (org.codehaus.groovy.ast.FieldNode)13 Expression (org.codehaus.groovy.ast.expr.Expression)10 ArrayList (java.util.ArrayList)8 Statement (org.codehaus.groovy.ast.stmt.Statement)8 PropertyNode (org.codehaus.groovy.ast.PropertyNode)6 MapExpression (org.codehaus.groovy.ast.expr.MapExpression)6 VariableExpression (org.codehaus.groovy.ast.expr.VariableExpression)6 ExpressionStatement (org.codehaus.groovy.ast.stmt.ExpressionStatement)6 MethodVisitor (org.objectweb.asm.MethodVisitor)6 AnnotationNode (org.codehaus.groovy.ast.AnnotationNode)5 ConstructorCallExpression (org.codehaus.groovy.ast.expr.ConstructorCallExpression)5 EmptyStatement (org.codehaus.groovy.ast.stmt.EmptyStatement)5 ThrowStatement (org.codehaus.groovy.ast.stmt.ThrowStatement)5 LinkedList (java.util.LinkedList)4