"Thinkinginjava" 43, the adorner mode mixed with the dynamic agent

Source: Internet
Author: User

: Net/mindview/util/twotuple.javapackage Net.mindview.util;public class Twotuple<a,b> {public  final A First;  Public final B second;  Public Twotuple (a A, b b) {first = A; second = B;}  Public String toString () {    return "(" + First + "," + Second + ")";  }} ///:~


Package lesson15_generices;//: Generics/mixins.javaimport java.util.*;interface timestamped {long GetStamp ();}  Class Timestampedimp implements timestamped {private final long timeStamp;  Public Timestampedimp () {timeStamp = new Date (). GetTime (); } public Long Getstamp () {return timeStamp;}} Interface Serialnumbered {long getserialnumber ();}  Class Serialnumberedimp implements serialnumbered {private static long counter = 1;  Private final long serialnumber = counter++; Public long Getserialnumber () {return serialnumber;}}  Interface Basic {public void set (String val); Public String get ();  Class Basicimp implements Basic {private String value;  public void Set (String val) {value = val;} Public String get () {return value;}} Class Mixin extends Basicimpimplements timestamped, serialnumbered {private timestamped TimeStamp = new Timestampedimp ()  ;  Private serialnumbered serialnumber = new Serialnumberedimp (); Public long Getstamp () {return timestamp.getstamp ();} public Long Getserialnumber () {return serialnumber.getserialnumber ();    }}public class Mixins {public static void main (string[] args) {Mixin mixin1 = new Mixin (), mixin2 = new Mixin ();    Mixin1.set ("test string 1");    Mixin2.set ("Test string 2");    System.out.println (Mixin1.get () + "" + mixin1.getstamp () + "" + Mixin1.getserialnumber ());  System.out.println (Mixin2.get () + "" + mixin2.getstamp () + "" + Mixin2.getserialnumber ()); }}/* Output: (Sample) test string 1 1132437151359 1test string 2 1132437151359 2*///:~

/*** Book: Thinking in Java * Features: mixed with dynamic agent * file: dynamicproxymixin.java* time: April 21, 2015 10:38:44* Author: cutter_point*/package Lesson15_generices;import Java.lang.reflect.invocationhandler;import Java.lang.reflect.method;import Java.lang.reflect.proxy;import Java.util.hashmap;import Java.util.map;import Net.mindview.util.twotuple;import Static Net.mindview.util.tuple.*;class Mixinproxy implements Invocationhandler{map<string, object> Delegetesbymethod;public Mixinproxy (Twotuple<object, class<?>> ... pairs)//This sentence indicates that multiple twotuple<object, Class<?>> type parameter {delegetesbymethod = new hashmap<string, object> (); for (Twotuple<object, Class<? >> pair:pairs) {for (Method Method:pair.second.getMethods ())//Get all methods of this class, traverse {String methodName = Method.getname ( if (!delegetesbymethod.containskey (methodName))//If this map contains the specified key, it returns Truedelegetesbymethod.put (MethodName, Pair.first);//Bar A class with this method is put in and this method forms a mapping}}} @Overridepublic object invoke (Object proxy, method, object[] args) throwsThrowable{string methodName = Method.getname ();//The name of the acquisition method object delegate = Delegetesbymethod.get (methodName);// Obtain the corresponding method according to the method name System.out.println ("Invoke Method inside Dynamic agent"); return Method.invoke (delegate, args);//callback}public static Object Newinstance (Twotuple ... pairs) {class interfaces[] = new class[pairs.length];//Create a Class array of length pairs.length for (int i = 0; i < Pairs.length; ++i) {Interfaces[i] = (Class) pairs[i].second;//get all the methods of the class, initialize the array, which holds the second parameter, this should be the type information}//get C1 class loader, When the proxy class is implemented, use the same classloader classloader cl = Pairs[0].first.getclass (). getClassLoader ();//Returns a proxy class instance for the specified interface, The interface can assign a method call to the specified invocation handler. /** * C1 is the load class used by the proxy object, interfaces is the list of interfaces to be implemented by the proxy class, and the third parameter, new Mixinproxy (pairs), is the call handler that assigns the method call */return proxy.newproxyinstance (CL, interfaces, new Mixinproxy (Pairs));}} public class Dynamicproxymixin{public static void Main (string[] args) {Object mixin = mixinproxy.newinstance (Tuple (new Basicimp (), Basic.class), tuple (new Timestampedimp (), Timestamped.class), tuple (new Serialnumberedimp (), Serialnu Mbered.class)); Basic B = (Basic) mixin;    timestamped t = (timestamped) mixin;    serialnumbered s = (serialnumbered) mixin; B.set ("Cutter_point");//When the Set method is called, the agent intercepts, executes the proxy System.out.println (B.get ()) before implementation, or calls the Get method again, triggering the SYSTEM.OUT.PRINTLN (T.getstamp ());//Ibid. System.out.println (S.getserialnumber ());//Ditto}}

Output:

Invoke method inside the dynamic agent
Invoke method inside the dynamic agent
Cutter_point
Invoke method inside the dynamic agent
1431995142154
Invoke method inside the dynamic agent
1








"Thinkinginjava" 43, the adorner mode mixed with the dynamic agent

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.