什么是注解:
注解或注释,英文单词Annotation,注解Annotation是一种引用数据类型。编译之后也是生成 xxx.class文件。
语法:
[修饰符列表] @interface 注解类型名{
}
@注解类名
Deprecated 用 @Deprecated 注释的程序元素,不鼓励程序员使用这样的元素,通常是因为它很危险或存在更好的选择。
Override 表示一个方法声明打算重写超类中的另一个方法声明。
SuppressWarnings 指示应该在注释元素(以及包含在该注释元素中的所有程序元素)中取消显示指定的编译器警告。
public @interface Override{
}
介绍:
@Override 这个注解只能注解方法。
@Override 这个注解是给编译器参考的,和运行阶段没关系
凡是java中的方法带有这个注解的,编译器都会进行编译器检测,如果这个方法不是重写父类的方法,编译器报错。
代码演示:
public class AnnotationTest02 {
@Override
public String toString(){
return "toString";
}
}
什么是元注解:
用来标注 “注解类型” 的注解,称为元注解。常见的元注解有 @Target,@Retention。
关于Target 注解:
这是一个元注解,用来标注 “注解类型” 的注解,这个Target注解用来标注 “被标注的注解” 可以出现在哪些位置上。
如:
@Target(ElementType.METHOD) :表示 “被标注的注解” 只能出现在方法上。
关于Retention注解:
这是一个元注解,用来标注 “注解类型” 的注解,这个Retention注解用来标注 “被标注的注解” 最终保存到哪里。
如:
@Retention(RetentionPolicy.SOURCE) : 表示 “被标注的注解” 被保留在java源文件中。
@Retention(RetentionPolicy.CLASS) : 表示 “被标注的注解” 被保留在class文件中。
@Retention(RetentionPolicy.RUNTIME): 表示 “被标注的注解” 被保留在class文件中,并且可以被反射机制所读取到。
介绍:
被Deprecated注解标注的元素,表示已过时。这个注解主要是向其他程序员传达一个信息,告知已过时,有更好的解决方案存在。
public @interface MyAnnotation {
/**
* 我们通常在注解当中可以定义属性,以下这个是MyAnnotation的name属性。
* @return
*/
String name();
String color();
int age() default 25; //属性指定默认值。
}
如果一个注解当中有属性,那么必须给属性赋值(除非该属性使用default指定了默认值)。
语法:
@注解名(属性名=属性值,属性名=属性值)
如:
public class MyAnnotationTest {
// @MyAnnotation 报错原因:如果一个注解当中有属性,那么必须给属性赋值。
//@MyAnnotation(属性名=属性值)
//@MyAnnotation(name = "小马",color="red",age=13)
@MyAnnotation(name = "小马",color="red")
public void doSome(){
}
}
当注解中定义的属性名是value,并且只有一个属性值的话,在使用注解时,该属性名value可以省略不写。若注解定义两个属性,这两个属性必须写。
如:
public @interface MyAnnotation {
/*
* 指定一个value属性
* */
String value();
}
public class MyAnnotationTest {
@MyAnnotation(value = "xiaoma")
public void doSome(){
}
@MyAnnotation("hehe")
public void doOther(){
}
}
注解当中的属性可以是哪些类型:
byte short int long float double boolean char String Class enum类型,以及以上每一种数组类型。
当注解中的属性是一个数组时,如果数组只有一个元素,大括号可以省略。
如:
public enum Season {
SPRING,SUMMER,AUTUMN,WINTER
}
public @interface OtherAnnotation {
//年龄属性
int age();
//邮箱地址属性,支持多个
String[] email();
//季节数组 Season是一个枚举类型
Season[] seasonArray();
}
public class OtherAnnotationTest {
//数组是大括号
@OtherAnnotation(age = 25,email = {"[email protected]","[email protected]"},seasonArray = {Season.SPRING,Season.SUMMER})
public void doSome(){
}
//如果数组只有一个元素,大括号可以省略
@OtherAnnotation(age = 25,email = "[email protected]",seasonArray = Season.SPRING)
public void doOther(){
}
}
boolean isAnnotationPresent(Class<? extends Annotation> annotationClass);//如果指定类型的注释存在于此元素上,则返 回 true,否则返回 false。
<A extends Annotation> getAnnotation(Class<A> annotationClass);// 获取注解对象
String value(); //获取注解对象的属性
/*
自定义注解类
*/
//只允许该注解只能标注类,方法
@Target({ElementType.TYPE,ElementType.METHOD})
//希望这个注解可以被反射
@Retention(RetentionPolicy.RUNTIME)
public @interface MyAnnotation {
String value() default "小马";
}
/*
测试类
*/
@MyAnnotation
public class MyAnnotationTest {
// @MyAnnotation
int i;
//@MyAnnotation
public MyAnnotationTest(){}
@MyAnnotation
public void doSome(){
// @MyAnnotation
int i ;
}
}
/*
反射类
*/
public class ReflectAnnotationTest {
public static void main(String[] args) throws Exception {
//获取类
Class c=Class.forName("com.ma.annotation5.MyAnnotationTest");
// 判断类上面是否有@MyAnnotation
System.out.println(c.isAnnotationPresent(MyAnnotation.class));//true
if(c.isAnnotationPresent(MyAnnotation.class)){
// 获取注解对象
MyAnnotation myAnnotation= (MyAnnotation) c.getAnnotation(MyAnnotation.class);
System.out.println("类上面的注解对象"+myAnnotation);//类上面的注解对象@com.ma.annotation5.MyAnnotation()
//获取注解对象的属性 和调接口没区别。
String value=myAnnotation.value();
System.out.println(value); //小马
}
/*
自定义注解类
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface MyAnnotation {
String username();
String password();
}
/*
获取方法上注解的值
*/
public class MyAnnotationTest {
@MyAnnotation(username = "小马",password = "123")
public void doSome(){
}
public static void main(String[] args) throws Exception {
//获取MyAnnotationTest的doSome方法上面的注解信息。
Class c=Class.forName("com.ma.annotation6.MyAnnotationTest");
// 获取doSome()方法
Method doSomeMethod=c.getDeclaredMethod("doSome");
//判断该方法上是否存在这个注解
if(doSomeMethod.isAnnotationPresent(MyAnnotation.class)){
MyAnnotation myAnnotation= doSomeMethod.getAnnotation(MyAnnotation.class);
System.out.println(myAnnotation.username());
System.out.println(myAnnotation.password());
}
}
}
注解在开发中的作用:
需求:假设有这样一个注解,叫做:@Id,这个注解只能出现在类上,当这个类上有这个注解的时候,要求这个类必须有一个int类型的id属性,如果没有这个属性就报异常,如果有这个属性就正常执行。
代码演示:
/*
自定义注解类
*/
//表示这个注解只能出现在类上面
//这个注解@Id用来标注类,被标注的类中必须有一个int类型的id属性,没有就报异常
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface Id {
}
/*
user类
*/
@Id
public class User {
int id;
}
/*
自定义异常类
*/
public class NotHasIdPropertyException extends RuntimeException{
public NotHasIdPropertyException() {
}
public NotHasIdPropertyException(String s) {
super(s);
}
}
/*
测试类
*/
public class Test {
public static void main(String[] args) throws Exception {
Class userClass=Class.forName("com.ma.annotation7.User");
//判断类上是否存在Id注解
if(userClass.isAnnotationPresent(Id.class)){
// 当一个类中有Id注解的时候,要求类中必须存在int类型的id属性
// 如果没有int类型的id属性则报异常
// 获取类的属性
Field[]fields=userClass.getDeclaredFields();
boolean isRight=false; //给一个默认的标记
for (Field field:
fields) {
if ("id".equals(field.getName()) && "int".equals(field.getType().getSimpleName())){
//表示这个类是合法的。有@Id注解,则这个类中必须有int 类型的id
isRight=true; //表示合法
break;
}
}
if(!isRight){
throw new NotHasIdPropertyException("被Id注解标注的类中必须要有一个int类型的id属性");
}
}
}
}