Javaナレッジポイントまとめ(動的バイトコード操作-JavassistのAPI使用)
Javaナレッジポイントまとめ(動的バイトコード操作-JavassistのAPI使用)
@(Java知識点まとめ)[Java,動的バイトコード操作]
参考記事:トランスファ
操作例:
@(Java知識点まとめ)[Java,動的バイトコード操作]
参考記事:トランスファ
操作例:
public @interface Author {
String name();
int year();
}
@Author(name = "gs",year=2015)
public class Emp {
private int num;
private String name;
public Emp() {
}
public Emp(int num, String name) {
this();
this.num = num;
this.name = name;
}
public void sayHello(String name){
System.out.print(" ,");
}
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.Arrays;
import javassist.ClassPool;
import javassist.CtClass;
import javassist.CtConstructor;
import javassist.CtField;
import javassist.CtMethod;
import javassist.CtNewMethod;
import javassist.NotFoundException;
/**
* javassist API
*
* @author Administrator
*
*/
public class Test11 {
//
public static void test1(ClassPool pool, CtClass cc) {
try {
cc = pool.get("com.gs.Emp");
byte[] bytes = cc.toBytecode(); //
System.out.println(Arrays.toString(bytes));
System.out.println(cc.getName()); //
System.out.println(cc.getSimpleName());
System.out.println(cc.getSuperclass()); //
System.out.println(cc.getInterfaces()); //
} catch (Exception e) {
e.printStackTrace();
}
}
// ,
public static void test2(ClassPool pool, CtClass cc) {
try {
/* CtClass writeFile()、toClass()、toBytecode() Class ,
* Javassist CtClass , ,JVM Class , 。
, CtClass , , defrost() */
cc.defrost();
//
CtMethod add = CtNewMethod.make("public int add(int a,int b){return $1+$2;}", cc);
//
CtMethod subtraction = new CtMethod(CtClass.intType, "subtraction",
new CtClass[] { CtClass.intType, CtClass.intType }, cc);
subtraction.setModifiers(Modifier.PUBLIC);
subtraction.setBody("return $1-$2;");
cc.addMethod(add);
cc.addMethod(subtraction);
//
Class clazz = cc.toClass();
Emp emp = (Emp) clazz.newInstance();
// Method method = clazz.getDeclaredMethod("add", new
// Class[]{int.class,int.class});
Method method = clazz.getDeclaredMethod("subtraction", new Class[] { int.class, int.class });
Object result = method.invoke(emp, 200, 300);
System.out.println(result);
} catch (Exception e) {
}
}
//
public static void test3(ClassPool pool, CtClass cc) {
try {
cc.defrost();
CtMethod m1 = cc.getDeclaredMethod("sayHello", new CtClass[] { pool.get("java.lang.String") });
m1.insertBefore("System.out.println(\" \");");
m1.insertAfter("System.out.println($1);");
//
Class clazz = cc.toClass();
Emp emp = (Emp) clazz.newInstance();
Method method = clazz.getDeclaredMethod("sayHello", new Class[] { String.class });
method.invoke(emp, " ");
} catch (Exception e) {
e.printStackTrace();
}
}
//
public static void test4(ClassPool pool, CtClass cc) {
try {
// CtField.make("private double salary;", cc);
CtField field = new CtField(CtClass.doubleType, "salary", cc);
field.setModifiers(Modifier.PRIVATE);
cc.addField(field);
// cc.getDeclaredField("salary"); //
CtMethod method = CtNewMethod.getter("getSalary", field);
cc.addMethod(method);
CtMethod method2 = CtNewMethod.getter("setSalary", field);
cc.addMethod(method2);
} catch (Exception e) {
e.printStackTrace();
}
}
//
public static void test5(ClassPool pool, CtClass cc) {
try {
cc.defrost();
CtConstructor[] cs = cc.getConstructors();
for (CtConstructor c : cs) {
System.out.println(c.getLongName());
c.insertAfter("System.out.println(\" \");");
}
} catch (Exception e) {
e.printStackTrace();
}
}
//
public static void test6(ClassPool pool, CtClass cc) {
try {
cc.defrost();
Object[] annotations = cc.getAnnotations();
Author author = (Author) annotations[0];
System.out.println("name:" + author.name() + ",year:" + author.year());
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
ClassPool pool = ClassPool.getDefault();
CtClass cc = null;
try {
cc = pool.get("com.gs.Emp");
} catch (NotFoundException e) {
e.printStackTrace();
}
test1(pool, cc);
//test2(pool, cc);
test3(pool, cc);
test5(pool, cc);
test6(pool, cc);
}
}