2014-11-04 21 views
-1

所以我试图为一个参数类创建一个代理。如何创建一个作为参数给出的类的代理

public static Object lookup(Class<?> cl, 
          CommunicationModule communicationModule) { 
    InvocationHandler handler = new InvocationHandler() { ... }; 
    cl proxy = (cl) Proxy.newProxyInstance(cl.class.getClassLoader(), 
              new Class[] { cl.class }, handler); 
    return proxy; 
} 

但是这不起作用的原因。这有什么问题?

+0

有什么错误? – 2014-11-04 09:30:04

+0

“cl不能解析为类型” – user2443088 2014-11-04 09:33:42

回答

1

cl是参数的名称,而不是类型。

我猜.newProxyInstance()方法具有Proxy(或Object)返回类型,所以你只需要做:

Object proxy = Proxy.newProxyInstance(cl.class.getClassLoader(), 
            new Class[] { cl }, 
            handler); 
+1

好吧,这种回答我的问题。它工作时,我没有 对象代理= Proxy.newProxyInstance(cl.class.getClassLoader(), 新类[] {CL}, 处理程序); – user2443088 2014-11-04 09:45:55

+0

这是可能的,是的。正如我告诉过你的,你需要检查'newProxyInstance'方法的返回类型,它似乎是'Object'。 :) – 2014-11-04 09:58:24

相关问题