From 604e8741298bcd2607936a1205bc746e367ea546 Mon Sep 17 00:00:00 2001 From: chiba Date: Sun, 11 Apr 2010 16:51:16 +0000 Subject: [PATCH] fixes for JASSIST-42 and JASSIST-97 git-svn-id: http://anonsvn.jboss.org/repos/javassist/trunk@524 30ef5769-5b8d-40dd-aea6-55b5d6557bb3 --- .../javassist/util/proxy/ProxyFactory.java | 10 +-- .../test/javassist/proxy/ProxySimpleTest.java | 64 +++++++++++++++++++ 2 files changed, 69 insertions(+), 5 deletions(-) create mode 100644 src/test/test/javassist/proxy/ProxySimpleTest.java diff --git a/src/main/javassist/util/proxy/ProxyFactory.java b/src/main/javassist/util/proxy/ProxyFactory.java index 93b449c3..e407778a 100644 --- a/src/main/javassist/util/proxy/ProxyFactory.java +++ b/src/main/javassist/util/proxy/ProxyFactory.java @@ -780,7 +780,7 @@ public class ProxyFactory { Method m = (Method)e.getValue(); int mod = m.getModifiers(); if (!Modifier.isFinal(mod) && !Modifier.isStatic(mod) - && isVisible(mod, packageName, m) && filter.isHandled(m)) { + && isVisible(mod, packageName, m) && (filter == null || filter.isHandled(m))) { setBit(signature, idx); } } @@ -888,11 +888,11 @@ public class ProxyFactory { Method meth = (Method)e.getValue(); int mod = meth.getModifiers(); if (!Modifier.isFinal(mod) && !Modifier.isStatic(mod) - && isVisible(mod, packageName, meth)) + && isVisible(mod, packageName, meth)) if (testBit(signature, index)) - if (methodFilter == null || methodFilter.isHandled(meth)) - override(className, meth, prefix, index, - keyToDesc(key), cf, cp); + if (methodFilter == null || methodFilter.isHandled(meth)) + override(className, meth, prefix, index, + keyToDesc(key), cf, cp); index++; } diff --git a/src/test/test/javassist/proxy/ProxySimpleTest.java b/src/test/test/javassist/proxy/ProxySimpleTest.java new file mode 100644 index 00000000..f74fce45 --- /dev/null +++ b/src/test/test/javassist/proxy/ProxySimpleTest.java @@ -0,0 +1,64 @@ +package test.javassist.proxy; + +import junit.framework.TestCase; + +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.ObjectInputStream; +import java.io.ObjectOutputStream; +import java.io.Serializable; +import java.lang.reflect.Method; + +import javassist.util.proxy.ProxyFactory; + +public class ProxySimpleTest extends TestCase { + public void testReadWrite() throws Exception { + final String fileName = "read-write.bin"; + ProxyFactory.ClassLoaderProvider cp = ProxyFactory.classLoaderProvider; + ProxyFactory.classLoaderProvider = new ProxyFactory.ClassLoaderProvider() { + public ClassLoader get(ProxyFactory pf) { + return new javassist.Loader(); + } + }; + ProxyFactory pf = new ProxyFactory(); + pf.setSuperclass(ReadWriteData.class); + Object data = pf.createClass().newInstance(); + // Object data = new ReadWriteData(); + ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(fileName)); + oos.writeObject(data); + oos.close(); + ProxyFactory.classLoaderProvider = cp; + + ObjectInputStream ois = new ObjectInputStream(new FileInputStream(fileName)); + Object data2 = ois.readObject(); + ois.close(); + int i = ((ReadWriteData)data2).foo(); + assertEquals(4, i); + } + + public static class ReadWriteData implements Serializable { + public int foo() { return 4; } + } + + public void testWriteReplace() throws Exception { + ProxyFactory pf = new ProxyFactory(); + pf.setSuperclass(WriteReplace.class); + Object data = pf.createClass().newInstance(); + assertEquals(data, ((WriteReplace)data).writeReplace()); + + ProxyFactory pf2 = new ProxyFactory(); + pf2.setSuperclass(WriteReplace2.class); + Object data2 = pf2.createClass().newInstance(); + Method meth = data2.getClass().getDeclaredMethod("writeReplace", new Class[0]); + assertEquals("javassist.util.proxy.SerializedProxy", + meth.invoke(data2, new Object[0]).getClass().getName()); + } + + public static class WriteReplace implements Serializable { + public Object writeReplace() { return this; } + } + + public static class WriteReplace2 implements Serializable { + public Object writeReplace(int i) { return new Integer(i); } + } +} -- 2.39.5