Java基础知识——注解

学习代码

package com.kevinlsui.base;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
 * 元注解:负责注解其它注解的注解
 * @Target : Annotation所修饰的范围
 *              ElementType.{CONSTRUCTOR,构造器(constructor)} 
 *              ElementType.{FIELD,字段(field)} 
 *              ElementType.{LOCAL_VARIABLE,局部变量(local_variable)} 
 *              ElementType.{METHOD,方法(method)} 
 *              ElementType.{PACKAGE,包(package)} 
 *              ElementType.{PARAMETER,参数(parameter)} 
 *              ElementType.{TYPE,包括类、接口、注解、枚举(type)} 
 * @Retention : 用于标明生存时间
 *                 RetentionPolicy.RUNTIME (运行时保留)
 *                 RetentionPolicy.SOURCE (仅仅源码保留)
 *                 RetentionPolicy.CLASS (编译后class文件中保留)
 * @Documented : 生成javadoc时会包含注解
 * @Inherited : 允许子类继承 
 */

@Target({ElementType.TYPE,ElementType.FIELD})//使用大括号,定义多个范围
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
public @interface MyAnnotation {
    //返回值类型:基本数据类型(int/long/float/double/boolean/char/short/byte)、String、class、enum、annotation、以上类型的数组
    //修饰符:public/(默认)
    String name();
    String alias() default ""; 
}

package com.kevinlsui.base;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
 * 元注解:负责注解其它注解的注解
 * @Target : Annotation所修饰的范围
 *              ElementType.{CONSTRUCTOR,构造器(constructor)} 
 *              ElementType.{FIELD,字段(field)} 
 *              ElementType.{LOCAL_VARIABLE,局部变量(local_variable)} 
 *              ElementType.{METHOD,方法(method)} 
 *              ElementType.{PACKAGE,包(package)} 
 *              ElementType.{PARAMETER,参数(parameter)} 
 *              ElementType.{TYPE,包括类、接口、注解、枚举(type)} 
 * @Retention : 用于标明生存时间
 *                 RetentionPolicy.RUNTIME (运行时保留)
 *                 RetentionPolicy.SOURCE (仅仅源码保留)
 *                 RetentionPolicy.CLASS (编译后class文件中保留)
 * @Documented : 生成javadoc时会包含注解
 * @Inherited : 允许子类继承 
 */

@Target({ElementType.TYPE,ElementType.FIELD,ElementType.METHOD})
@Retention(RetentionPolicy.CLASS)
@Documented
public @interface MyAnnotation2 {
    //返回值类型:基本数据类型(int/long/float/double/boolean/char/short/byte)、String、class、enum、annotation、以上类型的数组
    //修饰符:public/(默认)

    String value() default "222"; 
}

package com.kevinlsui.base;

@MyAnnotation(name="people")
@MyAnnotation2("mya2")
public class People {
    public void say(){

    }
    public void haha(){

    }
}

package com.kevinlsui.base;

@MyAnnotation(name="kevinlsui",alias="ke")
public class User extends People{

    @MyAnnotation(name="haha")
    private String friend;

    @Override
    @MyAnnotation2("2222")
    public void say() {
        System.out.println("haha");
    }
}

package com.kevinlsui.base;

public class User2 extends People{

}

package com.kevinlsui.base;

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.Method;

public class Test {

    public static void main(String[] args) {
        //1.注解的继承性测试,@Inherited
        User2 u = new User2();
        MyAnnotation mya = u.getClass().getAnnotation(MyAnnotation.class);
        if(mya != null){
            System.out.println("name:"+mya.name());
            System.out.println("alias:"+mya.alias());
        }
        //此时将myAnnotation2切换为runtime
        MyAnnotation2 mya2 = u.getClass().getAnnotation(MyAnnotation2.class);
        System.out.println(mya2 == null);

        if(mya2 != null){
            System.out.println("value:"+mya2.value());
        }
        System.out.println("=======================");

        //2.注解运行时获取值(一般使用的都是运行时注解,源码和class级的无法用代码控制)
        //反射相关知识

        Class clasz = new User().getClass();
        //Class clasz = User.class;
        //MyAnnotation[] myas = (MyAnnotation[]) clasz.getAnnotations();
        if(clasz.isAnnotationPresent(MyAnnotation.class)){
            MyAnnotation an = (MyAnnotation) clasz.getAnnotation(MyAnnotation.class);
            System.out.println("name:"+an.name());
            System.out.println("alias:"+an.alias());
        }

        System.out.println("====方法====");
        Method[] me = clasz.getDeclaredMethods();//获取本类声明的方法,不分访问权限
        //Method[] me = clasz.getMethods();//可能包括父类继承来的
        for(Method m : me){
            System.out.println(m.getName());
            Annotation[] a = m.getAnnotations();
            //@Override的作用范围不到运行期
            //@MyAnotation2的作用域也不是运行期的
            //所以获取不到注解,length=0
            System.out.println(a.length);

        }

        System.out.println("====字段====");
        Field[] f = clasz.getDeclaredFields();
        //Field[] f = clasz.getFields();
        for(Field fi : f){
            System.out.println(fi.getName());
            MyAnnotation a = fi.getAnnotation(MyAnnotation.class);
            System.out.println(a.name());
            System.out.println(a.alias());
        }


    }

}
文章目录
  1. 1. 学习代码
|