use of org.mvel2.asm.ClassWriter in project drools by kiegroup.
the class TraitMapPropertyWrapperClassBuilderImpl method buildContainsValue.
protected void buildContainsValue(ClassWriter cw, String wrapperName, ClassDefinition trait, ClassDefinition core) {
String internalWrapper = BuildUtils.getInternalType(wrapperName);
MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, "containsValue", "(" + Type.getDescriptor(Object.class) + ")Z", null, null);
mv.visitCode();
// null check
mv.visitVarInsn(ALOAD, 1);
Label l99 = new Label();
mv.visitJumpInsn(IFNONNULL, l99);
for (FieldDefinition field : core.getFieldsDefinitions()) {
if (!BuildUtils.isPrimitive(field.getTypeName())) {
extractAndTestNotNull(mv, wrapperName, core, field);
}
}
mv.visitVarInsn(ALOAD, 0);
mv.visitFieldInsn(GETFIELD, internalWrapper, "map", Type.getDescriptor(Map.class));
mv.visitInsn(ACONST_NULL);
mv.visitMethodInsn(INVOKEINTERFACE, Type.getInternalName(Map.class), "containsValue", "(" + Type.getDescriptor(Object.class) + ")Z", true);
mv.visitInsn(IRETURN);
mv.visitLabel(l99);
// non-null values check
for (FieldDefinition field : core.getFieldsDefinitions()) {
mv.visitVarInsn(ALOAD, 1);
TraitFactoryImpl.invokeExtractor(mv, wrapperName, core, field);
if (BuildUtils.isPrimitive(field.getTypeName())) {
TraitFactoryImpl.valueOf(mv, field.getTypeName());
}
mv.visitMethodInsn(INVOKEVIRTUAL, Type.getInternalName(Object.class), "equals", "(" + Type.getDescriptor(Object.class) + ")Z", false);
Label l0 = new Label();
mv.visitJumpInsn(IFEQ, l0);
mv.visitInsn(ICONST_1);
mv.visitInsn(IRETURN);
mv.visitLabel(l0);
}
mv.visitVarInsn(ALOAD, 0);
mv.visitFieldInsn(GETFIELD, internalWrapper, "map", Type.getDescriptor(Map.class));
mv.visitVarInsn(ALOAD, 1);
mv.visitMethodInsn(INVOKEINTERFACE, Type.getInternalName(Map.class), "containsValue", "(" + Type.getDescriptor(Object.class) + ")Z", true);
mv.visitInsn(IRETURN);
mv.visitMaxs(0, 0);
mv.visitEnd();
}
use of org.mvel2.asm.ClassWriter in project drools by kiegroup.
the class TraitMapPropertyWrapperClassBuilderImpl method buildContainsKey.
protected void buildContainsKey(ClassWriter cw, String name, ClassDefinition core) {
String internalWrapper = BuildUtils.getInternalType(name);
MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, "containsKey", "(" + Type.getDescriptor(Object.class) + ")Z", null, null);
mv.visitCode();
for (FieldDefinition field : core.getFieldsDefinitions()) {
invokeContainsKey(mv, field.getName());
}
mv.visitVarInsn(ALOAD, 0);
mv.visitFieldInsn(GETFIELD, internalWrapper, "map", Type.getDescriptor(Map.class));
mv.visitVarInsn(ALOAD, 1);
mv.visitMethodInsn(INVOKEINTERFACE, Type.getInternalName(Map.class), "containsKey", "(" + Type.getDescriptor(Object.class) + ")Z", true);
mv.visitInsn(IRETURN);
mv.visitMaxs(0, 0);
mv.visitEnd();
}
use of org.mvel2.asm.ClassWriter in project drools by kiegroup.
the class TraitMapPropertyWrapperClassBuilderImpl 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();
for (FieldDefinition field : core.getFieldsDefinitions()) {
invokeRemove(mv, wrapperName, core, field.getName(), field);
}
int j = 0;
for (FieldDefinition field : trait.getFieldsDefinitions()) {
boolean isSoftField = TraitRegistryImpl.isSoftField(field, j++, mask);
if (isSoftField) {
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, "map", Type.getDescriptor(Map.class));
mv.visitLdcInsn(field.getName());
mv.visitMethodInsn(INVOKEINTERFACE, Type.getInternalName(Map.class), "get", "(" + Type.getDescriptor(Object.class) + ")" + Type.getDescriptor(Object.class), true);
mv.visitVarInsn(ASTORE, 2);
mv.visitVarInsn(ALOAD, 0);
mv.visitFieldInsn(GETFIELD, internalWrapper, "map", Type.getDescriptor(Map.class));
mv.visitLdcInsn(field.getName());
mv.visitInsn(AsmUtil.zero(field.getTypeName()));
if (BuildUtils.isPrimitive(field.getTypeName())) {
TraitFactoryImpl.valueOf(mv, field.getTypeName());
}
mv.visitMethodInsn(INVOKEINTERFACE, Type.getInternalName(Map.class), "put", "(" + Type.getDescriptor(Object.class) + Type.getDescriptor(Object.class) + ")" + Type.getDescriptor(Object.class), true);
mv.visitInsn(POP);
mv.visitVarInsn(ALOAD, 2);
mv.visitInsn(ARETURN);
mv.visitLabel(l2);
}
}
mv.visitVarInsn(ALOAD, 0);
mv.visitFieldInsn(GETFIELD, internalWrapper, "map", Type.getDescriptor(Map.class));
mv.visitVarInsn(ALOAD, 1);
mv.visitMethodInsn(INVOKEINTERFACE, Type.getInternalName(Map.class), "remove", "(" + Type.getDescriptor(Object.class) + ")" + Type.getDescriptor(Object.class), true);
mv.visitVarInsn(ASTORE, 2);
mv.visitVarInsn(ALOAD, 2);
mv.visitInsn(ARETURN);
mv.visitMaxs(0, 0);
mv.visitEnd();
}
use of org.mvel2.asm.ClassWriter in project drools by kiegroup.
the class TraitMapPropertyWrapperClassBuilderImpl 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(trait.getName(), core.getDefinedClass().getName());
String name = TraitFactoryImpl.getPropertyWrapperName(trait, core);
String internalWrapper = BuildUtils.getInternalType(name);
String descrCore = Type.getDescriptor(core.getDefinedClass());
String internalCore = Type.getInternalName(core.getDefinedClass());
ClassWriter cw = createClassWriter(classLoader, ACC_PUBLIC + ACC_SUPER, internalWrapper, Type.getDescriptor(Object.class) + Type.getDescriptor(Map.class) + Type.getDescriptor(MapWrapper.class), // "Ljava/lang/Object;Ljava/util/Map<Ljava/lang/String;Ljava/lang/Object;>;Lorg/drools/factmodel/traits/MapWrapper;",
Type.getInternalName(Object.class), new String[] { Type.getInternalName(Map.class), Type.getInternalName(MapWrapper.class), Type.getInternalName(Serializable.class) });
cw.visitInnerClass(Type.getInternalName(Map.Entry.class), Type.getInternalName(Map.class), "Entry", ACC_PUBLIC + ACC_STATIC + ACC_ABSTRACT + ACC_INTERFACE);
{
fv = cw.visitField(0, "object", descrCore, null, null);
fv.visitEnd();
}
{
fv = cw.visitField(0, "map", Type.getDescriptor(Map.class), "Ljava/util/Map<Ljava/lang/String;Ljava/lang/Object;>;", null);
fv.visitEnd();
}
{
mv = cw.visitMethod(ACC_PUBLIC, "<init>", "(" + descrCore + Type.getDescriptor(Map.class) + ")V", "(" + descrCore + "Ljava/util/Map<Ljava/lang/String;Ljava/lang/Object;>;)V", null);
mv.visitCode();
mv.visitVarInsn(ALOAD, 0);
mv.visitMethodInsn(INVOKESPECIAL, Type.getInternalName(Object.class), "<init>", "()V", false);
mv.visitVarInsn(ALOAD, 0);
mv.visitVarInsn(ALOAD, 1);
mv.visitFieldInsn(PUTFIELD, internalWrapper, "object", descrCore);
mv.visitVarInsn(ALOAD, 0);
mv.visitVarInsn(ALOAD, 2);
mv.visitFieldInsn(PUTFIELD, internalWrapper, "map", Type.getDescriptor(Map.class));
mv.visitVarInsn(ALOAD, 1);
mv.visitVarInsn(ALOAD, 2);
mv.visitMethodInsn(INVOKEVIRTUAL, internalCore, "_setDynamicProperties", "(" + Type.getDescriptor(Map.class) + ")V", false);
initSoftFields(mv, trait, core, internalWrapper, mask, 2);
mv.visitInsn(RETURN);
mv.visitMaxs(0, 0);
mv.visitEnd();
}
buildSize(cw, name, core);
buildIsEmpty(cw, name, core);
buildGet(cw, name, core);
buildPut(cw, name, core);
buildClear(cw, name, trait, core, mask);
buildRemove(cw, name, trait, core, mask);
buildContainsKey(cw, name, core);
buildContainsValue(cw, name, trait, core);
buildKeyset(cw, name, core);
buildValues(cw, name, core);
buildEntryset(cw, name, core);
buildCommonMethods(cw, name);
cw.visitEnd();
return cw.toByteArray();
}
use of org.mvel2.asm.ClassWriter in project drools by kiegroup.
the class TraitTripleProxyClassBuilderImpl 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 internalCore = Type.getInternalName(core.getDefinedClass());
String descrCore = Type.getDescriptor(core.getDefinedClass());
String internalTrait = Type.getInternalName(getTrait().getDefinedClass());
MixinInfo mixinInfo = findMixinInfo(traitClass);
ClassWriter cw = createClassWriter(classLoader, ACC_PUBLIC + ACC_SUPER, internalProxy, null, Type.getInternalName(proxyBaseClass), new String[] { internalTrait, Type.getInternalName(Externalizable.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_PRIVATE, "store", Type.getDescriptor(TripleStore.class), null, null);
fv.visitEnd();
}
{
fv = cw.visitField(ACC_PRIVATE, "storeId", Type.getDescriptor(String.class), null, null);
fv.visitEnd();
}
if (mixinInfo != null) {
for (Class<?> mixinClass : mixinInfo.mixinClasses) {
{
fv = cw.visitField(ACC_PRIVATE, getMixinName(mixinClass), Type.getDescriptor(mixinClass), null, null);
fv.visitEnd();
}
}
}
{
mv = cw.visitMethod(ACC_PUBLIC, "<init>", "(" + descrCore + Type.getDescriptor(TripleStore.class) + Type.getDescriptor(TripleFactory.class) + Type.getDescriptor(BitSet.class) + Type.getDescriptor(BitSet.class) + Type.getDescriptor(boolean.class) + ")V", null, null);
mv.visitCode();
mv.visitVarInsn(ALOAD, 0);
mv.visitVarInsn(ALOAD, 2);
mv.visitMethodInsn(INVOKEVIRTUAL, Type.getInternalName(TripleStore.class), "getId", "()" + Type.getDescriptor(String.class), false);
mv.visitFieldInsn(PUTFIELD, internalProxy, "storeId", Type.getDescriptor(String.class));
buildConstructorCore(mv, internalProxy, internalWrapper, internalCore, descrCore, mixinInfo);
initFields(mv, internalProxy);
mv.visitInsn(RETURN);
mv.visitMaxs(0, 0);
mv.visitEnd();
}
{
mv = cw.visitMethod(ACC_PUBLIC, "setObject", "(" + Type.getDescriptor(Object.class) + ")V", null, null);
mv.visitCode();
mv.visitVarInsn(ALOAD, 0);
mv.visitVarInsn(ALOAD, 1);
mv.visitTypeInsn(CHECKCAST, internalCore);
mv.visitFieldInsn(PUTFIELD, internalProxy, "object", descrCore);
mv.visitInsn(RETURN);
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, 0);
mv.visitVarInsn(ALOAD, 1);
mv.visitMethodInsn(INVOKESPECIAL, Type.getInternalName(proxyBaseClass), "writeExternal", "(" + Type.getDescriptor(ObjectOutput.class) + ")V", false);
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, "storeId", Type.getDescriptor(String.class));
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, "store", Type.getDescriptor(TripleStore.class));
mv.visitMethodInsn(INVOKEINTERFACE, Type.getInternalName(ObjectOutput.class), "writeObject", "(" + Type.getDescriptor(Object.class) + ")V", true);
mv.visitInsn(RETURN);
// mv.visitMaxs( 2, 2 );
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(INVOKESPECIAL, Type.getInternalName(proxyBaseClass), "readExternal", "(" + Type.getDescriptor(ObjectInput.class) + ")V", false);
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(String.class));
mv.visitFieldInsn(PUTFIELD, internalProxy, "storeId", Type.getDescriptor(String.class));
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(TripleStore.class));
mv.visitFieldInsn(PUTFIELD, internalProxy, "store", Type.getDescriptor(TripleStore.class));
mv.visitInsn(RETURN);
// mv.visitMaxs( 3, 2 );
mv.visitMaxs(0, 0);
mv.visitEnd();
}
helpBuildClass(core, cw, internalProxy, descrCore, mask);
buildProxyAccessors(mask, cw, proxyName, core, mixinInfo);
boolean hasKeys = false;
for (FactField ff : getTrait().getFields()) {
if (ff.isKey()) {
hasKeys = true;
break;
}
}
if (!hasKeys) {
buildEqualityMethods(cw, proxyName, core.getClassName());
} else {
buildKeyedEqualityMethods(cw, getTrait(), proxyName);
}
buildMixinMethods(proxyName, mixinInfo, cw);
buildCommonMethods(cw, proxyName);
buildExtendedMethods(cw, getTrait(), core);
buildShadowMethods(cw, trait, core);
cw.visitEnd();
return cw.toByteArray();
}
Aggregations