2009年6月23日

cglib - Code Generation Library

cglib - Code Generation Library

import java.lang.reflect.Method;
import java.util.Arrays;

import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;
import net.sf.cglib.util.ParallelSorter;
import net.sf.cglib.util.StringSwitcher;

public class Main {

public static void main(String[] args) {
String[] strs = { "Sunday", "Monday" };
int[] ints = { 0, 1 };
StringSwitcher ss = StringSwitcher.create(strs, ints, true);
System.out.println(ss.intValue("Monday"));

Object[][] arrays = {{ "1", "4", "3", "2", "0" }};
ParallelSorter ps = ParallelSorter.create(arrays);
ps.quickSort(0);
System.out.println(Arrays.toString(arrays[0]));

Enhancer e = new Enhancer();
e.setSuperclass(Main.class);
e.setCallback(new MethodInterceptor(){
public Object intercept(Object obj, Method method,
Object[] args, MethodProxy proxy) throws Throwable {
args[0] = args[0] + "!";
return "-- " + proxy.invokeSuper(obj, args) + " --";
}
});
Main m = (Main)e.create();
System.out.println(m.echo("World"));
}

public String echo(String name) {
return "Hello " + name;
}

}
/*
1
[0, 1, 2, 3, 4]
-- Hello World! --
*/

沒有留言:

網誌存檔