1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
|
package test.javassist.proxy;
import java.lang.reflect.Method;
import javassist.util.proxy.ProxyFactory;
import javassist.util.proxy.MethodHandler;
import javassist.util.proxy.MethodFilter;
import javassist.util.proxy.ProxyObject;
import javassist.util.proxy.Proxy;
import junit.framework.TestCase;
@SuppressWarnings({"rawtypes","unchecked"})
public class JBPAPP9257Test extends TestCase {
public void testGetHandler() throws Exception {
ProxyFactory f = new ProxyFactory();
f.setSuperclass(Foo.class);
f.setFilter(new MethodFilter() {
public boolean isHandled(Method m) {
// ignore finalize()
return !m.getName().equals("finalize");
}
});
Class c = f.createClass();
MethodHandler mi = new MethodHandler() {
public Object invoke(Object self, Method m, Method proceed,
Object[] args) throws Throwable {
System.out.println("Name: " + m.getName());
return proceed.invoke(self, args) + "!"; // execute the original
// method.
}
};
Foo foo = (Foo)c.getConstructor().newInstance();
try {
((ProxyObject)foo).setHandler(mi);
fail("foo is a ProxyObject!");
} catch (ClassCastException e) {}
((Proxy)foo).setHandler(mi);
assertEquals("I'm doing something!", foo.doSomething());
assertEquals("This is a secret handler!", foo.getHandler());
}
public void testGetHandler2() throws Exception {
ProxyFactory f = new ProxyFactory();
f.setSuperclass(Foo2.class);
f.setFilter(new MethodFilter() {
public boolean isHandled(Method m) {
// ignore finalize()
return !m.getName().equals("finalize");
}
});
Class c = f.createClass();
MethodHandler mi = new MethodHandler() {
public Object invoke(Object self, Method m, Method proceed,
Object[] args) throws Throwable {
System.out.println("Name: " + m.getName());
return proceed.invoke(self, args) + "!"; // execute the original
// method.
}
};
Foo2 foo = (Foo2)c.getConstructor().newInstance();
try {
((ProxyObject)foo).setHandler(mi);
fail("foo is a ProxyObject!");
} catch (ClassCastException e) {}
((Proxy)foo).setHandler(mi);
assertEquals("do something!", foo.doSomething());
assertEquals("return a string!", foo.getHandler());
}
}
|