Home > Java > javaTutorial > How to use annotations and reflection in Java

How to use annotations and reflection in Java

王林
Release: 2023-05-10 14:04:06
forward
1282 people have browsed it

1. Annotation

1.1 What is annotation

Annotations are not the program itself. They can be read during program compilation, class loading and runtime, and execute the corresponding processing. The format of the annotation is "@annotation name (parameter value)", which can be attached to packages, classes, methods and fields, and the annotation is accessed through the reflection mechanism.

1.2 Built-in annotation

@Override: Restrict subclasses to override methods

This annotation indicates that the method of the parent class is overridden. When a class overrides a parent class method, ensure that the subclass actually overrides the parent class method to avoid low-level errors

/**
 * 该注解标识覆盖的是其父类的方法,当子类重写父类方法时,确保子类确实重写了父类的方法,避免出现低级错误
 * @return
 */
@Override
public String toString() {
    return super.toString();
}
Copy after login

@Deprecated: The mark is obsolete

This An annotation indicates that a property, method, or class is obsolete (a program element that programmers are discouraged from using, usually because it is dangerous or because a better alternative exists). When other programs use the obsolete property, method or class, the compiler will give a warning (strikethrough).

/**
 * 该注解表示此方法已过时,存在危险,不推荐使用,其有代替方法,如果继续使用会通过删除线进行标识
 */
@Deprecated
public static void test() {
    System.out.println("标记已过时");
}
Copy after login

@SuppressWarnings (parameter): Suppress compiler warnings

The classes, methods and properties affected by this annotation will suppress the display of compiler warnings, and its parameters are mainly for Warning instructions and cancellation (unchecked), etc.

@SuppressWarnings("取消此类的所有警告")
public class BuiltAnnotation {
    
    @SuppressWarnings("取消此属性的警告")
    private String username;

    @SuppressWarnings("取消此方法的警告")
    public static void main(String[] args) {
        // ...
    }
}
Copy after login

1.3 Meta-annotation (meta-annotation)

The role of meta-annotation is to annotate other annotations. Java defines 4 standard meta-annotation types, which are used to provide support for other annotations. The scope and type are described, and other annotations can be customized through meta-annotations.

@Target: Describe the scope of use of annotations

For example, @Target(ElementType.METHOD) indicates that it acts on a method, @Target(ElementType.TYPE) indicates that it acts on Class or interface etc.

/**
 * @Target注解:描述注解的使用范围
 */
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.ANNOTATION_TYPE)
public @interface Target {
    /**
     * 类或接口:ElementType.TYPE;
     * 字段:ElementType.FIELD;
     * 方法:ElementType.METHOD;
     * 构造方法:ElementType.CONSTRUCTOR;
     * 方法参数:ElementType.PARAMETER;
     * ...
     */
    ElementType[] value();
}
Copy after login

@Retention: Indicates at what level the annotation information needs to be saved, used to describe the life cycle of the annotation

Usually customized annotations are Use @Retention(RetentionPolicy.RUNTIME), which is a runtime effect.

/**
 * @Retention:表示需要在什么级别保存该注解信息,用于描述注解的生命周期
 */
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.ANNOTATION_TYPE)
public @interface Retention {
    /**
     * RetentionPolicy.SOURCE:仅编译期,注解将被编译器丢弃。
     * RetentionPolicy.CLASS:仅class文件,注释将由编译器记录在类文件中,但VM不需要在运行时保留,如果不指定则默认为class。
     * RetentionPolicy.RUNTIME:运行期,注释将由编译器记录在类文件中,并由VM在运行时保留,因此可以反射读取。通常自定义的注解都是RUNTIME。
     * 范围:RUNTIME>CLASS>SOURCE
     */
    RetentionPolicy value();
}
Copy after login

@Document: Indicates that this annotation will be included in javadoc

##@Iherited: An annotation that defines whether a subclass can inherit the parent class definition.

@Inherited is only useful for annotations of the @Target(ElementType.TYPE) type, and can only be inheritance of class, and is invalid for inheritance of interface:

1.4 Custom annotations

Define annotations

/**
 * 1. 使用@interface定义注解;
 * 3. 通过元注解配置该注解,配置注解的使用范围和生命周期等
 * @author Loner
 */
@Target({ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface Report{
    /**
     * 2. 添加参数、默认值,如果没有默认值,就必须给参数赋值,一般把最常用的参数定义为value(),推荐所有参数都尽量设置默认值
     * 形式为:参数类型 参数名();
     */
    int type() default 0;
    String value() default "LonerMJ";
}
Copy after login

Use annotations

@Report(type = 1, value = "test")
public class CustomerAnnotation {
    @Report(type = 1, value = "test")
    public void testCustomerAnnotation() {
        System.out.println("测试自定义注解");
    }
}
Copy after login

2. Reflection

2.1 Reflection and Reflection mechanism

Reflection

Reflection (reflection) means that the program can get all the information of an object during runtime.

Reflection mechanism

The reflection mechanism means that when the program is running, it obtains the content information of any class through the Reflection API, and can directly operate the internal properties and methods of any object. .

2.2 How to obtain the Class class and common methods

The java.lang.Class class is the core class that implements reflection. After the class is loaded, a class will be generated in the method area of ​​the heap memory. Class object (a class has only one Class object), this object contains the complete structural information of the class, and the structure of the class can be seen through this object.

How to obtain the Class class

public class InstantiationClass {
    public static void main(String[] args) throws ClassNotFoundException {
        Teacher teacher = new Teacher("张三", "123456");

        // 方式一:调用Class类的静态方法forName(String className)
        Class<?> c1 = Class.forName("com.loner.mj.reflection.Teacher");

        // 方式二:已知某个类的实例,调用该实例的getClass()方法,getClass是Object类中的方法。
        Class<? extends Teacher> c2 = teacher.getClass();

        // 方式三:已知具体类,通过类的class属性获取,该方法最安全可靠,程序性能最高
        Class<Teacher> c3 = Teacher.class;

        // 方式四:通过基本内置类型的包装类的TYPE属性获得CLass实例
        Class<Integer> c4 = Integer.TYPE;

        // 方式五:通过当前子类的Class对象获得父类的Class对象
        Class<?> c5 = c1.getSuperclass();
    }
}
Copy after login

Common methods of the Class class

Method nameMethod function##static Class forName(String name)Obiect newInstance()String getName()Class getSuperclass()Class[] getinterfaces()ClassLoader getClassLoader()Method getDeclareMethod(String name, Class ... parameterTypes)Method[] getDeclareMethods()Method[] getMethods()Field getDeclareField(String name)Field [] getDeclareFields()Field[] getFields()Constructor getConstructor(Class... parameterTypesConstructor getConstructors()A getAnnotation(Class annotationClass)Annotation[] getDeclaredAnnotations()
public class ReflectionMethods {
    public static void main(String[] args) throws NoSuchFieldException, NoSuchMethodException {
        Class<Worker> workerClass = Worker.class;

        /**
         * 类
         */
        System.out.println(workerClass.getName());
        System.out.println(workerClass.getSimpleName());
        System.out.println(workerClass.getSuperclass());
        System.out.println(workerClass.getPackage());
        Class<?>[] interfaces = workerClass.getInterfaces();
        for (Class<?> i : interfaces) {
            System.out.println(i);
        }

        /**
         * 属性
         */
        // 获取所有的属性
        Field[] declaredFields = workerClass.getDeclaredFields();
        for (Field declaredField : declaredFields) {
            System.out.println(declaredField);
        }
        // 获取指定属性
        System.out.println(workerClass.getDeclaredField("username"));
        // 获取所有公共属性
        Field[] fields = workerClass.getFields();
        for (Field field : fields) {
            System.out.println(field);
        }

        /**
         * 构造方法
         */
        // 获取所有构造方法
        Constructor<?>[] declaredConstructors = workerClass.getDeclaredConstructors();
        for (Constructor<?> declaredConstructor : declaredConstructors) {
            System.out.println(declaredConstructor);
        }
        // 获取指定的构造方法
        System.out.println(workerClass.getDeclaredConstructor(String.class, String.class));

        /**
         * 方法
         */
        // 获取所有的方法
        Method[] declaredMethods = workerClass.getDeclaredMethods();
        for (Method declaredMethod : declaredMethods) {
            System.out.println(declaredMethod);
        }
        // 获取指定方法
        System.out.println(workerClass.getDeclaredMethod("getUsername", null));
        // 获取所有功能方法
        Method[] methods = workerClass.getMethods();
        for (Method method : methods) {
            System.out.println(method);
        }
    }
}
Copy after login

哪些类型具有Class对象

public class InstantiationClass {
    public static void main(String[] args) throws ClassNotFoundException {
        // 类(外部类,成员(成员内部类,静态内部类),局部内部类,匿名内部类。)
        Class<Object> objectClass = Object.class;
        // 接口
        Class<Comparable> comparableClass = Comparable.class;
        // 数组
        Class<String[]> stringClass = String[].class;
        Class<int[][]> intClass = int[][].class;
        // 枚举
        Class<ElementType> elementTypeClass = ElementType.class;
        // 注解
        Class<Override> overrideClass = Override.class;
        // 基本数据类型
        Class<Integer> integerClass = Integer.class;
        // void
        Class<Void> voidClass = void.class;
        // Class
        Class<Class> classClass = Class.class;
    }
}
Copy after login

2.3 反射的使用

反射操作对象

public class UseClass {
    public static void main(String[] args) throws NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException, NoSuchFieldException {
        Class<User> userClass = User.class;

        /**
         * 通过构造器实例化对象:不使用构造器,默认通过无参构造进行对象创建
         */
        Constructor<User> declaredConstructor = userClass.getDeclaredConstructor(String.class, String.class);
        User user = declaredConstructor.newInstance("张三", "123456");
        System.out.println(user);

        /**
         * 调用方法并执行相关操作
         */
        Method setUsername = userClass.getDeclaredMethod("setUsername", String.class);
        // invoke(Object, 参数):激活,即执行相关操作为该对象
        setUsername.invoke(user, "李四");
        Method setPassword = userClass.getDeclaredMethod("setPassword", String.class);
        setPassword.invoke(user, "123456");
        System.out.println(user);

        /**
         * 操作属性:通过反射直接操作私有属性会报错,需要通过setAccessible(ture)关闭访问安全检查,此方法属性、方法和构造都具有,会影响效率
         */
        Field username = userClass.getDeclaredField("username");
        username.setAccessible(true);
        username.set(user, "用户名");
        System.out.println(user);
    }
}
Copy after login

反射操作泛型

Java采用泛型擦除的机制来引入泛型,Java中的泛型仅仅是给编译器javac使用的,确保数据的安全性和免去强制类型转换问题。但是,一旦编译完成,所有和泛型有关的类型全部擦除。

为了通过反射操作这些类型,Java新增了ParameterizedType,GenericArrayType,TypeVariable和WildcardType几种类型来代表不能被归一到Class类中的类型但是又和原始类型齐名的类型。

ParameterizedType:表示一种参数化类型,比如Collection

GenericArrayType:表示一种元素类型是参数化类型或者类型变量的数组类型

TypeVariable:是各种类型变量的公共父接口

WildcardType:代表一种通配符类型表达式

public class ClassOperateGenerics {
    public Map<String, String> list() {
        System.out.println("返回值是泛型");
        return new HashMap<>();
    }

    public void test(Map<String, User> map, List<Integer> list) {
        System.out.println("参数是泛型");
    }

    public static void main(String[] args) throws NoSuchMethodException {
        /**
         * 获取方法参数的泛型
         */
        Method method = ClassOperateGenerics.class.getMethod("test", Map.class, List.class);
        // 获取所有方法参数的泛型
        Type[] genericParameterTypes = method.getGenericParameterTypes();
        for (Type genericParameterType : genericParameterTypes) {
            // java.util.Map<java.lang.String, com.loner.mj.reflection.User>
            System.out.println(genericParameterType);
            if (genericParameterType instanceof ParameterizedType) {
                // 获取所有泛型的真实参数
                Type[] actualTypeArguments = ((ParameterizedType) genericParameterType).getActualTypeArguments();
                for (Type actualTypeArgument : actualTypeArguments) {
                    // String, User, Integer
                    System.out.println(actualTypeArgument);
                }
            }
        }

        /**
         * 获取方法返回值的泛型
         */
        Method list = ClassOperateGenerics.class.getMethod("list", null);
        // 获取方法返回值的泛型
        Type genericReturnType = list.getGenericReturnType();
        if (genericReturnType instanceof ParameterizedType) {
            // 获取所有泛型的真实参数
            Type[] actualTypeArguments = ((ParameterizedType) genericReturnType).getActualTypeArguments();
            for (Type actualTypeArgument : actualTypeArguments) {
                System.out.println(actualTypeArgument);
            }
        }
    }
}
Copy after login

反射操作注解

public class ClassOperateAnnotation {
    public static void main(String[] args) throws NoSuchFieldException {
        Class<People> peopleClass = People.class;

        // 获取类的所有注解
        Annotation[] declaredAnnotations = peopleClass.getDeclaredAnnotations();
        for (Annotation declaredAnnotation : declaredAnnotations) {
            System.out.println(declaredAnnotation);
        }
        // 获取类的注解的值
        Table declaredAnnotation = peopleClass.getDeclaredAnnotation(Table.class);
        System.out.println(declaredAnnotation.value());

        // 获取属性的注解
        Field name = peopleClass.getDeclaredField("name");
        Fields annotation = name.getAnnotation(Fields.class);
        System.out.println(annotation.name());
    }
}
Copy after login

The above is the detailed content of How to use annotations and reflection in Java. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
Returns the Class object of the specified class name
Call the parameterless constructor and return an instance of the Class object
Return this The name of the entity (class, interface, array class or void) represented by the Class object
Returns the Class object of the parent class of the current Class object
Returns the interface of the current Class object
Return the class loader of this class
Get the method name that matches the parameter list Method
Get all non-inherited methods
Get all non-private methods
Get the specified attribute
Get all properties
Get all non-private properties
Get the constructor method matching the parameter list
Get all constructors of the class
Return the specified annotation
Return all annotations