|
|
|||||
ProjectDocumentationSamplesLinks |
Code Generation LibraryCGLIB outputs generated classes. Byte code generation and class file format manipulation is hidden in static methods. It was designed to implement Transparent Persistence for JAVA, but can be used to implement aspects like Security or Validation. Simple example : Implement MethodInterceptor
public class Trace implements MethodInterceptor{
private static Trace trace = new Trace();
public boolean invokeSuper( Object obj, Method method,
Object args[]) throws Throwable{
//invoke supper in generated code
System.out.println("before: " + method );
return true ;
}
public Object afterReturn(
Object obj, Method method,
Object args[],boolean invokedSuper,
Object retValFromSuper,Throwable e
)throws Throwable{
//print "this" and method signature
System.out.println("after: " + method );
return retValFromSuper;
}
public static Object newInstance(Class clazz){
//generate code
return Enhancer.enhance(clazz, null, trace);
}
}
Use static method to instantiate object:
List list = (List)Trace.newInstance( Vector.class ); list.add( "TEST" );This code must produce output like this: before: public boolean Vector.add( Object ) after: public boolean Vector.add( Object ) |
||||