use of org.simpleflatmapper.ow2asm.ClassWriter in project SimpleFlatMapper by arnaudroger.
the class GetterBuilder method createObjectGetter.
public static byte[] createObjectGetter(final String className, final Field field) throws Exception {
ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS);
MethodVisitor mv;
Class<?> target = field.getDeclaringClass();
Class<?> property = field.getType();
String targetType = toType(target);
String propertyType = toType(property);
String classType = toType(className);
cw.visit(V1_6, ACC_PUBLIC + ACC_FINAL + ACC_SUPER, classType, "Ljava/lang/Object;L" + GETTER_TYPE + "<L" + targetType + ";" + AsmUtils.toTargetTypeDeclaration(propertyType) + ">;", "java/lang/Object", new String[] { GETTER_TYPE });
{
mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);
mv.visitCode();
mv.visitVarInsn(ALOAD, 0);
mv.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>", "()V", false);
mv.visitInsn(RETURN);
mv.visitMaxs(1, 1);
mv.visitEnd();
}
{
mv = cw.visitMethod(ACC_PUBLIC, "get", "(" + AsmUtils.toTargetTypeDeclaration(targetType) + ")" + AsmUtils.toTargetTypeDeclaration(propertyType), null, new String[] { "java/lang/Exception" });
mv.visitCode();
mv.visitVarInsn(ALOAD, 1);
mv.visitFieldInsn(GETFIELD, targetType, field.getName(), AsmUtils.toTargetTypeDeclaration(propertyType));
mv.visitInsn(ARETURN);
mv.visitMaxs(1, 2);
mv.visitEnd();
}
appendBridge(cw, targetType, propertyType, classType);
appendToString(cw);
cw.visitEnd();
return AsmUtils.writeClassToFile(className, cw.toByteArray());
}
use of org.simpleflatmapper.ow2asm.ClassWriter in project SimpleFlatMapper by arnaudroger.
the class GetterBuilder method createObjectGetter.
public static byte[] createObjectGetter(final String className, final Method method) throws Exception {
ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS);
MethodVisitor mv;
Class<?> target = method.getDeclaringClass();
Class<?> property = method.getReturnType();
String targetType = toType(target);
String propertyType = toType(property);
String classType = toType(className);
cw.visit(V1_6, ACC_PUBLIC + ACC_FINAL + ACC_SUPER, classType, "Ljava/lang/Object;L" + GETTER_TYPE + "<L" + targetType + ";" + AsmUtils.toTargetTypeDeclaration(propertyType) + ">;", "java/lang/Object", new String[] { GETTER_TYPE });
{
mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);
mv.visitCode();
mv.visitVarInsn(ALOAD, 0);
mv.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>", "()V", false);
mv.visitInsn(RETURN);
mv.visitMaxs(1, 1);
mv.visitEnd();
}
{
mv = cw.visitMethod(ACC_PUBLIC, "get", "(" + AsmUtils.toTargetTypeDeclaration(targetType) + ")" + AsmUtils.toTargetTypeDeclaration(propertyType), null, new String[] { "java/lang/Exception" });
mv.visitCode();
mv.visitVarInsn(ALOAD, 1);
AsmUtils.invoke(mv, target, method.getName(), "()" + AsmUtils.toTargetTypeDeclaration(propertyType));
mv.visitInsn(ARETURN);
mv.visitMaxs(1, 2);
mv.visitEnd();
}
appendBridge(cw, targetType, propertyType, classType);
appendToString(cw);
cw.visitEnd();
return AsmUtils.writeClassToFile(className, cw.toByteArray());
}
use of org.simpleflatmapper.ow2asm.ClassWriter in project SimpleFlatMapper by arnaudroger.
the class InstantiatorBuilder method createInstantiator.
public static <S> byte[] createInstantiator(final String className, final Class<?> sourceClass, final ExecutableInstantiatorDefinition instantiatorDefinition, final Map<Parameter, Getter<? super S, ?>> injections) throws Exception {
ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS);
Class<?> targetClass = TypeHelper.toClass(BiInstantiatorBuilder.getTargetType(instantiatorDefinition));
String targetType = AsmUtils.toAsmType(targetClass);
String sourceType = AsmUtils.toWrapperType(sourceClass);
String classType = AsmUtils.toAsmType(className);
String instantiatorType = AsmUtils.toAsmType(Instantiator.class);
cw.visit(V1_6, ACC_PUBLIC + ACC_FINAL + ACC_SUPER, classType, "Ljava/lang/Object;L" + instantiatorType + "<L" + targetType + ";>;", "java/lang/Object", new String[] { instantiatorType });
Parameter[] parameters = instantiatorDefinition.getParameters();
appendGetters(injections, cw);
appendInit(injections, cw, sourceType, classType);
appendNewInstanceBuilderOnMethod(sourceClass, instantiatorDefinition, injections, cw, targetType, sourceType, classType, parameters);
appendBridgeMethod(cw, targetType, sourceType, classType);
appendToString(injections, cw, parameters);
cw.visitEnd();
return AsmUtils.writeClassToFile(className, cw.toByteArray());
}
use of org.simpleflatmapper.ow2asm.ClassWriter in project SimpleFlatMapper by arnaudroger.
the class SetterBuilder method createObjectSetter.
public static byte[] createObjectSetter(final String className, final Method method) throws Exception {
ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS);
MethodVisitor mv;
Class<?> target = method.getDeclaringClass();
Class<?> property = method.getParameterTypes()[0];
String targetType = toType(target);
String propertyType = toType(property);
String classType = toType(className);
cw.visit(V1_6, ACC_PUBLIC + ACC_FINAL + ACC_SUPER, classType, "Ljava/lang/Object;L" + SETTER_TYPE + "<L" + targetType + ";" + AsmUtils.toTargetTypeDeclaration(propertyType) + ">;", "java/lang/Object", new String[] { SETTER_TYPE });
{
mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);
mv.visitCode();
mv.visitVarInsn(ALOAD, 0);
mv.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>", "()V", false);
mv.visitInsn(RETURN);
mv.visitMaxs(1, 1);
mv.visitEnd();
}
appendSet(method, cw, target, targetType, propertyType, classType);
appendToString(cw);
cw.visitEnd();
return AsmUtils.writeClassToFile(className, cw.toByteArray());
}
use of org.simpleflatmapper.ow2asm.ClassWriter in project SimpleFlatMapper by arnaudroger.
the class SetterBuilder method createObjectSetter.
public static byte[] createObjectSetter(final String className, final Field field) throws Exception {
ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS);
MethodVisitor mv;
Class<?> target = field.getDeclaringClass();
Class<?> property = field.getType();
String targetType = toType(target);
String propertyType = toType(property);
String classType = toType(className);
cw.visit(V1_6, ACC_PUBLIC + ACC_FINAL + ACC_SUPER, classType, "Ljava/lang/Object;L" + SETTER_TYPE + "<L" + targetType + ";" + AsmUtils.toTargetTypeDeclaration(propertyType) + ">;", "java/lang/Object", new String[] { SETTER_TYPE });
{
mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);
mv.visitCode();
mv.visitVarInsn(ALOAD, 0);
mv.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>", "()V", false);
mv.visitInsn(RETURN);
mv.visitMaxs(1, 1);
mv.visitEnd();
}
appendSet(field, cw, target, targetType, propertyType, classType);
appendToString(cw);
cw.visitEnd();
return AsmUtils.writeClassToFile(className, cw.toByteArray());
}
Aggregations