Skip to content

Commit

Permalink
Add an option to JdkProxySource allowing to unwrap UndeclaredThrowabl…
Browse files Browse the repository at this point in the history
…eException
  • Loading branch information
reda-alaoui committed Dec 6, 2023
1 parent 7eec556 commit 11f9cfe
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 23 deletions.
23 changes: 17 additions & 6 deletions src/main/java/org/apache/commons/pool2/proxy/BaseProxyHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/
package org.apache.commons.pool2.proxy;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import org.apache.commons.pool2.UsageTracking;
Expand All @@ -32,19 +33,22 @@ class BaseProxyHandler<T> {

private volatile T pooledObject;
private final UsageTracking<T> usageTracking;
private final boolean unwrapInvocationTargetException;


/**
* Create a new wrapper for the given pooled object.
*
* @param pooledObject The object to wrap
* @param usageTracking The instance, if any (usually the object pool) to
* be provided with usage tracking information for this
* wrapped object
* @param pooledObject The object to wrap
* @param usageTracking The instance, if any (usually the object pool) to
* be provided with usage tracking information for this
* wrapped object
* @param unwrapInvocationTargetException True to make the proxy throw {@link InvocationTargetException#getTargetException()} instead of {@link InvocationTargetException}
*/
BaseProxyHandler(final T pooledObject, final UsageTracking<T> usageTracking) {
BaseProxyHandler(final T pooledObject, final UsageTracking<T> usageTracking, boolean unwrapInvocationTargetException) {
this.pooledObject = pooledObject;
this.usageTracking = usageTracking;
this.unwrapInvocationTargetException = unwrapInvocationTargetException;
}


Expand Down Expand Up @@ -76,7 +80,14 @@ Object doInvoke(final Method method, final Object[] args) throws Throwable {
if (usageTracking != null) {
usageTracking.use(object);
}
return method.invoke(object, args);
try {
return method.invoke(object, args);
} catch (InvocationTargetException e) {
if (unwrapInvocationTargetException) {
throw e.getTargetException();
}
throw e;
}
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/
package org.apache.commons.pool2.proxy;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import org.apache.commons.pool2.UsageTracking;
Expand All @@ -37,13 +38,14 @@ class CglibProxyHandler<T> extends BaseProxyHandler<T>
/**
* Create a CGLib proxy instance.
*
* @param pooledObject The object to wrap
* @param usageTracking The instance, if any (usually the object pool) to
* be provided with usage tracking information for this
* wrapped object
* @param pooledObject The object to wrap
* @param usageTracking The instance, if any (usually the object pool) to
* be provided with usage tracking information for this
* wrapped object
* @param unwrapInvocationTargetException True to make the proxy throw {@link InvocationTargetException#getTargetException()} instead of {@link InvocationTargetException}
*/
CglibProxyHandler(final T pooledObject, final UsageTracking<T> usageTracking) {
super(pooledObject, usageTracking);
CglibProxyHandler(final T pooledObject, final UsageTracking<T> usageTracking, boolean unwrapInvocationTargetException) {
super(pooledObject, usageTracking, unwrapInvocationTargetException);
}

@Override
Expand Down
17 changes: 15 additions & 2 deletions src/main/java/org/apache/commons/pool2/proxy/CglibProxySource.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/
package org.apache.commons.pool2.proxy;

import java.lang.reflect.InvocationTargetException;
import org.apache.commons.pool2.UsageTracking;

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

private final Class<? extends T> superclass;
private final boolean unwrapInvocationTargetException;

/**
* Create a new proxy source for the given class.
*
* @param superclass The class to proxy
* @param unwrapInvocationTargetException True to make the proxy throw {@link InvocationTargetException#getTargetException()} instead of {@link InvocationTargetException}
*/
public CglibProxySource(final Class<? extends T> superclass, boolean unwrapInvocationTargetException) {
this.superclass = superclass;
this.unwrapInvocationTargetException = unwrapInvocationTargetException;
}

/**
* Constructs a new proxy source for the given class.
*
* @param superclass The class to proxy
*/
public CglibProxySource(final Class<? extends T> superclass) {
this.superclass = superclass;
this(superclass, false);
}

@Override
Expand All @@ -47,7 +60,7 @@ public T createProxy(final T pooledObject, final UsageTracking<T> usageTracking)
enhancer.setSuperclass(superclass);

final CglibProxyHandler<T> proxyInterceptor =
new CglibProxyHandler<>(pooledObject, usageTracking);
new CglibProxyHandler<>(pooledObject, usageTracking, unwrapInvocationTargetException);
enhancer.setCallback(proxyInterceptor);

@SuppressWarnings("unchecked")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package org.apache.commons.pool2.proxy;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import org.apache.commons.pool2.UsageTracking;
Expand All @@ -34,13 +35,14 @@ class JdkProxyHandler<T> extends BaseProxyHandler<T>
/**
* Create a Java reflection proxy instance.
*
* @param pooledObject The object to wrap
* @param usageTracking The instance, if any (usually the object pool) to
* be provided with usage tracking information for this
* wrapped object
* @param pooledObject The object to wrap
* @param usageTracking The instance, if any (usually the object pool) to
* be provided with usage tracking information for this
* wrapped object
* @param unwrapInvocationTargetException True to make the proxy throw {@link InvocationTargetException#getTargetException()} instead of {@link InvocationTargetException}
*/
JdkProxyHandler(final T pooledObject, final UsageTracking<T> usageTracking) {
super(pooledObject, usageTracking);
JdkProxyHandler(final T pooledObject, final UsageTracking<T> usageTracking, boolean unwrapInvocationTargetException) {
super(pooledObject, usageTracking, unwrapInvocationTargetException);
}


Expand Down
19 changes: 16 additions & 3 deletions src/main/java/org/apache/commons/pool2/proxy/JdkProxySource.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/
package org.apache.commons.pool2.proxy;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Proxy;
import java.util.Arrays;

Expand All @@ -32,19 +33,32 @@ public class JdkProxySource<T> implements ProxySource<T> {

private final ClassLoader classLoader;
private final Class<?>[] interfaces;
private final boolean unwrapInvocationTargetException;


/**
* Create a new proxy source for the given interfaces.
*
* @param classLoader The class loader with which to create the proxy
* @param interfaces The interfaces to proxy
* @param unwrapInvocationTargetException True to make the proxy throw {@link InvocationTargetException#getTargetException()} instead of {@link InvocationTargetException}
*/
public JdkProxySource(final ClassLoader classLoader, final Class<?>[] interfaces) {
public JdkProxySource(final ClassLoader classLoader, final Class<?>[] interfaces, boolean unwrapInvocationTargetException) {
this.classLoader = classLoader;
// Defensive copy
this.interfaces = new Class<?>[interfaces.length];
System.arraycopy(interfaces, 0, this.interfaces, 0, interfaces.length);
this.unwrapInvocationTargetException = unwrapInvocationTargetException;
}

/**
* Constructs a new proxy source for the given interfaces.
*
* @param classLoader The class loader with which to create the proxy
* @param interfaces The interfaces to proxy
*/
public JdkProxySource(final ClassLoader classLoader, final Class<?>[] interfaces) {
this(classLoader, interfaces, false);
}


Expand All @@ -53,11 +67,10 @@ public T createProxy(final T pooledObject, final UsageTracking<T> usageTracking)
@SuppressWarnings("unchecked")
final
T proxy = (T) Proxy.newProxyInstance(classLoader, interfaces,
new JdkProxyHandler<>(pooledObject, usageTracking));
new JdkProxyHandler<>(pooledObject, usageTracking, unwrapInvocationTargetException));
return proxy;
}


@Override
public T resolveProxy(final T proxy) {
@SuppressWarnings("unchecked")
Expand Down

0 comments on commit 11f9cfe

Please sign in to comment.