diff options
author | chibash <chiba@javassist.org> | 2017-04-15 23:57:23 +0900 |
---|---|---|
committer | chibash <chiba@javassist.org> | 2017-04-15 23:57:23 +0900 |
commit | 25fdc6bafad29e4b1c7b0f4b340641a4d7f8c476 (patch) | |
tree | d0de0248b3f29a39966d3e11957763a08c89115b /src/test | |
parent | 804ad141e0c3c9c41834ca71d124ad9cc7510d43 (diff) | |
download | javassist-25fdc6bafad29e4b1c7b0f4b340641a4d7f8c476.tar.gz javassist-25fdc6bafad29e4b1c7b0f4b340641a4d7f8c476.zip |
adds javassist.util.proxy.ProxyFactory#onlyPublicMethods. If true, Javassist uses java.lang.invoke.MethodHandles.Lookup to load a class.
Diffstat (limited to 'src/test')
-rw-r--r-- | src/test/test/javassist/proxy/ProxySimpleTest.java | 69 |
1 files changed, 68 insertions, 1 deletions
diff --git a/src/test/test/javassist/proxy/ProxySimpleTest.java b/src/test/test/javassist/proxy/ProxySimpleTest.java index 249a2542..bbae9d8d 100644 --- a/src/test/test/javassist/proxy/ProxySimpleTest.java +++ b/src/test/test/javassist/proxy/ProxySimpleTest.java @@ -1,7 +1,6 @@ package test.javassist.proxy; import junit.framework.TestCase; -import testproxy.ProxyTester.ReadWriteData; import java.io.FileInputStream; import java.io.FileOutputStream; @@ -209,4 +208,72 @@ public class ProxySimpleTest extends TestCase { public static class Default4 extends Default3 { public int baz() { return super.baz(); } } + + public void testPublicProxy() throws Exception { + ProxyFactory f = new ProxyFactory(); + f.writeDirectory = "./proxy"; + f.setSuperclass(PubProxy.class); + Class c = f.createClass(); + MethodHandler mi = new MethodHandler() { + public Object invoke(Object self, Method m, Method proceed, + Object[] args) throws Throwable { + PubProxy.result += args[0].toString(); + return proceed.invoke(self, args); + } + }; + PubProxy.result = ""; + PubProxy foo = (PubProxy)c.getConstructor().newInstance(); + ((Proxy)foo).setHandler(mi); + foo.foo(1); + foo.bar(2); + foo.baz(3); + assertEquals("c1p2q3r", PubProxy.result); + } + + public static class PubProxy { + public static String result; + public PubProxy() { result += "c"; } + PubProxy(int i) { result += "d"; } + void foo(int i) { result += "p"; } + protected void bar(int i) { result += "q"; } + public void baz(int i) { result += "r"; } + } + + public void testPublicProxy2() throws Exception { + boolean b = ProxyFactory.onlyPublicMethods; + ProxyFactory.onlyPublicMethods = true; + ProxyFactory f = new ProxyFactory(); + f.writeDirectory = "./proxy"; + f.setSuperclass(PubProxy2.class); + Class c = f.createClass(); + MethodHandler mi = new MethodHandler() { + public Object invoke(Object self, Method m, Method proceed, + Object[] args) throws Throwable { + PubProxy2.result += args[0].toString(); + return proceed.invoke(self, args); + } + }; + + PubProxy2.result = ""; + try { + PubProxy2 foo = (PubProxy2)c.getConstructor().newInstance(); + ((Proxy)foo).setHandler(mi); + foo.foo(1); // mi does not intercept this call. + foo.bar(2); + foo.baz(3); + assertEquals("cp2q3r", PubProxy2.result); + } + finally { + ProxyFactory.onlyPublicMethods = b; + } + } + + public static class PubProxy2 { + public static String result; + public PubProxy2() { result += "c"; } + PubProxy2(int i) { result += "d"; } + void foo(int i) { result += "p"; } + protected void bar(int i) { result += "q"; } + public void baz(int i) { result += "r"; } + } } |