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

时间:2013-06-10 10:30:07

标签: java generics

我需要查找我当前的类是否具有参数的方法,该参数的类型是其泛型类型的Integer是Integer。

我在main中写了以下内容:

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

有什么问题?

4 个答案:

答案 0 :(得分:1)

如果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) {
    ...
}

如果您不需要索引。

答案 1 :(得分:0)

请尝试使用以下代码,您必须比较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");
            }
        }
    }

答案 2 :(得分:0)

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

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

而不是

if (result instanceof lookingForClass)

答案 3 :(得分:0)

由于Type Erasure,您无法获得该类型。如果类型参数是无界的,Java编译器将“将泛型类型中的所有类型参数替换为其边界或对象”。 例如:

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

编译到

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

因此无法在运行时获取类型信息。 另见相关问题: Get the generic type of an extending generic class in the constructor of the superclass in Java?