Java reflection API

Implementation of the Java reflection mechanism java.lang.reflect classes are in the package, java.lang.Class class is a Java reflection API in the core classes.

java.lang.Class class

java.lang.Class class is the key to achieving reflection, an instance of Java class Class represent a data type, including classes, interfaces, enumerations, annotations (the Annotation), an array of basic data types and void. Class no public constructor, Class instances are created automatically by the JVM in the class load.

Examples of Class obtained in the program code may be implemented by the following code:

// 1. 通过类型class静态变量
Class clz1 = String.class;
String str = "Hello";

// 2. 通过对象的getClass()方法
Class clz2 = str.getClass();

Each type includes classes and interfaces, there is a static class instance variable available Class. In addition, each object has a getClass () method available Class instance, the method is an example of a method provided by the Object class.

Class class provides information about the many ways you can get run-time object, the following code shows some of these methods.

public class ReflectionTest01 {
    public static void main(String[] args) {
        // 获得Class实例
        // 1.通过类型class静态变量
        Class clz1 = String.class;
        String str = "Hello";
        // 2.通过对象的getClass()方法
        Class clz2 = str.getClass();
        // 获得int类型Class实例
        Class clz3 = int.class;
        // 获得Integer类型Class实例
        Class clz4 = Integer.class;
        System.out.println("clz2类名称:" + clz2.getName());
        System.out.println("clz2是否为接口:" + clz2.isInterface());
        System.out.println("clz2是否为数组对象:" + clz2.isArray());
        System.out.println("clz2父类名称:" + clz2.getSuperclass().getName());
        System.out.println("clz2是否为基本类型:" + clz2.isPrimitive());
        System.out.println("clz3是否为基本类型:" + clz3.isPrimitive());
        System.out.println("clz4是否为基本类型:" + clz4.isPrimitive());
    }
}

Results are as follows:

clz2类名称:java.lang.String
clz2是否为接口:false
clz2是否为数组对象:false
clz2父类名称:java.lang.Object
clz2是否为基本类型:false
clz3是否为基本类型:true
clz4是否为基本类型:false

note: Difference between the above code, line 10 and line 12. int is the basic data type, the output result is true; Integer is a class, a reference data type, the output result is false.

java.lang.reflect 包

java.lang.reflect package provides classes used in reflection, the major classes are described as follows:

	Constructor 类:提供类的构造方法信息。
	
	Field 类:提供类或接口中成员变量信息。
	
	Method 类:提供类或接口成员方法信息。
	
	Array 类:提供了动态创建和访问 Java 数组的方法。
	
	Modifier 类:提供类和成员访问修饰符信息。

code show as below:

public class ReflectionTest {
    public static void main(String[] args) {
        try {
            // 动态加载xx类的运行时对象
            Class c = Class.forName("java.lang.String");
            // 获取成员方法集合
            Method[] methods = c.getDeclaredMethods();
            // 遍历成员方法集合
            for (Method method : methods) {
                // 打印权限修饰符,如public、protected、private
                System.out.print(Modifier.toString(method.getModifiers()));
                // 打印返回值类型名称
                System.out.print(" " + method.getReturnType().getName() + " ");
                // 打印方法名称
                System.out.println(method.getName() + "();");
            }
        } catch (ClassNotFoundException e) {
            System.out.println("找不到指定类");
        }
    }
}

The code line 5 is run when you create a class by Class static methods forName (String) object that is type the full name of the parameter string, if can not find the class in the class path, then throw a ClassNotFoundException, 17 lines of code.

Line 7 returns the code is a member of a class object array method Class instance method getDeclaredMethods (). The first line of code member 9 is traversing a set of methods, wherein the element is a Method type.

method.getModifiers line of code 11 () method returns modifier constant access code, int type, such as a representative of public, the meaning of these numbers may be converted to a string represented by Modifier.toString (int) method. The first line of code 13 for obtaining the type of the return value, then call getName () method returns the name of the type of getReturnType by Method () method. The first line of code 15 method.getName () Returns the name of the method.

Published 457 original articles · won praise 94 · views 10000 +

Guess you like

Origin blog.csdn.net/weixin_45743799/article/details/104727945