java reflection mechanism - java class

Java reflection mechanism

Java reflection mechanism allows us to check class, interface, variable and method information at runtime (Runtime) other than compile time (Compile Time). Reflection also allows us to instantiate objects at runtime, call methods, and get the values ​​of variables by calling get/set methods.

java class

Using the Java reflection mechanism, you can check the information of Java classes at runtime. Checking the information of Java classes is often the first thing you do when you use the Java reflection mechanism. By obtaining the class information, you can obtain the following related content:

  1. Class object
  2. class name
  3. modifier
  4. package information
  5. father
  6. Implemented interface
  7. Constructor
  8. method
  9. variable
  10. annotation
  11. Class object

  12. Before checking the class information, you first need to get the Class object of the class. All types in Java including primitive types (int, long, float, etc.), even arrays have objects of class Class associated with them. If you know the name of a class at compile time, then you can get the Class object of a class as follows.
    Class myObjectClass = MyObject.class;

    If you don't know the class name at compile time, but you can get the string of the class name at runtime, then you can get the Class object by doing this:

    String className = ... ;//在运行期获取的类名字符串
    Class class = Class.forName(className);

    When using the Class.forName() method, you must provide the full name of the class, including the name of the package in which the class resides. For example, if the MyObject class is located in the com.jenkov.myapp package, then its full name is com.jenkov.myapp.MyObject. If the corresponding class is not found in the compilation path (classpath) when the Class.forName() method is called, a ClassNotFoundException will be thrown.

  13. class name

    You can get both versions of the class name from the Class object.

    Return the fully qualified class name (including the package name) of the class via the getName() method:

    Class aClass = ... //获取Class对象,具体方式可见Class对象小节
    String className = aClass.getName();

    If you just want to get the class name (without the package name), then you can use the getSimpleName() method:

    Class aClass = ... //获取Class对象,具体方式可见Class对象小节
    String simpleClassName = aClass.getSimpleName();
  14. modifier

    The modifiers of a class can be accessed through the Class object, that is, keywords such as public, private, static, etc. You can use the following methods to get the modifiers of a class:

    Class  aClass = ... //获取Class对象,具体方式可见Class对象小节
    int modifiers = aClass.getModifiers();

    修饰符都被包装成一个int类型的数字,这样每个修饰符都是一个位标识(flag bit),这个位标识可以设置和清除修饰符的类型。 可以使用 java.lang.reflect.Modifier 类中的方法来检查修饰符的类型:

    Modifier.isAbstract(int modifiers);
    Modifier.isFinal(int modifiers);
    Modifier.isInterface(int modifiers);
    Modifier.isNative(int modifiers);
    Modifier.isPrivate(int modifiers);
    Modifier.isProtected(int modifiers);
    Modifier.isPublic(int modifiers);
    Modifier.isStatic(int modifiers);
    Modifier.isStrict(int modifiers);
    Modifier.isSynchronized(int modifiers);
    Modifier.isTransient(int modifiers);
    Modifier.isVolatile(int modifiers);
  15. 包信息

    可以使用 Class 对象通过如下的方式获取包信息:

    Class  aClass = ... //获取Class对象,具体方式可见Class对象小节
    Package package = aClass.getPackage();
  16. 父类

    通过 Class 对象你可以访问类的父类,如下例:

    Class superclass = aClass.getSuperclass();

    可以看到 superclass 对象其实就是一个 Class 类的实例,所以你可以继续在这个对象上进行反射操作。

    实现的接口

    可以通过如下方式获取指定类所实现的接口集合:

    Class  aClass = ... //获取Class对象,具体方式可见Class对象小节
    Class[] interfaces = aClass.getInterfaces();

    由于一个类可以实现多个接口,因此 getInterfaces(); 方法返回一个 Class 数组,在 Java 中接口同样有对应的 Class 对象。 注意:getInterfaces() 方法仅仅只返回当前类所实现的接口。当前类的父类如果实现了接口,这些接口是不会在返回的 Class 集合中的,尽管实际上当前类其实已经实现了父类接口。

    构造器

    你可以通过如下方式访问一个类的构造方法:

    Constructor[] constructors = aClass.getConstructors();

    更多有关 Constructor 的信息可以访问 Constructors

    方法

    你可以通过如下方式访问一个类的所有方法:

    Method[] method = aClass.getMethods();

    变量

    你可以通过如下方式访问一个类的成员变量:

    Field[] method = aClass.getFields();

    更多有关 Field 的信息可以访问 Fields

    注解

    你可以通过如下方式访问一个类的注解:

    Annotation[] annotations = aClass.getAnnotations();

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326193857&siteId=291194637