Skip to content

Commit 8bf8fd8

Browse files
committed
Add an option to JdkProxySource allowing to unwrap UndeclaredThrowableException
1 parent 50ad5b5 commit 8bf8fd8

8 files changed

+157
-42
lines changed

src/main/java/org/apache/commons/pool3/proxy/BaseProxyHandler.java

+21-10
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
*/
1717
package org.apache.commons.pool3.proxy;
1818

19+
import java.lang.reflect.InvocationTargetException;
1920
import java.lang.reflect.Method;
2021

2122
import org.apache.commons.pool3.UsageTracking;
@@ -32,18 +33,21 @@ class BaseProxyHandler<T> {
3233

3334
private volatile T pooledObject;
3435
private final UsageTracking<T> usageTracking;
36+
private final boolean unwrapInvocationTargetException;
3537

36-
/**
37-
* Constructs a new wrapper for the given pooled object.
38-
*
39-
* @param pooledObject The object to wrap
40-
* @param usageTracking The instance, if any (usually the object pool) to
41-
* be provided with usage tracking information for this
42-
* wrapped object
43-
*/
44-
BaseProxyHandler(final T pooledObject, final UsageTracking<T> usageTracking) {
38+
/**
39+
* Constructs a new wrapper for the given pooled object.
40+
*
41+
* @param pooledObject The object to wrap
42+
* @param usageTracking The instance, if any (usually the object pool) to
43+
* be provided with usage tracking information for this
44+
* wrapped object
45+
* @param unwrapInvocationTargetException True to make the proxy throw {@link InvocationTargetException#getTargetException()} instead of {@link InvocationTargetException}
46+
*/
47+
BaseProxyHandler(final T pooledObject, final UsageTracking<T> usageTracking, boolean unwrapInvocationTargetException) {
4548
this.pooledObject = pooledObject;
4649
this.usageTracking = usageTracking;
50+
this.unwrapInvocationTargetException = unwrapInvocationTargetException;
4751
}
4852

4953
/**
@@ -73,7 +77,14 @@ Object doInvoke(final Method method, final Object[] args) throws Throwable {
7377
if (usageTracking != null) {
7478
usageTracking.use(object);
7579
}
76-
return method.invoke(object, args);
80+
try {
81+
return method.invoke(object, args);
82+
} catch (InvocationTargetException e) {
83+
if (unwrapInvocationTargetException) {
84+
throw e.getTargetException();
85+
}
86+
throw e;
87+
}
7788
}
7889

7990
/**

src/main/java/org/apache/commons/pool3/proxy/CglibProxyHandler.java

+8-6
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
*/
1717
package org.apache.commons.pool3.proxy;
1818

19+
import java.lang.reflect.InvocationTargetException;
1920
import java.lang.reflect.Method;
2021

2122
import org.apache.commons.pool3.UsageTracking;
@@ -36,13 +37,14 @@ final class CglibProxyHandler<T> extends BaseProxyHandler<T>
3637
/**
3738
* Constructs a CGLib proxy instance.
3839
*
39-
* @param pooledObject The object to wrap
40-
* @param usageTracking The instance, if any (usually the object pool) to
41-
* be provided with usage tracking information for this
42-
* wrapped object
40+
* @param pooledObject The object to wrap
41+
* @param usageTracking The instance, if any (usually the object pool) to
42+
* be provided with usage tracking information for this
43+
* wrapped object
44+
* @param unwrapInvocationTargetException True to make the proxy throw {@link InvocationTargetException#getTargetException()} instead of {@link InvocationTargetException}
4345
*/
44-
CglibProxyHandler(final T pooledObject, final UsageTracking<T> usageTracking) {
45-
super(pooledObject, usageTracking);
46+
CglibProxyHandler(final T pooledObject, final UsageTracking<T> usageTracking, boolean unwrapInvocationTargetException) {
47+
super(pooledObject, usageTracking, unwrapInvocationTargetException);
4648
}
4749

4850
@Override

src/main/java/org/apache/commons/pool3/proxy/CglibProxySource.java

+15-2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
*/
1717
package org.apache.commons.pool3.proxy;
1818

19+
import java.lang.reflect.InvocationTargetException;
1920
import org.apache.commons.pool3.UsageTracking;
2021

2122
import net.sf.cglib.proxy.Enhancer;
@@ -31,14 +32,26 @@
3132
public class CglibProxySource<T> implements ProxySource<T> {
3233

3334
private final Class<? extends T> superclass;
35+
private final boolean unwrapInvocationTargetException;
36+
37+
/**
38+
* Constructs a new proxy source for the given class.
39+
*
40+
* @param superclass The class to proxy
41+
* @param unwrapInvocationTargetException True to make the proxy throw {@link InvocationTargetException#getTargetException()} instead of {@link InvocationTargetException}
42+
*/
43+
public CglibProxySource(final Class<? extends T> superclass, boolean unwrapInvocationTargetException) {
44+
this.superclass = superclass;
45+
this.unwrapInvocationTargetException = unwrapInvocationTargetException;
46+
}
3447

3548
/**
3649
* Constructs a new proxy source for the given class.
3750
*
3851
* @param superclass The class to proxy
3952
*/
4053
public CglibProxySource(final Class<? extends T> superclass) {
41-
this.superclass = superclass;
54+
this(superclass, false);
4255
}
4356

4457
@SuppressWarnings("unchecked") // Case to T on return
@@ -48,7 +61,7 @@ public T createProxy(final T pooledObject, final UsageTracking<T> usageTracking)
4861
enhancer.setSuperclass(superclass);
4962

5063
final CglibProxyHandler<T> proxyInterceptor =
51-
new CglibProxyHandler<>(pooledObject, usageTracking);
64+
new CglibProxyHandler<>(pooledObject, usageTracking, unwrapInvocationTargetException);
5265
enhancer.setCallback(proxyInterceptor);
5366

5467
return (T) enhancer.create();

src/main/java/org/apache/commons/pool3/proxy/JdkProxyHandler.java

+8-6
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package org.apache.commons.pool3.proxy;
1818

1919
import java.lang.reflect.InvocationHandler;
20+
import java.lang.reflect.InvocationTargetException;
2021
import java.lang.reflect.Method;
2122

2223
import org.apache.commons.pool3.UsageTracking;
@@ -34,13 +35,14 @@ final class JdkProxyHandler<T> extends BaseProxyHandler<T>
3435
/**
3536
* Constructs a Java reflection proxy instance.
3637
*
37-
* @param pooledObject The object to wrap
38-
* @param usageTracking The instance, if any (usually the object pool) to
39-
* be provided with usage tracking information for this
40-
* wrapped object
38+
* @param pooledObject The object to wrap
39+
* @param usageTracking The instance, if any (usually the object pool) to
40+
* be provided with usage tracking information for this
41+
* wrapped object
42+
* @param unwrapInvocationTargetException True to make the proxy throw {@link InvocationTargetException#getTargetException()} instead of {@link InvocationTargetException}
4143
*/
42-
JdkProxyHandler(final T pooledObject, final UsageTracking<T> usageTracking) {
43-
super(pooledObject, usageTracking);
44+
JdkProxyHandler(final T pooledObject, final UsageTracking<T> usageTracking, boolean unwrapInvocationTargetException) {
45+
super(pooledObject, usageTracking, unwrapInvocationTargetException);
4446
}
4547

4648
@Override

src/main/java/org/apache/commons/pool3/proxy/JdkProxySource.java

+16-2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
*/
1717
package org.apache.commons.pool3.proxy;
1818

19+
import java.lang.reflect.InvocationTargetException;
1920
import java.lang.reflect.Proxy;
2021
import java.util.Arrays;
2122

@@ -32,24 +33,37 @@ public class JdkProxySource<T> implements ProxySource<T> {
3233

3334
private final ClassLoader classLoader;
3435
private final Class<?>[] interfaces;
36+
private final boolean unwrapInvocationTargetException;
3537

3638
/**
3739
* Constructs a new proxy source for the given interfaces.
3840
*
3941
* @param classLoader The class loader with which to create the proxy
4042
* @param interfaces The interfaces to proxy
43+
* @param unwrapInvocationTargetException True to make the proxy throw {@link InvocationTargetException#getTargetException()} instead of {@link InvocationTargetException}
4144
*/
42-
public JdkProxySource(final ClassLoader classLoader, final Class<?>[] interfaces) {
45+
public JdkProxySource(final ClassLoader classLoader, final Class<?>[] interfaces, boolean unwrapInvocationTargetException) {
4346
this.classLoader = classLoader;
4447
// Defensive copy
4548
this.interfaces = Arrays.copyOf(interfaces, interfaces.length);
49+
this.unwrapInvocationTargetException = unwrapInvocationTargetException;
50+
}
51+
52+
/**
53+
* Constructs a new proxy source for the given interfaces.
54+
*
55+
* @param classLoader The class loader with which to create the proxy
56+
* @param interfaces The interfaces to proxy
57+
*/
58+
public JdkProxySource(final ClassLoader classLoader, final Class<?>[] interfaces) {
59+
this(classLoader, interfaces, false);
4660
}
4761

4862
@SuppressWarnings("unchecked") // Cast to T on return.
4963
@Override
5064
public T createProxy(final T pooledObject, final UsageTracking<T> usageTracking) {
5165
return (T) Proxy.newProxyInstance(classLoader, interfaces,
52-
new JdkProxyHandler<>(pooledObject, usageTracking));
66+
new JdkProxyHandler<>(pooledObject, usageTracking, unwrapInvocationTargetException));
5367
}
5468

5569
@SuppressWarnings("unchecked")

0 commit comments

Comments
 (0)