繁体   English   中英

查找方法参数类型或参数泛型类型

[英]Find method parameter type or parameter generic type

我需要查找当前类是否具有带有其类型为Integer或其通用类型为Integer的参数的方法。

我主要写了以下内容:

public static main(String[] args){
   Class<?> clazz = Class.forName("Test");
   Class<?> lookingForClass = Integer.class;
   Method[] method = clazz.getMethods();
   for (int i = 0; i < method.length; i++) {
       Type[] types = method[i].getGenericParameterTypes();
       for (int j = 0; j < types.length; j++) {
           Type type = types[j];
           Class<?> result = type.getClass();
           if (type instanceof ParameterizedType) {
               ParameterizedType pt = (ParameterizedType) type;
               Type[] fieldArgTypes = pt.getActualTypeArguments();
               result = (Class<?>) fieldArgTypes[0];
           }
           if (result instanceof lookingForClass)
               System.out.println("found it");
           }
      }
}

public static void findTowInArray(List<Integer> A) {

}

public static void findTowInArray(Integer A) {

}

public static void findTowInArray(String A) {

}

但是我在if (result instanceof lookingForClass)上收到编译错误

Incompatible conditional operand types Class<capture#6-of ?> and lookingForClass

怎么了?

如果Type已经是一个类,请不要在其上调用getClass()。

public static void main(String[] args) throws ClassNotFoundException{
    Class<?> clazz = Class.forName("Test");
    Class<?> lookingForClass = Integer.class;
    Method[] methods = clazz.getMethods();
    for (int i = 0; i < methods.length; i++) {
        Method method = methods[i];
        Type[] types = method.getGenericParameterTypes();
        for (int j = 0; j < types.length; j++) {
            Type type = types[j];
            Class<?> result = type instanceof Class<?> ? (Class<?>)type : type.getClass();
            if (type instanceof ParameterizedType) {
                ParameterizedType pt = (ParameterizedType) type;
                Type[] fieldArgTypes = pt.getActualTypeArguments();
                result = (Class<?>) fieldArgTypes[0];
            }
            if (result.equals(lookingForClass))
                System.out.println("found " + method);
            }
       }
 }

我更喜欢像

for (final Method method : methods) {
    ...
}

如果您不需要索引。

请尝试使用下面的代码,您必须比较type和Integer.class

public static void main(String[] args) throws ClassNotFoundException
    {
        Class<?> clazz = Class.forName("Test");
        Class<Integer> lookingForClass = Integer.class;
        Method[] method = clazz.getMethods();
        for (int i = 0; i < method.length; i++)
        {
            Type[] types = method[i].getParameterTypes();

            for (int j = 0; j < types.length; j++)
            {
                Type type = types[j];
                Class<?> result = type.getClass();
                if (type instanceof ParameterizedType)
                {
                    ParameterizedType pt = (ParameterizedType) type;
                    Type[] fieldArgTypes = pt.getActualTypeArguments();
                    result = (Class<?>) fieldArgTypes[0];
                }
                if (type ==  lookingForClass)
                    System.out.println("found it");
            }
        }
    }

收集以上所有评论,解决方案是:

if (lookingForClass.isAssignableFrom(result) || type.equals(lookingForClass))
    System.out.println("found it");

并不是

if (result instanceof lookingForClass)

由于Type Erasure,您无法获得类型 Java编译器将“使用类型界限或对象替换通用类型中的所有类型参数(如果类型参数不受限制)”。 例如:

public class Test<T> {
    public void setT(T t) {}
}

编译为

public class Test {
    public void setT(Object t) {}
}

因此,不可能在运行时获取类型信息。 另请参阅相关问题: 在Java中的超类的构造函数中获取扩展的泛型类的泛型类型?

暂无
暂无

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

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