Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/333.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
获取java中方法或构造函数参数的注释对象_Java_Reflection_Parameters_Annotations - Fatal编程技术网

获取java中方法或构造函数参数的注释对象

获取java中方法或构造函数参数的注释对象,java,reflection,parameters,annotations,Java,Reflection,Parameters,Annotations,我正在使用Scannotation扫描类文件,并获取所有在该类的任何元素上都有注释的类。通过使用反射,我已经能够找到方法中参数的所有注释,但是我需要这些注释的对象,以便以后可以获取其参数(或者您如何称呼它) 这是我代码的一部分,它将返回我想要的注释,但我不能使用它们 public Set<Class> getParametersAnnotatedBy(Class<? extends Annotation> annotation) { for (St

我正在使用Scannotation扫描类文件,并获取所有在该类的任何元素上都有注释的类。通过使用反射,我已经能够找到方法中参数的所有注释,但是我需要这些注释的对象,以便以后可以获取其参数(或者您如何称呼它)

这是我代码的一部分,它将返回我想要的注释,但我不能使用它们

    public Set<Class> getParametersAnnotatedBy(Class<? extends Annotation> annotation) {
        for (String s : annotated) { 
        //annotated is set containing names of annotated classes
                    clazz = Class.forName(s); 
                    for (Method m : clazz.getDeclaredMethods()) {
                        int i = 0;
                        Class[] params = m.getParameterTypes();
                        for (Annotation[] ann : m.getParameterAnnotations()) {
                            for (Annotation a : ann) {
                                if (annotation.getClass().isInstance(a.getClass())) {
                                    parameters.add(a.getClass());
                                    //here i add annotation to a set
                                }
                            }
                        }
                    }
                }
            }
但到目前为止,我还不能这样做,使用反射。。。我将非常感谢任何指示,提前谢谢。
注:是否有任何方法可以获取参数对象,如字段对应字段、方法对应方法等?

您需要使用
a.annotationType
。当您在注释上调用getClass时,您实际上得到了它的值。要获得真实的类,您需要调用
annotationType
,而不是
getClass

if (annotation.getClass() == a.annotationType()) {
            parameters.add(a.annotationType());
            // here i add annotation to a set
        }

您需要使用
a.annotationType
。当您在注释上调用getClass时,您实际上得到了它的值。要获得真实的类,您需要调用
annotationType
,而不是
getClass

if (annotation.getClass() == a.annotationType()) {
            parameters.add(a.annotationType());
            // here i add annotation to a set
        }

这有点帮助,谢谢。这实际上并没有解决我的问题,但我意识到,有一种更简单的方法来解决。现在我只需要为方法、构造函数、它的参数和注释创建某种包装器,到目前为止,我不知道这有什么帮助,谢谢。这实际上并没有解决我的问题,但我意识到,有一种更简单的方法来解决。现在我只需要为方法、构造函数、参数和注释创建某种包装器,到目前为止,我不知道如何。。。