From 5e4572a90f9a11618f5a076ae573932b71a7e13e Mon Sep 17 00:00:00 2001
From: chibash -version 3.21
javassist.tools.Callback
was modified to be Java 1.4 compatible.
+The parameter type of Callback#result()
was changed.
Example of how to create and insert a callback:
** ctMethod.insertAfter(new Callback("Thread.currentThread()") { - * public void result(Object... objects) { + * public void result(Object[] objects) { * Thread thread = (Thread) objects[0]; * // do something with thread... * } @@ -37,7 +37,7 @@ import java.util.UUID; *Contains utility methods for inserts callbacks in
*CtBehaviour
, example:* insertAfter(ctBehaviour, new Callback("Thread.currentThread(), dummyString") { - * public void result(Object... objects) { + * public void result(Object[] objects) { * Thread thread = (Thread) objects[0]; * // do something with thread... * } @@ -45,10 +45,11 @@ import java.util.UUID; ** * @author Marten Hedborg + * @author Shigeru Chiba */ public abstract class Callback { - public static HashMapcallbacks = new HashMap (); + public static HashMap callbacks = new HashMap(); private final String sourceCode; @@ -70,9 +71,8 @@ public abstract class Callback { * * @param objects Objects that the bytecode in callback returns */ - public abstract void result(Object... objects); + public abstract void result(Object[] objects); - @Override public String toString(){ return sourceCode(); } diff --git a/src/test/javassist/JvstTest.java b/src/test/javassist/JvstTest.java index 791b0820..9a671de6 100644 --- a/src/test/javassist/JvstTest.java +++ b/src/test/javassist/JvstTest.java @@ -842,7 +842,7 @@ public class JvstTest extends JvstTestRoot { // cloader.loadClass(cc.getName()); java.io.File genDir = new java.io.File("."); java.net.URLClassLoader ucl = new java.net.URLClassLoader( - new java.net.URL[] { genDir.toURL() }, null); + new java.net.URL[] { genDir.toURI().toURL() }, null); Class intf = ucl.loadClass("test1.MkInterface"); } diff --git a/src/test/javassist/JvstTest2.java b/src/test/javassist/JvstTest2.java index 411c1e97..4ff84784 100644 --- a/src/test/javassist/JvstTest2.java +++ b/src/test/javassist/JvstTest2.java @@ -516,15 +516,15 @@ public class JvstTest2 extends JvstTestRoot { CtClass out = sloader.get("test2.Anon"); CtClass inner = sloader.get("test2.Anon$1"); if (System.getProperty("java.vm.version").startsWith("1.4")) - assertTrue(inner.getEnclosingMethod() == null); + assertTrue(inner.getEnclosingBehavior() == null); else { - assertEquals("make", inner.getEnclosingMethod().getName()); + assertEquals("make", inner.getEnclosingBehavior().getName()); assertEquals(out, inner.getDeclaringClass()); assertEquals(out, - inner.getEnclosingMethod().getDeclaringClass()); + inner.getEnclosingBehavior().getDeclaringClass()); } - assertNull(out.getEnclosingMethod()); + assertNull(out.getEnclosingBehavior()); assertNull(out.getEnclosingBehavior()); CtClass inner2 = sloader.get("test2.Anon$Anon2$1"); diff --git a/src/test/test/javassist/proxy/ProxyFactoryCompatibilityTest.java b/src/test/test/javassist/proxy/ProxyFactoryCompatibilityTest.java index 5b72d82e..0b255064 100644 --- a/src/test/test/javassist/proxy/ProxyFactoryCompatibilityTest.java +++ b/src/test/test/javassist/proxy/ProxyFactoryCompatibilityTest.java @@ -3,6 +3,7 @@ package test.javassist.proxy; import javassist.*; import javassist.util.proxy.MethodFilter; import javassist.util.proxy.MethodHandler; +import javassist.util.proxy.Proxy; import javassist.util.proxy.ProxyFactory; import javassist.util.proxy.ProxyObject; import junit.framework.TestCase; @@ -71,22 +72,23 @@ public class ProxyFactoryCompatibilityTest extends TestCase factory.setSuperclass(TestClass.class); factory.setInterfaces(new Class[] { TestInterface.class}); factory.setFilter(filter); - factory.setHandler(handler); // create the same class twice and check that it is reused Class proxyClass3 = factory.createClass(); System.out.println("created third class " + proxyClass3.getName()); TestClass proxy3 = (TestClass)proxyClass3.newInstance(); + ((Proxy)proxy3).setHandler(handler); proxy3.testMethod(); assertTrue(proxy3.isTestCalled()); Class proxyClass4 = factory.createClass(); System.out.println("created fourth class " + proxyClass4.getName()); TestClass proxy4 = (TestClass)proxyClass4.newInstance(); + ((Proxy)proxy4).setHandler(handler); proxy4.testMethod(); assertTrue(proxy4.isTestCalled()); - assertTrue(proxyClass3 != proxyClass4); + assertTrue(proxyClass3 == proxyClass4); } /** diff --git a/src/test/testproxy/ProxyFactoryPerformanceTest.java b/src/test/testproxy/ProxyFactoryPerformanceTest.java index 5ce0adb6..d2081ae2 100644 --- a/src/test/testproxy/ProxyFactoryPerformanceTest.java +++ b/src/test/testproxy/ProxyFactoryPerformanceTest.java @@ -104,7 +104,7 @@ class ProxyMaker extends Thread implements MethodHandler { factory.setSuperclass(SampleBean.class); factory.setInterfaces(SampleBean.class.getInterfaces()); factory.setFilter(FINALIZE_FILTER); - factory.setHandler(this); + // factory.setHandler(this); Class proxyClass = factory.createClass(); //System.out.println("proxy name: " + proxyClass.getName()); diff --git a/src/test/testproxy/ProxyTester.java b/src/test/testproxy/ProxyTester.java index e06848f8..77bdfa05 100644 --- a/src/test/testproxy/ProxyTester.java +++ b/src/test/testproxy/ProxyTester.java @@ -8,6 +8,7 @@ import javassist.util.proxy.ProxyFactory; import javassist.util.proxy.MethodFilter; import javassist.util.proxy.MethodHandler; import javassist.util.proxy.ProxyObject; +import javassist.util.proxy.Proxy; import junit.framework.Assert; import junit.framework.TestCase; import java.io.*; @@ -44,7 +45,7 @@ public class ProxyTester extends TestCase { return proceed.invoke(self, args); else if (m.getReturnType() == int.class) - return new Integer(3); + return Integer.valueOf(3); else return "OK"; } @@ -60,11 +61,12 @@ public class ProxyTester extends TestCase { ProxyFactory f = new ProxyFactory(); f.setSuperclass(Target.class); Interceptor1 interceptor = new Interceptor1(); - f.setHandler(interceptor); + // f.setHandler(interceptor); f.setFilter(finalizeRemover); f.writeDirectory = "."; Class c = f.createClass(); Target obj = (Target)c.newInstance(); + ((Proxy)obj).setHandler(interceptor); obj.m(); assertEquals(true, obj.m(true)); assertEquals((byte)1, obj.m1((byte)1)); @@ -87,10 +89,11 @@ public class ProxyTester extends TestCase { ProxyFactory f = new ProxyFactory(); f.setSuperclass(Target1.class); Interceptor1 interceptor = new Interceptor1(); - f.setHandler(interceptor); + // f.setHandler(interceptor); f.setFilter(finalizeRemover); Class c = f.createClass(); Target1 obj = (Target1)c.newInstance(); + ((Proxy)obj).setHandler(interceptor); assertEquals(null, obj.m(null)); assertEquals(1, interceptor.counter); } @@ -98,10 +101,11 @@ public class ProxyTester extends TestCase { public void testObject() throws Exception { ProxyFactory f = new ProxyFactory(); Interceptor1 interceptor = new Interceptor1(); - f.setHandler(interceptor); + // f.setHandler(interceptor); f.setFilter(finalizeRemover); Class c = f.createClass(); Object obj = (Object)c.newInstance(); + ((Proxy)obj).setHandler(interceptor); System.out.println(obj.toString()); assertEquals(2, interceptor.counter); } @@ -110,10 +114,11 @@ public class ProxyTester extends TestCase { ProxyFactory f = new ProxyFactory(); f.writeDirectory = "."; Interceptor1 interceptor = new Interceptor1(); - f.setHandler(interceptor); + // f.setHandler(interceptor); f.setFilter(finalizeRemover); Class c = f.createClass(); Object obj = (Object)c.newInstance(); + ((Proxy)obj).setHandler(interceptor); System.out.println("setter1: " + obj.toString()); ((ProxyObject)obj).setHandler(new MethodHandler() { public Object invoke(Object self, Method m, Method proceed, @@ -128,7 +133,7 @@ public class ProxyTester extends TestCase { public void testString() throws Exception { ProxyFactory f = new ProxyFactory(); Interceptor1 interceptor = new Interceptor1(); - f.setHandler(interceptor); + // f.setHandler(interceptor); f.setFilter(finalizeRemover); f.setSuperclass(String.class); try { @@ -143,7 +148,7 @@ public class ProxyTester extends TestCase { public void testConstructor() throws Exception { ProxyFactory f = new ProxyFactory(); Interceptor1 interceptor = new Interceptor1(); - f.setHandler(interceptor); + // f.setHandler(interceptor); f.setFilter(finalizeRemover); f.setSuperclass(Target2.class); Class c = f.createClass(); @@ -156,7 +161,8 @@ public class ProxyTester extends TestCase { assertEquals(0, m1.getExceptionTypes().length); assertEquals("java.io.IOException", m2.getExceptionTypes()[0].getName()); - Target2 t2 = (Target2)con1.newInstance(new Object[] { new Integer(1) }); + Target2 t2 = (Target2)con1.newInstance(new Object[] { Integer.valueOf(1) }); + ((Proxy)t2).setHandler(interceptor); System.out.println(t2.toString()); assertEquals(2, interceptor.counter); @@ -169,11 +175,12 @@ public class ProxyTester extends TestCase { public void testInterface() throws Exception { ProxyFactory f = new ProxyFactory(); Interceptor2 interceptor2 = new Interceptor2(); - f.setHandler(interceptor2); + // f.setHandler(interceptor2); f.setFilter(finalizeRemover); f.setInterfaces(new Class[] { Target3.class }); Class c = f.createClass(); Target3 obj = (Target3)c.newInstance(); + ((Proxy)obj).setHandler(interceptor2); assertEquals("OK", obj.m()); System.out.println(obj.toString()); assertEquals(3, interceptor2.counter); @@ -182,17 +189,19 @@ public class ProxyTester extends TestCase { public void test2Interfaces() throws Exception { ProxyFactory f = new ProxyFactory(); Interceptor2 interceptor2 = new Interceptor2(); - f.setHandler(interceptor2); + // f.setHandler(interceptor2); f.setFilter(finalizeRemover); f.setInterfaces(new Class[] { Target3.class, Target4.class }); Class c = f.createClass(); Target3 obj = (Target3)c.newInstance(); + ((Proxy)obj).setHandler(interceptor2); assertEquals("OK", obj.m()); System.out.println(obj.toString()); assertEquals(3, interceptor2.counter); interceptor2.counter = 0; Target4 obj4 = (Target4)c.newInstance(); + ((Proxy)obj4).setHandler(interceptor2); assertEquals(3, obj4.bar4()); assertEquals(3, obj4.foo4()); assertEquals(2, interceptor2.counter); @@ -201,7 +210,7 @@ public class ProxyTester extends TestCase { public void testFilter() throws Exception { ProxyFactory f = new ProxyFactory(); Interceptor2 interceptor2 = new Interceptor2(); - f.setHandler(interceptor2); + // f.setHandler(interceptor2); f.setFilter(finalizeRemover); f.setInterfaces(new Class[] { Target3.class }); f.setFilter(new MethodFilter() { @@ -211,6 +220,7 @@ public class ProxyTester extends TestCase { }); Class c = f.createClass(); Target3 obj = (Target3)c.newInstance(); + ((Proxy)obj).setHandler(interceptor2); assertEquals("OK", obj.m()); System.out.println(obj.toString()); assertEquals(1, interceptor2.counter); @@ -243,10 +253,11 @@ public class ProxyTester extends TestCase { ProxyFactory f = new ProxyFactory(); f.setSuperclass(Target5.class); Interceptor1 interceptor = new Interceptor1(); - f.setHandler(interceptor); + // f.setHandler(interceptor); f.setFilter(finalizeRemover); Class c = f.createClass(); - Target5 obj = (Target5)f.create(new Class[] { int.class }, new Object[] { new Integer(3) }); + Target5 obj = (Target5)f.create(new Class[] { int.class }, new Object[] { Integer.valueOf(3) }); + ((Proxy)obj).setHandler(interceptor); assertEquals(3, obj.get()); } @@ -256,10 +267,11 @@ public class ProxyTester extends TestCase { f.writeDirectory = "."; f.setSuperclass(BridgeMethod.class); Interceptor1 interceptor = new Interceptor1(); - f.setHandler(interceptor); + // f.setHandler(interceptor); f.setFilter(finalizeRemover); Class c = f.createClass(); BridgeMethod obj = (BridgeMethod)c.newInstance(); + ((Proxy)obj).setHandler(interceptor); Integer value = obj.m1(); assertEquals(7, value.intValue()); BridgeMethodInf inf = (BridgeMethodInf)obj; @@ -310,7 +322,8 @@ public class ProxyTester extends TestCase { ProxyFactory.classLoaderProvider = cp; } - public void testCache() throws Exception { + @SuppressWarnings("deprecation") + public void testCache() throws Exception { boolean prev = ProxyFactory.useCache; ProxyFactory.useCache = true; ProxyFactory f = new ProxyFactory(); @@ -321,8 +334,9 @@ public class ProxyTester extends TestCase { assertEquals(c, f2.createClass()); ProxyFactory f3 = new ProxyFactory(); f3.setSuperclass(Cache1.class); - f3.setHandler(new Interceptor1()); + f3.setHandler(new Interceptor1()); // deprecated assertFalse(c == f3.createClass()); + ProxyFactory.useCache = true; ProxyFactory f4 = new ProxyFactory(); f4.setSuperclass(Cache1.class); f4.setInterfaces(new Class[] { Cache2.class }); -- cgit v1.2.3