Search in sources :

Example 71 with InnerClassNode

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

the class AsmClassGenerator method visitClass.

// GroovyClassVisitor interface
// --------------------------------------------------------------------------
@Override
public void visitClass(final ClassNode classNode) {
    referencedClasses.clear();
    WriterControllerFactory factory = classNode.getNodeMetaData(WriterControllerFactory.class);
    controller = new WriterController();
    if (factory != null) {
        controller = factory.makeController(controller);
    }
    controller.init(this, context, classVisitor, classNode);
    if (controller.shouldOptimizeForInt() || factory != null) {
        OptimizingStatementWriter.setNodeMeta(controller.getTypeChooser(), classNode);
    }
    classVisitor = controller.getClassVisitor();
    try {
        int bytecodeVersion = controller.getBytecodeVersion();
        Object min = classNode.getNodeMetaData(MINIMUM_BYTECODE_VERSION);
        if (min instanceof Integer) {
            int minVersion = (int) min;
            if ((bytecodeVersion ^ V_PREVIEW) < minVersion) {
                bytecodeVersion = minVersion;
            }
        }
        classVisitor.visit(bytecodeVersion, adjustedClassModifiersForClassWriting(classNode), controller.getInternalClassName(), BytecodeHelper.getGenericsSignature(classNode), controller.getInternalBaseClassName(), BytecodeHelper.getClassInternalNames(classNode.getInterfaces()));
        classVisitor.visitSource(sourceFile, null);
        if (classNode instanceof InnerClassNode) {
            // GROOVY-9842
            makeInnerClassEntry(classNode.getOuterClass());
            // GROOVY-4649, et al.
            makeInnerClassEntry(classNode);
            MethodNode enclosingMethod = classNode.getEnclosingMethod();
            if (enclosingMethod != null) {
                classVisitor.visitOuterClass(BytecodeHelper.getClassInternalName(classNode.getOuterClass().getName()), 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
                visitAnnotations(classNode, packageNode, classVisitor);
            }
        } else {
            visitAnnotations(classNode, classVisitor);
            visitTypeParameters(classNode, classVisitor);
            visitType(classNode.getUnresolvedSuperClass(), classVisitor, newSuperTypeReference(-1), "", true);
            ClassNode[] interfaces = classNode.getInterfaces();
            for (int i = 0; i < interfaces.length; i++) {
                visitType(interfaces[i], classVisitor, newSuperTypeReference(i), "", true);
            }
            if (classNode.isInterface()) {
                String outerClassName = classNode.getName();
                String name = outerClassName + "$" + context.getNextInnerClassIdx();
                controller.setInterfaceClassLoadingClass(new InterfaceHelperClassNode(Optional.ofNullable(classNode.getOuterClass()).orElse(classNode), name, ACC_SUPER | ACC_STATIC | ACC_SYNTHETIC, 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-4649, GROOVY-6750, GROOVY-6808
        for (Iterator<InnerClassNode> it = classNode.getInnerClasses(); it.hasNext(); ) {
            makeInnerClassEntry(it.next());
        }
        if (sealedNative(classNode)) {
            for (ClassNode sub : classNode.getPermittedSubclasses()) {
                classVisitor.visitPermittedSubclass(sub.getName());
            }
        }
        if (classNode.isRecord()) {
            visitRecordComponents(classNode);
        }
        classVisitor.visitEnd();
    } catch (GroovyRuntimeException e) {
        e.setModule(classNode.getModule());
        throw e;
    } catch (NullPointerException | NegativeArraySizeException e) {
        String m = e.getClass().getSimpleName() + " while processing " + sourceFile;
        GroovyRuntimeException gre = new GroovyRuntimeException(m, e);
        gre.setModule(classNode.getModule());
        throw gre;
    }
}
Also used : InterfaceHelperClassNode(org.codehaus.groovy.ast.InterfaceHelperClassNode) ClassNode(org.codehaus.groovy.ast.ClassNode) InnerClassNode(org.codehaus.groovy.ast.InnerClassNode) GroovyRuntimeException(groovy.lang.GroovyRuntimeException) InnerClassNode(org.codehaus.groovy.ast.InnerClassNode) WriterController(org.codehaus.groovy.classgen.asm.WriterController) MethodNode(org.codehaus.groovy.ast.MethodNode) 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 72 with InnerClassNode

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

the class SuperCallTraitTransformer method getHelper.

private ClassNode getHelper(final ClassNode traitType) {
    // been created when referenced; create a placeholder to be resolved later.
    if (!traitType.redirect().getInnerClasses().hasNext() && getSourceUnit().getAST().getClasses().contains(traitType.redirect())) {
        ClassNode helperType = new InnerClassNode(traitType, Traits.helperClassName(traitType), ACC_PUBLIC | ACC_STATIC | ACC_ABSTRACT | ACC_SYNTHETIC, ClassHelper.OBJECT_TYPE, ClassNode.EMPTY_ARRAY, null).getPlainNodeReference();
        helperType.setRedirect(null);
        traitType.redirect().setNodeMetaData(UNRESOLVED_HELPER_CLASS, helperType);
        return helperType;
    }
    return Traits.findHelper(traitType);
}
Also used : ClassNode(org.codehaus.groovy.ast.ClassNode) InnerClassNode(org.codehaus.groovy.ast.InnerClassNode) InnerClassNode(org.codehaus.groovy.ast.InnerClassNode)

Example 73 with InnerClassNode

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

the class Traits method findHelpers.

static TraitHelpersTuple findHelpers(final ClassNode trait) {
    ClassNode helperClassNode = null;
    ClassNode fieldHelperClassNode = null;
    ClassNode staticFieldHelperClassNode = null;
    Iterator<InnerClassNode> innerClasses = trait.redirect().getInnerClasses();
    if (innerClasses != null && innerClasses.hasNext()) {
        // trait defined in same source unit
        while (innerClasses.hasNext()) {
            ClassNode icn = innerClasses.next();
            if (icn.getName().endsWith(Traits.FIELD_HELPER)) {
                fieldHelperClassNode = icn;
            } else if (icn.getName().endsWith(Traits.STATIC_FIELD_HELPER)) {
                staticFieldHelperClassNode = icn;
            } else if (icn.getName().endsWith(Traits.TRAIT_HELPER)) {
                helperClassNode = icn;
            }
        }
    } else {
        // precompiled trait
        try {
            final ClassLoader classLoader = trait.getTypeClass().getClassLoader();
            String helperClassName = Traits.helperClassName(trait);
            helperClassNode = ClassHelper.make(Class.forName(helperClassName, false, classLoader));
            try {
                fieldHelperClassNode = ClassHelper.make(classLoader.loadClass(Traits.fieldHelperClassName(trait)));
                staticFieldHelperClassNode = ClassHelper.make(classLoader.loadClass(Traits.staticFieldHelperClassName(trait)));
            } catch (ClassNotFoundException e) {
            // not a problem, the field helpers may be absent
            }
        } catch (ClassNotFoundException e) {
            throw new GroovyBugError("Couldn't find trait helper classes on compile classpath!", e);
        }
    }
    return new TraitHelpersTuple(helperClassNode, fieldHelperClassNode, staticFieldHelperClassNode);
}
Also used : ClassNode(org.codehaus.groovy.ast.ClassNode) InnerClassNode(org.codehaus.groovy.ast.InnerClassNode) GroovyBugError(org.codehaus.groovy.GroovyBugError) InnerClassNode(org.codehaus.groovy.ast.InnerClassNode)

Example 74 with InnerClassNode

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

the class InnerClassVisitor method visitClass.

@Override
public void visitClass(ClassNode node) {
    classNode = node;
    InnerClassNode innerClass = null;
    if (!node.isEnum() && !node.isInterface() && node instanceof InnerClassNode) {
        innerClass = (InnerClassNode) node;
        if (innerClass.getVariableScope() == null && (innerClass.getModifiers() & ACC_STATIC) == 0) {
            innerClass.addField("this$0", ACC_FINAL | ACC_SYNTHETIC, node.getOuterClass().getPlainNodeReference(), null);
        }
    }
    super.visitClass(node);
    if (node.isEnum() || node.isInterface())
        return;
    if (innerClass == null)
        return;
    if (node.getSuperClass().isInterface() || Traits.isAnnotatedWithTrait(node.getSuperClass())) {
        node.addInterface(node.getUnresolvedSuperClass());
        node.setUnresolvedSuperClass(ClassHelper.OBJECT_TYPE);
    }
}
Also used : InnerClassNode(org.codehaus.groovy.ast.InnerClassNode)

Example 75 with InnerClassNode

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

the class InnerClassCompletionVisitor method visitClass.

@Override
public void visitClass(final ClassNode node) {
    classNode = node;
    thisField = null;
    InnerClassNode innerClass = null;
    if (!node.isEnum() && !node.isInterface() && node instanceof InnerClassNode) {
        innerClass = (InnerClassNode) node;
        thisField = innerClass.getField("this$0");
        if (innerClass.getVariableScope() == null && innerClass.getDeclaredConstructors().isEmpty()) {
            // add empty default constructor
            addGeneratedConstructor(innerClass, ACC_PUBLIC, Parameter.EMPTY_ARRAY, null, null);
        }
    }
    if (node.isEnum() || node.isInterface())
        return;
    // use Iterator.hasNext() to check for available inner classes
    if (node.getInnerClasses().hasNext())
        addDispatcherMethods(node);
    if (innerClass == null)
        return;
    super.visitClass(node);
    addMopMethods(innerClass);
}
Also used : InnerClassNode(org.codehaus.groovy.ast.InnerClassNode)

Aggregations

InnerClassNode (org.codehaus.groovy.ast.InnerClassNode)78 ClassNode (org.codehaus.groovy.ast.ClassNode)67 MethodNode (org.codehaus.groovy.ast.MethodNode)33 FieldNode (org.codehaus.groovy.ast.FieldNode)20 Parameter (org.codehaus.groovy.ast.Parameter)20 BlockStatement (org.codehaus.groovy.ast.stmt.BlockStatement)20 ArrayList (java.util.ArrayList)17 MethodCallExpression (org.codehaus.groovy.ast.expr.MethodCallExpression)16 ClassExpression (org.codehaus.groovy.ast.expr.ClassExpression)15 ConstructorCallExpression (org.codehaus.groovy.ast.expr.ConstructorCallExpression)15 Expression (org.codehaus.groovy.ast.expr.Expression)15 ExpressionStatement (org.codehaus.groovy.ast.stmt.ExpressionStatement)14 VariableExpression (org.codehaus.groovy.ast.expr.VariableExpression)13 Statement (org.codehaus.groovy.ast.stmt.Statement)13 ConstructorNode (org.codehaus.groovy.ast.ConstructorNode)12 ClosureExpression (org.codehaus.groovy.ast.expr.ClosureExpression)12 ConstantExpression (org.codehaus.groovy.ast.expr.ConstantExpression)11 EnumConstantClassNode (org.codehaus.groovy.ast.EnumConstantClassNode)10 GenericsType (org.codehaus.groovy.ast.GenericsType)10 ArgumentListExpression (org.codehaus.groovy.ast.expr.ArgumentListExpression)9