在这上面看了个反响,感觉不错 http://lsy.javaeye.com/blog/220264 但有个疑问,请大家帮忙看看public String[] getPayments(String[] payments, List<Product> products) {
return payments;
}Method getPayments = BaseOrder.class.getMethod("getPayments",
new Class[] { String[].class, List.class });
types = getPayments.getGenericParameterTypes();
assertTrue("The first parameter of this method is GenericArrayType.",
types[0] instanceof GenericArrayType);
GenericArrayType gType = (GenericArrayType) types[0];
assertEquals("The GenericArrayType's component is String.",
String.class, gType.getGenericComponentType());
}
这段很有意思,总感觉把 type[0] 也就是 String[] ,能够assert成 GenericArrayType 有点说不过去例如:
如果修改一下 getPayments 为
public String[] getPayments(String payments, List<Product>),
相应的修改
Method getPayments = BaseOrder.class.getMethod("getPayments",
new Class[] { String.class, List.class});那么types = getPayments.getGenericParameterTypes();
types[0] 就不是 ParameterizeType 了,而是String.class
再例如:如果修改一下 getPayments 为
public String[] getPayments(String[] payments),
相应的修改
Method getPayments = BaseOrder.class.getMethod("getPayments",
new Class[] { String[].class});那么types = getPayments.getGenericParameterTypes();
types[0] 就不是GenericArrayType了,而是String[].class
疑问 :
这怎么解释呢? 怎么偏偏只有
public String[] getPayments(String[] payments, List<Product> products) 的 types[0] 是 GenericArrayType
return payments;
}Method getPayments = BaseOrder.class.getMethod("getPayments",
new Class[] { String[].class, List.class });
types = getPayments.getGenericParameterTypes();
assertTrue("The first parameter of this method is GenericArrayType.",
types[0] instanceof GenericArrayType);
GenericArrayType gType = (GenericArrayType) types[0];
assertEquals("The GenericArrayType's component is String.",
String.class, gType.getGenericComponentType());
}
这段很有意思,总感觉把 type[0] 也就是 String[] ,能够assert成 GenericArrayType 有点说不过去例如:
如果修改一下 getPayments 为
public String[] getPayments(String payments, List<Product>),
相应的修改
Method getPayments = BaseOrder.class.getMethod("getPayments",
new Class[] { String.class, List.class});那么types = getPayments.getGenericParameterTypes();
types[0] 就不是 ParameterizeType 了,而是String.class
再例如:如果修改一下 getPayments 为
public String[] getPayments(String[] payments),
相应的修改
Method getPayments = BaseOrder.class.getMethod("getPayments",
new Class[] { String[].class});那么types = getPayments.getGenericParameterTypes();
types[0] 就不是GenericArrayType了,而是String[].class
疑问 :
这怎么解释呢? 怎么偏偏只有
public String[] getPayments(String[] payments, List<Product> products) 的 types[0] 是 GenericArrayType
If a formal parameter type is a parameterized type, the Type object returned for it must accurately reflect the actual type parameters used in the source code.
也就是说,只要参数里有一个是参数化类型的,那这个方法就会提供精确的类型信息,反之则不会。
如果把代码改成
public String[] getPayments(String[] payments, List products) {
return payments;
}
那么types[0] instanceof GenericArrayType就返回false了
如果一个形参是参数化类型,该形参返回的类型是在方法体内(in source code)使用的实际类型。这个payments 是一个 String[] 怎么能说他是 parameterized type呢?不过按照xstom19的代码,确实返回的是String[].class,问题清晰了不少 * Returns an array of <tt>Type</tt> objects that represent the formal
* parameter types, in declaration order, of the method represented by
* this <tt>Method</tt> object. Returns an array of length 0 if the
* underlying method takes no parameters.
*
* <p>If a formal parameter type is a parameterized type,
* the <tt>Type</tt> object returned for it must accurately reflect
* the actual type parameters used in the source code.
*
* <p>If a formal parameter type is a type variable or a parameterized
* type, it is created. Otherwise, it is resolved.
*
* @return an array of Types that represent the formal
* parameter types of the underlying method, in declaration order
* @throws GenericSignatureFormatError
* if the generic method signature does not conform to the format
* specified in the Java Virtual Machine Specification, 3rd edition
* @throws TypeNotPresentException if any of the parameter
* types of the underlying method refers to a non-existent type
* declaration
* @throws MalformedParameterizedTypeException if any of
* the underlying method's parameter types refer to a parameterized
* type that cannot be instantiated for any reason
2. public M getManufactory(){
3. return manufactory;
4. }
5. }
GenericArrayType
如果泛型参数是一个泛型的数组,那么泛型Type就是GenericArrayType,它的getGenericComponentType()将返回被JVM编译后实际的数组对象getGenericParameterTypes()首先判断该方法的参数中是否有泛型信息,有那么返回泛型Type的数组,没有那么直接按照 Class的数组返回;而getParameterTypes()就直接按照Class的数组返回。非常相似吧,其原因就是这些成对的方法都有一个共同点就是判断是否有泛型信息
这句话错滴,String[]应该是GenericArrayType
的确是6楼所说的
只要参数里有一个是参数化类型的,那这个方法就会提供精确的类型信息,反之则不会。
写了个简单易懂的Testpackage Test;
import java.util.*;
import java.lang.reflect.*;
class Animal
{
}
public class Test2
{
public void f(String[] s,List<String> list)
{
}
public void f(String[] s)
{
}
public String g(Type type)
{
if(type instanceof ParameterizedType )
{
return "ParameterizedType";
}
if(type instanceof GenericArrayType)
{
return "GenericArrayType";
}
if(type instanceof Class)
{
return Class.class.getName();
}
return "type";
}
public static void main(String[] args)throws Exception
{
Test2 t2=new Test2();
Method m=Test2.class.getMethod("f",new Class[]{String[].class,List.class});
Type[] types=m.getGenericParameterTypes();
System.out.println(t2.g(types[0]));
Method m1=Test2.class.getMethod("f",new Class[]{String[].class});
Type[] types1=m1.getGenericParameterTypes();
System.out.println(t2.g(types1[0]));
}
}