Reflective structure the package: a reflective essay (a)
Preface:
Source learning based on JDK 8
A, Interface
1, structure
- java.lang.reflect.AnnotatedElement
- java.lang.reflect.AnnotatedType
* java.lang.reflect.AnnotatedArrayType
* java.lang.reflect.AnnotatedParameterizedType
* java.lang.reflect.AnnotatedTypeVariable
* java.lang.reflect.AnnotatedWildcardType
- java.lang.reflect.GenericArrayType
- java.lang.reflect.GenericDeclaration
- java.lang.reflect.TypeVariable
- java.lang.reflect.InvocationHandler
- java.lang.reflect.Member
- java.lang.reflect.Type
- java.lang.reflect.ParameterizedType
- java.lang.reflect.WildcardType
2. Classification Overview
2.1,java.lang.reflect.AnnotatedElement
AnnotatedElement This interface represents a marked annotated element that runs within the JVM, can be Class, Method, Field, Constructor, Package and other popular terms like that than if you bought the goods that you are the customer, to buy goods labeled annotations, like this customer category, by this method interface you can get information annotated above a method, Field, Constructor, Package, etc., provided that these elements are implements this interface.
Methods within the interface is relatively common, it is not introduced one by one, only to distinguish between getAnnotation () and getDeclaredAnnotation () of the difference between these two methods, different classes of call, the result is different, but better summary of. For class Class and Package is the same (Package Class is calling the method):
getAnnotation () Returns all annotations on the element, including inherited from parent classes down (of course, must be inherited annotation is labeled @Inherited element annotation)
getDeclaredAnnotation () Returns all annotations on the element, does not include inherited from the parent class
For Method, Field, Constructor, Parameter, this method returns the two values are the same, all annotations are returned on the element.
2.2,AnnotatedType、AnnotatedParameterizedType、AnnotatedTypeVariable、AnnotatedWildcardType
Wherein AnnotatedType parent several other interfaces, to a method getType (), primarily used to obtain the type of annotation elements, most obvious as follows:
public class Test implements @Annotation Interface {
}
//通过AnnotatedType a = Test.class.getAnnotatedSuperclass()
//通过这个a就可以获取@Annotation这个注解
Other interfaces temporarily study (study of individual little fuzzy, students can have a clear message Share Share)
2.3,GenericArrayType
Get a generic array type, such as List
2.4,GenericDeclaration
Generic statement, the class that implements this interface can declare a generic, but so far, only three of its subclasses Class, Constructor, Method. Need to say is a generic statement and the use of generics are not the same thing as:
The result should be quite clear, you can use generics on the Field, but it can not take the initiative to declare a generic, if the current class is not generic, the Field can not use generics, but Method and Constructor is different, they are in class on under no circumstances can declare generic generic. The interface getTypeParameters only a method, the method is used to obtain generic type, as FIG Test12
2.5,InvocationHandler
This interface is not introduced, JDK proxy must use an interface, I think we've all seen
2.5,Member
Some information recording tag for reflecting object
//标记用来只获取公开的信息(只被public修饰的,包括从超类继承的)
public static final int PUBLIC = 0;
//标记用来获取所有的信息(不包括继承的),无视修饰符
public static final int DECLARED = 1;
//获取申明类
public Class<?> getDeclaringClass();
//获取名称,比如 int a = 1;中的a public void test();中的test
public String getName();
//获取修饰符 比如 public、 static、final 等
public int getModifiers();
//是否人工合成的
public boolean isSynthetic();
2.6,ParameterizedType
Parameterized type, i.e. a generic internal follows:
// 获取实际类型参数,例如:
// public Test extends Test1<String>{} 中的String
Type[] getActualTypeArguments();
// 获取声明泛型的对象类型 如:Test<T> 中的Test
Type getRawType();
// 获取所有者类型,如果有内部类时,内部类上有泛型,调用这个方法返回的便是内部类外面的那个类
Type getOwnerType();
2.7,Type
Java language, the parent of all types of interfaces
2.8,TypeVariable
Type variable, it refers to the generic type, such as Test
//获得该类型变量的上限,也就是泛型中extend右边的值,如 List<T extends Test> 中的Test,因为泛型可以是多个,所以返回值是数组,如果没有extends,则默认是Object
Type[] getBounds();
//获得该类型变量的声明实体,如:List<T extends Test> 中的List
D getGenericDeclaration();
//获得类型变量的名称,如:List<T extends Test> 中的T
String getName();
//该方法暂时没搞清
AnnotatedType[] getAnnotatedBounds();
2.8,WildcardType
Wildcard type, such as:? Extends classA ,? super classB. Methods as below:
//获取泛型表达式的上界 ? extends ClassA 中的ClassA
Type[] getUpperBounds();
//获取泛型表达式的下界 ? super ClassB 中的ClassB
Type[] getLowerBounds();
二、Class
1, structure
- java.lang.reflect.AccessibleObject
- java.lang.reflect.Executable
- java.lang.reflect.Constructor
- java.lang.reflect.Field
- java.lang.reflect.Array
- java.lang.reflect.Modifier
- java.lang.reflect.Parameter
- java.lang.reflect.Proxy
- java.lang.reflect.ReflectAccess
- java.lang.reflect.ReflectPermission
java.lang.reflect.WeakCache
2. Classification Overview
2.1,AccessibleObject
Look at the official explanation: AccessibleObject class is the base class fields, methods and constructors of objects. It provides the ability to use reflective objects marked as prohibited by default Java language access control checks. When a field, method or constructor are used to set or get a new instance field, or invoke methods to create and initialize the class, will perform access checks (for public, default (package) access, protected, and private members).
Set flag allows access to complex applications with sufficient privilege (e.g., Java object serialization or other persistence mechanism) so as to generally prohibit the operation target object in reflection.
By default, the reflecting object is not accessible.
Personal feeling, this class is not set to bypass jvm control checks, the core method setAccessible, when we want to perform a method to get the reflection or by reflection on the field to get the assignment and other operations, we need to set to true setAccessible the ability to bypass the control checks the Java language. Some say the Internet is acting on the authority modifier, such as access to private needs to be set to not allow public access to, in fact, such understanding is wrong, check the Java language, controlling not just access modifier for check-ups, but also not to be and so do the final inspection, for example, we all know that with the final modified variables can not be modified, the same will complain if by reflection on public final modified variable assignment, as shown below
, So set setAccessible (true), just to bypass access rules, but also some other rules to bypass the Java language, such as the final modification of immutable rules.
2.2 Array
This class project in general rarely used, mainly through the class, call the native build the array, not much to share here, are interested can play their own
2.3,Constructor,Field,Method,Parameter
It's common these categories, there is not much to share, Detailed behind
2.4,Executable
This class is a common method of extracting Method and Constructor shared class
2.5,Modifier
All the qualifier in the Java language, using different int values represent different modifications words, do not explain
2.6,ReflectAccess和ReflectPermission
Both are reflected permissions-related classes, not many share
2.7,WeakCache
A weak reflection process cache, nothing special function