Search in sources :

Example 16 with ConstructorNode

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

the class InheritConstructorsASTTransformation method processClass.

private void processClass(ClassNode cNode, AnnotationNode node) {
    if (cNode.isInterface()) {
        addError("Error processing interface '" + cNode.getName() + "'. " + MY_TYPE_NAME + " only allowed for classes.", cNode);
        return;
    }
    boolean copyConstructorAnnotations = memberHasValue(node, "constructorAnnotations", true);
    boolean copyParameterAnnotations = memberHasValue(node, "parameterAnnotations", true);
    ClassNode sNode = cNode.getSuperClass();
    List<AnnotationNode> superAnnotations = sNode.getAnnotations(MY_TYPE);
    if (superAnnotations.size() == 1) {
        // We need @InheritConstructors from parent classes processed first
        // so force that order here. The transformation is benign on an already
        // processed node so processing twice in any order won't matter bar
        // a very small time penalty.
        processClass(sNode, node);
    }
    for (ConstructorNode cn : sNode.getDeclaredConstructors()) {
        addConstructorUnlessAlreadyExisting(cNode, cn, copyConstructorAnnotations, copyParameterAnnotations);
    }
}
Also used : ClassNode(org.codehaus.groovy.ast.ClassNode) AnnotationNode(org.codehaus.groovy.ast.AnnotationNode) ConstructorNode(org.codehaus.groovy.ast.ConstructorNode)

Example 17 with ConstructorNode

use of org.codehaus.groovy.ast.ConstructorNode in project spring-boot by spring-projects.

the class ResolveDependencyCoordinatesTransformationTests method transformationOfAnnotationOnConstructor.

@Test
public void transformationOfAnnotationOnConstructor() {
    ClassNode classNode = new ClassNode("Test", 0, new ClassNode(Object.class));
    this.moduleNode.addClass(classNode);
    ConstructorNode constructorNode = new ConstructorNode(0, null);
    constructorNode.addAnnotation(this.grabAnnotation);
    classNode.addMethod(constructorNode);
    assertGrabAnnotationHasBeenTransformed();
}
Also used : ClassNode(org.codehaus.groovy.ast.ClassNode) ConstructorNode(org.codehaus.groovy.ast.ConstructorNode) Test(org.junit.Test)

Example 18 with ConstructorNode

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

the class StaticTypeCheckingVisitor method visitClass.

@Override
public void visitClass(final ClassNode node) {
    if (shouldSkipClassNode(node))
        return;
    if (extension.beforeVisitClass(node)) {
        extension.afterVisitClass(node);
        return;
    }
    Object type = node.getNodeMetaData(StaticTypesMarker.INFERRED_TYPE);
    if (type != null) {
        // transformation has already been run on this class node
        // so we'll use a silent collector in order not to duplicate errors
        typeCheckingContext.pushErrorCollector();
    }
    typeCheckingContext.pushEnclosingClassNode(node);
    Set<MethodNode> oldVisitedMethod = typeCheckingContext.alreadyVisitedMethods;
    typeCheckingContext.alreadyVisitedMethods = new LinkedHashSet<MethodNode>();
    super.visitClass(node);
    Iterator<InnerClassNode> innerClasses = node.getInnerClasses();
    while (innerClasses.hasNext()) {
        InnerClassNode innerClassNode = innerClasses.next();
        visitClass(innerClassNode);
    }
    typeCheckingContext.alreadyVisitedMethods = oldVisitedMethod;
    node.putNodeMetaData(StaticTypesMarker.INFERRED_TYPE, node);
    // works in a two pass sequence and we don't want to skip the second pass
    for (MethodNode methodNode : node.getMethods()) {
        methodNode.putNodeMetaData(StaticTypeCheckingVisitor.class, Boolean.TRUE);
    }
    for (ConstructorNode constructorNode : node.getDeclaredConstructors()) {
        constructorNode.putNodeMetaData(StaticTypeCheckingVisitor.class, Boolean.TRUE);
    }
    extension.afterVisitClass(node);
}
Also used : MethodNode(org.codehaus.groovy.ast.MethodNode) ConstructorNode(org.codehaus.groovy.ast.ConstructorNode) InnerClassNode(org.codehaus.groovy.ast.InnerClassNode)

Example 19 with ConstructorNode

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

the class ClosureWriter method writeClosure.

public void writeClosure(ClosureExpression expression) {
    CompileStack compileStack = controller.getCompileStack();
    MethodVisitor mv = controller.getMethodVisitor();
    ClassNode classNode = controller.getClassNode();
    AsmClassGenerator acg = controller.getAcg();
    // generate closure as public class to make sure it can be properly invoked by classes of the
    // Groovy runtime without circumventing JVM access checks (see CachedMethod for example).
    int mods = ACC_PUBLIC;
    if (classNode.isInterface()) {
        mods |= ACC_STATIC;
    }
    ClassNode closureClass = getOrAddClosureClass(expression, mods);
    String closureClassinternalName = BytecodeHelper.getClassInternalName(closureClass);
    List constructors = closureClass.getDeclaredConstructors();
    ConstructorNode node = (ConstructorNode) constructors.get(0);
    Parameter[] localVariableParams = node.getParameters();
    mv.visitTypeInsn(NEW, closureClassinternalName);
    mv.visitInsn(DUP);
    if (controller.isStaticMethod() || compileStack.isInSpecialConstructorCall()) {
        (new ClassExpression(classNode)).visit(acg);
        (new ClassExpression(controller.getOutermostClass())).visit(acg);
    } else {
        mv.visitVarInsn(ALOAD, 0);
        controller.getOperandStack().push(ClassHelper.OBJECT_TYPE);
        loadThis();
    }
    // on the stack
    for (int i = 2; i < localVariableParams.length; i++) {
        Parameter param = localVariableParams[i];
        String name = param.getName();
        loadReference(name, controller);
        if (param.getNodeMetaData(ClosureWriter.UseExistingReference.class) == null) {
            param.setNodeMetaData(ClosureWriter.UseExistingReference.class, Boolean.TRUE);
        }
    }
    // we may need to pass in some other constructors
    //cv.visitMethodInsn(INVOKESPECIAL, innerClassinternalName, "<init>", prototype + ")V");
    mv.visitMethodInsn(INVOKESPECIAL, closureClassinternalName, "<init>", BytecodeHelper.getMethodDescriptor(ClassHelper.VOID_TYPE, localVariableParams), false);
    controller.getOperandStack().replace(ClassHelper.CLOSURE_TYPE, localVariableParams.length);
}
Also used : ClassNode(org.codehaus.groovy.ast.ClassNode) InnerClassNode(org.codehaus.groovy.ast.InnerClassNode) ClassExpression(org.codehaus.groovy.ast.expr.ClassExpression) AsmClassGenerator(org.codehaus.groovy.classgen.AsmClassGenerator) MethodVisitor(org.objectweb.asm.MethodVisitor) ConstructorNode(org.codehaus.groovy.ast.ConstructorNode) Parameter(org.codehaus.groovy.ast.Parameter) List(java.util.List)

Example 20 with ConstructorNode

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

the class StaticInvocationWriter method writeInvokeConstructor.

@Override
public void writeInvokeConstructor(final ConstructorCallExpression call) {
    MethodNode mn = call.getNodeMetaData(StaticTypesMarker.DIRECT_METHOD_CALL_TARGET);
    if (mn == null) {
        super.writeInvokeConstructor(call);
        return;
    }
    if (writeAICCall(call))
        return;
    ConstructorNode cn;
    if (mn instanceof ConstructorNode) {
        cn = (ConstructorNode) mn;
    } else {
        cn = new ConstructorNode(mn.getModifiers(), mn.getParameters(), mn.getExceptions(), mn.getCode());
        cn.setDeclaringClass(mn.getDeclaringClass());
    }
    TupleExpression args = makeArgumentList(call.getArguments());
    if (cn.isPrivate()) {
        ClassNode classNode = controller.getClassNode();
        ClassNode declaringClass = cn.getDeclaringClass();
        if (declaringClass != classNode) {
            MethodNode bridge = null;
            if (call.getNodeMetaData(StaticTypesMarker.PV_METHODS_ACCESS) != null) {
                Map<MethodNode, MethodNode> bridgeMethods = declaringClass.getNodeMetaData(StaticCompilationMetadataKeys.PRIVATE_BRIDGE_METHODS);
                bridge = bridgeMethods != null ? bridgeMethods.get(cn) : null;
            }
            if (bridge != null && bridge instanceof ConstructorNode) {
                ArgumentListExpression newArgs = new ArgumentListExpression(new ConstantExpression(null));
                for (Expression arg : args) {
                    newArgs.addExpression(arg);
                }
                cn = (ConstructorNode) bridge;
                args = newArgs;
            } else {
                controller.getSourceUnit().addError(new SyntaxException("Cannot call private constructor for " + declaringClass.toString(false) + " from class " + classNode.toString(false), call.getLineNumber(), call.getColumnNumber(), mn.getLastLineNumber(), call.getLastColumnNumber()));
            }
        }
    }
    String ownerDescriptor = prepareConstructorCall(cn);
    int before = controller.getOperandStack().getStackLength();
    loadArguments(args.getExpressions(), cn.getParameters());
    finnishConstructorCall(cn, ownerDescriptor, controller.getOperandStack().getStackLength() - before);
}
Also used : ClassNode(org.codehaus.groovy.ast.ClassNode) InnerClassNode(org.codehaus.groovy.ast.InnerClassNode) ExtensionMethodNode(org.codehaus.groovy.transform.stc.ExtensionMethodNode) MethodNode(org.codehaus.groovy.ast.MethodNode) TemporaryVariableExpression(org.codehaus.groovy.transform.sc.TemporaryVariableExpression) ConstructorNode(org.codehaus.groovy.ast.ConstructorNode) SyntaxException(org.codehaus.groovy.syntax.SyntaxException)

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