Search in sources :

Example 56 with ConstructorNode

use of org.codehaus.groovy.ast.ConstructorNode in project groovy by apache.

the class SingletonASTTransformation method createConstructor.

private void createConstructor(ClassNode classNode, FieldNode field, String propertyName, boolean isStrict) {
    final List<ConstructorNode> cNodes = classNode.getDeclaredConstructors();
    ConstructorNode foundNoArg = null;
    for (ConstructorNode cNode : cNodes) {
        final Parameter[] parameters = cNode.getParameters();
        if (parameters == null || parameters.length == 0) {
            foundNoArg = cNode;
            break;
        }
    }
    if (isStrict && !cNodes.isEmpty()) {
        for (ConstructorNode cNode : cNodes) {
            addError("@Singleton didn't expect to find one or more additional constructors: remove constructor(s) or set strict=false", cNode);
        }
    }
    if (foundNoArg == null) {
        final BlockStatement body = new BlockStatement();
        body.addStatement(ifS(notNullX(varX(field)), throwS(ctorX(make(RuntimeException.class), args(constX("Can't instantiate singleton " + classNode.getName() + ". Use " + classNode.getName() + "." + propertyName))))));
        classNode.addConstructor(new ConstructorNode(ACC_PRIVATE, body));
    }
}
Also used : ConstructorNode(org.codehaus.groovy.ast.ConstructorNode) Parameter(org.codehaus.groovy.ast.Parameter) BlockStatement(org.codehaus.groovy.ast.stmt.BlockStatement)

Example 57 with ConstructorNode

use of org.codehaus.groovy.ast.ConstructorNode in project groovy by apache.

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 58 with ConstructorNode

use of org.codehaus.groovy.ast.ConstructorNode in project groovy by apache.

the class ImmutableASTTransformation method validateConstructors.

private boolean validateConstructors(ClassNode cNode) {
    List<ConstructorNode> declaredConstructors = cNode.getDeclaredConstructors();
    for (ConstructorNode constructorNode : declaredConstructors) {
        // allow constructors added by other transforms if flagged as safe
        Object nodeMetaData = constructorNode.getNodeMetaData(IMMUTABLE_SAFE_FLAG);
        if (nodeMetaData != null && ((Boolean) nodeMetaData)) {
            continue;
        }
        // TODO: allow constructors which only call provided constructor?
        addError("Explicit constructors not allowed for " + MY_TYPE_NAME + " class: " + cNode.getNameWithoutPackage(), constructorNode);
        return false;
    }
    return true;
}
Also used : ConstructorNode(org.codehaus.groovy.ast.ConstructorNode)

Example 59 with ConstructorNode

use of org.codehaus.groovy.ast.ConstructorNode in project groovy by apache.

the class ImmutableASTTransformation method createNoArgConstructor.

private static void createNoArgConstructor(ClassNode cNode) {
    Statement body = stmt(ctorX(ClassNode.THIS, args(new MapExpression())));
    doAddConstructor(cNode, new ConstructorNode(ACC_PUBLIC, Parameter.EMPTY_ARRAY, ClassNode.EMPTY_ARRAY, body));
}
Also used : MapExpression(org.codehaus.groovy.ast.expr.MapExpression) BlockStatement(org.codehaus.groovy.ast.stmt.BlockStatement) Statement(org.codehaus.groovy.ast.stmt.Statement) EmptyStatement(org.codehaus.groovy.ast.stmt.EmptyStatement) ConstructorNode(org.codehaus.groovy.ast.ConstructorNode)

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