blob: 2b41af7a632fba4f0fa076022cd967b6ce45510f (
plain)
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
|
package javassist.proxyfactory;
import javassist.util.proxy.MethodHandler;
import javassist.util.proxy.ProxyFactory;
import javassist.util.proxy.ProxyObject;
import junit.framework.TestCase;
import java.io.*;
import java.lang.reflect.Method;
/**
* <a href="mailto:struberg@yahoo.de">Mark Struberg</a>
*/
public class ProxyFactoryTest extends TestCase {
public void testMethodHandlers() throws Exception {
ProxyFactory fact = new ProxyFactory();
fact.setSuperclass(MyCls.class);
Class proxyClass = fact.createClass();
MyMethodHandler myHandler = new MyMethodHandler();
myHandler.setX(4711);
MyCls myCls = (MyCls) proxyClass.getConstructor().newInstance();
((ProxyObject) myCls).setHandler(myHandler);
MethodHandler h2 = ((ProxyObject) myCls).getHandler();
assertNotNull(h2);
assertTrue(h2 instanceof MyMethodHandler);
}
public void testSerialize() throws Exception {
ProxyFactory fact = new ProxyFactory();
fact.setSuperclass(MyCls.class);
Class proxyClass = fact.createClass();
MyMethodHandler myHandler = new MyMethodHandler();
myHandler.setX(4711);
MyCls myCls = (MyCls) proxyClass.getConstructor().newInstance();
((ProxyObject) myCls).setHandler(myHandler);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(baos);
oos.writeObject(myCls);
byte[] ba = baos.toByteArray();
ByteArrayInputStream bais = new ByteArrayInputStream(ba);
ObjectInputStream ois = new ObjectInputStream(bais);
MyCls myCls2 = (MyCls) ois.readObject();
MethodHandler h2 = ((ProxyObject) myCls2).getHandler();
assertNotNull(h2);
assertTrue(h2 instanceof MyMethodHandler);
}
public static class MyMethodHandler implements MethodHandler, Serializable {
private int x;
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public Object invoke(Object self, Method thisMethod, Method proceed, Object[] args) throws Throwable {
// actually do nothing!
return null;
}
}
public void testJira127() throws Exception {
ProxyFactory proxyFactory = new ProxyFactory();
proxyFactory.setInterfaces(new Class[]{ JIRA127Sub.class });
proxyFactory.createClass();
}
public interface JIRA127 {
JIRA127 get();
}
public interface JIRA127Sub extends JIRA127 {
JIRA127Sub get();
}
public void testDefaultMethod() throws Exception {
ProxyFactory proxyFactory = new ProxyFactory();
//proxyFactory.writeDirectory = "./dump";
proxyFactory.setInterfaces(new Class[]{ TestDefaultI.class });
Class intf = proxyFactory.createClass();
TestDefaultI obj = (TestDefaultI)intf.getConstructor().newInstance();
obj.foo();
ProxyFactory proxyFactory2 = new ProxyFactory();
//proxyFactory2.writeDirectory = "./dump";
proxyFactory2.setSuperclass(TestDefaultC.class);
Class clazz2 = proxyFactory2.createClass();
TestDefaultC obj2 = (TestDefaultC)clazz2.getConstructor().newInstance();
obj2.foo();
obj2.bar();
ProxyFactory proxyFactory3 = new ProxyFactory();
proxyFactory3.setSuperclass(TestDefaultC2.class);
Class clazz3 = proxyFactory3.createClass();
TestDefaultC2 obj3 = (TestDefaultC2)clazz3.getConstructor().newInstance();
obj3.foo();
obj3.bar();
obj3.baz();
}
public static interface TestDefaultI {
default int foo() { return 10; }
}
public static class TestDefaultC implements TestDefaultI {
public int foo() { return 1; }
public int bar() { return TestDefaultI.super.foo(); }
}
public static class TestDefaultC2 extends TestDefaultC {
public int baz() { return super.foo(); }
}
}
|