论坛首页 Java企业应用论坛

关于java代理类编写碰到的一些问题

浏览 6310 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2013-04-26  
		Object[] elements = new Object[3];//定义存放代理对象的数组
	
   	  	ShapeProxyHandler proxyHandler1 = new ShapeProxyHandler(new Circle());
   	  	Object proxy1 = Proxy.newProxyInstance(null, new Class[] { Shape.class } , proxyHandler1);
	    elements[0] = proxy1;
	    


这段代码,其中Circle是我自己定义的本包的类,Shape是Cirlce的父类(抽象类)
执行的时候,出现如下错误

Exception in thread "main" java.lang.IllegalArgumentException: class reflect.Circle is not visible from class loader
at java.lang.reflect.Proxy.getProxyClass(Unknown Source)
at java.lang.reflect.Proxy.newProxyInstance(Unknown Source)
at reflect.ProxyTestShape.main(ProxyTestShape.java:13)

感觉是默认的类加载器,找不到这个reflect.Circle,查看Proxy的getProxy方法,看到这句代码
 String interfaceName = interfaces[i].getName();
	    Class interfaceClass = null;
	    try {
		interfaceClass = Class.forName(interfaceName, false, loader);
	    } catch (ClassNotFoundException e) {
	    }
	    if (interfaceClass != interfaces[i]) {
		throw new IllegalArgumentException(
		    interfaces[i] + " is not visible from class loader");
	    }

	    /*
	     * Verify that the Class object actually represents an
	     * interface.
	     */
	    if (!interfaceClass.isInterface()) {
		throw new IllegalArgumentException(
		    interfaceClass.getName() + " is not an interface");
	    }

应该是里面的这句
interfaceClass = Class.forName(interfaceName, false, loader);
和这句
  if (interfaceClass != interfaces[i]) {
throw new IllegalArgumentException(
    interfaces[i] + " is not visible from class loader");
    }


从代码来看,应该加载进来的interfaceClass 和我传递进去的Shape.class不一样,这个要怎么解决呢?是类加载器的问题吗?
   发表时间:2013-04-26  
网上搜到这样一个回复,不过还是不知道怎么解决
原文地址:http://stackoverflow.com/questions/211176/interface-is-not-visible-from-classloader-when-using-a-proxy

When your DynamicProxy tries to do Class.forName(youInterfaceClass.getName()) the resulting java.lang.Class instance is different from the one you passed when you created the proxy. In other words you have two class objects with the same name and the proxy is not sure which one is the right one (doesn't matter whether they are the same).

Usually, this happens when the interface you are trying to proxy is in a library loaded through two different classloaders (i.e. Tomcat's 'common' and 'application').

If this doesn't help, please post more info on your application - especially if you are using any application server, Spring or OSGi.
0 请登录后投票
   发表时间:2013-04-26   最后修改:2013-04-26
知道了,原来是对下面这个方法的误用
Proxy.newProxyInstance(null, new Class[] { Shape.class } , proxyHandler1); 
第二个参数,Class数组里面应该存放的是接口类型,而不能是抽象类


本来以为是上面的原因,可是后来修改成Interface还是不行....
0 请登录后投票
   发表时间:2013-04-26  
搞定了,改成这样就可以了,
ShapeInterface是我定义的接口
ShapeProxyHandler proxyHandler1 = new ShapeProxyHandler(new Circle());
   	  	Object proxy1 = Proxy.newProxyInstance(ShapeInterface.class.getClassLoader(), new Class[] { ShapeInterface.class } , proxyHandler1);
	    elements[0] = proxy1;
0 请登录后投票
   发表时间:2013-06-18  
可以这样写:

@SuppressWarnings("unchecked")
public T createProxy() {
Class<T> cls = (Class<T>) target.getClass();
if (cls.getInterfaces() != null && cls.getInterfaces().length>0)
return (T) Proxy.newProxyInstance(cls.getClassLoader(), cls
.getInterfaces(), this);
return target;
}
0 请登录后投票
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics