我有自定义的注释类其注释字段调用方法
@Retention(RetentionPolicy.RUNTIME)
public @interface Range {
int min();
int max();
}
我里面有很多场的另一个类,我想使用的注释,以检查该字段的值是否是在范围。
public class AsdFile {
@Range(min = 0, max = 59)
private int sec;
@Range(min = 0, max = 59)
private int minsec;
@Range(min = 0, max = 24)
private int hour;
@Range(min = 1, max = 31)
private int day;
,我希望写在这个类中的方法控制这样的
public void validateRange(int input)
{
Field[] fields = this.getClass().getDeclaredFields();
for(Field field: fields)
{
if (field.isAnnotationPresent(Range.class))
{
Range range = field.getAnnotation(Range.class);
int max = range.max();
int min = range.min();
if ((input <= max) && (input >= min))
{
System.out.println("Value in range");
}
else
{
System.out.println("Value is not in range");
}
}
}
}
我想控制的具体注释的价值,但我不知道该怎么做的范围是
看来你的代码没问题。什么是错误? – NFE 2014-11-23 13:09:43
它检查范围那样 – user2957741 2014-11-23 13:11:54
秒所有注释的字段:34 最大:59分钟:0 价值范围 最大:59分钟:0 价值范围 最大:24分钟:0 不在范围 最大:31min: 1 不在范围内 max:12min:1 不在范围内,但我想控制只是第二个 – user2957741 2014-11-23 13:12:48