|
4 | 4 | import java.lang.reflect.InvocationTargetException; |
5 | 5 | import java.lang.reflect.Method; |
6 | 6 | import java.lang.reflect.Proxy; |
| 7 | +import java.util.Arrays; |
| 8 | +import java.util.HashSet; |
| 9 | +import java.util.Set; |
7 | 10 | import java.util.concurrent.ExecutionException; |
8 | 11 | import java.util.concurrent.Future; |
9 | 12 |
|
@@ -57,177 +60,38 @@ public static <T> T enable(T client) { |
57 | 60 | */ |
58 | 61 | static final class ClientInvocationHandler implements InvocationHandler { |
59 | 62 |
|
60 | | - private final Client target; |
| 63 | + private static final Set<Class<?>> PROXIED_RETURN_TYPES = new HashSet<>(Arrays.asList( |
| 64 | + WebTarget.class, Invocation.Builder.class, Invocation.class, AsyncInvoker.class, Future.class)); |
61 | 65 |
|
62 | | - ClientInvocationHandler(Client target) { |
63 | | - this.target = target; |
64 | | - } |
65 | | - |
66 | | - @Override |
67 | | - public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { |
68 | | - try { |
69 | | - if (WebTarget.class.equals(method.getReturnType())) { |
70 | | - return createProxy(WebTarget.class, |
71 | | - new WebTargetInvocationHandler((WebTarget) method.invoke(target, args))); |
72 | | - } else { |
73 | | - return method.invoke(target, args); |
74 | | - } |
75 | | - } catch (InvocationTargetException e) { |
76 | | - throw e.getTargetException(); |
77 | | - } |
78 | | - } |
79 | | - |
80 | | - } |
81 | | - |
82 | | - /** |
83 | | - * JDK Dynamic Proxy InvocationHandler for JAX-RS WebTarget. |
84 | | - */ |
85 | | - static final class WebTargetInvocationHandler implements InvocationHandler { |
86 | | - |
87 | | - private final WebTarget target; |
88 | | - |
89 | | - WebTargetInvocationHandler(WebTarget target) { |
90 | | - this.target = target; |
91 | | - } |
92 | | - |
93 | | - @Override |
94 | | - public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { |
95 | | - try { |
96 | | - if (WebTarget.class.equals(method.getReturnType())) { |
97 | | - return createProxy(WebTarget.class, |
98 | | - new WebTargetInvocationHandler((WebTarget) method.invoke(target, args))); |
99 | | - } else if (Invocation.Builder.class.equals(method.getReturnType())) { |
100 | | - return createProxy(Invocation.Builder.class, |
101 | | - new InvocationBuilderInvocationHandler((Invocation.Builder) method.invoke(target, args))); |
102 | | - } else { |
103 | | - return method.invoke(target, args); |
104 | | - } |
105 | | - } catch (InvocationTargetException e) { |
106 | | - throw e.getTargetException(); |
107 | | - } |
108 | | - } |
109 | | - |
110 | | - } |
111 | | - |
112 | | - /** |
113 | | - * JDK Dynamic Proxy InvocationHandler for JAX-RS Invocation.Builder. |
114 | | - */ |
115 | | - private static final class InvocationBuilderInvocationHandler implements InvocationHandler { |
116 | | - |
117 | | - private final Invocation.Builder target; |
118 | | - |
119 | | - InvocationBuilderInvocationHandler(Invocation.Builder target) { |
120 | | - this.target = target; |
121 | | - } |
122 | | - |
123 | | - @Override |
124 | | - public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { |
125 | | - try { |
126 | | - if (Invocation.Builder.class.equals(method.getReturnType())) { |
127 | | - return createProxy(Invocation.Builder.class, |
128 | | - new InvocationBuilderInvocationHandler((Invocation.Builder) method.invoke(target, args))); |
129 | | - } else if (Invocation.class.equals(method.getReturnType())) { |
130 | | - return createProxy(Invocation.class, |
131 | | - new InvocationInvocationHandler((Invocation) method.invoke(target, args))); |
132 | | - } else if (AsyncInvoker.class.equals(method.getReturnType())) { |
133 | | - return createProxy(AsyncInvoker.class, |
134 | | - new AsyncInvokerInvocationHandler((AsyncInvoker) method.invoke(target, args))); |
135 | | - } else { |
136 | | - return method.invoke(target, args); |
137 | | - } |
138 | | - } catch (InvocationTargetException e) { |
139 | | - if (e.getTargetException() instanceof ProblemWrapper) { |
140 | | - throw ((ProblemWrapper) e.getTargetException()).getProblem(); |
141 | | - } |
142 | | - throw e.getTargetException(); |
143 | | - } |
144 | | - } |
145 | | - |
146 | | - } |
147 | | - |
148 | | - /** |
149 | | - * JDK Dynamic Proxy InvocationHandler for JAX-RS Invocation. |
150 | | - */ |
151 | | - private static final class InvocationInvocationHandler implements InvocationHandler { |
152 | | - |
153 | | - private final Invocation target; |
| 66 | + private final Object target; |
154 | 67 |
|
155 | | - InvocationInvocationHandler(Invocation target) { |
| 68 | + ClientInvocationHandler(Object target) { |
156 | 69 | this.target = target; |
157 | 70 | } |
158 | 71 |
|
159 | 72 | @Override |
160 | 73 | public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { |
161 | | - try { |
162 | | - if (Invocation.class.equals(method.getReturnType())) { |
163 | | - return createProxy(Invocation.class, |
164 | | - new InvocationInvocationHandler((Invocation) method.invoke(target, args))); |
165 | | - } else if (Future.class.equals(method.getReturnType())) { |
166 | | - return createProxy(Future.class, |
167 | | - new FutureInvocationHandler((Future<?>) method.invoke(target, args))); |
168 | | - } else { |
169 | | - return method.invoke(target, args); |
170 | | - } |
171 | | - } catch (InvocationTargetException e) { |
172 | | - if (e.getTargetException() instanceof ProblemWrapper) { |
173 | | - throw ((ProblemWrapper) e.getTargetException()).getProblem(); |
| 74 | + if (PROXIED_RETURN_TYPES.contains(method.getReturnType())) { |
| 75 | + try { |
| 76 | + return createProxy(method.getReturnType(), |
| 77 | + new ClientInvocationHandler(method.invoke(target, args))); |
| 78 | + } catch (InvocationTargetException e) { |
| 79 | + throw e.getTargetException(); |
174 | 80 | } |
175 | | - throw e.getTargetException(); |
176 | | - } |
177 | | - } |
178 | | - |
179 | | - } |
180 | | - |
181 | | - /** |
182 | | - * JDK Dynamic Proxy InvocationHandler for JAX-RS AsyncInvoker. |
183 | | - */ |
184 | | - private static final class AsyncInvokerInvocationHandler implements InvocationHandler { |
185 | | - |
186 | | - private final AsyncInvoker target; |
187 | | - |
188 | | - AsyncInvokerInvocationHandler(AsyncInvoker target) { |
189 | | - this.target = target; |
190 | | - } |
191 | | - |
192 | | - @Override |
193 | | - public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { |
194 | | - try { |
195 | | - if (Future.class.equals(method.getReturnType())) { |
196 | | - return createProxy(Future.class, |
197 | | - new FutureInvocationHandler((Future<?>) method.invoke(target, args))); |
198 | | - } else { |
| 81 | + } else { |
| 82 | + try { |
199 | 83 | return method.invoke(target, args); |
200 | | - } |
201 | | - } catch (InvocationTargetException e) { |
202 | | - throw e.getTargetException(); |
203 | | - } |
204 | | - } |
205 | | - |
206 | | - } |
207 | | - |
208 | | - /** |
209 | | - * JDK Dynamic Proxy InvocationHandler for Future returned by JAX-RS Client. |
210 | | - */ |
211 | | - private static final class FutureInvocationHandler implements InvocationHandler { |
212 | | - |
213 | | - private final Future<?> target; |
214 | | - |
215 | | - FutureInvocationHandler(Future<?> target) { |
216 | | - this.target = target; |
217 | | - } |
218 | | - |
219 | | - @Override |
220 | | - public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { |
221 | | - try { |
222 | | - return method.invoke(target, args); |
223 | | - } catch (InvocationTargetException e) { |
224 | | - if (e.getTargetException() instanceof ExecutionException) { |
225 | | - ExecutionException executionException = (ExecutionException) e.getTargetException(); |
226 | | - if (executionException.getCause() instanceof ProblemWrapper) { |
227 | | - throw ((ProblemWrapper) executionException.getCause()).getProblem(); |
| 84 | + } catch (InvocationTargetException e) { |
| 85 | + if (e.getTargetException() instanceof ProblemWrapper) { |
| 86 | + throw ((ProblemWrapper) e.getTargetException()).getProblem(); |
| 87 | + } else if (e.getTargetException() instanceof ExecutionException) { |
| 88 | + ExecutionException executionException = (ExecutionException) e.getTargetException(); |
| 89 | + if (executionException.getCause() instanceof ProblemWrapper) { |
| 90 | + throw ((ProblemWrapper) executionException.getCause()).getProblem(); |
| 91 | + } |
228 | 92 | } |
| 93 | + throw e.getTargetException(); |
229 | 94 | } |
230 | | - throw e.getTargetException(); |
231 | 95 | } |
232 | 96 | } |
233 | 97 |
|
|
0 commit comments