
e sono rimasto attonito...
poi ho visto quest'altro..

e non vedo l'ora di averlo anche io !!!!! chissà tra quanto mi arriva !
public class Empty {
}
import java.io.IOException;
import org.objectweb.asm.ClassReader;
import org.objectweb.asm.ClassWriter;
import org.objectweb.asm.FieldVisitor;
import org.objectweb.asm.MethodVisitor;
import org.objectweb.asm.Opcodes;
public class EntityTransformer implements Opcodes {
/*a class for writing fields and methods*/
ClassWriter cw;
/*the name of the target class*/
String className;
private ClassReader cr;
/**
* The default constructor: it generates bytecode for 1.5 JVM,
* creating a default constructor in the target class.
*/
EntityTransformer(String className) throws IOException {
this.className = className;
this.cr = new ClassReader(className);
this.cw = new ClassWriter(cr, true);
this.cw.visit(V1_5, ACC_PUBLIC + ACC_SUPER,
className.replace('.', '/'), null, "java/lang/Object", null);
MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, "", "()V", null,
null);
mv.visitCode();
mv.visitVarInsn(ALOAD, 0);
mv.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "", "()V");
mv.visitInsn(RETURN);
mv.visitMaxs(1, 1);
mv.visitEnd();
}
....
}
void createSetter(String propertyName, String type) {
/*
* Field First
*
* Remind yourself: Object Types are in the form L; <-
* Notice the Comma
*/
FieldVisitor fv = cw.visitField(ACC_PRIVATE, propertyName, "L"
+ type.replace('.', '/') + ";", null, null);
fv.visitEnd();
String methodName = "set" + propertyName.substring(0, 1).toUpperCase()
+ propertyName.substring(1);
MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, methodName, "(L"
+ type.replace('.', '/') + ";)V", null, null);
mv.visitCode();
mv.visitVarInsn(ALOAD, 0);
mv.visitVarInsn(ALOAD, 1);
mv.visitFieldInsn(PUTFIELD, className.replace('.', '/'), propertyName,
"L" + type.replace('.', '/') + ";");
mv.visitInsn(RETURN);
mv.visitMaxs(2, 2);
mv.visitEnd();
}
void createGetter(String propertyName, String returnType) {
String methodName = "get" + propertyName.substring(0, 1).toUpperCase()
+ propertyName.substring(1);
MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, methodName, "()L"
+ returnType.replace('.', '/')+";", null, null);
mv.visitCode();
mv.visitVarInsn(ALOAD, 0);
mv.visitFieldInsn(GETFIELD, className.replace('.', '/'), propertyName,
"L"+returnType.replace('.', '/')+";");
mv.visitInsn(ARETURN);
mv.visitMaxs(0, 0);
mv.visitEnd();
}
private Class loadClass(byte[] b) {
// override classDefine (as it is protected) and define the class.
Class clazz = null;
try {
ClassLoader loader = ClassLoader.getSystemClassLoader();
Class cls = Class.forName("java.lang.ClassLoader");
java.lang.reflect.Method method = cls.getDeclaredMethod(
"defineClass", new Class[]{String.class, byte[].class,
int.class, int.class});
// protected method invocaton
method.setAccessible(true);
try {
Object[] args = new Object[]{className, b, new Integer(0),
new Integer(b.length)};
clazz = (Class) method.invoke(loader, args);
} finally {
method.setAccessible(false);
}
} catch (Exception e) {
e.printStackTrace();
System.exit(1);
}
return clazz;
}
public Class transform(String[] properties) throws IOException {
for (String prop : properties) {
//property name, type parameter
createSetter(prop, "java.lang.String");
//property name, return type
createGetter(prop, "java.lang.String");
}
return loadClass(cw.toByteArray());
}