use of org.mvel2.asm.ClassWriter in project drools by kiegroup.
the class AbstractProxyClassBuilderImpl method buildShadowMethod.
protected void buildShadowMethod(ClassWriter cw, ClassDefinition trait, ClassDefinition core, Method m) {
MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, m.getName(), Type.getMethodDescriptor(m), null, null);
mv.visitCode();
mv.visitVarInsn(ALOAD, 0);
mv.visitMethodInsn(INVOKEVIRTUAL, BuildUtils.getInternalType(TraitFactoryImpl.getProxyName(trait, core)), "getCore", Type.getMethodDescriptor(Type.getType(core.getDefinedClass())), false);
for (int j = 0; j < m.getParameterTypes().length; j++) {
mv.visitVarInsn(AsmUtil.varType(m.getParameterTypes()[j].getName()), j + 1);
}
mv.visitMethodInsn(INVOKEVIRTUAL, Type.getInternalName(core.getDefinedClass()), m.getName(), Type.getMethodDescriptor(m), core.getDefinedClass().isInterface());
mv.visitInsn(AsmUtil.returnType(m.getReturnType().getName()));
mv.visitMaxs(0, 0);
mv.visitEnd();
}
use of org.mvel2.asm.ClassWriter in project drools by kiegroup.
the class ClassGenerator method generateBytecode.
public byte[] generateBytecode() {
if (bytecode == null) {
ClassWriter cw = createClassWriter(writableClassLoader.asClassLoader(), access, getClassDescriptor(), signature, getSuperClassDescriptor(), toInteralNames(interfaces));
for (int i = 0; i < classParts.size(); i++) {
// don't use iterator to allow method visits to add more class fields and methods
classParts.get(i).write(this, cw);
}
if (staticInitializer != null) {
staticInitializer.write(this, cw);
}
cw.visitEnd();
bytecode = cw.toByteArray();
if (ClassBuilderFactory.DUMP_GENERATED_CLASSES) {
dumpGeneratedClass(bytecode);
}
}
return bytecode;
}
use of org.mvel2.asm.ClassWriter in project drools by kiegroup.
the class DefaultBeanClassBuilder method buildTraitMap.
/**
* A traitable class is a special class with support for dynamic properties and types.
*
* This method builds the trait map, containing the references to the proxies
* for each trait carried by an object at a given time.
*
* @param cw
* @param classDef
*/
protected void buildTraitMap(ClassWriter cw, ClassDefinition classDef) {
FieldVisitor fv = cw.visitField(ACC_PRIVATE, TraitableBean.TRAITSET_FIELD_NAME, Type.getDescriptor(Map.class), "Ljava/util/Map<Ljava/lang/String;Lorg/drools/core/factmodel/traits/Thing;>;", null);
fv.visitEnd();
MethodVisitor mv;
mv = cw.visitMethod(ACC_PUBLIC, "_getTraitMap", Type.getMethodDescriptor(Type.getType(Map.class), new Type[] {}), "()Ljava/util/Map<Ljava/lang/String;Lorg/drools/factmodel/traits/Thing;>;", null);
mv.visitCode();
mv.visitVarInsn(ALOAD, 0);
mv.visitFieldInsn(GETFIELD, BuildUtils.getInternalType(classDef.getName()), TraitableBean.TRAITSET_FIELD_NAME, Type.getDescriptor(Map.class));
mv.visitInsn(ARETURN);
mv.visitMaxs(0, 0);
mv.visitEnd();
mv = cw.visitMethod(ACC_PUBLIC, "_setTraitMap", Type.getMethodDescriptor(Type.getType(void.class), new Type[] { Type.getType(Map.class) }), null, null);
mv.visitCode();
mv.visitVarInsn(ALOAD, 0);
mv.visitVarInsn(ALOAD, 1);
mv.visitFieldInsn(PUTFIELD, BuildUtils.getInternalType(classDef.getName()), TraitableBean.TRAITSET_FIELD_NAME, Type.getDescriptor(Map.class));
mv.visitInsn(RETURN);
mv.visitMaxs(0, 0);
mv.visitEnd();
mv = cw.visitMethod(ACC_PUBLIC, "addTrait", Type.getMethodDescriptor(Type.getType(void.class), new Type[] { Type.getType(String.class), Type.getType(Thing.class) }), "(Ljava/lang/String;Lorg/drools/core/factmodel/traits/Thing;)V", null);
mv.visitCode();
mv.visitVarInsn(ALOAD, 0);
mv.visitMethodInsn(INVOKEVIRTUAL, BuildUtils.getInternalType(classDef.getName()), "_getTraitMap", Type.getMethodDescriptor(Type.getType(Map.class), new Type[] {}));
mv.visitVarInsn(ALOAD, 1);
mv.visitVarInsn(ALOAD, 2);
mv.visitMethodInsn(INVOKEINTERFACE, Type.getInternalName(Map.class), "put", Type.getMethodDescriptor(Type.getType(Object.class), new Type[] { Type.getType(Object.class), Type.getType(Object.class) }));
mv.visitInsn(POP);
mv.visitInsn(RETURN);
mv.visitMaxs(0, 0);
mv.visitEnd();
mv = cw.visitMethod(ACC_PUBLIC, "getTrait", Type.getMethodDescriptor(Type.getType(Thing.class), new Type[] { Type.getType(String.class) }), Type.getMethodDescriptor(Type.getType(Thing.class), new Type[] { Type.getType(String.class) }), null);
mv.visitCode();
mv.visitVarInsn(ALOAD, 0);
mv.visitMethodInsn(INVOKEVIRTUAL, BuildUtils.getInternalType(classDef.getName()), "_getTraitMap", Type.getMethodDescriptor(Type.getType(Map.class), new Type[] {}));
mv.visitVarInsn(ALOAD, 1);
mv.visitMethodInsn(INVOKEINTERFACE, Type.getInternalName(Map.class), "get", Type.getMethodDescriptor(Type.getType(Object.class), new Type[] { Type.getType(Object.class) }));
mv.visitTypeInsn(CHECKCAST, Type.getInternalName(Thing.class));
mv.visitInsn(ARETURN);
mv.visitMaxs(0, 0);
mv.visitEnd();
mv = cw.visitMethod(ACC_PUBLIC, "hasTrait", Type.getMethodDescriptor(Type.getType(boolean.class), new Type[] { Type.getType(String.class) }), null, null);
mv.visitCode();
mv.visitVarInsn(ALOAD, 0);
mv.visitMethodInsn(INVOKEVIRTUAL, BuildUtils.getInternalType(classDef.getName()), "_getTraitMap", Type.getMethodDescriptor(Type.getType(Map.class), new Type[] {}));
Label l0 = new Label();
mv.visitJumpInsn(IFNONNULL, l0);
mv.visitInsn(ICONST_0);
mv.visitInsn(IRETURN);
mv.visitLabel(l0);
mv.visitVarInsn(ALOAD, 0);
mv.visitMethodInsn(INVOKEVIRTUAL, BuildUtils.getInternalType(classDef.getName()), "_getTraitMap", Type.getMethodDescriptor(Type.getType(Map.class), new Type[] {}));
mv.visitVarInsn(ALOAD, 1);
mv.visitMethodInsn(INVOKEINTERFACE, Type.getInternalName(Map.class), "containsKey", Type.getMethodDescriptor(Type.getType(boolean.class), new Type[] { Type.getType(Object.class) }));
mv.visitInsn(IRETURN);
mv.visitMaxs(0, 0);
mv.visitEnd();
mv = cw.visitMethod(ACC_PUBLIC, "hasTraits", Type.getMethodDescriptor(Type.getType(boolean.class), new Type[] {}), null, null);
mv.visitCode();
mv.visitVarInsn(ALOAD, 0);
mv.visitFieldInsn(GETFIELD, BuildUtils.getInternalType(classDef.getName()), TraitableBean.TRAITSET_FIELD_NAME, Type.getDescriptor(Map.class));
Label l5 = new Label();
mv.visitJumpInsn(IFNULL, l5);
mv.visitVarInsn(ALOAD, 0);
mv.visitFieldInsn(GETFIELD, BuildUtils.getInternalType(classDef.getName()), TraitableBean.TRAITSET_FIELD_NAME, Type.getDescriptor(Map.class));
mv.visitMethodInsn(INVOKEINTERFACE, Type.getInternalName(Map.class), "isEmpty", Type.getMethodDescriptor(Type.BOOLEAN_TYPE, new Type[] {}));
mv.visitJumpInsn(IFNE, l5);
mv.visitInsn(ICONST_1);
Label l4 = new Label();
mv.visitJumpInsn(GOTO, l4);
mv.visitLabel(l5);
mv.visitInsn(ICONST_0);
mv.visitLabel(l4);
mv.visitInsn(IRETURN);
mv.visitMaxs(0, 0);
mv.visitEnd();
mv = cw.visitMethod(ACC_PUBLIC, "removeTrait", Type.getMethodDescriptor(Type.getType(Collection.class), new Type[] { Type.getType(String.class) }), Type.getMethodDescriptor(Type.getType(Collection.class), new Type[] { Type.getType(String.class) }), null);
mv.visitCode();
mv.visitVarInsn(ALOAD, 0);
mv.visitMethodInsn(INVOKEVIRTUAL, BuildUtils.getInternalType(classDef.getName()), "_getTraitMap", Type.getMethodDescriptor(Type.getType(Map.class), new Type[] {}));
mv.visitTypeInsn(CHECKCAST, TraitTypeMapConstants.TYPE_NAME);
mv.visitVarInsn(ALOAD, 1);
mv.visitMethodInsn(INVOKEVIRTUAL, TraitTypeMapConstants.TYPE_NAME, "removeCascade", Type.getMethodDescriptor(Type.getType(Collection.class), new Type[] { Type.getType(String.class) }));
mv.visitInsn(ARETURN);
mv.visitMaxs(0, 0);
mv.visitEnd();
mv = cw.visitMethod(ACC_PUBLIC, "removeTrait", Type.getMethodDescriptor(Type.getType(Collection.class), new Type[] { Type.getType(BitSet.class) }), Type.getMethodDescriptor(Type.getType(Collection.class), new Type[] { Type.getType(BitSet.class) }), null);
mv.visitCode();
mv.visitVarInsn(ALOAD, 0);
mv.visitMethodInsn(INVOKEVIRTUAL, BuildUtils.getInternalType(classDef.getName()), "_getTraitMap", Type.getMethodDescriptor(Type.getType(Map.class), new Type[] {}));
mv.visitTypeInsn(CHECKCAST, TraitTypeMapConstants.TYPE_NAME);
mv.visitVarInsn(ALOAD, 1);
mv.visitMethodInsn(INVOKEVIRTUAL, TraitTypeMapConstants.TYPE_NAME, "removeCascade", Type.getMethodDescriptor(Type.getType(Collection.class), new Type[] { Type.getType(BitSet.class) }));
mv.visitInsn(ARETURN);
mv.visitMaxs(0, 0);
mv.visitEnd();
mv = cw.visitMethod(ACC_PUBLIC, "getTraits", Type.getMethodDescriptor(Type.getType(Collection.class), new Type[] {}), "()Ljava/util/Collection<Ljava/lang/String;>;", null);
mv.visitCode();
mv.visitVarInsn(ALOAD, 0);
mv.visitMethodInsn(INVOKEVIRTUAL, BuildUtils.getInternalType(classDef.getName()), "_getTraitMap", Type.getMethodDescriptor(Type.getType(Map.class), new Type[] {}));
mv.visitMethodInsn(INVOKEINTERFACE, Type.getInternalName(Map.class), "keySet", Type.getMethodDescriptor(Type.getType(Set.class), new Type[] {}));
mv.visitInsn(ARETURN);
mv.visitMaxs(0, 0);
mv.visitEnd();
mv = cw.visitMethod(ACC_PUBLIC, "_setBottomTypeCode", Type.getMethodDescriptor(Type.getType(void.class), new Type[] { Type.getType(BitSet.class) }), null, null);
mv.visitCode();
mv.visitVarInsn(ALOAD, 0);
mv.visitFieldInsn(GETFIELD, BuildUtils.getInternalType(classDef.getName()), TraitableBean.TRAITSET_FIELD_NAME, Type.getDescriptor(Map.class));
mv.visitTypeInsn(CHECKCAST, TraitTypeMapConstants.TYPE_NAME);
mv.visitVarInsn(ALOAD, 1);
mv.visitMethodInsn(INVOKEVIRTUAL, TraitTypeMapConstants.TYPE_NAME, "setBottomCode", Type.getMethodDescriptor(Type.getType(void.class), new Type[] { Type.getType(BitSet.class) }));
mv.visitInsn(RETURN);
mv.visitMaxs(0, 0);
mv.visitEnd();
mv = cw.visitMethod(ACC_PUBLIC, "getMostSpecificTraits", Type.getMethodDescriptor(Type.getType(Collection.class), new Type[] {}), "()Ljava/util/Collection<Lorg/drools/core/factmodel/traits/Thing;>;", null);
mv.visitCode();
mv.visitVarInsn(ALOAD, 0);
mv.visitFieldInsn(GETFIELD, BuildUtils.getInternalType(classDef.getName()), TraitableBean.TRAITSET_FIELD_NAME, Type.getDescriptor(Map.class));
Label l99 = new Label();
mv.visitJumpInsn(IFNULL, l99);
mv.visitVarInsn(ALOAD, 0);
mv.visitFieldInsn(GETFIELD, BuildUtils.getInternalType(classDef.getName()), TraitableBean.TRAITSET_FIELD_NAME, Type.getDescriptor(Map.class));
mv.visitTypeInsn(CHECKCAST, TraitTypeMapConstants.TYPE_NAME);
mv.visitMethodInsn(INVOKEVIRTUAL, TraitTypeMapConstants.TYPE_NAME, "getMostSpecificTraits", Type.getMethodDescriptor(Type.getType(Collection.class), new Type[] {}));
mv.visitInsn(ARETURN);
mv.visitLabel(l99);
mv.visitMethodInsn(INVOKESTATIC, Type.getInternalName(Collections.class), "emptySet", Type.getMethodDescriptor(Type.getType(Set.class), new Type[] {}));
mv.visitMethodInsn(INVOKESTATIC, Type.getInternalName(Collections.class), "unmodifiableCollection", Type.getMethodDescriptor(Type.getType(Collection.class), new Type[] { Type.getType(Collection.class) }));
mv.visitInsn(ARETURN);
mv.visitMaxs(0, 0);
mv.visitEnd();
mv = cw.visitMethod(ACC_PUBLIC, "getCurrentTypeCode", Type.getMethodDescriptor(Type.getType(BitSet.class), new Type[] {}), null, null);
mv.visitCode();
mv.visitVarInsn(ALOAD, 0);
mv.visitFieldInsn(GETFIELD, BuildUtils.getInternalType(classDef.getName()), TraitableBean.TRAITSET_FIELD_NAME, Type.getDescriptor(Map.class));
Label l3 = new Label();
mv.visitJumpInsn(IFNONNULL, l3);
mv.visitInsn(ACONST_NULL);
mv.visitInsn(ARETURN);
mv.visitLabel(l3);
mv.visitVarInsn(ALOAD, 0);
mv.visitFieldInsn(GETFIELD, BuildUtils.getInternalType(classDef.getName()), TraitableBean.TRAITSET_FIELD_NAME, Type.getDescriptor(Map.class));
mv.visitTypeInsn(CHECKCAST, TraitTypeMapConstants.TYPE_NAME);
mv.visitMethodInsn(INVOKEVIRTUAL, TraitTypeMapConstants.TYPE_NAME, "getCurrentTypeCode", Type.getMethodDescriptor(Type.getType(BitSet.class), new Type[] {}));
mv.visitInsn(ARETURN);
mv.visitMaxs(0, 0);
mv.visitEnd();
}
use of org.mvel2.asm.ClassWriter in project drools by kiegroup.
the class DefaultBeanClassBuilder method buildDynamicPropertyMap.
/**
* A traitable class is a special class with support for dynamic properties and types.
*
* This method builds the property map, containing the key/values pairs to implement
* any property defined in a trait interface but not supported by the traited class
* fields.
*
* @param cw
* @param def
*/
protected void buildDynamicPropertyMap(ClassWriter cw, ClassDefinition def) {
FieldVisitor fv = cw.visitField(Opcodes.ACC_PRIVATE, TraitableBean.MAP_FIELD_NAME, Type.getDescriptor(Map.class), "Ljava/util/Map<Ljava/lang/String;Ljava/lang/Object;>;", null);
fv.visitEnd();
MethodVisitor mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "_getDynamicProperties", Type.getMethodDescriptor(Type.getType(Map.class), new Type[] {}), "()Ljava/util/Map<Ljava/lang/String;Ljava/lang/Object;>;", null);
mv.visitCode();
mv.visitVarInsn(ALOAD, 0);
mv.visitFieldInsn(GETFIELD, BuildUtils.getInternalType(def.getName()), TraitableBean.MAP_FIELD_NAME, Type.getDescriptor(Map.class));
mv.visitInsn(ARETURN);
mv.visitMaxs(0, 0);
mv.visitEnd();
mv = cw.visitMethod(ACC_PUBLIC, "_setDynamicProperties", Type.getMethodDescriptor(Type.getType(void.class), new Type[] { Type.getType(Map.class) }), "(Ljava/util/Map<Ljava/lang/String;Ljava/lang/Object;>;)V", null);
mv.visitCode();
mv.visitVarInsn(ALOAD, 0);
mv.visitVarInsn(ALOAD, 1);
mv.visitFieldInsn(PUTFIELD, BuildUtils.getInternalType(def.getName()), TraitableBean.MAP_FIELD_NAME, Type.getDescriptor(Map.class));
mv.visitInsn(RETURN);
mv.visitMaxs(0, 0);
mv.visitEnd();
}
use of org.mvel2.asm.ClassWriter in project drools by kiegroup.
the class DefaultEnumClassBuilder method buildConstructors.
protected void buildConstructors(ClassWriter cw, EnumClassDefinition classDef) {
MethodVisitor mv;
final StringBuilder argTypesBuilder = new StringBuilder();
int size = 0;
for (FieldDefinition fld : classDef.getFieldsDefinitions()) {
argTypesBuilder.append(BuildUtils.getTypeDescriptor(fld.getTypeName()));
size += BuildUtils.sizeOf(fld.getTypeName());
}
{
int ofs = 3;
mv = cw.visitMethod(ACC_PRIVATE, "<init>", "(Ljava/lang/String;I" + argTypesBuilder.toString() + ")V", "(" + argTypesBuilder.toString() + ")V", null);
mv.visitCode();
mv.visitVarInsn(ALOAD, 0);
mv.visitVarInsn(ALOAD, 1);
mv.visitVarInsn(ILOAD, 2);
mv.visitMethodInsn(INVOKESPECIAL, "java/lang/Enum", "<init>", "(Ljava/lang/String;I)V");
for (FieldDefinition fld : classDef.getFieldsDefinitions()) {
mv.visitVarInsn(ALOAD, 0);
mv.visitVarInsn(AsmUtil.varType(fld.getTypeName()), ofs);
mv.visitFieldInsn(PUTFIELD, BuildUtils.getInternalType(classDef.getName()), fld.getName(), BuildUtils.getTypeDescriptor(fld.getTypeName()));
ofs += BuildUtils.sizeOf(fld.getTypeName());
}
mv.visitInsn(RETURN);
mv.visitMaxs(3, ofs);
mv.visitEnd();
}
{
mv = cw.visitMethod(ACC_STATIC, "<clinit>", "()V", null, null);
mv.visitCode();
int N = classDef.getEnumLiterals().size();
mv.visitTypeInsn(NEW, BuildUtils.getInternalType(classDef.getClassName()));
for (int j = 0; j < N; j++) {
EnumLiteralDefinition lit = classDef.getEnumLiterals().get(j);
mv.visitInsn(DUP);
mv.visitLdcInsn(lit.getName());
AsmUtil.pushInt(mv, j);
List<String> args = lit.getConstructorArgs();
for (int k = 0; k < args.size(); k++) {
String argType = classDef.getField(k).getTypeName();
mv.visitLdcInsn(args.get(k));
mv.visitMethodInsn(INVOKESTATIC, "org/mvel2/MVEL", "eval", "(Ljava/lang/String;)Ljava/lang/Object;");
if (BuildUtils.isPrimitive(argType)) {
mv.visitTypeInsn(CHECKCAST, BuildUtils.getInternalType(BuildUtils.box(argType)));
mv.visitMethodInsn(INVOKEVIRTUAL, BuildUtils.getInternalType(BuildUtils.box(argType)), BuildUtils.numericMorph(BuildUtils.box(argType)), "()" + BuildUtils.getTypeDescriptor(argType));
} else {
mv.visitTypeInsn(CHECKCAST, BuildUtils.getInternalType(argType));
}
}
mv.visitMethodInsn(INVOKESPECIAL, BuildUtils.getInternalType(classDef.getClassName()), "<init>", "(Ljava/lang/String;I" + argTypesBuilder.toString() + ")V");
mv.visitFieldInsn(PUTSTATIC, BuildUtils.getInternalType(classDef.getClassName()), lit.getName(), BuildUtils.getTypeDescriptor(classDef.getClassName()));
mv.visitTypeInsn(NEW, BuildUtils.getInternalType(classDef.getClassName()));
}
AsmUtil.pushInt(mv, N);
mv.visitTypeInsn(ANEWARRAY, BuildUtils.getInternalType(classDef.getClassName()));
for (int j = 0; j < N; j++) {
EnumLiteralDefinition lit = classDef.getEnumLiterals().get(j);
mv.visitInsn(DUP);
AsmUtil.pushInt(mv, j);
mv.visitFieldInsn(GETSTATIC, BuildUtils.getInternalType(classDef.getClassName()), lit.getName(), BuildUtils.getTypeDescriptor(classDef.getClassName()));
mv.visitInsn(AASTORE);
}
mv.visitFieldInsn(PUTSTATIC, BuildUtils.getInternalType(classDef.getClassName()), "$VALUES", "[" + BuildUtils.getTypeDescriptor(classDef.getClassName()));
mv.visitInsn(RETURN);
mv.visitMaxs(4 + size, 0);
mv.visitEnd();
}
{
mv = cw.visitMethod(ACC_PUBLIC + ACC_STATIC, "valueOf", "(Ljava/lang/String;)" + BuildUtils.getTypeDescriptor(classDef.getClassName()), null, null);
mv.visitCode();
mv.visitLdcInsn(Type.getType(BuildUtils.getTypeDescriptor(classDef.getClassName())));
mv.visitVarInsn(ALOAD, 0);
mv.visitMethodInsn(INVOKESTATIC, "java/lang/Enum", "valueOf", "(Ljava/lang/Class;Ljava/lang/String;)Ljava/lang/Enum;");
mv.visitTypeInsn(CHECKCAST, BuildUtils.getInternalType(classDef.getClassName()));
mv.visitInsn(ARETURN);
mv.visitMaxs(2, 1);
mv.visitEnd();
}
}
Aggregations