Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/mercurial/2.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_Generics_Annotations - Fatal编程技术网

Java注释值

Java注释值,java,generics,annotations,Java,Generics,Annotations,我有几个注释,其中一个注释带有一些值: @Retention(RUNTIME) @Target(PARAMETER) public @interface Size { int min() default 1; int max() default Integer.MAX_VALUE; } 现在我有一些代码需要动态读取注释的所有此类值,并获取字段的类型和值。所以我不想对我得到的注释类型进行检查,而是更动态地进行检查。我注意到注释有点像一个带有一些方法的接口,所以我想我可能可以这

我有几个注释,其中一个注释带有一些值:

@Retention(RUNTIME)
@Target(PARAMETER)
public @interface Size {
     int min() default 1;
     int max() default Integer.MAX_VALUE;
}
现在我有一些代码需要动态读取注释的所有此类值,并获取字段的类型和值。所以我不想对我得到的注释类型进行检查,而是更动态地进行检查。我注意到注释有点像一个带有一些方法的接口,所以我想我可能可以这样做:

Method[] methods = annotation.annotationType().getMethods();

Class<?>[] constructorParams = new Class<?>[methods.length];
Object[] values = new Object[methods.length];
int i = 0;
for(Method m : methods) {
    constructorParams[i] = m.getReturnType();
    values[i++] = m.invoke(annotation);
}

Constructor<? extends CustomValidator> constructor = clazz.getConstructor(constructorParams);
return constructor.newInstance(values);
Method[]methods=annotation.annotationType().getMethods();
Class[]constructorParams=新类[methods.length];
Object[]value=新对象[methods.length];
int i=0;
用于(方法m:方法){
constructorParams[i]=m.getReturnType();
值[i++]=m.invoke(注释);
}

构造函数ok,给出上述代码,更改此语句:

Method[] methods = annotation.annotationType().getMethods();
我同意这一说法

Method[] methods = annotation.annotationType().getDeclaredMethods();

修复它

你试过了吗?它起作用了吗?我看到的唯一问题是,您假设每个方法都是零参数方法。@interface方法不能有参数。