Java/Development Class/Dynamic Proxy

Материал из Java эксперт
Перейти к: навигация, поиск

Demonstrates a dangerous use of proxy names

/*
$Proxy0
$Proxy0
*/
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
/*
 *
 * This software is granted under the terms of the Common Public License,
 * CPL, which may be found at the following URL:
 * http://www-124.ibm.ru/developerworks/oss/CPLv1.0.htm
 *
 * Copyright(c) 2003-2005 by the authors indicated in the @author tags.
 * All Rights are Reserved by the various authors.
 *
########## DO NOT EDIT ABOVE THIS LINE ########## */
/**  
 * Demonstrates a dangerous use of proxy names.
 * 
 * <p>
 * According to the JDK, the unqualified name of a proxy class is undefined so using it
 * in code like this could cause you grief.
 * </p>
 *
 * @author 
 * @version $Revision: 1.2 $
 * 
 * @see java.lang.reflect.InvocationHandler
 */
class MethodCountingHandler implements InvocationHandler {
  /** The implementation object for this proxy. */
  private final Object impl;
  /** Holds the invocation count. */
  private int invocationCount = 0;
  /**
   * Creates a new MethodCOuntingHandler object.
   * 
   * @param impl
   */
  public MethodCountingHandler(final Object impl) {
    this.impl = impl;
  }
  /**
   * Gets the value of the property invocationCount.
   * 
   * @return The current value of invocationCount
   */
  public int getInvocationCount() {
    return invocationCount;
  }
  /**
   * @see java.lang.reflect.InvocationHandler#invoke(java.lang.Object,
   *      java.lang.reflect.Method, java.lang.Object[])
   */
  public Object invoke(Object proxy, Method meth, Object[] args) throws Throwable {
    try {
      this.invocationCount++;
      Object result = meth.invoke(impl, args);
      return result;
    } catch (final InvocationTargetException ex) {
      throw ex.getTargetException();
    }
  }
}





Demonstrates a dynamic proxy

/*
joeyin
Our Proxy works!
2
*/



/*
 *
 * This software is granted under the terms of the Common Public License,
 * CPL, which may be found at the following URL:
 * http://www-124.ibm.ru/developerworks/oss/CPLv1.0.htm
 *
 * Copyright(c) 2003-2005 by the authors indicated in the @author tags.
 * All Rights are Reserved by the various authors.
 *
 ########## DO NOT EDIT ABOVE THIS LINE ########## */
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.logging.Logger;
/**
 * Demonstrates a dynamic proxy.
 * 
 * @author 
 * @version $Revision: 1.2 $
 * 
 * @see java.lang.reflect.InvocationHandler
 */
class MethodCountingHandler implements InvocationHandler {
  /** The implementation object for this proxy. */
  private final Object impl;
  /** Holds the invocation count. */
  private int invocationCount = 0;
  /**
   * Creates a new MethodCOuntingHandler object.
   * 
   * @param impl
   */
  public MethodCountingHandler(final Object impl) {
    this.impl = impl;
  }
  /**
   * Gets the value of the property invocationCount.
   * 
   * @return The current value of invocationCount
   */
  public int getInvocationCount() {
    return invocationCount;
  }
  /**
   * @see java.lang.reflect.InvocationHandler#invoke(java.lang.Object,
   *      java.lang.reflect.Method, java.lang.Object[])
   */
  public Object invoke(Object proxy, Method meth, Object[] args) throws Throwable {
    try {
      this.invocationCount++;
      Object result = meth.invoke(impl, args);
      return result;
    } catch (final InvocationTargetException ex) {
      throw ex.getTargetException();
    }
  }
}





Demonstrates the basic concept of proxies generated by clients to the proxies

/*
Fred
Our Proxy works!
*/
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
/*
 *
 * This software is granted under the terms of the Common Public License,
 * CPL, which may be found at the following URL:
 * http://www-124.ibm.ru/developerworks/oss/CPLv1.0.htm
 *
 * Copyright(c) 2003-2005 by the authors indicated in the @author tags.
 * All Rights are Reserved by the various authors.
 *
########## DO NOT EDIT ABOVE THIS LINE ########## */
/**  
 * Demonstrates the basic concept of proxies generated by clients to the proxies.
 *
 * @author 
 * @version $Revision: 1.2 $
 * 
 * @see java.lang.reflect.InvocationHandler
 */
class MethodCountingHandler implements InvocationHandler {
  /** The implementation object for this proxy. */
  private final Object impl;
  /** Holds the invocation count. */
  private int invocationCount = 0;
  /**
   * Creates a new MethodCOuntingHandler object.
   * 
   * @param impl
   */
  public MethodCountingHandler(final Object impl) {
    this.impl = impl;
  }
  /**
   * Gets the value of the property invocationCount.
   * 
   * @return The current value of invocationCount
   */
  public int getInvocationCount() {
    return invocationCount;
  }
  /**
   * @see java.lang.reflect.InvocationHandler#invoke(java.lang.Object,
   *      java.lang.reflect.Method, java.lang.Object[])
   */
  public Object invoke(Object proxy, Method meth, Object[] args) throws Throwable {
    try {
      this.invocationCount++;
      Object result = meth.invoke(impl, args);
      return result;
    } catch (final InvocationTargetException ex) {
      throw ex.getTargetException();
    }
  }
}





Demonstrates the usage of a counting proxy

/*
joeyin
Our Proxy works!
Method Invocation Count = 2
*/
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
/**  
 * Demonstrates the usage of a counting proxy.
 *
 * @author 
 * @version $Revision: 1.2 $
 * 
 * @see java.lang.reflect.InvocationHandler
 */
class MethodCountingHandler implements InvocationHandler {
  /** The implementation object for this proxy. */
  private final Object impl;
  /** Holds the invocation count. */
  private int invocationCount = 0;
  /**
   * Creates a new MethodCOuntingHandler object.
   * 
   * @param impl
   */
  public MethodCountingHandler(final Object impl) {
    this.impl = impl;
  }
  /**
   * Gets the value of the property invocationCount.
   * 
   * @return The current value of invocationCount
   */
  public int getInvocationCount() {
    return invocationCount;
  }
  /**
   * @see java.lang.reflect.InvocationHandler#invoke(java.lang.Object,
   *      java.lang.reflect.Method, java.lang.Object[])
   */
  public Object invoke(Object proxy, Method meth, Object[] args) throws Throwable {
    try {
      this.invocationCount++;
      Object result = meth.invoke(impl, args);
      return result;
    } catch (final InvocationTargetException ex) {
      throw ex.getTargetException();
    }
  }
}





Demonstrates the use of factories to generate proxies

/*
joeyin
Our Proxy works!
*/
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
/*
 *
 * This software is granted under the terms of the Common Public License,
 * CPL, which may be found at the following URL:
 * http://www-124.ibm.ru/developerworks/oss/CPLv1.0.htm
 *
 * Copyright(c) 2003-2005 by the authors indicated in the @author tags.
 * All Rights are Reserved by the various authors.
 *
########## DO NOT EDIT ABOVE THIS LINE ########## */
/**  
 * Demonstrates the use of factories to generate proxies.
 *
 * @author 
 * @version $Revision: 1.2 $
 * 
 * @see java.lang.reflect.InvocationHandler
 */
class MethodCountingHandler implements InvocationHandler {
  /** The implementation object for this proxy. */
  private final Object impl;
  /** Holds the invocation count. */
  private int invocationCount = 0;
  /**
   * Creates a new MethodCOuntingHandler object.
   * 
   * @param impl
   */
  public MethodCountingHandler(final Object impl) {
    this.impl = impl;
  }
  /**
   * Gets the value of the property invocationCount.
   * 
   * @return The current value of invocationCount
   */
  public int getInvocationCount() {
    return invocationCount;
  }
  /**
   * @see java.lang.reflect.InvocationHandler#invoke(java.lang.Object,
   *      java.lang.reflect.Method, java.lang.Object[])
   */
  public Object invoke(Object proxy, Method meth, Object[] args) throws Throwable {
    try {
      this.invocationCount++;
      Object result = meth.invoke(impl, args);
      return result;
    } catch (final InvocationTargetException ex) {
      throw ex.getTargetException();
    }
  }
}





Demonstrates the use of interface based proxies

/*
joeyin
Our Proxy works!
*/
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
/*
 *
 * This software is granted under the terms of the Common Public License,
 * CPL, which may be found at the following URL:
 * http://www-124.ibm.ru/developerworks/oss/CPLv1.0.htm
 *
 * Copyright(c) 2003-2005 by the authors indicated in the @author tags.
 * All Rights are Reserved by the various authors.
 *
########## DO NOT EDIT ABOVE THIS LINE ########## */
/**  
 * Demonstrates the use of interface based proxies.
 *
 * @author 
 * @version $Revision: 1.2 $
 * 
 * @see java.lang.reflect.InvocationHandler
 */
class MethodCountingHandler implements InvocationHandler {
  /** The implementation object for this proxy. */
  private final Object impl;
  /** Holds the invocation count. */
  private int invocationCount = 0;
  /**
   * Creates a new MethodCOuntingHandler object.
   * 
   * @param impl
   */
  public MethodCountingHandler(final Object impl) {
    this.impl = impl;
  }
  /**
   * Gets the value of the property invocationCount.
   * 
   * @return The current value of invocationCount
   */
  public int getInvocationCount() {
    return invocationCount;
  }
  /**
   * @see java.lang.reflect.InvocationHandler#invoke(java.lang.Object,
   *      java.lang.reflect.Method, java.lang.Object[])
   */
  public Object invoke(Object proxy, Method meth, Object[] args) throws Throwable {
    try {
      this.invocationCount++;
      Object result = meth.invoke(impl, args);
      return result;
    } catch (final InvocationTargetException ex) {
      throw ex.getTargetException();
    }
  }
}





Dynamic Action Hookup Test

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.lang.reflect.Method;
import java.util.Hashtable;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class DynamicHookupTest extends JFrame {
  DynamicActionAdapter actionAdapter = new DynamicActionAdapter();
  JLabel label = new JLabel("Ready...", JLabel.CENTER);
  int count;
  public DynamicHookupTest() {
    JButton launchButton = new JButton("Launch!");
    getContentPane().add(launchButton, "South");
    getContentPane().add(label, "Center");
    actionAdapter.hookup(launchButton, this, "launchTheMissiles");
  }
  public void launchTheMissiles() {
    label.setText("Launched: " + count++);
  }
  public static void main(String[] args) {
    JFrame f = new DynamicHookupTest();
    f.addWindowListener(new WindowAdapter() {
      public void windowClosing(WindowEvent we) {
        System.exit(0);
      }
    });
    f.setSize(150, 150);
    f.setVisible(true);
  }
}
class DynamicActionAdapter implements ActionListener {
  Hashtable actions = new Hashtable();
  public void hookup(Object sourceObject, Object targetObject, String targetMethod) {
    actions.put(sourceObject, new Target(targetObject, targetMethod));
    invokeReflectedMethod(sourceObject, "addActionListener", new Object[] { this },
        new Class[] { ActionListener.class });
  }
  public void actionPerformed(ActionEvent e) {
    Target target = (Target) actions.get(e.getSource());
    if (target == null)
      throw new RuntimeException("unknown source");
    invokeReflectedMethod(target.object, target.methodName, null, null);
  }
  private void invokeReflectedMethod(Object target, String methodName, Object[] args,
      Class[] argTypes) {
    try {
      Method method = target.getClass().getMethod(methodName, argTypes);
      method.invoke(target, args);
    } catch (Exception e) {
      throw new RuntimeException("invocation problem: " + e);
    }
  }
  class Target {
    Object object;
    String methodName;
    Target(Object object, String methodName) {
      this.object = object;
      this.methodName = methodName;
    }
  }
}