使用Reflection获取构造函数(或方法)参数注释

时间:2012-03-09 14:04:45

标签: java reflection constructor annotations

如何从构造函数参数中的参数获取注释。我试过......

Class<?>[] params = constructor.getParameterTypes();
    if(params.length > 0) {
        paramValues = new Object[params.length];
        for(int i=0; i<params.length; i++) {                        
        Annotation[] constructorAnnotations = params[i].getAnnotations(); //This does not work.
        }   
    }

1 个答案:

答案 0 :(得分:7)

constructor.getParameterAnnotations()返回每个参数的注释。例如,第二个参数的注释是:

Annotation[] annotations = constructor.getParameterAnnotations()[1]