Search in sources :

Example 51 with ConstructorNode

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

the class PackageScopeASTTransformation method visitClassNode.

private void visitClassNode(ClassNode cNode, List<PackageScopeTarget> value) {
    String cName = cNode.getName();
    if (cNode.isInterface() && value.size() != 1 && value.get(0) != PackageScopeTarget.CLASS) {
        addError("Error processing interface '" + cName + "'. " + MY_TYPE_NAME + " not allowed for interfaces except when targeting Class level.", cNode);
    }
    if (value.contains(groovy.transform.PackageScopeTarget.CLASS)) {
        if (cNode.isSyntheticPublic())
            revertVisibility(cNode);
        else
            addError("Can't use " + MY_TYPE_NAME + " for class '" + cNode.getName() + "' which has explicit visibility.", cNode);
    }
    if (value.contains(groovy.transform.PackageScopeTarget.METHODS)) {
        final List<MethodNode> mList = cNode.getMethods();
        for (MethodNode mNode : mList) {
            if (mNode.isSyntheticPublic())
                revertVisibility(mNode);
        }
    }
    if (value.contains(groovy.transform.PackageScopeTarget.CONSTRUCTORS)) {
        final List<ConstructorNode> cList = cNode.getDeclaredConstructors();
        for (MethodNode mNode : cList) {
            if (mNode.isSyntheticPublic())
                revertVisibility(mNode);
        }
    }
    if (value.contains(PackageScopeTarget.FIELDS)) {
        final List<PropertyNode> pList = cNode.getProperties();
        List<PropertyNode> foundProps = new ArrayList<PropertyNode>();
        List<String> foundNames = new ArrayList<String>();
        for (PropertyNode pNode : pList) {
            foundProps.add(pNode);
            foundNames.add(pNode.getName());
        }
        for (PropertyNode pNode : foundProps) {
            pList.remove(pNode);
        }
        final List<FieldNode> fList = cNode.getFields();
        for (FieldNode fNode : fList) {
            if (foundNames.contains(fNode.getName())) {
                revertVisibility(fNode);
            }
        }
    }
}
Also used : MethodNode(org.codehaus.groovy.ast.MethodNode) FieldNode(org.codehaus.groovy.ast.FieldNode) ConstructorNode(org.codehaus.groovy.ast.ConstructorNode) PropertyNode(org.codehaus.groovy.ast.PropertyNode) ArrayList(java.util.ArrayList)

Example 52 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 53 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 54 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 55 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