zoukankan      html  css  js  c++  java
  • Cglib学习报错 java.lang.reflect.InvocationTargetException-->null

    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

     

  • 相关阅读:
    Convert.ToInt32()和int.Parse()的区别
    C# 提取字符串中的数字
    asp.net FileUpload 控件上传文件 以二进制的形式存入数据库并将图片显示出来
    angularjs如何在ng-repeat过程中控制字符串长度超过指定长度后面内容以省略号显示
    html label 标签的 for 属性
    bootstrap-paginator 分页控件的使用
    时间戳的转换
    Server.MapPath() 解析
    Angularjs 日期格式转换
    1.docker 慕课入门
  • 原文地址:https://www.cnblogs.com/luoluoshidafu/p/5535716.html
Copyright © 2011-2022 走看看