Skip to content

Commit 12eb3f9

Browse files
committed
Issue #76 - Refactor code to split method in several
1 parent 474a74c commit 12eb3f9

File tree

1 file changed

+21
-11
lines changed

1 file changed

+21
-11
lines changed

src/main/java/ch/powerunit/extensions/exceptions/InternalHelper.java

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@
1919
*/
2020
package ch.powerunit.extensions.exceptions;
2121

22+
import java.lang.reflect.InvocationHandler;
2223
import java.lang.reflect.InvocationTargetException;
24+
import java.lang.reflect.Method;
2325
import java.lang.reflect.Proxy;
2426
import java.util.ArrayList;
2527
import java.util.Arrays;
@@ -31,19 +33,27 @@ final class InternalHelper {
3133
private InternalHelper() {
3234
}
3335

34-
@SuppressWarnings("unchecked")
3536
public static <T extends ExceptionHandlerSupport<?, ?, ?>> T documented(T target, Supplier<String> toString) {
37+
return proxy(target, (proxy, method, args) -> {
38+
if (method.toString().endsWith(".toString()")) {
39+
return toString.get();
40+
}
41+
return passthruInvoker(target, method, args);
42+
});
43+
}
44+
45+
private static Object passthruInvoker(Object target, Method method, Object[] args) throws Throwable {
46+
try {
47+
return method.invoke(target, args);
48+
} catch (InvocationTargetException e) {
49+
throw e.getCause();
50+
}
51+
}
52+
53+
@SuppressWarnings("unchecked")
54+
private static <T extends ExceptionHandlerSupport<?, ?, ?>> T proxy(T target, InvocationHandler handler) {
3655
return (T) Proxy.newProxyInstance(target.getClass().getClassLoader(),
37-
allInterfaces(target.getClass()).stream().distinct().toArray(Class[]::new), (proxy, method, args) -> {
38-
if (method.toString().endsWith(".toString()")) {
39-
return toString.get();
40-
}
41-
try {
42-
return method.invoke(target, args);
43-
} catch (InvocationTargetException e) {
44-
throw e.getCause();
45-
}
46-
});
56+
allInterfaces(target.getClass()).stream().distinct().toArray(Class[]::new), handler);
4757
}
4858

4959
private static List<Class<?>> allInterfaces(Class<?> target) {

0 commit comments

Comments
 (0)