1

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

我主要写了以下内容:

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

4 回答 4

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

如果您不需要索引。

于 2013-06-10T11:18:38.263 回答
0

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

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

编译为

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

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

于 2013-06-10T11:25:03.877 回答
0

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

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

并不是

if (result instanceof lookingForClass)
于 2013-06-10T11:00:45.887 回答
0

请尝试使用下面的代码,你必须比较类型和 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");
            }
        }
    }
于 2013-06-10T10:47:08.947 回答