Java programming ideas - Chapter 14 type information (b) reflection

VI reflection: the runtime class information

  We already know, at compile time, the compiler must know all the classes to be handled by the RTTI. It reflected provide a mechanism - for checking the available methods, and return the name of the method. The difference is that a process known RTTI class and an unknown class for reflection treatment. Class class libraries together with java.lang.reflect support the concept of reflection, the library comprises a Field, Method and the Constructor (Member each class implements the interface). These types are created by the JVM run, the kind used to represent the members of the class corresponding to the unknown. Using the Constructor (constructor) create a new object, and to read and modify fields Field objects (fields) associated with the get (), set () method calls a method associated with an object Method (method) invoke () method. Thus, class information anonymous objects can be completely settled at run time, but at compile time does not need to know anything.

  In fact, when a reflective object of unknown type deal, JVM simply checking the object, before doing anything else you must first load the Class object of this class . Thus, the class of the .class file is available (on local or network) so that the difference between the reflection and RTTI only when necessary for the JVM: For RTTI, the compiler checks .class file and open at compile time, As for the reflection is, .class file is not available at compile time, so check the .class file is open and running. Reflected in the need to create a more dynamic code is useful.

Seven, dynamic proxies

  Acting is the basic design patterns: provide for other subject an agent, in order to control the object, and the object before or after adding what they want added.

Copy the code
{Interface interface 
    void doSomething (); 

    void doSomeOtherThing (String args); 
} 

class RealObject the implements Interface { 

    @Override 
    public void doSomething () { 
        System.out.println ( "doSomething"); 
    } 

    @Override 
    public void doSomeOtherThing (String args) { 
        System.out.println ( "doSomeOtherThing" + args); 
    } 
} 

class SimpleProxy the implements Interface { 

    Private Interface proxyid; 

    public SimpleProxy (proxyid Interface) { 
        this.proxyId = proxyid; 
    } 

    @Override 
    public void doSomething() {
        // add the original method doSomething this is output on a new agency after things
        // agent like a company after the purchase additional game 
        System.out.println ( "SimpleProxy doSomething"); 
        proxyId.doSomething (); 
    } 

    @Override 
    public void doSomeOtherThing (String args) { 
        proxyId.doSomeOtherThing (args); 
        / / stuff can be added before or after all rows in the original 
        System.out.println ( "SimpleProxy doSomeOtherThing" + args); 
    } 
} 

public class SimpleProxyDemo { 
    static void Consumer (Interface I) { 
        i.doSomething (); 
        i.doSomeOtherThing ( "GI woli Giao Yi"); 
    } 

    public static void main (String [] args) {  
        Consumer (new new RealObject ());
        System.out.println ( "----- ----- -----");
        consumer(new SimpleProxy(new RealObject()));
    }
}
Copy the code

result:

Copy the code
doSomething
doSomeOtherThing yi gi woli giao
-----  -----  -----
SimpleProxy doSomething
doSomething
doSomeOtherThing yi gi woli giao
SimpleProxy doSomeOtherThing yi gi woli giao
Copy the code

  Because the consumer () accepted Interface, so it is RealObject or SimpleProxy, can be used as parameters, and SimpleProxy put a foot Acting RealObject added a lot of our own things.

  Java dynamic proxy further, because it can dynamically create proxies and dynamically handle calls to the proxy method is more ago. In the dynamic proxy all calls made will be redirected to a single call processor, its job is to reveal the type of call and determine appropriate countermeasures.

Copy the code
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

interface Interface {
    void doSomething();

    void doSomeOtherThing(String args);
}

class RealObject implements Interface {

    @Override
    public void doSomething() {
        System.out.println("doSomething");
    }

    @Override
    public void doSomeOtherThing(String args) {
        System.out.println("doSomeOtherThing" + args);
    }
}

class DynamicProxyHandler implements InvocationHandler {
    private Object proxyId;

    public DynamicProxyHandler(Object proxyId) {
        this.proxyId = proxyId;
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        System.out.println("**** proxy:" + proxy.getClass() + ", method" + method + ", args:" + args);
        if (args != null) {
            for (Object arg : args) {
                System.out.println(" " + arg);
            }
        }
        return method.invoke(proxyId, args);
    }
}

public class SimpleProxyDemo {
    static void consumer(Interface i) {
        i.doSomething();
        i.doSomeOtherThing(" yi gi woli giao");
    }

    public static void main(String[] args) {
        RealObject = new new RealObject RealObject (); 
        consumer(realObject);
        System.out.println ( "----- ----- -----"); 
     // dynamic proxy can represent anything Interface proxy = (Interface) Proxy.newProxyInstance (Interface.class.getClassLoader (), new new Class [] {} Interface.class, new new DynamicProxyHandler (realObject)); Consumer (Proxy); } }
Copy the code

result:

Copy the code
doSomething
doSomeOtherThing yi gi woli giao
-----  -----  -----
**** proxy:class $Proxy0, methodpublic abstract void Interface.doSomething(), args:null
doSomething
**** proxy:class $Proxy0, methodpublic abstract void Interface.doSomeOtherThing(java.lang.String), 
args:[Ljava.lang.Object;@7ea987ac  yi gi woli giao
doSomeOtherThing yi gi woli giao
Copy the code

By Proxy.newProxyInstance () to create a dynamic proxy, this method requires three parameters:

1. The class loader: which may be obtained from a class loader object has been loaded in;

2. The proxy implements a list of interfaces you want to (can not be a class or an abstract class, only the interface);

3. InvocationHandler an implementation of the interface;

Invoke may also be implemented in the name of the method according to different approaches, such as:

Copy the code
    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        System.out.println("**** proxy:" + proxy.getClass() + ", method" + method + ", args:" + args);
        if (args != null) {
            for (Object arg : args) {
                System.out.println(" " + arg);
            }
        }
        if (method.getName().equals("doSomething")) {
            System.out.println("this is the proxy for doSomething");
        }
        return method.invoke(proxyId, args);
    }
Copy the code

You can also perform more operations of method parameters or because you've got them enjoy the use of your power of attorney before adding it ~ ~ buy it within ten.

Nine, interface type information

  An important goal is to allow the programmer interface keyword spacer member, thereby reducing the coupling. Reflection, you can call all methods, even private. Alone is final and can not be modified, the system will not throw any unusual circumstances accept any modification attempt, but any changes will not actually occur at run time.

    void callMethod(Object a, String methodName) throws Exception {
        Method method = a.getClass().getDeclaredMethod(methodName);
        method.setAccessible(true);
        method.invoke(a);
    }

 

 

http://www.72177.com/htm/201911/25/4507597.htm
http://www.72177.com/htm/201911/25/4507596.htm
http://www.72177.com/htm/201911/25/4507564.htm
http://www.72177.com/htm/201911/25/4507562.htm
http://www.72177.com/htm/201911/25/4507560.htm
http://www.72177.com/htm/201911/25/4507558.htm
http://www.72177.com/htm/201911/25/4507555.htm
http://www.72177.com/htm/201911/25/4507553.htm
http://www.72177.com/htm/201911/25/4507551.htm
http://www.72177.com/htm/201911/25/4507549.htm
http://www.72177.com/htm/201911/25/4507546.htm
http://www.72177.com/htm/201911/25/4507544.htm
http://www.72177.com/htm/201911/25/4507542.htm
http://www.72177.com/htm/201911/25/4507540.htm
http://www.72177.com/htm/201911/25/4507538.htm
http://www.72177.com/htm/201911/25/4507535.htm
http://www.72177.com/htm/201911/25/4507533.htm
http://www.72177.com/htm/201911/25/4507531.htm
http://www.72177.com/htm/201911/25/4507529.htm
http://www.72177.com/htm/201911/25/4507526.htm
http://www.72177.com/htm/201911/25/4507524.htm
http://www.72177.com/htm/201911/25/4507522.htm
http://www.72177.com/htm/201911/25/4507520.htm
http://www.72177.com/htm/201911/25/4507517.htm
http://www.72177.com/htm/201911/25/4507515.htm

Guess you like

Origin www.cnblogs.com/cider/p/11929258.html