2010-01-20 146 views
1

假设的类型我有一个函数如何找到注释功能参数

public int doSomething(@QueryParam("id") String name, int x){ .... } 

我怎样才能找到注解参数“名”的类型。我有一个处理函数doSomething的java.lang.reflect.Method实例,并使用函数getParameterAnnotations(),我可以获得注释@QueryParam,但无法访问应用它的参数。我该怎么做呢 ?

回答

2
void doSomething(@WebParam(name="paramName") int param) { } 

Method method = Test.class.getDeclaredMethod("doSomething", int.class); 
Annotation[][] annotations = method.getParameterAnnotations(); 

for (int i = 0; i < annotations.length; i ++) { 
    for (Annotation annotation : annotations[i]) { 
     System.out.println(annotation); 
    } 
} 

此输出:

@javax.jws.WebParam(targetNamespace=, partName=, name=paramName, 
    header=false, mode=IN) 

为了解释 - 阵列是二维的,因为首先必须的参数的阵列,然后为每个参数你有注释的阵列。

您可以验证你期望与instanceof(或Class.isAssignableFrom(..)注释的类型。