简体   繁体   English

查找方法是否使用反射重载

[英]Find if method is overloaded using Reflection

There is similar question regarding this but in the answers the parameter types are used. 关于此也有类似的问题 ,但在答案中使用了参数类型。

My question is that having a class what should be done to find out whether the method returned using reflection is overloaded or not? 我的问题是拥有一个类,该怎么做才能找出使用反射返回的方法是否过载?

Possible ways can be: 1) Looping to find the same method again 可能的方法可能是:1)循环再次查找相同的方法

    List<Method> overloaded = new ArrayList<Method>();
    Method[] declaredMethods = A.class.getDeclaredMethods();

    for(int i = 0; i < declaredMethods.length; i++){
        for(int j = (i + 1); j < declaredMethods.length; j++){
            if(declaredMethods[j].getName().equals(declaredMethods[i].getName())){
                overloaded.add(declaredMethods[i]);
            }
        }
    }

    System.out.println(overloaded);

Is there some way to find out overloaded methods which has a better algorithmic complexity than this? 有什么方法可以找出算法比这更好的重载方法吗? Does some library provide the same functionality? 某些库是否提供相同的功能?

You could use a Set . 您可以使用Set if the add() operation returns false the method name is already in the set. 如果add()操作返回false则方法名称已在集合中。
Java Reflection has no method to determine whether a method is overloaded. Java Reflection没有确定方法是否重载的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM