Java - 获取接口中方法的签名,其代理实现也是如此
我正在寻找一种方法来提取 Java 中签名的本质。原因是我想使用签名作为 java.lang.reflect.Proxies 的 Map 中的唯一键。
使用这段代码:
public interface MyInterface {
public int compute(String s);
}
...
public static void main (String... args) throws Exception {
InvocationHandler handler = ...;
MyInterface i = (MyInterface) Proxy.newProxyInstance(
Beans.class.getClassLoader(),
new Class<?>[] { MyInterface.class },
handler);
Method m1 = MyInterface.class.getMethod("compute", String.class);
Method m2 = i.getClass().getMethod("compute", String.class);
System.out.printf("%b%f", m1.equals(m2));
}
结果显然是错误的。
这段代码不是我将使用的代码,因为我需要它在 InvocableHandler 中,但我想知道是否关于代理实现和接口,获取 method.getName()
和 method.getParameterTypes()
就足够了,还是我也应该使用 method.getTypeParameters()
和 method.getParameterAnnotations()
?
简而言之:如何获得与接口及其 java.lang.reflect.Proxy 实现相同的方法签名?
I'm looking for a way to extract the essence of a signature in Java. The reason is that I want to use the signature as a unique key in a Map for my java.lang.reflect.Proxies.
With this code:
public interface MyInterface {
public int compute(String s);
}
...
public static void main (String... args) throws Exception {
InvocationHandler handler = ...;
MyInterface i = (MyInterface) Proxy.newProxyInstance(
Beans.class.getClassLoader(),
new Class<?>[] { MyInterface.class },
handler);
Method m1 = MyInterface.class.getMethod("compute", String.class);
Method m2 = i.getClass().getMethod("compute", String.class);
System.out.printf("%b%f", m1.equals(m2));
}
The result is obviously false.
This code is not the code I'll use, because I need it inside the InvocationHandler, but I'm wondering if regarding the Proxy implementations and the interface, getting method.getName()
and method.getParameterTypes()
is enough or I should use method.getTypeParameters()
and method.getParameterAnnotations()
as well?
In short: how to get a method signature that is the same for an interface and its java.lang.reflect.Proxy implementations?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我认为您希望将
Method
传递到InvocationHandler
中。I think you want the
Method
passed in theInvocationHandler
.使用 Method.toGenericString 的结果作为键怎么样?据我所知,它返回的字符串包含签名的所有详细信息。
另一件可能有用的事情是: Method m2 = i.getClass().getMethod("compute", String.class).getDeclaringClass().getMethod("compute", String.class); 。这可能会导致
m1.equals(m2)
返回 true。What about using the result of
Method.toGenericString
as the key? The string it returns includes all details of the signature, as far as I can tell.Another thing that might be useful is:
Method m2 = i.getClass().getMethod("compute", String.class).getDeclaringClass().getMethod("compute", String.class);
. That might result inm1.equals(m2)
returning true.我只是在执行此操作时遇到了代码错误,我获取了名称、genericReturnType 和 genericParameterTypes,并且在使用代理时遇到了问题,因为我丢失了通用信息。
我改用 name、returnType 和parameterTypes,它工作得很好...
我也考虑过使用 declaringClass,但我删除了它,并且不记得我是如何做到这一点的...
I just had a bug with code doing this, I took name, genericReturnType and genericParameterTypes, and I had problems when working with proxies because I lost the generic information.
I switched to use name, returnType and parameterTypes and it works fine...
I also considered using declaringClass but I removed it and don't remember exactly how I did this...