1、注解,或者叫做注释,英文单词是Annotation
2、注解Annotation是一种引用数据类型。编译之后也是生成xxx.class文件
3、怎么定义注解呢?语法格式?
[修饰符列表] @interface 注解类型名{
}
4、 关于JDK lang包下的Override注解,标识性注解,给编译器做参考的。
编译器看到方法上有这个注解的时候,编译器会自动检查该方法是否重写了父类的方法,如果没有重写,编译器报错。和运行期无关。
public class AnnotationTest02 {
// @Override注解只能注解方法
// @Override这个注解是给编译器参考的,和运行阶段没有关系
//凡是java中的方法带有这个注解的,编译器都会进行编译检查,如果这个方法不是重写父类的方法
//编译器报错
@Override
public String toString() {
return "toString";
}
}
5、@Deprecated,表示这个类已过时,向程序员告知已过时,观察其源码。
@Retention(RetentionPolicy.RUNTIME)
@Target(value={CONSTRUCTOR, FIELD, LOCAL_VARIABLE, METHOD, PACKAGE, MODULE, PARAMETER, TYPE})
public @interface Deprecated {
/**
* Returns the version in which the annotated element became deprecated.
* The version string is in the same format and namespace as the value of
* the {@code @since} javadoc tag. The default value is the empty
* string.
*
* @return the version string
* @since 9
*/
String since() default "";
/**
* Indicates whether the annotated element is subject to removal in a
* future version. The default value is {@code false}.
*
* @return whether the element is subject to removal
* @since 9
*/
boolean forRemoval() default false;
}
@Deprecated的元注解有:
@Retention(RetentionPolicy.RUNTIME)
@Target(value={CONSTRUCTOR, FIELD, LOCAL_VARIABLE, METHOD, PACKAGE, MODULE, PARAMETER, TYPE})
6、分析这这两个元注解
//只允许该注解可以标注类、方法、属性 @Target(value = {ElementType.TYPE, ElementType.METHOD,ElementType.FIELD}) //希望这个注解可以被反射 @Retention(RetentionPolicy.RUNTIME)//RUNTIME才能被反射己之所读到。
@Target(value = {ElementType.TYPE, ElementType.METHOD,ElementType.FIELD})
//希望这个注解可以被反射
@Retention(RetentionPolicy.RUNTIME)//RUNTIME才能被反射己之所读到。
public @interface MyAnnotation {
String value() default "西安雁塔区";
}
测试代码如下:
@MyAnnotation
public class MyAnnotationTest {
@MyAnnotation
int i;
}
注解@Retention(RetentionPolicy.RUNTIME)//RUNTIME才能被反射己之所读到。,通过反射测试定义的注解。
public class ReflectAnnotationTest {
public static void main(String[] args) throws Exception{
//获取这个类
Class c = Class.forName("com.lmgzxh.javase.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);
//获取注解对象的属性怎么办?和调接口没区别
System.out.println(myAnnotation.value());
}
//判断String类上是否有这个注解
Class c2 = Class.forName("java.lang.String");
System.out.println(c2.isAnnotationPresent(MyAnnotation.class));//false
}
}
7、注解的作用,规范代码
@Target(ElementType.TYPE)
//这个注解Id用来标注类,被标注的类必须有一个int类型的id属性,没有就报异常
//该注解可以被反射机制读取到
@Retention(RetentionPolicy.RUNTIME)
public @interface Id {
}
定义User类用Id注解
@Id
public class User {
int id;
String name;
}
定义一个异常
public class NotHasIdPropertyException extends RuntimeException{
public NotHasIdPropertyException(){
}
public NotHasIdPropertyException(String s){
super(s);
}
}
测试功能,,被标注的类必须有一个int类型的id属性,没有就报异常
public class Test {
public static void main(String[] args) throws Exception{
//获取类
Class userClass = Class.forName("com.lmgzxh.javase.annotation7.User");
boolean isOk = false;//给一个默认的标记
//判断类上是否存在Id注解
if(userClass.isAnnotationPresent(Id.class)){
//当一个类上面有@Id注解的时候,要求类中必须存在int类型的id属性
//如果没有int类型的id属性,报异常
Field[] fields = userClass.getDeclaredFields();
for (Field fi:fields
) {
if("id".equals(fi.getName())&& "int".equals(fi.getType().getSimpleName())){
//表示这个类是合法的类,有@Id注解,则这个类中必须有int类型的id
isOk = true;
break;
}
}
if(!isOk){
throw new NotHasIdPropertyException("被Id注解标注的类中必须有一个int id属性");
}
}
}
}