-------<a href= "http://www.itheima.com" target= "blank" >android training </a> <a href= "/http/ Www.itheima.com "target=" blank ">java training </a>, look forward to communicating with you! ----------
1. Reflection is in the running state, for any class (class file), you can know all the properties and methods of this class, for any one object, can only call its arbitrary methods and properties, the dynamic access to the information at the level of the dynamic method of invoking the object's function is called a Java reflection mechanism.
Reflection is actually loading a specified class dynamically and getting all the content in that class. The bytecode files are encapsulated into objects and the contents of the bytecode files are encapsulated into objects, making it easier to manipulate those members. is to reflect the various components in the Java class into the corresponding Java class. Simply put: Reflection technology can dissect a class
2. The benefits of reflection: greatly enhance the scalability of the program.
3. Basic steps of Reflection:
(1), Gets the class object, is the byte-code file object that obtains the specified name.
(2), instantiate the object, get the class's properties, methods, or constructors.
(3), accessing the property, invoking the method, and invoking the constructor to create the object.
4. Get the same byte-code file as the class, in these three ways.
1. Class Cls1= classes are relatively simple, or you need to specify the class name
2. Cls1.getclass (); You must define the specific class and create the object
3. Class.forName ("Full class name (with package name)");
5. Nine pre-defined class instance objects:
(1). Integer.class The byte code of the wrapper class
(2). Integer.type byte code of the basic type in Integer
(3). Int.class==integer.type
(4). Array
A. class.isprimitive ();
B. Class.isarray ();
As long as the types that appear in the source program have their own class instance objects, such as int[] void, and so on
(5). Create an object of the class
String name= "Com.dsa. Class name";
(6). Look for the name of the class file and load it into memory, not to produce a class object:
Class clazz=class.forname (name);
(7). The object that produces the class:
Object obj=clazz.newinstance ();
(8). Get a specified constructor method:
Constructor constructor= Class.forName (""). GetConstructor (String.class);
(9). Create an Instance Object
Object obj=constructor.newinstance ("abc");
6. Member variable filed class represents a member variable in a class
Reflectpoint pt1=new Reflectpoint (3,5);
Filed filedy=pt1.getclsss (). getfiled ("Y"); Represents only which object
Filed filedy=pt1.getclsss (). Getdeclaredfield ("X"); Gets whether the object is private or protected
Filedy.setaccessible (TRUE); Settings can be accessed, violent reflections
Filedy.get (PT1); Remove the value of Y
Change B in the field to a
Field[] Fields=obj.getclass (). GetFields ();//Get all objects
for (Field field:fields) {
if (Field.gettype () ==string.class) {//If it is a string
String oldValue = (string) field.get (obj); Get string contents
String newvalue = oldvalue.replace (' B ', ' a '); Replace the string contents
Field.set (Obj,newvalue); Assigning a new value to an object
7. Reflection of method class member methods
Gets the member method reflection form:
Method Methodcharat = String.class.getMethod ("charAt"--Name of the methods, int.class--this is the parameter type, if the null parameter function is NULL);
Object obj=clazz.newinstance (); Methodcharat.invoke (str1,1);
8. Reflection of arrays
Class Clazz=obj.getclass ();
if (Clazz.isarray ()) {
int len=array.getlength (obj);
for (int i=0;i<len;i==) {
Soparray.get (Obj,i))
}
}
9. Reflection to get generic information
By specifying the corresponding class object, the program can get all the field in the class, regardless of whether the field uses private method public. You can use GetType () to obtain the type of a Field object after it has been obtained.
class<?> type = F.gettype (); Get the type of field
However, this method is only valid for normal field, and if the field has a generic modification, it is not possible to get the generic parameters of the field exactly, such as map<string,integer>; in order to get the generic type of the specified field, we use:
Type GType = F.getgenerictype ();
Gets the generic type and then strongly converts the type object to Parameterizedtype, which represents an increase in the types after generics
Type Getrawtype (); Returns the type that is restricted by the generic type
Type[] Getactualtypearguments (); Returns the generic parameter type
10. Using reflection to get the type of generics (generic information)
Steps:
(1). Get the current class
(2). Get Target Fields
(3). Gets the type that contains the generic type Getgenerictype ()
(4). Strong go to subclass Parameterizedtype (because the type does not have any corresponding methods)
(5). Get generics True Type Getactualtypearguments ()
11. Example:
Package cn.itcast.day01;
Import Java.lang.reflect.Field;
Import Java.lang.reflect.ParameterizedType;
Import Java.lang.reflect.Type;
Import Java.util.HashMap;
Import Java.util.Map;
public class getgenerictypedemo{
map<string,integer> map = new hashmap<string,integer> ();
public static void Main (string[] args) throws Exception {
Class C = getgenerictypedemo.class;
Field f = C.getdeclaredfield ("map");
System.out.println (f);
System.out.println (F.getname ()); Map
Class<?> GetType () returns a Class object that identifies the claim type of the field represented by this Field object.
Class cl = F.gettype ();
SYSTEM.OUT.PRINTLN ("Get its type:" +CL);//Get its type: interface Java.util.Map
/**
* Type Getgenerictype () returns a Type object that represents the declared type of the field represented by this Field object.
* Type is the interface of class */
Type T = F.getgenerictype ();//contains generic types
System.out.println (t); Java.util.map<java.lang.string, Java.lang.Integer
Parameterizedtype pt = (parameterizedtype) t;//Strong go to its subclasses
t = Pt.getrawtype ();//class or interface of type
System.out.println (t);
type[] ts = pt.getactualtypearguments ();
for (Type type:ts) {
System.out.println (type);
}
}
}
The reflection logic in the Dark Horse programmer--java