Search in sources :

Example 56 with AnnotationVisitor

use of org.objectweb.asm.AnnotationVisitor in project drill by apache.

the class CheckMethodVisitorFsm method visitParameterAnnotation.

@Override
public AnnotationVisitor visitParameterAnnotation(final int parameter, final String desc, final boolean visible) {
    fsmCursor.transition("visitParameterAnnotation");
    final AnnotationVisitor annotationVisitor = super.visitParameterAnnotation(parameter, desc, visible);
    // TODO: add CheckAnnotationVisitorFsm
    return annotationVisitor;
}
Also used : AnnotationVisitor(org.objectweb.asm.AnnotationVisitor)

Example 57 with AnnotationVisitor

use of org.objectweb.asm.AnnotationVisitor in project groovy by apache.

the class AsmClassGenerator method visitAnnotationAttributes.

/**
     * Generate the annotation attributes.
     * @param an the node with an annotation
     * @param av the visitor to use
     */
private void visitAnnotationAttributes(AnnotationNode an, AnnotationVisitor av) {
    Map<String, Object> constantAttrs = new HashMap<String, Object>();
    Map<String, PropertyExpression> enumAttrs = new HashMap<String, PropertyExpression>();
    Map<String, Object> atAttrs = new HashMap<String, Object>();
    Map<String, ListExpression> arrayAttrs = new HashMap<String, ListExpression>();
    for (String name : an.getMembers().keySet()) {
        Expression expr = an.getMember(name);
        if (expr instanceof AnnotationConstantExpression) {
            atAttrs.put(name, ((AnnotationConstantExpression) expr).getValue());
        } else if (expr instanceof ConstantExpression) {
            constantAttrs.put(name, ((ConstantExpression) expr).getValue());
        } else if (expr instanceof ClassExpression) {
            constantAttrs.put(name, Type.getType(BytecodeHelper.getTypeDescription((expr.getType()))));
        } else if (expr instanceof PropertyExpression) {
            enumAttrs.put(name, (PropertyExpression) expr);
        } else if (expr instanceof ListExpression) {
            arrayAttrs.put(name, (ListExpression) expr);
        } else if (expr instanceof ClosureExpression) {
            ClassNode closureClass = controller.getClosureWriter().getOrAddClosureClass((ClosureExpression) expr, ACC_PUBLIC);
            constantAttrs.put(name, Type.getType(BytecodeHelper.getTypeDescription(closureClass)));
        }
    }
    for (Map.Entry entry : constantAttrs.entrySet()) {
        av.visit((String) entry.getKey(), entry.getValue());
    }
    for (Map.Entry entry : enumAttrs.entrySet()) {
        PropertyExpression propExp = (PropertyExpression) entry.getValue();
        av.visitEnum((String) entry.getKey(), BytecodeHelper.getTypeDescription(propExp.getObjectExpression().getType()), String.valueOf(((ConstantExpression) propExp.getProperty()).getValue()));
    }
    for (Map.Entry entry : atAttrs.entrySet()) {
        AnnotationNode atNode = (AnnotationNode) entry.getValue();
        AnnotationVisitor av2 = av.visitAnnotation((String) entry.getKey(), BytecodeHelper.getTypeDescription(atNode.getClassNode()));
        visitAnnotationAttributes(atNode, av2);
        av2.visitEnd();
    }
    visitArrayAttributes(an, arrayAttrs, av);
}
Also used : InnerClassNode(org.codehaus.groovy.ast.InnerClassNode) InterfaceHelperClassNode(org.codehaus.groovy.ast.InterfaceHelperClassNode) ClassNode(org.codehaus.groovy.ast.ClassNode) HashMap(java.util.HashMap) AnnotationNode(org.codehaus.groovy.ast.AnnotationNode) AnnotationVisitor(org.objectweb.asm.AnnotationVisitor) Map(java.util.Map) HashMap(java.util.HashMap)

Example 58 with AnnotationVisitor

use of org.objectweb.asm.AnnotationVisitor 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)

Example 59 with AnnotationVisitor

use of org.objectweb.asm.AnnotationVisitor in project groovy by apache.

the class AsmClassGenerator method visitAnnotationDefaultExpression.

void visitAnnotationDefaultExpression(AnnotationVisitor av, ClassNode type, Expression exp) {
    if (exp instanceof ClosureExpression) {
        ClassNode closureClass = controller.getClosureWriter().getOrAddClosureClass((ClosureExpression) exp, ACC_PUBLIC);
        Type t = Type.getType(BytecodeHelper.getTypeDescription(closureClass));
        av.visit(null, t);
    } else if (type.isArray()) {
        AnnotationVisitor avl = av.visitArray(null);
        ClassNode componentType = type.getComponentType();
        if (exp instanceof ListExpression) {
            ListExpression list = (ListExpression) exp;
            for (Expression lExp : list.getExpressions()) {
                visitAnnotationDefaultExpression(avl, componentType, lExp);
            }
        } else {
            visitAnnotationDefaultExpression(avl, componentType, exp);
        }
    } else if (ClassHelper.isPrimitiveType(type) || type.equals(ClassHelper.STRING_TYPE)) {
        ConstantExpression constExp = (ConstantExpression) exp;
        av.visit(null, constExp.getValue());
    } else if (ClassHelper.CLASS_Type.equals(type)) {
        ClassNode clazz = exp.getType();
        Type t = Type.getType(BytecodeHelper.getTypeDescription(clazz));
        av.visit(null, t);
    } else if (type.isDerivedFrom(ClassHelper.Enum_Type)) {
        PropertyExpression pExp = (PropertyExpression) exp;
        ClassExpression cExp = (ClassExpression) pExp.getObjectExpression();
        String desc = BytecodeHelper.getTypeDescription(cExp.getType());
        String name = pExp.getPropertyAsString();
        av.visitEnum(null, desc, name);
    } else if (type.implementsInterface(ClassHelper.Annotation_TYPE)) {
        AnnotationConstantExpression avExp = (AnnotationConstantExpression) exp;
        AnnotationNode value = (AnnotationNode) avExp.getValue();
        AnnotationVisitor avc = av.visitAnnotation(null, BytecodeHelper.getTypeDescription(avExp.getType()));
        visitAnnotationAttributes(value, avc);
    } else {
        throw new GroovyBugError("unexpected annotation type " + type.getName());
    }
    av.visitEnd();
}
Also used : InnerClassNode(org.codehaus.groovy.ast.InnerClassNode) InterfaceHelperClassNode(org.codehaus.groovy.ast.InterfaceHelperClassNode) ClassNode(org.codehaus.groovy.ast.ClassNode) GroovyBugError(org.codehaus.groovy.GroovyBugError) Type(org.objectweb.asm.Type) GenericsType(org.codehaus.groovy.ast.GenericsType) AnnotationNode(org.codehaus.groovy.ast.AnnotationNode) AnnotationVisitor(org.objectweb.asm.AnnotationVisitor)

Example 60 with AnnotationVisitor

use of org.objectweb.asm.AnnotationVisitor in project groovy by apache.

the class AsmClassGenerator method visitAnnotations.

private void visitAnnotations(AnnotatedNode targetNode, Object visitor) {
    for (AnnotationNode an : targetNode.getAnnotations()) {
        // skip built-in properties
        if (an.isBuiltIn())
            continue;
        if (an.hasSourceRetention())
            continue;
        AnnotationVisitor av = getAnnotationVisitor(targetNode, an, visitor);
        visitAnnotationAttributes(an, av);
        av.visitEnd();
    }
}
Also used : AnnotationNode(org.codehaus.groovy.ast.AnnotationNode) AnnotationVisitor(org.objectweb.asm.AnnotationVisitor)

Aggregations

AnnotationVisitor (org.objectweb.asm.AnnotationVisitor)64 AnnotationNode (org.codehaus.groovy.ast.AnnotationNode)12 ArrayList (java.util.ArrayList)9 Type (org.objectweb.asm.Type)9 MethodVisitor (org.objectweb.asm.MethodVisitor)7 LayoutlibDelegate (com.android.tools.layoutlib.annotations.LayoutlibDelegate)6 ClassNode (org.codehaus.groovy.ast.ClassNode)6 InnerClassNode (org.codehaus.groovy.ast.InnerClassNode)6 InterfaceHelperClassNode (org.codehaus.groovy.ast.InterfaceHelperClassNode)6 ClassVisitor (org.objectweb.asm.ClassVisitor)6 ClassReader (org.objectweb.asm.ClassReader)5 HashMap (java.util.HashMap)4 Map (java.util.Map)4 FieldVisitor (org.objectweb.asm.FieldVisitor)3 Label (org.objectweb.asm.Label)3 GroovyRuntimeException (groovy.lang.GroovyRuntimeException)2 InputStream (java.io.InputStream)2 List (java.util.List)2 GroovyBugError (org.codehaus.groovy.GroovyBugError)2 GenericsType (org.codehaus.groovy.ast.GenericsType)2