@SuppressWarnings("unchecked")
@SuppressWarnings(value={"unchecked", "deprecation"})
参数 | 说明 |
---|---|
deprecation | 使用了过时的类或方法的警告 |
unchecked | 执行了未检查的转换时的警告,如使用集合时未指定泛型 |
fallthrough | 当在switch语句使用时发生case穿透 |
path | 在类路径、源文件路径中有不存在路径的警告 |
serial | 当在可序列化的类上缺少serialVersionUID定义时的警告 |
finally | 任何finally子句不能完成时的警告 |
all | 关于以上所有情况的警告 |
修饰其他注解的注解叫做元注解。
修饰范围 | 取值 |
---|---|
类或接口 | ElementType.TYPE |
字段 | ElementType.FIELD |
方法 | ElementType.METHOD |
构造方法 | ElementType.CONSTRUCTOR |
方法参数 | ElementType.PARAMETER |
局部变量 | ElementType.LOCAL_VARIABLE |
包 | ElementType.PACKAGE |
@Target注解的源代码
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.ANNOTATION_TYPE)
public @interface Target {
ElementType[] value();
}
其中 value() 是参数名,ElementType[ ]是参数类型,是一个数组,可有多个值。
即使用方式为
@Target(value=ElementType.METHOD)
或
@Target(value={ElementType.METHOD,ElementType.TYPE})
取值RetentionPolicy | 作用 |
---|---|
SOURCE | 在源文件中有效(源文件保留注解) |
CLASS | 在class文件中有效(class保留) |
RUNTIME | 在运行时有效,此注解可以被反射机制读取 |
@Retention注解的源代码
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.ANNOTATION_TYPE)
public @interface Retention {
RetentionPolicy value();
}
通常我们自定义的Annotation使用Retention修饰时,值都为RUNTIME。
示例
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target(value={ElementType.METHOD,ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface DxtAnnotation {
String value() default "";
int age() default 0;
int id() default -1; //默认设为-1
}
使用时
@DxtAnnotation(value="dxt", age=18, id=111, schools={"蓝翔", "新东方"})
public void test(){
}
使用时如果注解只有一个参数value()时,则可以直接在括号中添加值,比如:
@DxtAnnotation("dxt")
public void test(){
}
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* 为Student类自定义一个注解
* 为类指定一个数据库中的表名,存储到value中
* @author dxt
*
*/
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface Table {
String value() default "";
}
注解2(修饰类中的属性)
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* 为Student中的属性自定义一个注解
* 说明属性对应数据库表中的字段信息
* @author dxt
*
*/
@Target(value = ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface Field {
String columnName(); //列名
String type(); //类型
int length(); //长度
}
Student类
/**
* student类
* @author dxt
*
*/
@Table(value = "tb_student")
public class Student {
@Field(columnName="id", type="int", length=10)
private int id;
@Field(columnName="sname", type="varchar", length=10)
private String name;
@Field(columnName="age", type="int", length=3)
private int age;
public Student(){
}
public Student(int id, String name, int age){
super();
this.id = id;
this.name = name;
this.age = age;
}
public void setId(int id){
this.id = id;
}
public int getId(){
return this.id;
}
public void setName(String name){
this.name = name;
}
public String getName(){
return this.name;
}
public void setAge(int age){
this.age = age;
}
public int getAge(){
return this.age;
}
}
反射获取注解
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
/**
* 使用反射机制读取注解的信息,模拟处理竹节信息的流程
* @author dxt
*
*/
public class Demo03 {
public static void main(String[] args){
try {
Class c = Class.forName("dxt.annotation.Student");
//获取类的所有注解
Annotation[] annos = c.getAnnotations();
for(Annotation a : annos){
System.out.println(a);
}
//依据注解类,直接获取指定注解
Table table = (Table)c.getAnnotation(Table.class);
System.out.println(table.value());
//获取类的属性的注解
Field f = c.getDeclaredField("name");
//教训:怎么可以把类名设置为 Field呢
dxt.annotation.Field fa = f.getAnnotation(dxt.annotation.Field.class);
System.out.println(fa.columnName()+"--"+fa.type()+"--"+fa.length());
} catch (Exception e) {
e.printStackTrace();
}
}
}