Java Reflection Learning <BR>The so-called reflection can be understood as the operation of obtaining object type information during runtime. Traditional programming methods require programmers to decide the types to use at the compile stage, but with the help of reflection, programmers can obtain this information dynamically and write more portable code. Strictly speaking, reflection is not a feature of a programming language, because the reflection mechanism can be implemented in any language, but if the programming language itself supports reflection, then the implementation of reflection will be much more convenient.
1. Obtain the type class <BR>We know that everything in Java is an object, and the objects we generally use inherit directly or indirectly from the Object class. The Object class contains a method called getClass, which can be used to obtain the type class of an instance. A type class refers to a class that represents a type, because everything is an object, and types are no exception. In Java, a type class is used to represent a type. All type classes are instances of the Class class. For example, here is the following piece of code:
A a = new A();
if(a.getClass()==A.class)
System.out.println("equal");
else System.out.println("unequal");
The result is "equal" printed out.
It can be seen that object a is an instance of A, a certain class of A. The result returned by using a.getClass() in the if statement is exactly the type class of A. To express a specific type of type class in Java, you can use " Type.class" method, because a.getClass() obtains the type class of A, which is A.class, so the result of executing the above code is to print out "equal". Special note is that type classes have a one-to-one correspondence. The type class of the parent class and the type class of the subclass are different. Therefore, assuming A is a subclass of B, the following code will get the output of "unequal":
A a = new A();
if(a.getClass()==B.class)
System.out.println("equal");
else System.out.println("unequal");
So, if you know an instance, you can get the type class of that object using the instance's "getClass()" method, and if you know a type, you can get the type class of that type using the ".class" method.
2. Obtain type information <BR>After obtaining the type class, you can call some of the methods to obtain type information. The main methods are:
getName():String: Get the full name of the type.
getSuperClass():Class: Get the direct parent class of this type. If the type has no direct parent class, return null.
getInterfaces():Class[]: Get all interfaces implemented by this type.
isArray():boolean: Determine whether the type is an array.
isEnum():boolean: Determine whether the type is an enumeration type.
isInterface():boolean: Determine whether the type is an interface.
isPrimitive():boolean: Determine whether the type is a basic type, that is, whether it is int, boolean, double, etc.
isAssignableFrom(Class cls):boolean: Determine whether this type is the parent (ancestor) class or parent (ancestor) interface of type cls.
getComponentType():Class: If the type is an array, returns the component type of the array.
In addition, operations such as type conversion can also be performed. The main methods are:
asSubclass(Class clazz):Class: Change this type