配置参数由注解类型定义
配置参数可以包括:
配置参数必须是常量
缺少某个配置参数将使用默认值
如果只写常量,相当于省略了value参数。(如果参数名称是value,可以省略参数名称)
如果只写注解,相当于全部使用默认值
public class A{
@Check(min=0,max=100,value=20)
private int m;
@Check(value=20)
private int n;
@Check(20) // 相当于 @Check(value=20)
private int x;
@Check
private int y;
}
public @interface Report{
int type() default 0;
String level() default "info";
String value() default "";
}
//只能用于方法的@Report注解
@Target( ElementType.METHOD)
public @interface Report{
int type() default 0;
String level() default "info";
String value() default "";
}
//用于方法和字段的@Report注解
@Target({
ElementType.METHOD,
ElementType.FIELD
})
public @interface Report{
int type() default 0;
String level() default "info";
String value() default "";
}
注意:@Retention不存在,则该注解默认为CLASS。通常自定义的注解都是RUNTIME
@Repeatable
@Target( ElementType.TYPE)
public @interface Report{
int type() default 0;
String level() default "info";
String value() default "";
}
@Report(type=1,level="debug")
@Report(type=2,level="warning")
public class hello(){
}
@Inherited
@Target( ElementType.TYPE)
public @interface Report{
int type() default 0;
String level() default "info";
String value() default "";
}
@Report(type=1)
public class Person(){
}
public class Student extends Person(){
}
用@interface定义注解
用元注解(meta annotation)配置注解
定义注解参数和默认值
@Target( ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface Report{
int type() default 0;
String level() default "info";
String value() default "";
}
Clss cls = Person.class;
//判断@Report是否存在
cls.isAnnotationPresent(Report.class);
//使用getAnnotation(Class)来获得一个注解,如果不存在则返回null
Report report = cls.getAnnotation("Report.class");
int type = report.type();
String level = report.level();
//判断一个annotation是否存在
Clss cls = Person.class;
if(cls.isAnnotationPresent(Report.class)){
Report report = cls.getAnnotation(Report.class);
if(report != null){
...
}
}
public String hello(@NotNull @Range(max=6) String name, @NotNull String prefix){
...
}
//由于读取方法参数的注解,
//方法参数名作为一个数组,而每个方法参数又可以定义多个注解,
//所以一次获得方法的所有注解,就有必须用二维数据来接收.
/*
annos 的值为:
{
{@NotNull, @Range(max=5)},
{@NotNull}
}
*/
Method m = ...
Annotation[][] annos = m.getParameterAnnotation();
Annotation[] annosOfName = annos[0]; //name参数的Annotation
for(Annotation anno : annosOfName){
if(anno instanceof Range){
Range r = (Range) anno;
...
}
}
//NotNull.java
@Retention(RetentionPolicy.RUNTIME) //不要漏写,否则运行时不起作用
@Target(ElementType.FIELD)
public @interface NotNull{
}
//Range.java
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface Range{
int min() default 1;
int max() default 100;
}
//Person.java
public class Person{
@NotNull
private String name;
@Range(max = 22)
private int age;
public Person(String name ,int age){
this.name = name;
this.age = age;
}
public static void main(String args[]) throws Exception{
Person p1 = new Person("xiao ming",25);
Person p2 = new Person(null,10);
checkPerson(p1);
checkPerson(p2);
}
static void checkPerson(Person p) throws Exception{
System.out.prinln("check" + p + "...");
Class cls = Person.class;
for (Field f : cls.getFields()){
checkField(f, p);
}
}
static void checkField(Field f, Person p) throws Exception{
if(f.isAnnotationPresent(NotNull.class)){
Object o = f.get(p);
if(o == null){
System.out.prinln("Error:field"+f.getName+"is null");
}
}
if(f.isAnnotationPresent(Range.class)){
Range range = f.getAnnotation(Range.class);
int n = (Integer) f.get(p);
if(n<range.min() || n > range.max()) {
System.out.prinln("Error:field"+f.getName+"is out of range.");
}
}
}
}
the end.