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 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 = traitRegistry.getFieldMask(getTrait().getName(), core.getDefinedClass().getName());
String name = TraitFactory.getPropertyWrapperName(getTrait(), core);
String masterName = TraitFactory.getProxyName(getTrait(), core);
Class<?> traitClass = getTrait().getDefinedClass();
String internalWrapper = BuildUtils.getInternalType(name);
String internalProxy = BuildUtils.getInternalType(masterName);
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(TraitTypeMap.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(TraitTypeMap.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(TraitFactory.SUFFIX) ? trait.getName().replace(TraitFactory.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, masterName, mixinInfo, cw);
buildKeys(core, masterName, cw);
buildMixinMethods(masterName, mixinInfo, cw);
buildCommonMethods(cw, masterName);
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 TraitTriplePropertyWrapperClassBuilderImpl method buildInitSoftFields.
protected void buildInitSoftFields(ClassWriter cw, String wrapperName, ClassDefinition trait, ClassDefinition core, BitSet mask) {
MethodVisitor mv = cw.visitMethod(ACC_PRIVATE, "initSoftFields", "()V", null, null);
mv.visitCode();
initSoftFields(mv, wrapperName, trait, core, mask);
mv.visitInsn(RETURN);
mv.visitMaxs(0, 0);
mv.visitEnd();
}
use of org.mvel2.asm.ClassWriter in project drools by kiegroup.
the class TraitTriplePropertyWrapperClassBuilderImpl method buildClear.
protected void buildClear(ClassWriter cw, String wrapperName, ClassDefinition trait, ClassDefinition core) {
String internalWrapper = BuildUtils.getInternalType(wrapperName);
MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, "clear", "()V", null, null);
mv.visitCode();
for (FieldDefinition field : core.getFieldsDefinitions()) {
if (field.isKey())
continue;
TraitFactory.invokeInjector(mv, wrapperName, core, field, true, 1);
}
mv.visitVarInsn(ALOAD, 0);
mv.visitMethodInsn(INVOKESPECIAL, Type.getInternalName(TripleBasedStruct.class), "clear", "()V", false);
mv.visitVarInsn(ALOAD, 0);
mv.visitMethodInsn(INVOKESPECIAL, internalWrapper, "clearSoftFields", "()V", false);
mv.visitInsn(RETURN);
mv.visitMaxs(0, 0);
mv.visitEnd();
}
use of org.mvel2.asm.ClassWriter in project drools by kiegroup.
the class TraitTriplePropertyWrapperClassBuilderImpl method buildRemove.
protected void buildRemove(ClassWriter cw, String wrapperName, ClassDefinition trait, ClassDefinition core, BitSet mask) {
String internalWrapper = BuildUtils.getInternalType(wrapperName);
MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, "remove", "(" + Type.getDescriptor(Object.class) + ")" + Type.getDescriptor(Object.class), null, null);
mv.visitCode();
int stack = 0;
for (FieldDefinition field : core.getFieldsDefinitions()) {
stack = Math.max(stack, BuildUtils.sizeOf(field.getTypeName()));
invokeRemove(mv, wrapperName, core, field.getName(), field);
}
int j = 0;
for (FieldDefinition field : trait.getFieldsDefinitions()) {
boolean isSoftField = TraitRegistry.isSoftField(field, j++, mask);
if (isSoftField) {
stack = Math.max(stack, BuildUtils.sizeOf(field.getTypeName()));
mv.visitLdcInsn(field.getName());
mv.visitVarInsn(ALOAD, 1);
mv.visitMethodInsn(INVOKEVIRTUAL, Type.getInternalName(String.class), "equals", "(" + Type.getDescriptor(Object.class) + ")Z", false);
Label l2 = new Label();
mv.visitJumpInsn(IFEQ, l2);
mv.visitVarInsn(ALOAD, 0);
mv.visitFieldInsn(GETFIELD, internalWrapper, "store", Type.getDescriptor(TripleStore.class));
mv.visitVarInsn(ALOAD, 0);
mv.visitLdcInsn(field.getName());
mv.visitMethodInsn(INVOKEVIRTUAL, internalWrapper, "propertyKey", "(" + Type.getDescriptor(Object.class) + ")" + Type.getDescriptor(Triple.class), false);
mv.visitMethodInsn(INVOKEVIRTUAL, Type.getInternalName(TripleStore.class), "get", "(" + Type.getDescriptor(Triple.class) + ")" + Type.getDescriptor(Triple.class), false);
mv.visitVarInsn(ASTORE, 2);
mv.visitVarInsn(ALOAD, 0);
mv.visitLdcInsn(field.getName());
mv.visitInsn(BuildUtils.zero(field.getTypeName()));
if (BuildUtils.isPrimitive(field.getTypeName())) {
TraitFactory.valueOf(mv, field.getTypeName());
}
mv.visitMethodInsn(INVOKESPECIAL, Type.getInternalName(TripleBasedStruct.class), "put", "(" + Type.getDescriptor(String.class) + Type.getDescriptor(Object.class) + ")" + Type.getDescriptor(Object.class), false);
mv.visitInsn(POP);
mv.visitVarInsn(ALOAD, 2);
mv.visitInsn(ARETURN);
mv.visitLabel(l2);
}
}
mv.visitVarInsn(ALOAD, 0);
mv.visitVarInsn(ALOAD, 1);
mv.visitMethodInsn(INVOKESPECIAL, Type.getInternalName(TripleBasedStruct.class), "remove", "(" + Type.getDescriptor(Object.class) + ")" + Type.getDescriptor(Object.class), false);
mv.visitVarInsn(ASTORE, 2);
mv.visitVarInsn(ALOAD, 2);
mv.visitInsn(ARETURN);
mv.visitMaxs(0, 0);
mv.visitEnd();
}
Aggregations