我尝试了这个代码,它返回实际的泛型类,所以似乎可以检索类型信息.但是这只适用于方法1和2.方法3似乎没有返回列表类型的字符串,因为海报假定因此失败.
public class Main { /** * @param args the command line arguments */ public static void main(String[] args) { try{ Method m = Main.class.getDeclaredMethod("method1", new Class[]{}); instanceOf(m, List.class, String.class); m = Main.class.getDeclaredMethod("method2", new Class[]{}); instanceOf(m, List.class, String.class); m = Main.class.getDeclaredMethod("method3", new Class[]{}); instanceOf(m, List.class, String.class); m = Main.class.getDeclaredMethod("method4", new Class[]{}); instanceOf(m, StringList.class); }catch(Exception e){ System.err.println(e.toString()); } } public static boolean instanceOf ( Method m, Class> returnedBaseClass, Class> ... genericParameters) { System.out.println("Testing method: " + m.getDeclaringClass().getName()+"."+ m.getName()); boolean instanceOf = false; instanceOf = returnedBaseClass.isAssignableFrom(m.getReturnType()); System.out.println("\tReturn type test succesfull: " + instanceOf + " (expected '"+returnedBaseClass.getName()+"' found '"+m.getReturnType().getName()+"')"); System.out.print("\tNumber of generic parameters matches: "); Type t = m.getGenericReturnType(); if(t instanceof ParameterizedType){ ParameterizedType pt = (ParameterizedType)t; Type[] actualGenericParameters = pt.getActualTypeArguments(); instanceOf = instanceOf && actualGenericParameters.length == genericParameters.length; System.out.println("" + instanceOf + " (expected "+ genericParameters.length +", found " + actualGenericParameters.length+")"); for (int i = 0; instanceOf && i < genericParameters.length; i++) { if (actualGenericParameters[i] instanceof Class) { instanceOf = instanceOf && genericParameters[i].isAssignableFrom( (Class) actualGenericParameters[i]); System.out.println("\tGeneric parameter no. " + (i+1) + " matches: " + instanceOf + " (expected '"+genericParameters[i].getName()+"' found '"+((Class) actualGenericParameters[i]).getName()+"')"); } else { instanceOf = false; System.out.println("\tFailure generic parameter is not a class"); } } } else { System.out.println("" + true + " 0 parameters"); } return instanceOf; } public Listmethod1() { return null; } public ArrayList method2() { return new ArrayList (); } public StringList method3() { return null; } public T method4() { return null; }
这输出:
Testing method: javaapplication2.Main.method1 Return type test succesfull: true (expected 'java.util.List' found 'java.util.List') Number of generic parameters matches: true (expected 1, found 1) Generic parameter no. 1 matches: true (expected 'java.lang.String' found 'java.lang.String') Testing method: javaapplication2.Main.method2 Return type test succesfull: true (expected 'java.util.List' found 'java.util.ArrayList') Number of generic parameters matches: true (expected 1, found 1) Generic parameter no. 1 matches: true (expected 'java.lang.String' found 'java.lang.String') Testing method: javaapplication2.Main.method3 Return type test succesfull: false (expected 'java.util.List' found 'com.sun.org.apache.xerces.internal.xs.StringList') Number of generic parameters matches: true 0 parameters Testing method: javaapplication2.Main.method4 Return type test succesfull: true (expected 'com.sun.org.apache.xerces.internal.xs.StringList' found 'com.sun.org.apache.xerces.internal.xs.StringList') Number of generic parameters matches: true 0 parameters