博客园 首页 私信博主 显示目录 隐藏目录 管理

Java反射

注解与反射

注解(Annotation)

1、内置注解

@Override //重写父类或接口的方法

2、元主解

@Target  //定义注解使用在那些地方
@Retention  //定义注解在什么地方有效
@Doucment //表示是否将我们的注解生成在JavaDoc中
@Inherited  //子类是否可以继承父类的注解

@Target

/** Class, interface (including annotation type), enum, or record
 * declaration */
TYPE,//可以放在类上
/** Field declaration (includes enum constants) */
FIELD,
/** Method declaration */
METHOD,//可以放在方法上
/** Formal parameter declaration */
PARAMETER,
/** Constructor declaration */
CONSTRUCTOR,
/** Local variable declaration */
LOCAL_VARIABLE,
/** Annotation type declaration */
ANNOTATION_TYPE,
/** Package declaration */
PACKAGE,
/**
 * Type parameter declaration
 *
 * @since 1.8
 */
TYPE_PARAMETER,
/**
 * Use of a type
 *
 * @since 1.8
 */
TYPE_USE,
/**
 * Module declaration.
 *
 * @since 9
 */
MODULE,
/**
 * {@preview Associated with records, a preview feature of the Java language.
 *
 *           This constant is associated with <i>records</i>, a preview
 *           feature of the Java language. Programs can only use this
 *           constant when preview features are enabled. Preview features
 *           may be removed in a future release, or upgraded to permanent
 *           features of the Java language.}
 *
 * Record component
 *
 * @jls 8.10.3 Record Members
 * @jls 9.7.4 Where Annotations May Appear
 *
 * @since 14
 */
@jdk.internal.PreviewFeature(feature=jdk.internal.PreviewFeature.Feature.RECORDS,
                             essentialAPI=true)
RECORD_COMPONENT;

@Retention

/**
 * Annotations are to be discarded by the compiler.
 */
SOURCE,
/**
 * Annotations are to be recorded in the class file by the compiler
 * but need not be retained by the VM at run time.  This is the default
 * behavior.
 */
CLASS,//编译文件
/**
 * Annotations are to be recorded in the class file by the compiler and
 * retained by the VM at run time, so they may be read reflectively.
 *
 * @see java.lang.reflect.AnnotatedElement
 */
RUNTIME//程序运行时

3、自定义注解

@interface  //自动继承Java.lang.annotation.Annotation接口

示例:

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

public class Demo01 {
    @MyAnnotation01(name = "张三",school = {"西北大学"},age = 12,myschool = {MyEnum.XBDX})
    public void test01(){}

    @MyAnnotation02("只有一个参数,属性名称为value时,可以省略名称")
    public void test02(){}

}

@Target({ElementType.METHOD,ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@interface MyAnnotation01{

    //注解的参数:参数类型 + 参数名();
    //如果没有默认值,可以不写参数
    String name();

    int id() default -1;

    int age();

    String[] school();

    MyEnum[] myschool();
}

enum MyEnum{
    XBDX,
    NCDX,
    SZDX
}
@Target({ElementType.METHOD,ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@interface MyAnnotation02{
    String value();
}

反射

公共使用类

class User{
    public int str;
    private String name;
    private int age;
    private int id;

    public User(){}

    public User(String name, int age, int id){
        this.name = name;
        this.age = age;
        this.id = id;
    }

    @Override
    public String toString() {
        return "User{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", id=" + id +
                '}';
    }

    private String say(){
        System.out.println("人说话了");
        return "人说话返回值";
    }

    @MyAnnotation03("张三")
    private int test(){
        return 1;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }
}

@Target({ElementType.TYPE,ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@interface MyAnnotation03{
    String value();
}

获取Class对象

//获取Class对象的方法
/**
 * 1、通过类的路径信息获取CLass对象
 * Class<?> user01 = Class.forName("com.company.test16.User");
 * 2、通过对象获取Class对象
 * Class<? extends User> user04 = user.getClass();
 * 3、通过类名获取Class对象
 * Class<User> user05 = User.class;
 * 4、基本数据类型的包装类可以通过TYPE属性获取Class对象
 * Class<Integer> type = Integer.TYPE;
 * 5、获取父类的Class对象
 * Class<?> superclass = user01.getSuperclass();
 */
// 通过反射获取类的Class对象
Class<?> user01 = Class.forName("com.company.test16.User");
Class<?> user02 = Class.forName("com.company.test16.User");
Class<?> user03 = Class.forName("com.company.test16.User");
// 通过对象获取Class对象
User user = new User();
Class<? extends User> user04 = user.getClass();
System.out.println("user04: " + user04.hashCode());
// 通过类名获取Class对象
Class<User> user05 = User.class;
System.out.println("user05: " + user05.hashCode());
// 基本数据类型的包装类可以通过TYPE属性获取Class对象
Class<Integer> type = Integer.TYPE;
System.out.println("type: " + type.hashCode());
// 获取父类的Class对象
Class<?> superclass = user01.getSuperclass();
System.out.println("superclass: " + superclass);
//一个类在内存中只有一个Class对象
//一个类被加载后,类整个结构都会被封装到CLass对象中
System.out.println("user01: " + user01.hashCode());
System.out.println("user02: " + user02.hashCode());
System.out.println("user03: " + user03.hashCode());

存在Class对象

//测试所有类型的CLass
public static void main(String[] args) {
    Class c1 = Object.class; //类
    Class c2 = Comparable.class;//接口
    Class<String[]> c3 = String[].class;//一维数组
    Class<int[][]> c4 = int[][].class;//二维数组
    Class<Override> c5 = Override.class;//注解
    Class<ElementType> c6 = ElementType.class;//枚举
    Class<Integer> c7 = Integer.class;//基本数据类型的包装类
    Class<Void> c8 = void.class;//void关键字
    Class<Class> c9 = Class.class;//Class类
}

通过Class获取类属性方法

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;

public class Demo01 {

    public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException {

        Class<?> user01 = Class.forName("com.company.test17.User");

        //获取类名
        String name = user01.getName();
        String simpleName = user01.getSimpleName();

        System.out.println("name:" + name);
        System.out.println("simpleName:" + simpleName);
        System.out.println("=========================获得类的属性:getFields");

        //获得类的属性
        //getDeclaredFields()获取所有的属性
        //getFields()获取public的属性
        Field[] field1s = user01.getFields();
        for (Field field : field1s) {
            //属性名称
            System.out.println(field.getName());
        }
        System.out.println("=========================获得类的属性:getDeclaredFields");
        Field[] field2s = user01.getDeclaredFields();
        for (Field field : field2s) {
            //属性名称
            System.out.println(field.getName());
        }
        System.out.println("=========================获取类方法:getMethods");
        //获取类方法
        //getMethods()获取本类及其父类的所有Public方法
        //getDeclaredMethods()获取本类的所有方法
        Method[] methods1 = user01.getMethods();
        for (Method method : methods1) {
            System.out.println(method.getName());
        }
        System.out.println("=========================获取类方法:getDeclaredMethods");
        Method[] methods2 = user01.getDeclaredMethods();
        for (Method method : methods2) {
            System.out.println(method.getName());
        }

        System.out.println("=========================获取指定类方法");
        Method method1 = user01.getMethod("getName", null);
        System.out.println("getName:" + method1.getName());
        Method method2 = user01.getMethod("setName", String.class);
        System.out.println("setName:" + method2.getName());

        System.out.println("=========================获取类构造器:getConstructors");
        //只能获取本类public构造器
        Constructor<?>[] constructors1 = user01.getConstructors();
        for (Constructor con : constructors1) {
            System.out.println(con);
        }
        //只能获取本类全部构造器
        System.out.println("=========================获取类构造器:getDeclaredConstructors");
        Constructor<?>[] constructors2 = user01.getDeclaredConstructors();
        for (Constructor con : constructors2) {
            System.out.println(con);
        }
        //获取本类指定构造器
        System.out.println("=========================获取指定类构造器");
        Constructor<?> constructor = user01.getDeclaredConstructor(String.class, int.class, int.class);

        System.out.println(constructor);
    }

}

输出结果:

name:com.company.test17.User
simpleName:User
=========================获得类的属性:getFields
str
=========================获得类的属性:getDeclaredFields
str
name
age
id
=========================获取类方法:getMethods
getName
toString
setName
getId
setAge
setId
getAge
wait
wait
wait
equals
hashCode
getClass
notify
notifyAll
=========================获取类方法:getDeclaredMethods
getName
toString
test
setName
getId
say
setAge
setId
getAge
=========================获取指定类方法
getName:getName
setName:setName
=========================获取类构造器:getConstructors
public com.company.test17.User()
public com.company.test17.User(java.lang.String,int,int)
=========================获取类构造器:getDeclaredConstructors
public com.company.test17.User()
public com.company.test17.User(java.lang.String,int,int)
=========================获取指定类构造器
public com.company.test17.User(java.lang.String,int,int)

通过Class对象构造一个实例对象

public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InstantiationException, InvocationTargetException, NoSuchFieldException {
    Class<?> user01 = Class.forName("com.company.test18.User");
    /**
     * 类必须要有无参构造
     * 构造器的访问权限必须足够
     */
    User user = (User) user01.newInstance();
    System.out.println(user);
    //通过构造器创建对象
    Constructor<?> constructor = user01.getDeclaredConstructor(String.class, int.class, int.class);
    User user1 = (User) constructor.newInstance("张三", 1, 2);
    System.out.println(user1);
    //通过反射调用一个方法
    //1、获取一个方法
    Method setName = user01.getDeclaredMethod("setName", String.class);
    //2、激活执行方法
    setName.invoke(user1,"李四");
    System.out.println(user1);
    //反射操作属性
    Field name = user01.getDeclaredField("name");
    //不能直接操作私有属性,可以通过setAccessible(true)将安全检查关闭
    name.setAccessible(true);
    name.set(user1,"王五");
    System.out.println(user1);
}

输出结果:

User{name='null', age=0, id=0}
User{name='张三', age=1, id=2}
User{name='李四', age=1, id=2}
User{name='王五', age=1, id=2}

通过反射操作注解

public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException {
    Class<?> aClass = Class.forName("com.company.test19.User");
    //获取类上注解
    Annotation[] annotations = aClass.getAnnotations();
    for (Annotation a : annotations) {
        System.out.println(a);
    }
    MyAnnotation03 annotation = aClass.getAnnotation(MyAnnotation03.class);
    System.out.println(annotation.value());
    //获取方法上的注解
    Method method = aClass.getDeclaredMethod("test");
    MyAnnotation03 annotation1 = method.getAnnotation(MyAnnotation03.class);
    System.out.println(annotation1.value());
}
posted @ 2021-05-31 12:03  莫逆追风  阅读(28)  评论(1)    收藏  举报