use of org.mvel2.asm.ClassWriter in project drools by kiegroup.
the class TraitMapProxyClassBuilderImpl method buildClass.
public byte[] buildClass(ClassDefinition core, ClassLoader classLoader) throws IOException, SecurityException, IllegalArgumentException, ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException, NoSuchFieldException {
FieldVisitor fv;
MethodVisitor mv;
// get the method bitmask
BitSet mask = traitRegistryImpl.getFieldMask(getTrait().getName(), core.getDefinedClass().getName());
String name = TraitFactoryImpl.getPropertyWrapperName(getTrait(), core);
String proxyName = TraitFactoryImpl.getProxyName(getTrait(), core);
Class<?> traitClass = getTrait().getDefinedClass();
String internalWrapper = BuildUtils.getInternalType(name);
String internalProxy = BuildUtils.getInternalType(proxyName);
String descrCore = Type.getDescriptor(core.getDefinedClass());
String internalCore = Type.getInternalName(core.getDefinedClass());
String internalTrait = Type.getInternalName(traitClass);
MixinInfo mixinInfo = findMixinInfo(traitClass);
ClassWriter cw = createClassWriter(classLoader, ACC_PUBLIC + ACC_SUPER, internalProxy, null, Type.getInternalName(proxyBaseClass), new String[] { internalTrait, Type.getInternalName(Serializable.class) });
{
fv = cw.visitField(ACC_PRIVATE + ACC_FINAL + ACC_STATIC, TraitType.traitNameField, Type.getDescriptor(String.class), null, null);
fv.visitEnd();
}
{
fv = cw.visitField(ACC_PUBLIC, "object", descrCore, null, null);
fv.visitEnd();
}
{
fv = cw.visitField(ACC_PUBLIC, "map", Type.getDescriptor(Map.class), "Ljava/util/Map<Ljava/lang/String;Ljava/lang/Object;>;", null);
fv.visitEnd();
}
if (mixinInfo != null) {
for (Class<?> mixinClass : mixinInfo.mixinClasses) {
{
fv = cw.visitField(ACC_PRIVATE, getMixinName(mixinClass), BuildUtils.getTypeDescriptor(mixinClass.getName()), null, null);
fv.visitEnd();
}
}
}
{
mv = cw.visitMethod(ACC_PUBLIC, "<init>", "(" + descrCore + Type.getDescriptor(Map.class) + Type.getDescriptor(BitSet.class) + Type.getDescriptor(BitSet.class) + Type.getDescriptor(boolean.class) + ")V", "(" + descrCore + "Ljava/util/Map<Ljava/lang/String;Ljava/lang/Object;>;" + Type.getDescriptor(BitSet.class) + Type.getDescriptor(BitSet.class) + Type.getDescriptor(boolean.class) + ")V", null);
mv.visitCode();
mv.visitVarInsn(ALOAD, 0);
mv.visitMethodInsn(INVOKESPECIAL, Type.getInternalName(proxyBaseClass), "<init>", "()V", false);
mv.visitVarInsn(ALOAD, 2);
Label l0 = new Label();
mv.visitJumpInsn(IFNONNULL, l0);
mv.visitTypeInsn(NEW, Type.getInternalName(ExternalizableLinkedHashMap.class));
mv.visitInsn(DUP);
mv.visitMethodInsn(INVOKESPECIAL, Type.getInternalName(ExternalizableLinkedHashMap.class), "<init>", "()V", false);
mv.visitVarInsn(ASTORE, 2);
mv.visitLabel(l0);
if (mixinInfo != null) {
for (Class<?> mixinClass : mixinInfo.mixinClasses) {
String mixin = getMixinName(mixinClass);
try {
Class actualArg = getPossibleConstructor(mixinClass, trait.getDefinedClass());
mv.visitVarInsn(ALOAD, 0);
mv.visitTypeInsn(NEW, Type.getInternalName(mixinClass));
mv.visitInsn(DUP);
mv.visitVarInsn(ALOAD, 0);
mv.visitMethodInsn(INVOKESPECIAL, Type.getInternalName(mixinClass), "<init>", "(" + Type.getDescriptor(actualArg) + ")V", false);
mv.visitFieldInsn(PUTFIELD, internalProxy, mixin, Type.getDescriptor(mixinClass));
} catch (NoSuchMethodException nsme) {
mv.visitVarInsn(ALOAD, 0);
mv.visitTypeInsn(NEW, Type.getInternalName(mixinClass));
mv.visitInsn(DUP);
mv.visitMethodInsn(INVOKESPECIAL, Type.getInternalName(mixinClass), "<init>", "()V", false);
mv.visitFieldInsn(PUTFIELD, internalProxy, mixin, Type.getDescriptor(mixinClass));
}
}
}
mv.visitVarInsn(ALOAD, 0);
mv.visitVarInsn(ALOAD, 1);
mv.visitFieldInsn(PUTFIELD, internalProxy, "object", descrCore);
mv.visitVarInsn(ALOAD, 0);
mv.visitVarInsn(ALOAD, 2);
mv.visitFieldInsn(PUTFIELD, internalProxy, "map", Type.getDescriptor(Map.class));
mv.visitVarInsn(ALOAD, 0);
mv.visitVarInsn(ALOAD, 3);
mv.visitMethodInsn(INVOKEVIRTUAL, internalProxy, "setTypeCode", Type.getMethodDescriptor(Type.VOID_TYPE, Type.getType(BitSet.class)), false);
mv.visitVarInsn(ALOAD, 0);
mv.visitTypeInsn(NEW, internalWrapper);
mv.visitInsn(DUP);
mv.visitVarInsn(ALOAD, 1);
mv.visitVarInsn(ALOAD, 2);
mv.visitMethodInsn(INVOKESPECIAL, internalWrapper, "<init>", "(" + descrCore + Type.getDescriptor(Map.class) + ")V", false);
mv.visitFieldInsn(PUTFIELD, internalProxy, "fields", Type.getDescriptor(Map.class));
mv.visitVarInsn(ALOAD, 1);
mv.visitMethodInsn(INVOKEVIRTUAL, internalCore, "_getDynamicProperties", "()" + Type.getDescriptor(Map.class), false);
Label l1 = new Label();
mv.visitJumpInsn(IFNONNULL, l1);
mv.visitVarInsn(ALOAD, 1);
mv.visitVarInsn(ALOAD, 2);
mv.visitMethodInsn(INVOKEVIRTUAL, internalCore, "_setDynamicProperties", "(" + Type.getDescriptor(Map.class) + ")V", false);
mv.visitLabel(l1);
mv.visitVarInsn(ALOAD, 1);
mv.visitMethodInsn(INVOKEVIRTUAL, internalCore, "_getTraitMap", "()" + Type.getDescriptor(Map.class), false);
Label l2 = new Label();
mv.visitJumpInsn(IFNONNULL, l2);
mv.visitVarInsn(ALOAD, 1);
mv.visitTypeInsn(NEW, Type.getInternalName(TraitTypeMapImpl.class));
mv.visitInsn(DUP);
mv.visitTypeInsn(NEW, Type.getInternalName(ExternalizableLinkedHashMap.class));
mv.visitInsn(DUP);
mv.visitMethodInsn(INVOKESPECIAL, Type.getInternalName(ExternalizableLinkedHashMap.class), "<init>", "()V", false);
mv.visitMethodInsn(INVOKESPECIAL, Type.getInternalName(TraitTypeMapImpl.class), "<init>", "(" + Type.getDescriptor(Map.class) + ")V", false);
mv.visitMethodInsn(INVOKEVIRTUAL, internalCore, "_setTraitMap", "(" + Type.getDescriptor(Map.class) + ")V", false);
mv.visitLabel(l2);
// core._setBottomTypeCode()
mv.visitVarInsn(ALOAD, 1);
mv.visitVarInsn(ALOAD, 4);
mv.visitMethodInsn(INVOKEVIRTUAL, internalCore, "_setBottomTypeCode", Type.getMethodDescriptor(Type.VOID_TYPE, Type.getType(BitSet.class)), false);
// core.addTrait
mv.visitVarInsn(ALOAD, 1);
mv.visitLdcInsn(trait.getName().endsWith(TraitFactoryImpl.SUFFIX) ? trait.getName().replace(TraitFactoryImpl.SUFFIX, "") : trait.getName());
mv.visitVarInsn(ALOAD, 0);
mv.visitMethodInsn(INVOKEVIRTUAL, internalCore, "addTrait", Type.getMethodDescriptor(Type.VOID_TYPE, Type.getType(String.class), Type.getType(Thing.class)), false);
mv.visitVarInsn(ALOAD, 0);
mv.visitVarInsn(ILOAD, 5);
mv.visitMethodInsn(INVOKESPECIAL, internalProxy, "synchFields", Type.getMethodDescriptor(Type.VOID_TYPE, Type.BOOLEAN_TYPE), false);
mv.visitInsn(RETURN);
// mv.visitMaxs( 5, 3 );
mv.visitMaxs(0, 0);
mv.visitEnd();
}
{
mv = cw.visitMethod(ACC_PUBLIC, "writeExternal", "(" + Type.getDescriptor(ObjectOutput.class) + ")V", null, new String[] { Type.getInternalName(IOException.class) });
mv.visitCode();
mv.visitVarInsn(ALOAD, 1);
mv.visitVarInsn(ALOAD, 0);
mv.visitMethodInsn(INVOKEVIRTUAL, internalProxy, "getObject", "()" + Type.getDescriptor(TraitableBean.class), false);
mv.visitMethodInsn(INVOKEINTERFACE, Type.getInternalName(ObjectOutput.class), "writeObject", "(" + Type.getDescriptor(Object.class) + ")V", true);
mv.visitVarInsn(ALOAD, 1);
mv.visitVarInsn(ALOAD, 0);
mv.visitFieldInsn(GETFIELD, internalProxy, "map", Type.getDescriptor(Map.class));
mv.visitMethodInsn(INVOKEINTERFACE, Type.getInternalName(ObjectOutput.class), "writeObject", "(" + Type.getDescriptor(Object.class) + ")V", true);
mv.visitVarInsn(ALOAD, 0);
mv.visitVarInsn(ALOAD, 1);
mv.visitMethodInsn(INVOKESPECIAL, Type.getInternalName(proxyBaseClass), "writeExternal", "(" + Type.getDescriptor(ObjectOutput.class) + ")V", false);
mv.visitInsn(RETURN);
mv.visitMaxs(0, 0);
mv.visitEnd();
}
{
mv = cw.visitMethod(ACC_PUBLIC, "readExternal", "(" + Type.getDescriptor(ObjectInput.class) + ")V", null, new String[] { Type.getInternalName(IOException.class), Type.getInternalName(ClassNotFoundException.class) });
mv.visitCode();
mv.visitVarInsn(ALOAD, 0);
mv.visitVarInsn(ALOAD, 1);
mv.visitMethodInsn(INVOKEINTERFACE, Type.getInternalName(ObjectInput.class), "readObject", "()" + Type.getDescriptor(Object.class), true);
mv.visitTypeInsn(CHECKCAST, internalCore);
mv.visitFieldInsn(PUTFIELD, internalProxy, "object", descrCore);
mv.visitVarInsn(ALOAD, 0);
mv.visitVarInsn(ALOAD, 1);
mv.visitMethodInsn(INVOKEINTERFACE, Type.getInternalName(ObjectInput.class), "readObject", "()" + Type.getDescriptor(Object.class), true);
mv.visitTypeInsn(CHECKCAST, Type.getInternalName(Map.class));
mv.visitFieldInsn(PUTFIELD, internalProxy, "map", Type.getDescriptor(Map.class));
mv.visitVarInsn(ALOAD, 0);
mv.visitVarInsn(ALOAD, 1);
mv.visitMethodInsn(INVOKESPECIAL, Type.getInternalName(proxyBaseClass), "readExternal", "(" + Type.getDescriptor(ObjectInput.class) + ")V", false);
mv.visitInsn(RETURN);
// mv.visitMaxs( 3, 2 );
mv.visitMaxs(0, 0);
mv.visitEnd();
}
helpBuildClass(core, cw, internalProxy, descrCore, mask);
buildFields(core, mask, proxyName, mixinInfo, cw);
buildKeys(core, proxyName, cw);
buildMixinMethods(proxyName, mixinInfo, cw);
buildCommonMethods(cw, proxyName);
buildExtendedMethods(cw, trait, core);
buildShadowMethods(cw, trait, core);
cw.visitEnd();
return cw.toByteArray();
}
use of org.mvel2.asm.ClassWriter in project drools by kiegroup.
the class TraitClassBuilderImpl method buildGetter.
protected void buildGetter(ClassWriter cw, FieldDefinition field, String name, String type, String generic) {
name = name.substring(0, 1).toUpperCase() + name.substring(1);
MethodVisitor mv = cw.visitMethod(ACC_PUBLIC + ACC_ABSTRACT, BuildUtils.getterName(name, type), "()" + BuildUtils.getTypeDescriptor(type), generic == null ? null : "()" + BuildUtils.getTypeDescriptor(type).replace(";", "<" + BuildUtils.getTypeDescriptor(generic) + ">;"), null);
mv.visitEnd();
}
use of org.mvel2.asm.ClassWriter in project drools by kiegroup.
the class TraitClassBuilderImpl method buildSetter.
protected void buildSetter(ClassWriter cw, FieldDefinition field, String name, String type, String generic) {
MethodVisitor mv = cw.visitMethod(ACC_PUBLIC + ACC_ABSTRACT, BuildUtils.setterName(name), "(" + BuildUtils.getTypeDescriptor(type) + ")V", generic == null ? null : "(" + BuildUtils.getTypeDescriptor(type).replace(";", "<" + BuildUtils.getTypeDescriptor(generic) + ">;") + ")V", null);
mv.visitEnd();
}
use of org.mvel2.asm.ClassWriter in project drools by kiegroup.
the class ClassFieldAccessorFactory method build3ArgConstructor.
/**
* Creates a constructor for the field extractor receiving
* the index, field type and value type
*/
private static void build3ArgConstructor(final Class<?> superClazz, final String className, final ClassWriter cw) {
MethodVisitor mv;
mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "<init>", Type.getMethodDescriptor(Type.VOID_TYPE, Type.getType(int.class), Type.getType(Class.class), Type.getType(ValueType.class)), null, null);
mv.visitCode();
final Label l0 = new Label();
mv.visitLabel(l0);
mv.visitVarInsn(Opcodes.ALOAD, 0);
mv.visitVarInsn(Opcodes.ILOAD, 1);
mv.visitVarInsn(Opcodes.ALOAD, 2);
mv.visitVarInsn(Opcodes.ALOAD, 3);
mv.visitMethodInsn(Opcodes.INVOKESPECIAL, Type.getInternalName(superClazz), "<init>", Type.getMethodDescriptor(Type.VOID_TYPE, Type.getType(int.class), Type.getType(Class.class), Type.getType(ValueType.class)));
final Label l1 = new Label();
mv.visitLabel(l1);
mv.visitInsn(Opcodes.RETURN);
final Label l2 = new Label();
mv.visitLabel(l2);
mv.visitLocalVariable("this", "L" + className + ";", null, l0, l2, 0);
mv.visitLocalVariable("index", Type.getDescriptor(int.class), null, l0, l2, 1);
mv.visitLocalVariable("fieldType", Type.getDescriptor(Class.class), null, l0, l2, 2);
mv.visitLocalVariable("valueType", Type.getDescriptor(ValueType.class), null, l0, l2, 3);
mv.visitMaxs(0, 0);
mv.visitEnd();
}
use of org.mvel2.asm.ClassWriter in project drools by kiegroup.
the class ClassFieldAccessorFactory method buildGetMethod.
/**
* Creates the proxy reader method for the given method
*/
protected static void buildGetMethod(final Class<?> originalClass, final String className, final Class<?> superClass, final Method getterMethod, final ClassWriter cw) {
final Class<?> fieldType = getterMethod.getReturnType();
Method overridingMethod;
try {
overridingMethod = superClass.getMethod(getOverridingGetMethodName(fieldType), ReteEvaluator.class, Object.class);
} catch (final Exception e) {
throw new RuntimeException("This is a bug. Please report back to JBoss Rules team.", e);
}
final MethodVisitor mv = cw.visitMethod(Opcodes.ACC_PUBLIC, overridingMethod.getName(), Type.getMethodDescriptor(overridingMethod), null, null);
mv.visitCode();
final Label l0 = new Label();
mv.visitLabel(l0);
mv.visitVarInsn(Opcodes.ALOAD, 2);
mv.visitTypeInsn(Opcodes.CHECKCAST, Type.getInternalName(originalClass));
if (originalClass.isInterface()) {
mv.visitMethodInsn(Opcodes.INVOKEINTERFACE, Type.getInternalName(originalClass), getterMethod.getName(), Type.getMethodDescriptor(getterMethod), true);
} else {
mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, Type.getInternalName(originalClass), getterMethod.getName(), Type.getMethodDescriptor(getterMethod), false);
}
mv.visitInsn(Type.getType(fieldType).getOpcode(Opcodes.IRETURN));
final Label l1 = new Label();
mv.visitLabel(l1);
mv.visitLocalVariable("this", "L" + className + ";", null, l0, l1, 0);
mv.visitLocalVariable("workingMemory", Type.getDescriptor(InternalWorkingMemory.class), null, l0, l1, 1);
mv.visitLocalVariable("object", Type.getDescriptor(Object.class), null, l0, l1, 2);
mv.visitMaxs(0, 0);
mv.visitEnd();
}
Aggregations