如何在Java中找到重载方法? [英] How to find an overloaded method in Java?

查看:176
本文介绍了如何在Java中找到重载方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

撰写类似

时的内容

  doit(43,44,hello); 

编译器知道要调用哪个重载方法。当我想通过反射做同样的事情时,我需要找出自己,方法是

  doit(整数,双,CharSequence ......); 

并通过类似



$ b <$ p的方式获取$ p> Class [] types = {Integer.class,double.class,CharSequence [] .class};
declaringClass.getDeclaredMethod(doit,types);

我想知道是否已经允许我写一些东西

 方法m = getMethod(declaringClass,doit,43,44,hello); 

我想知道是否有人这样做了,因为 JLS 在这方面有点复杂。






实际上,像 。 com / javase / specs / jls / se7 / html / jls-15.html#jls-15.12.2.2rel =noreferrer>阶段1 编译器只接受匹配而不进行装箱和拆箱的方法。当从上面调用我假设的 getMethod 时,原语及其包装器之间的区别已经丢失(因为通过varargs传递参数时的自动装箱)。这个问题似乎没有解决方案,所以让我们忽略它。



如答案所示, BeanUtils.invokeMethod 来得很近。它应该找到最佳匹配,无论它意味着什么。查看 MethodUtils .getMatchingAccessibleMethod 显示




  • it对varargs一无所知

  • 这是非确定性的



所以我正在寻找更好的东西。

解决方案

MethodHandle 是一种使用签名获取重载方法的新方法(java 7) :



示例:

 静态类A {
public String get(){
returnA;
}
}

静态类B扩展A {
public String get(){
returnB;
}
}

public static void main(String [] args)throws Throwable {

MethodHandles.Lookup lookup = MethodHandles.lookup();
MethodType mt = MethodType.methodType(String.class);
MethodHandle mh = lookup.findVirtual(A.class,get,mt);;

System.out.println(mh.invoke(new B()));
}

输出:

  B 


When writing something like

doit(43, 44, "hello");

the compiler knows which overloaded method is to be called. When I want to do the same via reflection, I need to find out myself, that the method is

doit(Integer, double, CharSequence...);

and obtain it via something like

Class[] types = {Integer.class, double.class, CharSequence[].class};
declaringClass.getDeclaredMethod("doit", types);

I wonder if there's already something allowing me to write just

Method m = getMethod(declaringClass, "doit", 43, 44, "hello");

I wonder if somebody did this already, as the JLS is a bit complicated in this respect.


Actually, behaving exactly like the compiler is impossible as in Phase 1 the compiler accepts only methods matching without boxing and unboxing. When calling my hypothetical getMethod from above, the distinction between primitives and their wrappers is already lost (because of autoboxing when passing arguments via varargs). This problem seems to have no solution, so let's ignore it.

As suggested in an answer, BeanUtils.invokeMethod comes close. It's supposed to find the best match, whatever it means. Looking at MethodUtils.getMatchingAccessibleMethod shows that

  • it knows nothing about varargs
  • it's non-deterministic

so I'm looking for something better.

解决方案

The MethodHandle is a new way to get a overloaded method using a signature (java 7):

Example:

static class A {
    public String get() {
        return "A";
    }
}

static class B extends A {
    public String get() {
        return "B";
    }
}

public static void main(String[] args) throws Throwable {

    MethodHandles.Lookup lookup = MethodHandles.lookup();
    MethodType mt = MethodType.methodType(String.class);
    MethodHandle mh = lookup.findVirtual(A.class, "get", mt);;

    System.out.println(mh.invoke(new B()));
}

Outputs:

B

这篇关于如何在Java中找到重载方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆