You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Tester.java 2.0KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. package javassist.proxyfactory;
  2. import junit.framework.*;
  3. import javassist.util.proxy.*;
  4. import java.io.ByteArrayInputStream;
  5. import java.io.ByteArrayOutputStream;
  6. import java.lang.reflect.Method;
  7. class Hand implements java.io.Serializable {
  8. /** default serialVersionUID */
  9. private static final long serialVersionUID = 1L;
  10. public int setHandler(int i) { return i; }
  11. int getHandler() { return 3; }
  12. }
  13. @SuppressWarnings({"rawtypes","unchecked","resource"})
  14. public class Tester extends TestCase {
  15. static class MHandler implements MethodHandler, java.io.Serializable {
  16. /** default serialVersionUID */
  17. private static final long serialVersionUID = 1L;
  18. public Object invoke(Object self, Method m, Method proceed,
  19. Object[] args) throws Throwable {
  20. System.out.println("Name: " + m.getName());
  21. return proceed.invoke(self, args);
  22. }
  23. }
  24. static MethodHandler mi = new MHandler();
  25. public void test() throws Exception {
  26. ProxyFactory f = new ProxyFactory();
  27. f.setSuperclass(Hand.class);
  28. Class c = f.createClass();
  29. Hand foo = (Hand)c.getConstructor().newInstance();
  30. ((Proxy)foo).setHandler(mi);
  31. assertTrue(ProxyFactory.isProxyClass(c));
  32. assertEquals(3, foo.getHandler());
  33. }
  34. public void test2() throws Exception {
  35. ProxyFactory f = new ProxyFactory();
  36. f.setSuperclass(Hand.class);
  37. Hand h = (Hand)f.create(new Class[0], new Object[0], mi);
  38. assertEquals(3, h.getHandler());
  39. ByteArrayOutputStream bos = new ByteArrayOutputStream();
  40. ProxyObjectOutputStream out = new ProxyObjectOutputStream(bos);
  41. out.writeObject(h);
  42. out.close();
  43. byte[] bytes = bos.toByteArray();
  44. ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
  45. ProxyObjectInputStream in = new ProxyObjectInputStream(bis);
  46. Hand h2 = (Hand)in.readObject();
  47. assertEquals(3, h2.getHandler());
  48. }
  49. }