When a program actively uses a class, if the class has not yet been loaded into memory, the system initializes the class by loading , connecting , initializing , and 3 steps.
class loading refers to reading class files into memory and creating a Java.lang.Class object for them. All classes in the system are actually instances of Java.lang.Class.
There are typically 3 ways to get class objects in Java:
1. Use the Class.forName (String clazzname) static method. The value of the string parameter is the fully qualified class name of a class (the full package name must be added).
2. Call the Class property of the classes to get the class object corresponding to it. such as Mainactivity.class.
3. Call the GetClass () method of the object. This method is a method in the Java.lang.Object class, so all Java objects can call the method, which returns the class object that the object belongs to.
The 2nd approach has the following advantages:
1. The code is more secure. The program can check the existence of the class object to be accessed during the compile phase.
2. Better program performance.
The Java reflection mechanism is to obtain information about the class at run time through the class object, such as class construction methods, methods, member variables, annotations, inner classes, external classes, interfaces, superclass class objects, modifiers, packages, class names.
methods for class classes1. Get the constructor method
// 返回此Class对象对应类的指定public构造器Constructor<T> getConstructor(Class<?>... parameterType);// 返回此Class对象对应类的所有public构造器Constructor<T>[] getConstructors();// 返回此Class对象对应类的指定构造器,与构造器访问权限无关getDeclaredConstructor(Class<?>... parameterType);// 返回此Class对象对应类的所有构造器,与构造器访问权限无关
2. Get the method
Method getMethod(String name, Class<?>... parameterTypes);Method[] getMethods();// 返回此Class对象对应类的指定方法,与方法的访问权限无关Method getDeclaredMethod(Strinig name, Class<?>... parameterTypes);Method getDeclaredMethods();
3. Get field
getField(String name);Field[]getFields()getDeclaredField(String name);Field[]getDeclaredFields();
Non- public method and non-public Fieldcan be obtained by using java.lang.reflect.AccessibleObject.setAccessible(boolean flag) the method described above.
Set the field or method to accessible so that the non-public method can be accessed.
4. Get annotation
<AAgetAnnotation(Class<A> annotationClass);Annotation[]getAnnotations();Annotation[]getDeclaredAnnotations();
Java annotations are used in many of the development frameworks of the system, and the reflection mechanism of Java provides the basis for these frameworks to be implemented.
5. Create an Object
T newinstance (); Equivalent to using the parameterless constructor new object, if the parameterless constructor is inaccessible, throws a illegalaccessexception exception, or throws a Instantiationexception exception if no argument constructor is available.
Copyright NOTICE: This article is the original blogger article, reprint please indicate the source http://blog.csdn.net/sun927
Summary of the Java reflection mechanism