java注解机制

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属性");
            }

        }

    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值