The method of construction of the reflection Java reflection

Previous Java Class reflection of class we introduced the key category of Class java reflection,
reflection is mapped by a java class to get a java class .
So, we naturally think of, there should be a class which attributes to be here for example, people with the name and age properties, all of a Person class will have a name, age these two properties, the same token, there is also a class constructors, fields, methods and other properties.
Next, we first understand the constructor, create an object constructor dynamically reflected.

[A] the Constructor class

Constructor object represents a constructor, then the object should be what ways do we guess there will get the name of this constructor, to obtain the class constructor belongs, produces an object and other methods by the constructor.
jdk document
Jdk document from where you can see getName () constructor method to obtain the name, getDeclaringClass () class to obtain their class, newInstance () to create the object.
In development, we often newInstance () method to dynamically create class Constructor objects.

[Di] Constructor object obtained

Before the presentation Constructor class method, first describes how to get Constructor, and the same Class, Constructor nor can new directly.
Here Insert Picture Description
The above two methods is jdk document Class class, these two methods are the methods for the specified configuration and access to all constructors.
So we know, want to get Constructor object, you must first obtain a Class object. Let us demonstrate the following

[Three] demo

We still get the String class do presentations.
Here Insert Picture Description
As can be seen from the figure, String class there are many constructors.
Chestnut presentation
1. Get all the constructors

    public static void main(String[] args) throws Exception {
        Class cls = Class.forName("java.lang.String");
        //获取所有构造方法
        Constructor[] constructor = cls.getConstructors();
        for(Constructor cons : constructor){
            System.out.println(cons);
        }
    }

result:

public java.lang.String(byte[],int)
public java.lang.String(byte[],int,int,java.nio.charset.Charset)
public java.lang.String(java.lang.StringBuffer)
public java.lang.String(byte[])
public java.lang.String(byte[],int,int)
public java.lang.String(byte[],java.nio.charset.Charset)
public java.lang.String(byte[],java.lang.String) throws java.io.UnsupportedEncodingException
public java.lang.String()
public java.lang.String(java.lang.StringBuilder)
public java.lang.String(byte[],int,int,int)
public java.lang.String(int[],int,int)
public java.lang.String(char[],int,int)
public java.lang.String(char[])
public java.lang.String(java.lang.String)
public java.lang.String(byte[],int,int,java.lang.String) throws java.io.UnsupportedEncodingException

Can guess from the output of, the class must have the Constructor methods, can be obtained before modifier construction methods, parameters, and the like thrown exception.



2. The method of obtaining a single structure

    public static void main(String[] args) throws Exception {
        Class cls = Class.forName("java.lang.String");
        //获取单个构造方法
        Constructor constructor = cls.getConstructor(StringBuffer.class);
        String constructorName = constructor.getName();
        System.out.println(constructorName);
    }

result:

java.lang.String

Java just like overloaded methods, determination of method overloading and various parameters based on the number of different types of parameters, the method is configured to obtain a particular method of obtaining the corresponding configuration type according to the number of parameters, the parameters.
Here Insert Picture Description
Original method parameter class type as a parameter of the incoming getConstructor method, to obtain the corresponding constructor parameter. Similarly access to other construction methods are the same.



3. Create an object via the constructor

    public static void main(String[] args) throws Exception {
        Class cls = Class.forName("java.lang.String");
        //获取单个构造方法
        Constructor constructor = cls.getConstructor(StringBuffer.class);
        //由于在编译时,无法检测到是什么类型的Constructor所以需要进行类型强转
        String str = (String)constructor.newInstance(new StringBuffer("abc"));
        //拿到反射出来的对象,调用charAt方法测试
        System.out.println(str.charAt(2));
    }

result:

c

Here are some of my friends may this error:

    public static void main(String[] args) throws Exception {
        Class cls = Class.forName("java.lang.String");
        //获取单个构造方法
        Constructor constructor = cls.getConstructor(StringBuffer.class);
        //由于在编译时,无法检测到是什么类型的Constructor所以需要进行类型强转
        String str = (String)constructor.newInstance("abc");
        //拿到反射出来的对象,调用charAt方法测试
        System.out.println(str.charAt(2));
    }

result:

Exception in thread "main" java.lang.IllegalArgumentException: argument type mismatch
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
    at club.leyvan.muzile.ConstructDemo.main(ConstructDemo.java:12)

Type batch with no errors, that is to say, the above code is the constructor parameter corresponding to this constructor StringBuffer, and pass in StringBuffer object parameter is not, it will be given.
Summary: use method of obtaining the type of class, to use the same type of the object instance above when calling the method.

[Tetrakis] newInstance Class in () method

Also in class Class newInstance method, see the Class class source code can know, when this method calls the default constructor with no arguments to create objects using caching mechanism, the no-argument constructor cached when calling newInstance method, remove the constructor with no arguments to create the object from the cache.
And in that the difference in the Constructor, the newInstance method of Class constructor with no arguments is called, there can not be called configuration parameter.
Next we say that the reflection member of the Java reflection of variables

Guess you like

Origin www.cnblogs.com/leyvan/p/12461691.html