Search in sources :

Example 51 with ClassWriter

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

the class DefaultEnumClassBuilder method buildGettersAndSetters.

protected void buildGettersAndSetters(ClassWriter cw, EnumClassDefinition classDef) {
    MethodVisitor mv;
    {
        mv = cw.visitMethod(ACC_PUBLIC + ACC_STATIC, "values", "()[" + BuildUtils.getTypeDescriptor(classDef.getClassName()), null, null);
        mv.visitCode();
        mv.visitFieldInsn(GETSTATIC, BuildUtils.getInternalType(classDef.getClassName()), "$VALUES", "[" + BuildUtils.getTypeDescriptor(classDef.getClassName()));
        mv.visitMethodInsn(INVOKEVIRTUAL, "[" + BuildUtils.getTypeDescriptor(classDef.getClassName()), "clone", "()Ljava/lang/Object;");
        mv.visitTypeInsn(CHECKCAST, "[" + BuildUtils.getTypeDescriptor(classDef.getClassName()));
        mv.visitInsn(ARETURN);
        mv.visitMaxs(1, 0);
        mv.visitEnd();
    }
    for (FieldDefinition fld : classDef.getFieldsDefinitions()) {
        mv = cw.visitMethod(ACC_PUBLIC, BuildUtils.getterName(fld.getName(), fld.getTypeName()), "()" + BuildUtils.getTypeDescriptor(fld.getTypeName()), null, null);
        mv.visitCode();
        mv.visitVarInsn(ALOAD, 0);
        mv.visitFieldInsn(GETFIELD, BuildUtils.getInternalType(classDef.getName()), fld.getName(), BuildUtils.getTypeDescriptor(fld.getTypeName()));
        mv.visitInsn(AsmUtil.returnType(fld.getTypeName()));
        mv.visitMaxs(BuildUtils.sizeOf(fld.getTypeName()), 1);
        mv.visitEnd();
        mv = cw.visitMethod(ACC_PUBLIC, BuildUtils.setterName(fld.getName()), "(" + BuildUtils.getTypeDescriptor(fld.getTypeName()) + ")V", null, null);
        mv.visitCode();
        mv.visitInsn(RETURN);
        mv.visitMaxs(0, 1 + BuildUtils.sizeOf(fld.getTypeName()));
        mv.visitEnd();
    }
}
Also used : FieldDefinition(org.drools.core.factmodel.FieldDefinition) MethodVisitor(org.mvel2.asm.MethodVisitor)

Example 52 with ClassWriter

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

the class ASMAccessorOptimizer method _initJIT.

/**
 * Does all the boilerplate for initiating the JIT.
 */
private void _initJIT() {
    if (isAdvancedDebugging()) {
        buildLog = new StringAppender();
    }
    cw = new ClassWriter(ClassWriter.COMPUTE_MAXS + ClassWriter.COMPUTE_FRAMES);
    synchronized (Runtime.getRuntime()) {
        cw.visit(OPCODES_VERSION, Opcodes.ACC_PUBLIC + Opcodes.ACC_SUPER, className = "ASMAccessorImpl_" + valueOf(cw.hashCode()).replaceAll("\\-", "_") + (System.currentTimeMillis() / 10) + ((int) (Math.random() * 100)), null, "java/lang/Object", new String[] { NAMESPACE + "compiler/Accessor" });
    }
    MethodVisitor m = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);
    m.visitCode();
    m.visitVarInsn(Opcodes.ALOAD, 0);
    m.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>", "()V");
    m.visitInsn(RETURN);
    m.visitMaxs(1, 1);
    m.visitEnd();
    (mv = cw.visitMethod(ACC_PUBLIC, "getValue", "(Ljava/lang/Object;Ljava/lang/Object;L" + NAMESPACE + "integration/VariableResolverFactory;)Ljava/lang/Object;", null, null)).visitCode();
}
Also used : StringAppender(org.mvel2.util.StringAppender) ClassWriter(org.mvel2.asm.ClassWriter) MethodVisitor(org.mvel2.asm.MethodVisitor)

Example 53 with ClassWriter

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

the class DefaultEnumClassBuilder method buildClass.

/**
 * Dynamically builds, defines and loads a class based on the given class definition
 *
 * @param classDef the class definition object structure
 *
 * @return the Class instance for the given class definition
 *
 * @throws java.io.IOException
 * @throws java.lang.reflect.InvocationTargetException
 * @throws IllegalAccessException
 * @throws NoSuchMethodException
 * @throws ClassNotFoundException
 * @throws IllegalArgumentException
 * @throws SecurityException
 * @throws NoSuchFieldException
 * @throws InstantiationException
 */
public byte[] buildClass(ClassDefinition classDef, ClassLoader classLoader) throws IOException, SecurityException, IllegalArgumentException, ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException, NoSuchFieldException {
    if (!(classDef instanceof EnumClassDefinition)) {
        throw new RuntimeException("FATAL : Trying to create an enum out of a bean class definition  " + classDef);
    }
    EnumClassDefinition edef = (EnumClassDefinition) classDef;
    ClassWriter cw = this.buildClassHeader(classLoader, edef);
    this.buildLiterals(cw, edef);
    this.buildFields(cw, edef);
    this.buildConstructors(cw, edef);
    this.buildGettersAndSetters(cw, edef);
    this.buildEqualityMethods(cw, edef);
    this.buildToString(cw, edef);
    cw.visitEnd();
    byte[] serializedClass = cw.toByteArray();
    return serializedClass;
}
Also used : ClassGenerator.createClassWriter(org.drools.core.rule.builder.dialect.asm.ClassGenerator.createClassWriter) ClassWriter(org.mvel2.asm.ClassWriter)

Example 54 with ClassWriter

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

the class TraitBuilderUtil method buildMixinMethods.

private static void buildMixinMethods(ClassWriter cw, String wrapperName, String mixin, Class mixinClass, MixinInfo mixinInfo, Collection<Method> mixinMethods, Set<String> createdSignatures) {
    for (Method method : mixinMethods) {
        String signature = TraitFactory.buildSignature(method);
        String methodSignature = method.getName() + signature;
        if (createdSignatures.contains(methodSignature)) {
            if (mixinInfo.throwsErrorOnConflict()) {
                throw new RuntimeException("Conflict on method: " + method.getName());
            }
            continue;
        }
        createdSignatures.add(methodSignature);
        {
            MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, method.getName(), signature, null, null);
            mv.visitCode();
            mv.visitVarInsn(ALOAD, 0);
            mv.visitFieldInsn(GETFIELD, BuildUtils.getInternalType(wrapperName), mixin, Type.getDescriptor(mixinClass));
            int j = 1;
            for (Class arg : method.getParameterTypes()) {
                mv.visitVarInsn(BuildUtils.varType(arg.getName()), j++);
            }
            mv.visitMethodInsn(INVOKEVIRTUAL, Type.getInternalName(mixinClass), method.getName(), signature, false);
            mv.visitInsn(BuildUtils.returnType(method.getReturnType().getName()));
            mv.visitMaxs(0, 0);
            mv.visitEnd();
        }
    }
}
Also used : Method(java.lang.reflect.Method) MethodVisitor(org.mvel2.asm.MethodVisitor)

Example 55 with ClassWriter

use of org.mvel2.asm.ClassWriter 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)

Aggregations

MethodVisitor (org.mvel2.asm.MethodVisitor)58 ClassWriter (org.mvel2.asm.ClassWriter)32 FieldDefinition (org.drools.core.factmodel.FieldDefinition)23 FieldVisitor (org.mvel2.asm.FieldVisitor)23 Map (java.util.Map)20 Label (org.mvel2.asm.Label)20 BitSet (java.util.BitSet)12 ClassGenerator.createClassWriter (org.drools.core.rule.builder.dialect.asm.ClassGenerator.createClassWriter)12 ClassGenerator.createClassWriter (org.drools.mvel.asm.ClassGenerator.createClassWriter)12 Serializable (java.io.Serializable)8 Type (org.mvel2.asm.Type)8 IOException (java.io.IOException)7 Method (java.lang.reflect.Method)7 ObjectInput (java.io.ObjectInput)6 ObjectOutput (java.io.ObjectOutput)6 Externalizable (java.io.Externalizable)4 Collection (java.util.Collection)4 Thing (org.drools.core.factmodel.traits.Thing)4 TraitFieldTMS (org.drools.core.factmodel.traits.TraitFieldTMS)3 ReactiveObject (org.drools.core.phreak.ReactiveObject)3