68
|
1 /*
|
|
2 Copyright (c) 2008 Franklin Schmidt <fschmidt@gmail.com>
|
|
3
|
|
4 Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
5 of this software and associated documentation files (the "Software"), to deal
|
|
6 in the Software without restriction, including without limitation the rights
|
|
7 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
8 copies of the Software, and to permit persons to whom the Software is
|
|
9 furnished to do so, subject to the following conditions:
|
|
10
|
|
11 The above copyright notice and this permission notice shall be included in
|
|
12 all copies or substantial portions of the Software.
|
|
13
|
|
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
17 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
18 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
19 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
20 THE SOFTWARE.
|
|
21 */
|
|
22
|
|
23 package fschmidt.db.util;
|
|
24
|
|
25 import java.lang.reflect.InvocationHandler;
|
|
26 import java.lang.reflect.InvocationTargetException;
|
|
27 import java.lang.reflect.Method;
|
|
28 import java.lang.reflect.Proxy;
|
|
29 import java.lang.reflect.Modifier;
|
|
30 import java.util.Map;
|
|
31 import java.util.HashMap;
|
|
32 import java.util.Arrays;
|
|
33
|
|
34
|
|
35 public class OverridingProxy<C,S> {
|
|
36 private final Class<C> clsC;
|
|
37 private final Map<MethodDesc,Method> methods = new HashMap<MethodDesc,Method>();
|
|
38
|
|
39 public OverridingProxy(Class<C> clsC,Class<S> clsS) {
|
|
40 this.clsC = clsC;
|
|
41 for( Method m : clsS.getDeclaredMethods() ) {
|
|
42 if( Modifier.isPublic(m.getModifiers()) ) {
|
|
43 m.setAccessible(true);
|
|
44 methods.put( new MethodDesc(m), m );
|
|
45 }
|
|
46 }
|
|
47 }
|
|
48
|
|
49 public C newInstance(C obj,S override) {
|
|
50 @SuppressWarnings("unchecked")
|
|
51 C c = (C)Proxy.newProxyInstance(
|
|
52 clsC.getClassLoader(),
|
|
53 new Class[]{clsC},
|
|
54 newInvocationHandler(obj,override)
|
|
55 );
|
|
56 return c;
|
|
57 }
|
|
58
|
|
59 protected InvocationHandler newInvocationHandler(C obj,S override) {
|
|
60 return new OverridingInvocationHandler(obj,override);
|
|
61 }
|
|
62
|
|
63 protected class OverridingInvocationHandler implements InvocationHandler {
|
|
64 protected final C obj;
|
|
65 protected final S override;
|
|
66
|
|
67 protected OverridingInvocationHandler(C obj,S override) {
|
|
68 this.obj = obj;
|
|
69 this.override = override;
|
|
70 }
|
|
71
|
|
72 public Object invoke(Object proxy,Method method, Object[] args)
|
|
73 throws Throwable
|
|
74 {
|
|
75 Method m2 = methods.get(new MethodDesc(method));
|
|
76 try {
|
|
77 return m2!=null ? m2.invoke(override,args) : method.invoke(obj,args);
|
|
78 } catch (InvocationTargetException e) {
|
|
79 throw e.getTargetException();
|
|
80 }
|
|
81 }
|
|
82 }
|
|
83
|
|
84 private static final class MethodDesc {
|
|
85 private final String name;
|
|
86 private final Class[] params;
|
|
87 private final Class rtn;
|
|
88
|
|
89 MethodDesc(Method method) {
|
|
90 this.name = method.getName();
|
|
91 this.params = method.getParameterTypes();
|
|
92 this.rtn = method.getReturnType();
|
|
93 }
|
|
94
|
|
95 public boolean equals(Object obj) {
|
|
96 if( !(obj instanceof MethodDesc) )
|
|
97 return false;
|
|
98 MethodDesc md = (MethodDesc)obj;
|
|
99 return name.equals(md.name) && Arrays.equals(params,md.params) && rtn.equals(md.rtn);
|
|
100 }
|
|
101
|
|
102 public int hashCode() {
|
|
103 return name.hashCode();
|
|
104 }
|
|
105
|
|
106 public String toString() {
|
|
107 StringBuilder buf = new StringBuilder();
|
|
108 buf.append( rtn ).append( " " ).append( name ).append( "(" );
|
|
109 if( params.length > 0 ) {
|
|
110 buf.append( params[0] );
|
|
111 for( int i=1; i<params.length; i++ ) {
|
|
112 buf.append( ',' ).append( params[i] );
|
|
113 }
|
|
114 }
|
|
115 buf.append( ")" );
|
|
116 return buf.toString();
|
|
117 }
|
|
118 }
|
|
119
|
|
120 }
|