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("开始");

proxy.invokeSuper(obj, args);

System.out.println("结束");

return null;

}

}

class gg{

public void GetBook() {

// TODO Auto-generated method stub

System.out.println("get book");

}

}

当Class  gg 的修饰符非public的时候 会报错 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$$bf75381d cannot access its superclass javacore.testForCglibProxy.gg1

at java.lang.ClassLoader.defineClass1(Native Method)

at java.lang.ClassLoader.defineClass(ClassLoader.java:760)

... 10 more

最新文章

  1. 夯实基础之php学习-2提高篇
  2. TemplateDataField
  3. 误删ibdata1文件恢复方法
  4. C#手动做一个负载均衡服务器
  5. table.appand(行数据) datagrid分页
  6. [ZZ] Maxwell 架构
  7. 支持状态对象复用的RPC框架——SnakeRPC
  8. VisualSVN Server 从此告别SVN记事本配置
  9. linux —— shell 编程(编程语法)
  10. .NET基础拾遗(7)多线程开发基础2
  11. Git 多人协作的工作模式
  12. 微软发布Win10开发者指南视频
  13. 【顶】在node环境下玩转less
  14. PSAM 卡的应用 操作方法
  15. Nowcoder 牛客练习赛23
  16. k8s系列~mgr的应用
  17. Codeforces 679C Bear and Square Grid
  18. 移除list中null元素
  19. NgDL:【第二周】NN基础
  20. Git 命令简单罗列

热门文章

  1. ios开发 - 获取从http上下载文件的大小
  2. Android开发之布局文件里实现OnClick事件关联处理方法
  3. Mysql processlist命令
  4. js 小总结
  5. iframe中,重新加载页面
  6. Xutils的get请求后,总是返回同样数据的问题解决方式
  7. socket连接和TCP连接的关系
  8. 【Android应用开发详解】实现第三方授权登录、分享以及获取用户资料
  9. EAI G4-lidar ROS配置
  10. 07 redi sorder set结构及命令详解