Search in sources :

Example 26 with AnnotationNode

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

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

the class ExternalizeVerifierASTTransformation method visit.

public void visit(ASTNode[] nodes, SourceUnit source) {
    init(nodes, source);
    AnnotatedNode parent = (AnnotatedNode) nodes[1];
    AnnotationNode anno = (AnnotationNode) nodes[0];
    if (!MY_TYPE.equals(anno.getClassNode()))
        return;
    if (parent instanceof ClassNode) {
        ClassNode cNode = (ClassNode) parent;
        if (!hasNoargConstructor(cNode)) {
            addError(MY_TYPE_NAME + ": An Externalizable class requires a no-arg constructor but none found", cNode);
        }
        if (!implementsExternalizable(cNode)) {
            addError(MY_TYPE_NAME + ": An Externalizable class must implement the Externalizable interface", cNode);
        }
        boolean includeFields = memberHasValue(anno, "includeFields", true);
        boolean checkPropertyTypes = memberHasValue(anno, "checkPropertyTypes", true);
        List<String> excludes = getMemberList(anno, "excludes");
        List<FieldNode> list = getInstancePropertyFields(cNode);
        if (includeFields) {
            list.addAll(getInstanceNonPropertyFields(cNode));
        }
        checkProps(list, excludes, checkPropertyTypes);
    }
}
Also used : ClassNode(org.codehaus.groovy.ast.ClassNode) FieldNode(org.codehaus.groovy.ast.FieldNode) AnnotationNode(org.codehaus.groovy.ast.AnnotationNode) AnnotatedNode(org.codehaus.groovy.ast.AnnotatedNode)

Example 28 with AnnotationNode

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

the class ImmutableASTTransformation 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 (!node.getClassNode().getName().endsWith(".Immutable"))
        return;
    List<PropertyNode> newProperties = new ArrayList<PropertyNode>();
    if (parent instanceof ClassNode) {
        final List<String> knownImmutableClasses = getKnownImmutableClasses(node);
        final List<String> knownImmutables = getKnownImmutables(node);
        ClassNode cNode = (ClassNode) parent;
        String cName = cNode.getName();
        if (!checkNotInterface(cNode, MY_TYPE_NAME))
            return;
        makeClassFinal(cNode);
        final List<PropertyNode> pList = getInstanceProperties(cNode);
        for (PropertyNode pNode : pList) {
            adjustPropertyForImmutability(pNode, newProperties);
        }
        for (PropertyNode pNode : newProperties) {
            cNode.getProperties().remove(pNode);
            addProperty(cNode, pNode);
        }
        final List<FieldNode> fList = cNode.getFields();
        for (FieldNode fNode : fList) {
            ensureNotPublic(cName, fNode);
        }
        boolean includeSuperProperties = false;
        if (hasAnnotation(cNode, TupleConstructorASTTransformation.MY_TYPE)) {
            AnnotationNode tupleCons = cNode.getAnnotations(TupleConstructorASTTransformation.MY_TYPE).get(0);
            includeSuperProperties = memberHasValue(tupleCons, "includeSuperProperties", true);
            if (unsupportedTupleAttribute(tupleCons, "excludes"))
                return;
            if (unsupportedTupleAttribute(tupleCons, "includes"))
                return;
            if (unsupportedTupleAttribute(tupleCons, "includeFields"))
                return;
            if (unsupportedTupleAttribute(tupleCons, "includeProperties"))
                return;
            if (unsupportedTupleAttribute(tupleCons, "includeSuperFields"))
                return;
            if (unsupportedTupleAttribute(tupleCons, "callSuper"))
                return;
            if (unsupportedTupleAttribute(tupleCons, "force"))
                return;
        }
        createConstructors(cNode, knownImmutableClasses, knownImmutables, includeSuperProperties);
        if (!hasAnnotation(cNode, EqualsAndHashCodeASTTransformation.MY_TYPE)) {
            createHashCode(cNode, true, false, false, null, null);
            createEquals(cNode, false, false, false, null, null);
        }
        if (!hasAnnotation(cNode, ToStringASTTransformation.MY_TYPE)) {
            createToString(cNode, false, false, null, null, false, true);
        }
        if (memberHasValue(node, MEMBER_ADD_COPY_WITH, true) && pList.size() > 0 && !hasDeclaredMethod(cNode, COPY_WITH_METHOD, 1)) {
            createCopyWith(cNode, pList);
        }
    }
}
Also used : ClassNode(org.codehaus.groovy.ast.ClassNode) FieldNode(org.codehaus.groovy.ast.FieldNode) AnnotationNode(org.codehaus.groovy.ast.AnnotationNode) PropertyNode(org.codehaus.groovy.ast.PropertyNode) AnnotatedNode(org.codehaus.groovy.ast.AnnotatedNode) ArrayList(java.util.ArrayList) ToStringASTTransformation.createToString(org.codehaus.groovy.transform.ToStringASTTransformation.createToString)

Example 29 with AnnotationNode

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

the class ToStringASTTransformation method visit.

public void visit(ASTNode[] nodes, SourceUnit source) {
    init(nodes, source);
    AnnotatedNode parent = (AnnotatedNode) nodes[1];
    AnnotationNode anno = (AnnotationNode) nodes[0];
    if (!MY_TYPE.equals(anno.getClassNode()))
        return;
    if (parent instanceof ClassNode) {
        ClassNode cNode = (ClassNode) parent;
        if (!checkNotInterface(cNode, MY_TYPE_NAME))
            return;
        boolean includeSuper = memberHasValue(anno, "includeSuper", true);
        boolean includeSuperProperties = memberHasValue(anno, "includeSuperProperties", true);
        boolean cacheToString = memberHasValue(anno, "cache", true);
        if (includeSuper && cNode.getSuperClass().getName().equals("java.lang.Object")) {
            addError("Error during " + MY_TYPE_NAME + " processing: includeSuper=true but '" + cNode.getName() + "' has no super class.", anno);
        }
        boolean includeNames = memberHasValue(anno, "includeNames", true);
        boolean includeFields = memberHasValue(anno, "includeFields", true);
        List<String> excludes = getMemberList(anno, "excludes");
        List<String> includes = getMemberList(anno, "includes");
        boolean ignoreNulls = memberHasValue(anno, "ignoreNulls", true);
        boolean includePackage = !memberHasValue(anno, "includePackage", false);
        if (hasAnnotation(cNode, CanonicalASTTransformation.MY_TYPE)) {
            AnnotationNode canonical = cNode.getAnnotations(CanonicalASTTransformation.MY_TYPE).get(0);
            if (excludes == null || excludes.isEmpty())
                excludes = getMemberList(canonical, "excludes");
            if (includes == null || includes.isEmpty())
                includes = getMemberList(canonical, "includes");
        }
        if (!checkIncludeExclude(anno, excludes, includes, MY_TYPE_NAME))
            return;
        createToString(cNode, includeSuper, includeFields, excludes, includes, includeNames, ignoreNulls, includePackage, cacheToString, includeSuperProperties);
    }
}
Also used : ClassNode(org.codehaus.groovy.ast.ClassNode) AnnotationNode(org.codehaus.groovy.ast.AnnotationNode) AnnotatedNode(org.codehaus.groovy.ast.AnnotatedNode) ToString(groovy.transform.ToString)

Example 30 with AnnotationNode

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

the class TupleConstructorASTTransformation method visit.

public void visit(ASTNode[] nodes, SourceUnit source) {
    init(nodes, source);
    AnnotatedNode parent = (AnnotatedNode) nodes[1];
    AnnotationNode anno = (AnnotationNode) nodes[0];
    if (!MY_TYPE.equals(anno.getClassNode()))
        return;
    if (parent instanceof ClassNode) {
        ClassNode cNode = (ClassNode) parent;
        if (!checkNotInterface(cNode, MY_TYPE_NAME))
            return;
        boolean includeFields = memberHasValue(anno, "includeFields", true);
        boolean includeProperties = !memberHasValue(anno, "includeProperties", false);
        boolean includeSuperFields = memberHasValue(anno, "includeSuperFields", true);
        boolean includeSuperProperties = memberHasValue(anno, "includeSuperProperties", true);
        boolean callSuper = memberHasValue(anno, "callSuper", true);
        boolean force = memberHasValue(anno, "force", true);
        boolean useSetters = memberHasValue(anno, "useSetters", true);
        List<String> excludes = getMemberList(anno, "excludes");
        List<String> includes = getMemberList(anno, "includes");
        if (hasAnnotation(cNode, CanonicalASTTransformation.MY_TYPE)) {
            AnnotationNode canonical = cNode.getAnnotations(CanonicalASTTransformation.MY_TYPE).get(0);
            if (excludes == null || excludes.isEmpty())
                excludes = getMemberList(canonical, "excludes");
            if (includes == null || includes.isEmpty())
                includes = getMemberList(canonical, "includes");
        }
        if (!checkIncludeExclude(anno, excludes, includes, MY_TYPE_NAME))
            return;
        // if @Immutable is found, let it pick up options and do work so we'll skip
        if (hasAnnotation(cNode, ImmutableASTTransformation.MY_TYPE))
            return;
        createConstructor(cNode, includeFields, includeProperties, includeSuperFields, includeSuperProperties, callSuper, force, excludes, includes, useSetters);
    }
}
Also used : ClassNode(org.codehaus.groovy.ast.ClassNode) AnnotationNode(org.codehaus.groovy.ast.AnnotationNode) AnnotatedNode(org.codehaus.groovy.ast.AnnotatedNode)

Aggregations

AnnotationNode (org.codehaus.groovy.ast.AnnotationNode)248 ClassNode (org.codehaus.groovy.ast.ClassNode)158 AnnotatedNode (org.codehaus.groovy.ast.AnnotatedNode)78 Expression (org.codehaus.groovy.ast.expr.Expression)68 ConstantExpression (org.codehaus.groovy.ast.expr.ConstantExpression)64 ClassExpression (org.codehaus.groovy.ast.expr.ClassExpression)60 MethodNode (org.codehaus.groovy.ast.MethodNode)57 FieldNode (org.codehaus.groovy.ast.FieldNode)54 ArrayList (java.util.ArrayList)53 MethodCallExpression (org.codehaus.groovy.ast.expr.MethodCallExpression)49 VariableExpression (org.codehaus.groovy.ast.expr.VariableExpression)48 InnerClassNode (org.codehaus.groovy.ast.InnerClassNode)44 ListExpression (org.codehaus.groovy.ast.expr.ListExpression)36 ArgumentListExpression (org.codehaus.groovy.ast.expr.ArgumentListExpression)35 BinaryExpression (org.codehaus.groovy.ast.expr.BinaryExpression)30 ClosureExpression (org.codehaus.groovy.ast.expr.ClosureExpression)30 Parameter (org.codehaus.groovy.ast.Parameter)29 DeclarationExpression (org.codehaus.groovy.ast.expr.DeclarationExpression)29 PropertyExpression (org.codehaus.groovy.ast.expr.PropertyExpression)29 ConstructorCallExpression (org.codehaus.groovy.ast.expr.ConstructorCallExpression)28