Reflection learning record

 

A reflective

Key reflections are considered dynamic languages, reflection Reflection API allows a program made by means of any type of internal information during execution, and the internal property and a method of direct manipulation of any object.

Two, Java reflection mechanism provides functionality

Determining at runtime object belongs to any class;

At runtime class of an object in any configuration;

Analyzing any class has member variables and methods at runtime;

At runtime call any member variables and methods of an object;

Generate dynamic proxy.

Third, reflection related API

on behalf of a class java.lang.Class

Method java.lang.reflect.Method representative of the class

On behalf of the class member variables java.lang.reflect.Field

Representative class constructor java.lang.reflect.Constructor

……

 

Fourth, simple example

public class Person {
    private String name;
    private int age;

    public Person() {
    }

    public Person(String name) {
        this.name = name;
    }

    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public void show(){
        System.out.println("我是一个人");
    }

    public void display(String nation){
        System.out.println("我的国籍是"+nation);
    }

    @Override
    public String toString() {
        return "Person[name="+name+",age="+age;
    }
}



Import org.junit.jupiter.api.Test;
 Import as java.lang.reflect.Field; 

public  class TestReflection { 

    // create a reflection before a class object and invoke the methods and properties 
    @Test
     public  void test1 () { 
        the Person Person = new new the Person (); 
        person.setAge ( 10 ); 
        person.setName ( "Bob" ); 
        System.out.println (Person); 
        person.show (); 
        person.display ( "China" ); 
    } 

    // create an object class by reflection before and invoke structure 
    @Test
     public  void test2 () throwsIllegalAccessException, an InstantiationException is, a NoSuchFieldException { 
        Class <Person> clazz = Person. Class ;
         // create corresponding object clazz runtime class class Person 
        Person Person = clazz.newInstance (); 
        System.out.println (Person); 
        Field, F1 = clazz.getField ( "name" ); 
        f1.set (Person, "Han Meimei" ); 
        System.out.println (Person); 
    } 
}

May run test2 () when the throw back the following exception:

No "name" the attribute exists. This exception may be due to a corresponding field does not exist, or the property is set corresponding to private, and private property, but also the reflection can not be obtained, it can be changed to public.

After modifying the phenomenon is:

This is by calling the specified property runtime class reflection, the public can call private property, and call the specified method can also call:

// call public property
Field, f1 = clazz.getField ( "name" );
f1.set (the Person,
"Han Meimei" ); System.out.println (the Person); // call private property, getDeclaredField statement calling off property, // setAccessible set access permissions Field, F2 = clazz.getDeclaredField ( "Age" ); f2.setAccessible ( to true ); f2.set (the Person, 20 ); System.out.println (the Person);
// by reflection to invoke the specified method runtime class 
Method, the method1 = clazz.getMethod ( "Show" ); 
method1.invoke (Person); 
Method, method2 = clazz.getMethod (. "The display", String class ); // add The method parameter types 
method2.invoke (person, "China China");

 

Fives

1, Class class

First understand the Class class, which is a reflection of the source,

All classes inherit from the Object class in the getClass () method, which returns a Class class, the class that is run when a particular object.

Way to produce objects are usually two:

Normal mode: introducing name "bags" required -> by instantiating new -> Get object instance

Reflection mode: instantiating objects -> getClass () Method -> get a complete "packet type" Name

Briefly: reflection, i.e., reflection by the name of the object class is determined, from the instance of the bags, and then turn again obtained from the runtime class instance, then the runtime class structure looking down therein. An object mirror can get their own properties, methods, and constructors implemented interface, etc. information.

Class is itself a class, Class object can only be created by the system, a class in the JVM will be only one instance of Class, is a Class object corresponding to the JVM has been loaded into a .class file, each instance of the class will remember which is generated by a class example, a complete structure can be obtained by the class class.

@Test
  public  void Test3 () { 
        the Person Person = new new the Person ();
         // through the class object runtime getClass (), which returns the runtime class 
        Class clazz = person.getClass (); 
        System.out.println (clazz) ; 
 }

Output:

We create a class, compile (javac.exe), generate the corresponding .class file,

After loading (java.exe, is done by the JVM class loader) this .class files,

After this .class file is loaded into memory, is a runtime class, stored in the cache, then the runtime class itself is a Class instance.

Class loading only once every run.

 

Guess you like

Origin www.cnblogs.com/chen-ying/p/11074578.html