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.

ProxySimpleTest.java 7.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. package test.javassist.proxy;
  2. import junit.framework.TestCase;
  3. import java.io.FileInputStream;
  4. import java.io.FileOutputStream;
  5. import java.io.ObjectInputStream;
  6. import java.io.ObjectOutputStream;
  7. import java.io.Serializable;
  8. import java.lang.reflect.Method;
  9. import javassist.util.proxy.MethodFilter;
  10. import javassist.util.proxy.MethodHandler;
  11. import javassist.util.proxy.Proxy;
  12. import javassist.util.proxy.ProxyFactory;
  13. public class ProxySimpleTest extends TestCase {
  14. String testResult;
  15. public void testProxyFactory() throws Exception {
  16. ProxyFactory f = new ProxyFactory();
  17. f.writeDirectory = "./proxy";
  18. f.setSuperclass(Foo.class);
  19. f.setFilter(new MethodFilter() {
  20. public boolean isHandled(Method m) {
  21. return m.getName().startsWith("f");
  22. }
  23. });
  24. Class c = f.createClass();
  25. MethodHandler mi = new MethodHandler() {
  26. public Object invoke(Object self, Method m, Method proceed,
  27. Object[] args) throws Throwable {
  28. testResult += args[0].toString();
  29. return proceed.invoke(self, args); // execute the original method.
  30. }
  31. };
  32. Foo foo = (Foo)c.newInstance();
  33. ((Proxy)foo).setHandler(mi);
  34. testResult = "";
  35. foo.foo(1);
  36. foo.foo2(2);
  37. foo.bar(3);
  38. assertEquals("12", testResult);
  39. }
  40. public static class Foo {
  41. public int foo(int i) { return i + 1; }
  42. public int foo2(int i) { return i + 2; }
  43. public int bar(int i) { return i + 1; }
  44. }
  45. public void testReadWrite() throws Exception {
  46. final String fileName = "read-write.bin";
  47. ProxyFactory.ClassLoaderProvider cp = ProxyFactory.classLoaderProvider;
  48. ProxyFactory.classLoaderProvider = new ProxyFactory.ClassLoaderProvider() {
  49. public ClassLoader get(ProxyFactory pf) {
  50. return new javassist.Loader();
  51. }
  52. };
  53. ProxyFactory pf = new ProxyFactory();
  54. pf.setSuperclass(ReadWriteData.class);
  55. Object data = pf.createClass().newInstance();
  56. // Object data = new ReadWriteData();
  57. ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(fileName));
  58. oos.writeObject(data);
  59. oos.close();
  60. ProxyFactory.classLoaderProvider = cp;
  61. ObjectInputStream ois = new ObjectInputStream(new FileInputStream(fileName));
  62. Object data2 = ois.readObject();
  63. ois.close();
  64. int i = ((ReadWriteData)data2).foo();
  65. assertEquals(4, i);
  66. }
  67. public static class ReadWriteData implements Serializable {
  68. public int foo() { return 4; }
  69. }
  70. public void testWriteReplace() throws Exception {
  71. ProxyFactory pf = new ProxyFactory();
  72. pf.setSuperclass(WriteReplace.class);
  73. Object data = pf.createClass().newInstance();
  74. assertEquals(data, ((WriteReplace)data).writeReplace());
  75. ProxyFactory pf2 = new ProxyFactory();
  76. pf2.setSuperclass(WriteReplace2.class);
  77. Object data2 = pf2.createClass().newInstance();
  78. Method meth = data2.getClass().getDeclaredMethod("writeReplace", new Class[0]);
  79. assertEquals("javassist.util.proxy.SerializedProxy",
  80. meth.invoke(data2, new Object[0]).getClass().getName());
  81. }
  82. public static class WriteReplace implements Serializable {
  83. public Object writeReplace() { return this; }
  84. }
  85. public static class WriteReplace2 implements Serializable {
  86. public Object writeReplace(int i) { return new Integer(i); }
  87. }
  88. String value244;
  89. public void testJIRA244() throws Exception {
  90. ProxyFactory factory = new ProxyFactory();
  91. factory.setSuperclass(Extended244.class);
  92. Extended244 e = (Extended244)factory.create(null, null, new MethodHandler() {
  93. @Override
  94. public Object invoke(Object self, Method thisMethod,
  95. Method proceed, Object[] args) throws Throwable {
  96. value244 += thisMethod.getDeclaringClass().getName();
  97. return proceed.invoke(self);
  98. }
  99. });
  100. value244 = "";
  101. assertEquals("base", e.base());
  102. System.out.println(value244);
  103. assertEquals(Extended244.class.getName(), value244);
  104. value244 = "";
  105. assertEquals("ext", e.extended());
  106. System.out.println(value244);
  107. assertEquals(Extended244.class.getName(), value244);
  108. value244 = "";
  109. assertEquals("base2ext2", e.base2());
  110. System.out.println(value244);
  111. assertEquals(Extended244.class.getName(), value244);
  112. }
  113. // if Base244 is private, then Extended244 has a bridge method for base().
  114. private static abstract class Base244 {
  115. public String base() { return "base"; }
  116. public String base2() { return "base2"; }
  117. }
  118. public static class Extended244 extends Base244 {
  119. public String extended() { return "ext"; }
  120. public String base2() { return super.base2() + "ext2"; }
  121. }
  122. String valueDefaultMethods = "";
  123. public void testDefaultMethods() throws Exception {
  124. valueDefaultMethods = "";
  125. ProxyFactory f = new ProxyFactory();
  126. f.writeDirectory = "./proxy";
  127. f.setSuperclass(Default3.class);
  128. Class c = f.createClass();
  129. MethodHandler mi = new MethodHandler() {
  130. public Object invoke(Object self, Method m, Method proceed,
  131. Object[] args) throws Throwable {
  132. valueDefaultMethods += "1";
  133. return proceed.invoke(self, args); // execute the original method.
  134. }
  135. };
  136. Default3 foo = (Default3)c.newInstance();
  137. ((Proxy)foo).setHandler(mi);
  138. foo.foo();
  139. foo.bar();
  140. assertEquals("11", valueDefaultMethods);
  141. }
  142. public void testDefaultMethods2() throws Exception {
  143. valueDefaultMethods = "";
  144. ProxyFactory f = new ProxyFactory();
  145. f.writeDirectory = "./proxy";
  146. f.setInterfaces(new Class[] { Default2.class });
  147. Class c = f.createClass();
  148. MethodHandler mi = new MethodHandler() {
  149. public Object invoke(Object self, Method m, Method proceed,
  150. Object[] args) throws Throwable {
  151. valueDefaultMethods += "1";
  152. return proceed.invoke(self, args); // execute the original method.
  153. }
  154. };
  155. Default2 foo = (Default2)c.newInstance();
  156. ((Proxy)foo).setHandler(mi);
  157. foo.foo();
  158. foo.bar();
  159. assertEquals("11", valueDefaultMethods);
  160. }
  161. public static interface Default1 {
  162. default int foo() { return 0; }
  163. default int baz() { return 2; }
  164. }
  165. public static interface Default2 extends Default1 {
  166. default int bar() { return 1; }
  167. }
  168. public static class Default3 implements Default2 {
  169. public int foo() { return Default2.super.foo(); }
  170. }
  171. public static class Default4 extends Default3 {
  172. public int baz() { return super.baz(); }
  173. }
  174. }