Package javacore.testforcglibproxy;
import Java.lang.reflect.Method;
import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;
Public class Getnewbook {
Public static void main (string[] args) {
cglibproxy cp = new cglibproxy ();
gg gnb1 = (gg)cp.getproxy (new GG ());
gnb1. GetBook ();
}
}
class Cglibproxy implements methodinterceptor{
Object Target;
Public object GetProxy (object target) {
This . Target=target;
enhancer ch = New Enhancer ();
Ch.setsuperclass (this. Target.getclass ());
Ch.setcallback (this);
return ch.create ();
}
@Override
Public object Intercept (object obj, method method, object[] args,
methodproxy proxy) throws throwable {
// TODO auto-generated method stub
System. out. println ("start");
proxy.invokesuper (obj, args);
System. out. println ("End");
return null;
}
}
class gg{
Public void GetBook () {
// TODO auto-generated method stub
System. out. println ("get Book");
}
}
When class GG modifies printable public, it will error cannot access its superclass
Exception in thread "main" net.sf.cglib.core.CodeGenerationException: Java.lang.reflect.invocationtargetexception-->null
At Net.sf.cglib.core.AbstractClassGenerator.create (abstractclassgenerator.java:237)
At Net.sf.cglib.proxy.Enhancer.createHelper (enhancer.java:377)
At Net.sf.cglib.proxy.Enhancer.create (enhancer.java:285)
At Javacore.testForCglibProxy.cglibProxy.getProxy (getnewbook.java:25)
At Javacore.testForCglibProxy.GetNewBook.main (getnewbook.java:13)
Caused by: java.lang.reflect.InvocationTargetException
At Sun.reflect.NativeMethodAccessorImpl.invoke0 (Native Method)
At Sun.reflect.NativeMethodAccessorImpl.invoke (nativemethodaccessorimpl.java:62)
At Sun.reflect.DelegatingMethodAccessorImpl.invoke (delegatingmethodaccessorimpl.java:43)
At Java.lang.reflect.Method.invoke (method.java:497)
At Net.sf.cglib.core.ReflectUtils.defineClass (reflectutils.java:384)
At Net.sf.cglib.core.AbstractClassGenerator.create (abstractclassgenerator.java:219)
... 4 more
caused By:java.lang.IllegalAccessError:class $javacore. testforcglibproxy.gg1$ $EnhancerByCGLIB $ $BF 75381d cannot Access its superclass JAVACORE.TESTFORCGLIBPROXY.GG1
At Java.lang.ClassLoader.defineClass1 (Native Method)
At Java.lang.ClassLoader.defineClass (classloader.java:760)
... Ten more
Cglib Learning Error Java.lang.reflect.invocationtargetexception-->null