Search in sources :

Example 1 with InterfaceHelperClassNode

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

the class AsmClassGenerator method visitClassExpression.

/**
     * load class object on stack
     */
public void visitClassExpression(ClassExpression expression) {
    ClassNode type = expression.getType();
    MethodVisitor mv = controller.getMethodVisitor();
    if (BytecodeHelper.isClassLiteralPossible(type) || BytecodeHelper.isSameCompilationUnit(controller.getClassNode(), type)) {
        if (controller.getClassNode().isInterface()) {
            InterfaceHelperClassNode interfaceClassLoadingClass = controller.getInterfaceClassLoadingClass();
            if (BytecodeHelper.isClassLiteralPossible(interfaceClassLoadingClass)) {
                BytecodeHelper.visitClassLiteral(mv, interfaceClassLoadingClass);
                controller.getOperandStack().push(ClassHelper.CLASS_Type);
                return;
            }
        } else {
            BytecodeHelper.visitClassLiteral(mv, type);
            controller.getOperandStack().push(ClassHelper.CLASS_Type);
            return;
        }
    }
    String staticFieldName = getStaticFieldName(type);
    referencedClasses.put(staticFieldName, type);
    String internalClassName = controller.getInternalClassName();
    if (controller.getClassNode().isInterface()) {
        internalClassName = BytecodeHelper.getClassInternalName(controller.getInterfaceClassLoadingClass());
        mv.visitFieldInsn(GETSTATIC, internalClassName, staticFieldName, "Ljava/lang/Class;");
    } else {
        mv.visitMethodInsn(INVOKESTATIC, internalClassName, "$get$" + staticFieldName, "()Ljava/lang/Class;", false);
    }
    controller.getOperandStack().push(ClassHelper.CLASS_Type);
}
Also used : InnerClassNode(org.codehaus.groovy.ast.InnerClassNode) InterfaceHelperClassNode(org.codehaus.groovy.ast.InterfaceHelperClassNode) ClassNode(org.codehaus.groovy.ast.ClassNode) InterfaceHelperClassNode(org.codehaus.groovy.ast.InterfaceHelperClassNode) MethodVisitor(org.objectweb.asm.MethodVisitor)

Example 2 with InterfaceHelperClassNode

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

the class CallSiteWriter method generateGetCallSiteArray.

private void generateGetCallSiteArray() {
    int visibility = (controller.getClassNode() instanceof InterfaceHelperClassNode) ? MOD_PUBSS : MOD_PRIVSS;
    MethodVisitor mv = controller.getClassVisitor().visitMethod(visibility, GET_CALLSITE_METHOD, GET_CALLSITE_DESC, null, null);
    controller.setMethodVisitor(mv);
    mv.visitCode();
    mv.visitFieldInsn(GETSTATIC, controller.getInternalClassName(), "$callSiteArray", "Ljava/lang/ref/SoftReference;");
    Label l0 = new Label();
    mv.visitJumpInsn(IFNULL, l0);
    mv.visitFieldInsn(GETSTATIC, controller.getInternalClassName(), "$callSiteArray", "Ljava/lang/ref/SoftReference;");
    mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/ref/SoftReference", "get", "()Ljava/lang/Object;", false);
    mv.visitTypeInsn(CHECKCAST, "org/codehaus/groovy/runtime/callsite/CallSiteArray");
    mv.visitInsn(DUP);
    mv.visitVarInsn(ASTORE, 0);
    Label l1 = new Label();
    mv.visitJumpInsn(IFNONNULL, l1);
    mv.visitLabel(l0);
    mv.visitMethodInsn(INVOKESTATIC, controller.getInternalClassName(), "$createCallSiteArray", "()Lorg/codehaus/groovy/runtime/callsite/CallSiteArray;", false);
    mv.visitVarInsn(ASTORE, 0);
    mv.visitTypeInsn(NEW, "java/lang/ref/SoftReference");
    mv.visitInsn(DUP);
    mv.visitVarInsn(ALOAD, 0);
    mv.visitMethodInsn(INVOKESPECIAL, "java/lang/ref/SoftReference", "<init>", "(Ljava/lang/Object;)V", false);
    mv.visitFieldInsn(PUTSTATIC, controller.getInternalClassName(), "$callSiteArray", "Ljava/lang/ref/SoftReference;");
    mv.visitLabel(l1);
    mv.visitVarInsn(ALOAD, 0);
    mv.visitFieldInsn(GETFIELD, "org/codehaus/groovy/runtime/callsite/CallSiteArray", "array", "[Lorg/codehaus/groovy/runtime/callsite/CallSite;");
    mv.visitInsn(ARETURN);
    mv.visitMaxs(0, 0);
    mv.visitEnd();
}
Also used : InterfaceHelperClassNode(org.codehaus.groovy.ast.InterfaceHelperClassNode) Label(org.objectweb.asm.Label) MethodVisitor(org.objectweb.asm.MethodVisitor)

Example 3 with InterfaceHelperClassNode

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

the class AsmClassGenerator method visitClass.

// GroovyClassVisitor interface
//-------------------------------------------------------------------------
public void visitClass(ClassNode classNode) {
    referencedClasses.clear();
    WriterControllerFactory factory = (WriterControllerFactory) classNode.getNodeMetaData(WriterControllerFactory.class);
    WriterController normalController = new WriterController();
    if (factory != null) {
        this.controller = factory.makeController(normalController);
    } else {
        this.controller = normalController;
    }
    this.controller.init(this, context, cv, classNode);
    if (controller.shouldOptimizeForInt() || factory != null) {
        OptimizingStatementWriter.setNodeMeta(controller.getTypeChooser(), classNode);
    }
    try {
        cv.visit(controller.getBytecodeVersion(), adjustedClassModifiersForClassWriting(classNode), controller.getInternalClassName(), BytecodeHelper.getGenericsSignature(classNode), controller.getInternalBaseClassName(), BytecodeHelper.getClassInternalNames(classNode.getInterfaces()));
        cv.visitSource(sourceFile, null);
        if (classNode instanceof InnerClassNode) {
            InnerClassNode innerClass = (InnerClassNode) classNode;
            MethodNode enclosingMethod = innerClass.getEnclosingMethod();
            if (enclosingMethod != null) {
                String outerClassName = BytecodeHelper.getClassInternalName(innerClass.getOuterClass().getName());
                cv.visitOuterClass(outerClassName, enclosingMethod.getName(), BytecodeHelper.getMethodDescriptor(enclosingMethod));
            }
        }
        if (classNode.getName().endsWith("package-info")) {
            PackageNode packageNode = classNode.getPackage();
            if (packageNode != null) {
                // pull them out of package node but treat them like they were on class node
                for (AnnotationNode an : packageNode.getAnnotations()) {
                    // skip built-in properties
                    if (an.isBuiltIn())
                        continue;
                    if (an.hasSourceRetention())
                        continue;
                    AnnotationVisitor av = getAnnotationVisitor(classNode, an, cv);
                    visitAnnotationAttributes(an, av);
                    av.visitEnd();
                }
            }
            cv.visitEnd();
            return;
        } else {
            visitAnnotations(classNode, cv);
        }
        if (classNode.isInterface()) {
            ClassNode owner = classNode;
            if (owner instanceof InnerClassNode) {
                owner = owner.getOuterClass();
            }
            String outerClassName = classNode.getName();
            String name = outerClassName + "$" + context.getNextInnerClassIdx();
            controller.setInterfaceClassLoadingClass(new InterfaceHelperClassNode(owner, name, 4128, ClassHelper.OBJECT_TYPE, controller.getCallSiteWriter().getCallSites()));
            super.visitClass(classNode);
            createInterfaceSyntheticStaticFields();
        } else {
            super.visitClass(classNode);
            MopWriter.Factory mopWriterFactory = classNode.getNodeMetaData(MopWriter.Factory.class);
            if (mopWriterFactory == null) {
                mopWriterFactory = MopWriter.FACTORY;
            }
            MopWriter mopWriter = mopWriterFactory.create(controller);
            mopWriter.createMopMethods();
            controller.getCallSiteWriter().generateCallSiteArray();
            createSyntheticStaticFields();
        }
        // GROOVY-6750 and GROOVY-6808
        for (Iterator<InnerClassNode> iter = classNode.getInnerClasses(); iter.hasNext(); ) {
            InnerClassNode innerClass = iter.next();
            makeInnerClassEntry(innerClass);
        }
        makeInnerClassEntry(classNode);
        cv.visitEnd();
    } catch (GroovyRuntimeException e) {
        e.setModule(classNode.getModule());
        throw e;
    } catch (NegativeArraySizeException nase) {
        throw new GroovyRuntimeException("NegativeArraySizeException while processing " + sourceFile, nase);
    } catch (NullPointerException npe) {
        throw new GroovyRuntimeException("NPE while processing " + sourceFile, npe);
    }
}
Also used : InnerClassNode(org.codehaus.groovy.ast.InnerClassNode) InterfaceHelperClassNode(org.codehaus.groovy.ast.InterfaceHelperClassNode) ClassNode(org.codehaus.groovy.ast.ClassNode) GroovyRuntimeException(groovy.lang.GroovyRuntimeException) InnerClassNode(org.codehaus.groovy.ast.InnerClassNode) WriterController(org.codehaus.groovy.classgen.asm.WriterController) MethodNode(org.codehaus.groovy.ast.MethodNode) AnnotationNode(org.codehaus.groovy.ast.AnnotationNode) AnnotationVisitor(org.objectweb.asm.AnnotationVisitor) InterfaceHelperClassNode(org.codehaus.groovy.ast.InterfaceHelperClassNode) PackageNode(org.codehaus.groovy.ast.PackageNode) MopWriter(org.codehaus.groovy.classgen.asm.MopWriter) WriterControllerFactory(org.codehaus.groovy.classgen.asm.WriterControllerFactory)

Example 4 with InterfaceHelperClassNode

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

the class AsmClassGenerator method visitClassExpression.

/**
     * load class object on stack
     */
public void visitClassExpression(ClassExpression expression) {
    ClassNode type = expression.getType();
    MethodVisitor mv = controller.getMethodVisitor();
    if (BytecodeHelper.isClassLiteralPossible(type) || BytecodeHelper.isSameCompilationUnit(controller.getClassNode(), type)) {
        if (controller.getClassNode().isInterface()) {
            InterfaceHelperClassNode interfaceClassLoadingClass = controller.getInterfaceClassLoadingClass();
            if (BytecodeHelper.isClassLiteralPossible(interfaceClassLoadingClass)) {
                BytecodeHelper.visitClassLiteral(mv, interfaceClassLoadingClass);
                controller.getOperandStack().push(ClassHelper.CLASS_Type);
                return;
            }
        } else {
            BytecodeHelper.visitClassLiteral(mv, type);
            controller.getOperandStack().push(ClassHelper.CLASS_Type);
            return;
        }
    }
    String staticFieldName = getStaticFieldName(type);
    referencedClasses.put(staticFieldName, type);
    String internalClassName = controller.getInternalClassName();
    if (controller.getClassNode().isInterface()) {
        internalClassName = BytecodeHelper.getClassInternalName(controller.getInterfaceClassLoadingClass());
        mv.visitFieldInsn(GETSTATIC, internalClassName, staticFieldName, "Ljava/lang/Class;");
    } else {
        mv.visitMethodInsn(INVOKESTATIC, internalClassName, "$get$" + staticFieldName, "()Ljava/lang/Class;", false);
    }
    controller.getOperandStack().push(ClassHelper.CLASS_Type);
}
Also used : InnerClassNode(org.codehaus.groovy.ast.InnerClassNode) InterfaceHelperClassNode(org.codehaus.groovy.ast.InterfaceHelperClassNode) ClassNode(org.codehaus.groovy.ast.ClassNode) InterfaceHelperClassNode(org.codehaus.groovy.ast.InterfaceHelperClassNode) MethodVisitor(org.objectweb.asm.MethodVisitor)

Example 5 with InterfaceHelperClassNode

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

the class AsmClassGenerator method visitClass.

// GroovyClassVisitor interface
//-------------------------------------------------------------------------
public void visitClass(ClassNode classNode) {
    referencedClasses.clear();
    WriterControllerFactory factory = (WriterControllerFactory) classNode.getNodeMetaData(WriterControllerFactory.class);
    WriterController normalController = new WriterController();
    if (factory != null) {
        this.controller = factory.makeController(normalController);
    } else {
        this.controller = normalController;
    }
    this.controller.init(this, context, cv, classNode);
    if (controller.shouldOptimizeForInt() || factory != null) {
        OptimizingStatementWriter.setNodeMeta(controller.getTypeChooser(), classNode);
    }
    try {
        cv.visit(controller.getBytecodeVersion(), adjustedClassModifiersForClassWriting(classNode), controller.getInternalClassName(), BytecodeHelper.getGenericsSignature(classNode), controller.getInternalBaseClassName(), BytecodeHelper.getClassInternalNames(classNode.getInterfaces()));
        cv.visitSource(sourceFile, null);
        if (classNode instanceof InnerClassNode) {
            InnerClassNode innerClass = (InnerClassNode) classNode;
            MethodNode enclosingMethod = innerClass.getEnclosingMethod();
            if (enclosingMethod != null) {
                String outerClassName = BytecodeHelper.getClassInternalName(innerClass.getOuterClass().getName());
                cv.visitOuterClass(outerClassName, enclosingMethod.getName(), BytecodeHelper.getMethodDescriptor(enclosingMethod));
            }
        }
        if (classNode.getName().endsWith("package-info")) {
            PackageNode packageNode = classNode.getPackage();
            if (packageNode != null) {
                // pull them out of package node but treat them like they were on class node
                for (AnnotationNode an : packageNode.getAnnotations()) {
                    // skip built-in properties
                    if (an.isBuiltIn())
                        continue;
                    if (an.hasSourceRetention())
                        continue;
                    AnnotationVisitor av = getAnnotationVisitor(classNode, an, cv);
                    visitAnnotationAttributes(an, av);
                    av.visitEnd();
                }
            }
            cv.visitEnd();
            return;
        } else {
            visitAnnotations(classNode, cv);
        }
        if (classNode.isInterface()) {
            ClassNode owner = classNode;
            if (owner instanceof InnerClassNode) {
                owner = owner.getOuterClass();
            }
            String outerClassName = classNode.getName();
            String name = outerClassName + "$" + context.getNextInnerClassIdx();
            controller.setInterfaceClassLoadingClass(new InterfaceHelperClassNode(owner, name, ACC_SUPER | ACC_SYNTHETIC | ACC_STATIC, ClassHelper.OBJECT_TYPE, controller.getCallSiteWriter().getCallSites()));
            super.visitClass(classNode);
            createInterfaceSyntheticStaticFields();
        } else {
            super.visitClass(classNode);
            MopWriter.Factory mopWriterFactory = classNode.getNodeMetaData(MopWriter.Factory.class);
            if (mopWriterFactory == null) {
                mopWriterFactory = MopWriter.FACTORY;
            }
            MopWriter mopWriter = mopWriterFactory.create(controller);
            mopWriter.createMopMethods();
            controller.getCallSiteWriter().generateCallSiteArray();
            createSyntheticStaticFields();
        }
        // GROOVY-6750 and GROOVY-6808
        for (Iterator<InnerClassNode> iter = classNode.getInnerClasses(); iter.hasNext(); ) {
            InnerClassNode innerClass = iter.next();
            makeInnerClassEntry(innerClass);
        }
        makeInnerClassEntry(classNode);
        cv.visitEnd();
    } catch (GroovyRuntimeException e) {
        e.setModule(classNode.getModule());
        throw e;
    } catch (NegativeArraySizeException nase) {
        throw new GroovyRuntimeException("NegativeArraySizeException while processing " + sourceFile, nase);
    } catch (NullPointerException npe) {
        throw new GroovyRuntimeException("NPE while processing " + sourceFile, npe);
    }
}
Also used : InnerClassNode(org.codehaus.groovy.ast.InnerClassNode) InterfaceHelperClassNode(org.codehaus.groovy.ast.InterfaceHelperClassNode) ClassNode(org.codehaus.groovy.ast.ClassNode) GroovyRuntimeException(groovy.lang.GroovyRuntimeException) InnerClassNode(org.codehaus.groovy.ast.InnerClassNode) WriterController(org.codehaus.groovy.classgen.asm.WriterController) MethodNode(org.codehaus.groovy.ast.MethodNode) AnnotationNode(org.codehaus.groovy.ast.AnnotationNode) AnnotationVisitor(org.objectweb.asm.AnnotationVisitor) InterfaceHelperClassNode(org.codehaus.groovy.ast.InterfaceHelperClassNode) PackageNode(org.codehaus.groovy.ast.PackageNode) MopWriter(org.codehaus.groovy.classgen.asm.MopWriter) WriterControllerFactory(org.codehaus.groovy.classgen.asm.WriterControllerFactory)

Aggregations

InterfaceHelperClassNode (org.codehaus.groovy.ast.InterfaceHelperClassNode)6 ClassNode (org.codehaus.groovy.ast.ClassNode)4 InnerClassNode (org.codehaus.groovy.ast.InnerClassNode)4 MethodVisitor (org.objectweb.asm.MethodVisitor)4 GroovyRuntimeException (groovy.lang.GroovyRuntimeException)2 AnnotationNode (org.codehaus.groovy.ast.AnnotationNode)2 MethodNode (org.codehaus.groovy.ast.MethodNode)2 PackageNode (org.codehaus.groovy.ast.PackageNode)2 MopWriter (org.codehaus.groovy.classgen.asm.MopWriter)2 WriterController (org.codehaus.groovy.classgen.asm.WriterController)2 WriterControllerFactory (org.codehaus.groovy.classgen.asm.WriterControllerFactory)2 AnnotationVisitor (org.objectweb.asm.AnnotationVisitor)2 Label (org.objectweb.asm.Label)2