use of org.simpleflatmapper.ow2asm.MethodVisitor in project SimpleFlatMapper by arnaudroger.
the class InstantiatorBuilder method appendInitBuilder.
private static <S> void appendInitBuilder(Map<Parameter, Getter<? super S, ?>> injections, ClassWriter cw, String sourceType, String classType, BuilderInstantiatorDefinition instantiatorDefinition) {
MethodVisitor mv;
mv = cw.visitMethod(ACC_PUBLIC, "<init>", "(Ljava/util/Map;L" + AsmUtils.toAsmType(Instantiator.class) + ";)V", "(Ljava/util/Map<Ljava.lang.String;L" + AsmUtils.toAsmType(Getter.class) + "<" + AsmUtils.toTargetTypeDeclaration(sourceType) + "*>;>;" + "L" + AsmUtils.toAsmType(Instantiator.class) + "<Ljava/lang/Void;L" + AsmUtils.toAsmType(BiInstantiatorBuilder.getTargetType(instantiatorDefinition.getBuilderInstantiator())) + ";>;)V", null);
mv.visitCode();
mv.visitVarInsn(ALOAD, 0);
mv.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>", "()V", false);
mv.visitVarInsn(ALOAD, 0);
mv.visitVarInsn(ALOAD, 2);
mv.visitFieldInsn(PUTFIELD, classType, "builderInstantiator", "L" + AsmUtils.toAsmType(Instantiator.class) + ";");
appendInitGetters(injections, classType, mv);
mv.visitInsn(RETURN);
mv.visitMaxs(3, 2);
mv.visitEnd();
}
use of org.simpleflatmapper.ow2asm.MethodVisitor in project SimpleFlatMapper by arnaudroger.
the class SetterBuilder method appendSet.
private static void appendSet(Method method, ClassWriter cw, Class<?> target, String targetType, String propertyType, String classType) {
MethodVisitor mv;
{
mv = cw.visitMethod(ACC_PUBLIC, "set", "(" + AsmUtils.toTargetTypeDeclaration(targetType) + AsmUtils.toTargetTypeDeclaration(propertyType) + ")V", null, new String[] { "java/lang/Exception" });
mv.visitCode();
mv.visitVarInsn(ALOAD, 1);
mv.visitVarInsn(ALOAD, 2);
AsmUtils.invoke(mv, target, method.getName(), "(" + AsmUtils.toTargetTypeDeclaration(propertyType) + ")" + AsmUtils.toTargetTypeDeclaration(method.getReturnType()));
mv.visitInsn(RETURN);
mv.visitMaxs(2, 3);
mv.visitEnd();
}
appendSynthetic(cw, targetType, propertyType, classType);
}
use of org.simpleflatmapper.ow2asm.MethodVisitor 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.MethodVisitor in project SimpleFlatMapper by arnaudroger.
the class SetterBuilder method appendToString.
private static void appendToString(ClassWriter cw) {
MethodVisitor mv;
mv = cw.visitMethod(ACC_PUBLIC, "toString", "()Ljava/lang/String;", null, null);
mv.visitCode();
mv.visitTypeInsn(NEW, "java/lang/StringBuilder");
mv.visitInsn(DUP);
mv.visitMethodInsn(INVOKESPECIAL, "java/lang/StringBuilder", "<init>", "()V", false);
mv.visitVarInsn(ASTORE, 1);
mv.visitVarInsn(ALOAD, 1);
mv.visitVarInsn(ALOAD, 0);
mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Object", "getClass", "()Ljava/lang/Class;", false);
mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Class", "getSimpleName", "()Ljava/lang/String;", false);
mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/StringBuilder", "append", "(Ljava/lang/String;)Ljava/lang/StringBuilder;", false);
mv.visitLdcInsn("{");
mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/StringBuilder", "append", "(Ljava/lang/String;)Ljava/lang/StringBuilder;", false);
mv.visitLdcInsn("}");
mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/StringBuilder", "append", "(Ljava/lang/String;)Ljava/lang/StringBuilder;", false);
mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/StringBuilder", "toString", "()Ljava/lang/String;", false);
mv.visitInsn(ARETURN);
mv.visitMaxs(2, 1);
mv.visitEnd();
}
use of org.simpleflatmapper.ow2asm.MethodVisitor 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