Search in sources :

Example 1 with RecordComponentVisitor

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

the class AsmClassGenerator method visitTypeAnnotations.

private void visitTypeAnnotations(final ClassNode sourceNode, final Object visitor, final TypeReference typeRef, final String typePathStr, boolean typeUse) {
    for (AnnotationNode an : sourceNode.getTypeAnnotations()) {
        if (an.isBuiltIn() || an.hasSourceRetention())
            continue;
        if (typeUse && !an.isTargetAllowed(AnnotationNode.TYPE_USE_TARGET))
            continue;
        AnnotationVisitor av = null;
        final TypePath typePath;
        try {
            typePath = TypePath.fromString(typePathStr);
        } catch (IllegalArgumentException ex) {
            throw new GroovyBugError("Illegal type path for " + sourceNode.getText() + ", typeRef = " + typeRef + ", typePath = " + typePathStr);
        }
        final int typeRefInt = typeRef.getValue();
        final String annotationDescriptor = BytecodeHelper.getTypeDescription(an.getClassNode());
        if (visitor instanceof ClassVisitor) {
            av = ((ClassVisitor) visitor).visitTypeAnnotation(typeRefInt, typePath, annotationDescriptor, an.hasRuntimeRetention());
        } else if (visitor instanceof MethodVisitor) {
            av = ((MethodVisitor) visitor).visitTypeAnnotation(typeRefInt, typePath, annotationDescriptor, an.hasRuntimeRetention());
        } else if (visitor instanceof FieldVisitor) {
            av = ((FieldVisitor) visitor).visitTypeAnnotation(typeRefInt, typePath, annotationDescriptor, an.hasRuntimeRetention());
        } else if (visitor instanceof RecordComponentVisitor) {
            av = ((RecordComponentVisitor) visitor).visitTypeAnnotation(typeRefInt, typePath, annotationDescriptor, an.hasRuntimeRetention());
        } else {
            throwException("Cannot create an AnnotationVisitor. Please report Groovy bug");
        }
        visitAnnotationAttributes(an, av);
        av.visitEnd();
    }
}
Also used : AnnotationNode(org.codehaus.groovy.ast.AnnotationNode) TypePath(org.objectweb.asm.TypePath) AnnotationVisitor(org.objectweb.asm.AnnotationVisitor) GroovyBugError(org.codehaus.groovy.GroovyBugError) RecordComponentVisitor(org.objectweb.asm.RecordComponentVisitor) ClassVisitor(org.objectweb.asm.ClassVisitor) FieldVisitor(org.objectweb.asm.FieldVisitor) TraceMethodVisitor(org.objectweb.asm.util.TraceMethodVisitor) MethodVisitor(org.objectweb.asm.MethodVisitor)

Example 2 with RecordComponentVisitor

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

the class AsmClassGenerator method visitRecordComponents.

private void visitRecordComponents(final ClassNode classNode) {
    List<RecordComponentNode> recordComponentNodeList = classNode.getRecordComponents();
    if (null == recordComponentNodeList)
        return;
    for (RecordComponentNode recordComponentNode : recordComponentNodeList) {
        final ClassNode type = recordComponentNode.getType();
        RecordComponentVisitor rcv = classVisitor.visitRecordComponent(recordComponentNode.getName(), BytecodeHelper.getTypeDescription(type), BytecodeHelper.getTypeGenericsSignature(type));
        visitAnnotations(recordComponentNode, rcv);
        // the int encoded value of the type reference is ALWAYS `318767104`
        // TODO Get the magic number `318767104` via `TypeReference.newXXX()`
        TypeReference typeRef = new TypeReference(318767104);
        visitTypeAnnotations(recordComponentNode.getType(), rcv, typeRef, "", true);
        rcv.visitEnd();
    }
}
Also used : InterfaceHelperClassNode(org.codehaus.groovy.ast.InterfaceHelperClassNode) ClassNode(org.codehaus.groovy.ast.ClassNode) InnerClassNode(org.codehaus.groovy.ast.InnerClassNode) RecordComponentNode(org.codehaus.groovy.ast.RecordComponentNode) RecordComponentVisitor(org.objectweb.asm.RecordComponentVisitor) TypeReference.newTypeReference(org.objectweb.asm.TypeReference.newTypeReference) TypeReference(org.objectweb.asm.TypeReference) TypeReference.newSuperTypeReference(org.objectweb.asm.TypeReference.newSuperTypeReference)

Aggregations

RecordComponentVisitor (org.objectweb.asm.RecordComponentVisitor)2 GroovyBugError (org.codehaus.groovy.GroovyBugError)1 AnnotationNode (org.codehaus.groovy.ast.AnnotationNode)1 ClassNode (org.codehaus.groovy.ast.ClassNode)1 InnerClassNode (org.codehaus.groovy.ast.InnerClassNode)1 InterfaceHelperClassNode (org.codehaus.groovy.ast.InterfaceHelperClassNode)1 RecordComponentNode (org.codehaus.groovy.ast.RecordComponentNode)1 AnnotationVisitor (org.objectweb.asm.AnnotationVisitor)1 ClassVisitor (org.objectweb.asm.ClassVisitor)1 FieldVisitor (org.objectweb.asm.FieldVisitor)1 MethodVisitor (org.objectweb.asm.MethodVisitor)1 TypePath (org.objectweb.asm.TypePath)1 TypeReference (org.objectweb.asm.TypeReference)1 TypeReference.newSuperTypeReference (org.objectweb.asm.TypeReference.newSuperTypeReference)1 TypeReference.newTypeReference (org.objectweb.asm.TypeReference.newTypeReference)1 TraceMethodVisitor (org.objectweb.asm.util.TraceMethodVisitor)1