Search in sources :

Example 1 with PackageNode

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

the class AntlrParserPlugin method packageDef.

// Top level control structures
//-------------------------------------------------------------------------
protected void packageDef(AST packageDef) {
    List<AnnotationNode> annotations = new ArrayList<AnnotationNode>();
    AST node = packageDef.getFirstChild();
    if (isType(ANNOTATIONS, node)) {
        processAnnotations(annotations, node);
        node = node.getNextSibling();
    }
    String name = qualifiedName(node);
    // TODO should we check package node doesn't already exist? conflict?
    PackageNode packageNode = setPackage(name, annotations);
    configureAST(packageNode, packageDef);
}
Also used : AST(antlr.collections.AST) AnnotationNode(org.codehaus.groovy.ast.AnnotationNode) ArrayList(java.util.ArrayList) PackageNode(org.codehaus.groovy.ast.PackageNode)

Example 2 with PackageNode

use of org.codehaus.groovy.ast.PackageNode 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 3 with PackageNode

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

the class BaseScriptASTTransformation method visit.

public void visit(ASTNode[] nodes, SourceUnit source) {
    init(nodes, source);
    AnnotatedNode parent = (AnnotatedNode) nodes[1];
    AnnotationNode node = (AnnotationNode) nodes[0];
    if (!MY_TYPE.equals(node.getClassNode()))
        return;
    if (parent instanceof DeclarationExpression) {
        changeBaseScriptTypeFromDeclaration((DeclarationExpression) parent, node);
    } else if (parent instanceof ImportNode || parent instanceof PackageNode) {
        changeBaseScriptTypeFromPackageOrImport(source, parent, node);
    } else if (parent instanceof ClassNode) {
        changeBaseScriptTypeFromClass((ClassNode) parent, node);
    }
}
Also used : ClassNode(org.codehaus.groovy.ast.ClassNode) AnnotationNode(org.codehaus.groovy.ast.AnnotationNode) AnnotatedNode(org.codehaus.groovy.ast.AnnotatedNode) DeclarationExpression(org.codehaus.groovy.ast.expr.DeclarationExpression) ImportNode(org.codehaus.groovy.ast.ImportNode) PackageNode(org.codehaus.groovy.ast.PackageNode)

Example 4 with PackageNode

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

the class Java5 method configureClassNode.

public void configureClassNode(CompileUnit compileUnit, ClassNode classNode) {
    try {
        Class clazz = classNode.getTypeClass();
        Field[] fields = clazz.getDeclaredFields();
        for (Field f : fields) {
            ClassNode ret = makeClassNode(compileUnit, f.getGenericType(), f.getType());
            FieldNode fn = new FieldNode(f.getName(), f.getModifiers(), ret, classNode, null);
            setAnnotationMetaData(f.getAnnotations(), fn);
            classNode.addField(fn);
        }
        Method[] methods = clazz.getDeclaredMethods();
        for (Method m : methods) {
            ClassNode ret = makeClassNode(compileUnit, m.getGenericReturnType(), m.getReturnType());
            Parameter[] params = makeParameters(compileUnit, m.getGenericParameterTypes(), m.getParameterTypes(), m.getParameterAnnotations());
            ClassNode[] exceptions = makeClassNodes(compileUnit, m.getGenericExceptionTypes(), m.getExceptionTypes());
            MethodNode mn = new MethodNode(m.getName(), m.getModifiers(), ret, params, exceptions, null);
            mn.setSynthetic(m.isSynthetic());
            setMethodDefaultValue(mn, m);
            setAnnotationMetaData(m.getAnnotations(), mn);
            mn.setGenericsTypes(configureTypeVariable(m.getTypeParameters()));
            classNode.addMethod(mn);
        }
        Constructor[] constructors = clazz.getDeclaredConstructors();
        for (Constructor ctor : constructors) {
            Parameter[] params = makeParameters(compileUnit, ctor.getGenericParameterTypes(), ctor.getParameterTypes(), ctor.getParameterAnnotations());
            ClassNode[] exceptions = makeClassNodes(compileUnit, ctor.getGenericExceptionTypes(), ctor.getExceptionTypes());
            classNode.addConstructor(ctor.getModifiers(), params, exceptions, null);
        }
        Class sc = clazz.getSuperclass();
        if (sc != null)
            classNode.setUnresolvedSuperClass(makeClassNode(compileUnit, clazz.getGenericSuperclass(), sc));
        makeInterfaceTypes(compileUnit, classNode, clazz);
        setAnnotationMetaData(classNode.getTypeClass().getAnnotations(), classNode);
        PackageNode packageNode = classNode.getPackage();
        if (packageNode != null) {
            setAnnotationMetaData(classNode.getTypeClass().getPackage().getAnnotations(), packageNode);
        }
    } catch (NoClassDefFoundError e) {
        throw new NoClassDefFoundError("Unable to load class " + classNode.toString(false) + " due to missing dependency " + e.getMessage());
    }
}
Also used : ClassNode(org.codehaus.groovy.ast.ClassNode) FieldNode(org.codehaus.groovy.ast.FieldNode) Constructor(java.lang.reflect.Constructor) Method(java.lang.reflect.Method) Field(java.lang.reflect.Field) MethodNode(org.codehaus.groovy.ast.MethodNode) Parameter(org.codehaus.groovy.ast.Parameter) PackageNode(org.codehaus.groovy.ast.PackageNode)

Example 5 with PackageNode

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

the class GeneralUtils method inSamePackage.

public static boolean inSamePackage(final ClassNode first, final ClassNode second) {
    PackageNode firstPackage = first.getPackage();
    PackageNode secondPackage = second.getPackage();
    return ((firstPackage == null && secondPackage == null) || firstPackage != null && secondPackage != null && firstPackage.getName().equals(secondPackage.getName()));
}
Also used : PackageNode(org.codehaus.groovy.ast.PackageNode)

Aggregations

PackageNode (org.codehaus.groovy.ast.PackageNode)17 ClassNode (org.codehaus.groovy.ast.ClassNode)10 AnnotationNode (org.codehaus.groovy.ast.AnnotationNode)5 MethodNode (org.codehaus.groovy.ast.MethodNode)5 GroovyRuntimeException (groovy.lang.GroovyRuntimeException)3 Constructor (java.lang.reflect.Constructor)3 Field (java.lang.reflect.Field)3 Method (java.lang.reflect.Method)3 FieldNode (org.codehaus.groovy.ast.FieldNode)3 Parameter (org.codehaus.groovy.ast.Parameter)3 Test (org.junit.jupiter.api.Test)3 MalformedParameterizedTypeException (java.lang.reflect.MalformedParameterizedTypeException)2 AnnotatedNode (org.codehaus.groovy.ast.AnnotatedNode)2 ImportNode (org.codehaus.groovy.ast.ImportNode)2 InnerClassNode (org.codehaus.groovy.ast.InnerClassNode)2 InterfaceHelperClassNode (org.codehaus.groovy.ast.InterfaceHelperClassNode)2 DeclarationExpression (org.codehaus.groovy.ast.expr.DeclarationExpression)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