-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathChallenge_24.java
56 lines (45 loc) · 1.79 KB
/
Challenge_24.java
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
package challenge21_30;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.ArrayList;
import java.util.List;
/**
* InvocationHandler is the interface implemented by the handler of a proxy class.
* Method the method that has been called
* Throwable is the super class of the exception class
*
* To create a proxy for some interface Tobeproxied
* InvocationHandler handler = new MyIvocationHandler(...);
* Tobeproxied proxy = Proxy.newProxyInstance(handler.getClassLoader(),
* Tobeproxied,
* handler);
*/
public class Challenge_24 implements InvocationHandler {
private final List proxied;
public Challenge_24( List proxied ) {this.proxied = proxied;}
@Override
public Object invoke( Object proxy, Method method, Object[] args ) throws Throwable {
if (method.getName().startsWith("remove")){
return false;
}
if (method.getName().startsWith(""+(char)99) || method.getName().startsWith(""+(char)101)){
return false;
}
return method.invoke(proxied, args);
}
public static void main( String[] args ) {
System.out.println(""+(char)99);
var proxy = (List) Proxy.newProxyInstance(Challenge_24.class.getClassLoader(),
new Class[]{List.class},
new Challenge_24(new ArrayList()));
proxy.add("Barney");
proxy.add("Homer");
proxy.add("Moe");
proxy.add("Homer");
proxy.add("Moe");
proxy.add(proxy.contains("Homer"));
proxy.add(proxy.equals(proxy));
System.out.println("proxy = " + proxy);
}
}