Search in sources :

Example 21 with AnnotationVisitor

use of org.mvel2.asm.AnnotationVisitor in project mvel by mvel.

the class MethodNode method accept.

/**
 * Makes the given method visitor visit this method.
 *
 * @param mv
 *            a method visitor.
 */
public void accept(final MethodVisitor mv) {
    // visits the method parameters
    int i, j, n;
    n = parameters == null ? 0 : parameters.size();
    for (i = 0; i < n; i++) {
        ParameterNode parameter = parameters.get(i);
        mv.visitParameter(parameter.name, parameter.access);
    }
    // visits the method attributes
    if (annotationDefault != null) {
        AnnotationVisitor av = mv.visitAnnotationDefault();
        AnnotationNode.accept(av, null, annotationDefault);
        if (av != null) {
            av.visitEnd();
        }
    }
    n = visibleAnnotations == null ? 0 : visibleAnnotations.size();
    for (i = 0; i < n; ++i) {
        AnnotationNode an = visibleAnnotations.get(i);
        an.accept(mv.visitAnnotation(an.desc, true));
    }
    n = invisibleAnnotations == null ? 0 : invisibleAnnotations.size();
    for (i = 0; i < n; ++i) {
        AnnotationNode an = invisibleAnnotations.get(i);
        an.accept(mv.visitAnnotation(an.desc, false));
    }
    n = visibleTypeAnnotations == null ? 0 : visibleTypeAnnotations.size();
    for (i = 0; i < n; ++i) {
        TypeAnnotationNode an = visibleTypeAnnotations.get(i);
        an.accept(mv.visitTypeAnnotation(an.typeRef, an.typePath, an.desc, true));
    }
    n = invisibleTypeAnnotations == null ? 0 : invisibleTypeAnnotations.size();
    for (i = 0; i < n; ++i) {
        TypeAnnotationNode an = invisibleTypeAnnotations.get(i);
        an.accept(mv.visitTypeAnnotation(an.typeRef, an.typePath, an.desc, false));
    }
    n = visibleParameterAnnotations == null ? 0 : visibleParameterAnnotations.length;
    for (i = 0; i < n; ++i) {
        List<?> l = visibleParameterAnnotations[i];
        if (l == null) {
            continue;
        }
        for (j = 0; j < l.size(); ++j) {
            AnnotationNode an = (AnnotationNode) l.get(j);
            an.accept(mv.visitParameterAnnotation(i, an.desc, true));
        }
    }
    n = invisibleParameterAnnotations == null ? 0 : invisibleParameterAnnotations.length;
    for (i = 0; i < n; ++i) {
        List<?> l = invisibleParameterAnnotations[i];
        if (l == null) {
            continue;
        }
        for (j = 0; j < l.size(); ++j) {
            AnnotationNode an = (AnnotationNode) l.get(j);
            an.accept(mv.visitParameterAnnotation(i, an.desc, false));
        }
    }
    if (visited) {
        instructions.resetLabels();
    }
    n = attrs == null ? 0 : attrs.size();
    for (i = 0; i < n; ++i) {
        mv.visitAttribute(attrs.get(i));
    }
    // visits the method's code
    if (instructions.size() > 0) {
        mv.visitCode();
        // visits try catch blocks
        n = tryCatchBlocks == null ? 0 : tryCatchBlocks.size();
        for (i = 0; i < n; ++i) {
            tryCatchBlocks.get(i).updateIndex(i);
            tryCatchBlocks.get(i).accept(mv);
        }
        // visits instructions
        instructions.accept(mv);
        // visits local variables
        n = localVariables == null ? 0 : localVariables.size();
        for (i = 0; i < n; ++i) {
            localVariables.get(i).accept(mv);
        }
        // visits local variable annotations
        n = visibleLocalVariableAnnotations == null ? 0 : visibleLocalVariableAnnotations.size();
        for (i = 0; i < n; ++i) {
            visibleLocalVariableAnnotations.get(i).accept(mv, true);
        }
        n = invisibleLocalVariableAnnotations == null ? 0 : invisibleLocalVariableAnnotations.size();
        for (i = 0; i < n; ++i) {
            invisibleLocalVariableAnnotations.get(i).accept(mv, false);
        }
        // visits maxs
        mv.visitMaxs(maxStack, maxLocals);
        visited = true;
    }
    mv.visitEnd();
}
Also used : AnnotationVisitor(org.mvel2.asm.AnnotationVisitor)

Example 22 with AnnotationVisitor

use of org.mvel2.asm.AnnotationVisitor in project drools by kiegroup.

the class TraitClassBuilderImpl method buildClass.

public byte[] buildClass(ClassDefinition classDef, ClassLoader classLoader) {
    init(classDef);
    ClassWriter cw = null;
    try {
        String cName = BuildUtils.getInternalType(classDef.getClassName());
        String genericTypes = BuildUtils.getGenericTypes(classDef.getInterfaces());
        String superType = Type.getInternalName(Object.class);
        String[] intfaces = null;
        if (Object.class.getName().equals(classDef.getSuperClass())) {
            String[] tmp = BuildUtils.getInternalTypes(classDef.getInterfaces());
            intfaces = new String[tmp.length + 2];
            System.arraycopy(tmp, 0, intfaces, 0, tmp.length);
            intfaces[tmp.length] = Type.getInternalName(Serializable.class);
            intfaces[tmp.length + 1] = Type.getInternalName(GeneratedFact.class);
        } else {
            String[] tmp = BuildUtils.getInternalTypes(classDef.getInterfaces());
            intfaces = new String[tmp.length + 3];
            System.arraycopy(tmp, 0, intfaces, 0, tmp.length);
            intfaces[tmp.length] = BuildUtils.getInternalType(classDef.getSuperClass());
            intfaces[tmp.length + 1] = Type.getInternalName(Serializable.class);
            intfaces[tmp.length + 2] = Type.getInternalName(GeneratedFact.class);
        }
        cw = createClassWriter(classLoader, ACC_PUBLIC + ACC_ABSTRACT + ACC_INTERFACE, cName, genericTypes, superType, intfaces);
        {
            if (classDef.getDefinedClass() == null || classDef.getDefinedClass().getAnnotation(Trait.class) == null) {
                AnnotationVisitor av0 = cw.visitAnnotation(Type.getDescriptor(Trait.class), true);
                for (AnnotationDefinition adef : classDef.getAnnotations()) {
                    if (Trait.class.getName().equals(adef.getName())) {
                        addAnnotationAttribute(adef, av0);
                        break;
                    }
                }
                av0.visitEnd();
            }
        }
        for (FieldDefinition field : classDef.getFieldsDefinitions()) {
            buildField(cw, field);
        }
        finalizeCreation(classDef);
        cw.visitEnd();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return cw.toByteArray();
}
Also used : AnnotationDefinition(org.drools.core.factmodel.AnnotationDefinition) Serializable(java.io.Serializable) FieldDefinition(org.drools.core.factmodel.FieldDefinition) AnnotationVisitor(org.mvel2.asm.AnnotationVisitor) ClassGenerator.createClassWriter(org.drools.core.rule.builder.dialect.asm.ClassGenerator.createClassWriter) ClassWriter(org.mvel2.asm.ClassWriter) GeneratedFact(org.drools.core.factmodel.GeneratedFact)

Example 23 with AnnotationVisitor

use of org.mvel2.asm.AnnotationVisitor in project drools by kiegroup.

the class DefaultBeanClassBuilder method buildFieldAnnotations.

protected void buildFieldAnnotations(FieldDefinition fieldDef, FieldVisitor fv) {
    if (fieldDef.getAnnotations() != null) {
        for (AnnotationDefinition ad : fieldDef.getAnnotations()) {
            AnnotationVisitor av = fv.visitAnnotation("L" + BuildUtils.getInternalType(ad.getName()) + ";", true);
            addAnnotationAttribute(ad, av);
            av.visitEnd();
        }
    }
}
Also used : AnnotationVisitor(org.mvel2.asm.AnnotationVisitor)

Example 24 with AnnotationVisitor

use of org.mvel2.asm.AnnotationVisitor in project drools by kiegroup.

the class DefaultBeanClassBuilder method addAnnotationAttribute.

public static void addAnnotationAttribute(AnnotationDefinition ad, AnnotationVisitor av) {
    for (String key : ad.getValues().keySet()) {
        AnnotationDefinition.AnnotationPropertyVal apv = ad.getValues().get(key);
        switch(apv.getValType()) {
            case STRINGARRAY:
                AnnotationVisitor subAv = av.visitArray(apv.getProperty());
                Object[] array = (Object[]) apv.getValue();
                for (Object o : array) {
                    subAv.visit(null, o);
                }
                subAv.visitEnd();
                break;
            case PRIMARRAY:
                av.visit(apv.getProperty(), apv.getValue());
                break;
            case ENUMARRAY:
                AnnotationVisitor subEnav = av.visitArray(apv.getProperty());
                Enum[] enArray = (Enum[]) apv.getValue();
                String aenumType = "L" + BuildUtils.getInternalType(enArray[0].getClass().getName()) + ";";
                for (Enum enumer : enArray) {
                    subEnav.visitEnum(null, aenumType, enumer.name());
                }
                subEnav.visitEnd();
                break;
            case CLASSARRAY:
                AnnotationVisitor subKlav = av.visitArray(apv.getProperty());
                Class[] klarray = (Class[]) apv.getValue();
                for (Class klass : klarray) {
                    subKlav.visit(null, Type.getType("L" + BuildUtils.getInternalType(klass.getName()) + ";"));
                }
                subKlav.visitEnd();
                break;
            case ENUMERATION:
                String enumType = "L" + BuildUtils.getInternalType(apv.getType().getName()) + ";";
                av.visitEnum(apv.getProperty(), enumType, ((Enum) apv.getValue()).name());
                break;
            case KLASS:
                String klassName = BuildUtils.getInternalType(((Class) apv.getValue()).getName());
                av.visit(apv.getProperty(), Type.getType("L" + klassName + ";"));
                break;
            case PRIMITIVE:
                av.visit(apv.getProperty(), apv.getValue());
                break;
            case STRING:
                av.visit(apv.getProperty(), apv.getValue());
                break;
        }
    }
}
Also used : AnnotationVisitor(org.mvel2.asm.AnnotationVisitor) ReactiveObject(org.drools.core.phreak.ReactiveObject)

Example 25 with AnnotationVisitor

use of org.mvel2.asm.AnnotationVisitor in project drools by kiegroup.

the class DefaultBeanClassBuilder method buildFieldAnnotations.

protected void buildFieldAnnotations(FieldDefinition fieldDef, FieldVisitor fv) {
    if (fieldDef.getAnnotations() != null) {
        for (AnnotationDefinition ad : fieldDef.getAnnotations()) {
            AnnotationVisitor av = fv.visitAnnotation("L" + BuildUtils.getInternalType(ad.getName()) + ";", true);
            addAnnotationAttribute(ad, av);
            av.visitEnd();
        }
    }
}
Also used : AnnotationDefinition(org.drools.core.factmodel.AnnotationDefinition) AnnotationVisitor(org.mvel2.asm.AnnotationVisitor)

Aggregations

AnnotationVisitor (org.mvel2.asm.AnnotationVisitor)26 AnnotationDefinition (org.drools.core.factmodel.AnnotationDefinition)5 ClassWriter (org.mvel2.asm.ClassWriter)4 Type (org.mvel2.asm.Type)3 Externalizable (java.io.Externalizable)2 IOException (java.io.IOException)2 ObjectInput (java.io.ObjectInput)2 ObjectOutput (java.io.ObjectOutput)2 Serializable (java.io.Serializable)2 Constructor (java.lang.reflect.Constructor)2 Method (java.lang.reflect.Method)2 BitSet (java.util.BitSet)2 Collection (java.util.Collection)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 Set (java.util.Set)2 FieldDefinition (org.drools.core.factmodel.FieldDefinition)2 GeneratedFact (org.drools.core.factmodel.GeneratedFact)2 ReactiveObject (org.drools.core.phreak.ReactiveObject)2 ClassGenerator.createClassWriter (org.drools.core.rule.builder.dialect.asm.ClassGenerator.createClassWriter)2