Repository: commons-pool Updated Branches: refs/heads/master 30d5db67a -> e7f09417f
[POOL-338] GenericObjectPool constructor throws an exception. Better exception messages in org.apache.commons.pool2.impl.BaseGenericObjectPool.setEvictionPolicyClassName(String). Project: http://git-wip-us.apache.org/repos/asf/commons-pool/repo Commit: http://git-wip-us.apache.org/repos/asf/commons-pool/commit/a4366f99 Tree: http://git-wip-us.apache.org/repos/asf/commons-pool/tree/a4366f99 Diff: http://git-wip-us.apache.org/repos/asf/commons-pool/diff/a4366f99 Branch: refs/heads/master Commit: a4366f9944c3f3a8f9d79d991a9e271e1e9befa5 Parents: 7aee6c9 Author: Gary Gregory <[email protected]> Authored: Thu Apr 5 12:01:06 2018 -0600 Committer: Gary Gregory <[email protected]> Committed: Thu Apr 5 12:01:06 2018 -0600 ---------------------------------------------------------------------- .../pool2/impl/BaseGenericObjectPool.java | 36 +++++++------------- 1 file changed, 13 insertions(+), 23 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/commons-pool/blob/a4366f99/src/main/java/org/apache/commons/pool2/impl/BaseGenericObjectPool.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/commons/pool2/impl/BaseGenericObjectPool.java b/src/main/java/org/apache/commons/pool2/impl/BaseGenericObjectPool.java index b523e83..b20b8d3 100644 --- a/src/main/java/org/apache/commons/pool2/impl/BaseGenericObjectPool.java +++ b/src/main/java/org/apache/commons/pool2/impl/BaseGenericObjectPool.java @@ -599,46 +599,36 @@ public abstract class BaseGenericObjectPool<T> extends BaseObject { * * @see #getEvictionPolicyClassName() */ - public final void setEvictionPolicyClassName( - final String evictionPolicyClassName) { + public final void setEvictionPolicyClassName(final String evictionPolicyClassName) { + final String EVICTION_POLICY_TYPE_NAME = EvictionPolicy.class.getName(); + final String exMessage = "Unable to create " + EVICTION_POLICY_TYPE_NAME + " instance of type " + + evictionPolicyClassName; try { Class<?> clazz; try { - clazz = Class.forName(evictionPolicyClassName, true, - Thread.currentThread().getContextClassLoader()); + clazz = Class.forName(evictionPolicyClassName, true, Thread.currentThread().getContextClassLoader()); } catch (final ClassNotFoundException e) { clazz = Class.forName(evictionPolicyClassName); } final Object policy = clazz.getConstructor().newInstance(); if (policy instanceof EvictionPolicy<?>) { @SuppressWarnings("unchecked") // safe, because we just checked the class - final - EvictionPolicy<T> evicPolicy = (EvictionPolicy<T>) policy; + final EvictionPolicy<T> evicPolicy = (EvictionPolicy<T>) policy; this.evictionPolicy = evicPolicy; } else { - throw new IllegalArgumentException("[" + evictionPolicyClassName + - "] does not implement EvictionPolicy"); + throw new IllegalArgumentException( + "[" + evictionPolicyClassName + "] does not implement " + EVICTION_POLICY_TYPE_NAME); } } catch (final ClassNotFoundException e) { - throw new IllegalArgumentException( - "Unable to create EvictionPolicy instance of type " + - evictionPolicyClassName, e); + throw new IllegalArgumentException(exMessage, e); } catch (final InstantiationException e) { - throw new IllegalArgumentException( - "Unable to create EvictionPolicy instance of type " + - evictionPolicyClassName, e); + throw new IllegalArgumentException(exMessage, e); } catch (final IllegalAccessException e) { - throw new IllegalArgumentException( - "Unable to create EvictionPolicy instance of type " + - evictionPolicyClassName, e); + throw new IllegalArgumentException(exMessage, e); } catch (final InvocationTargetException e) { - throw new IllegalArgumentException( - "Unable to create EvictionPolicy instance of type " + - evictionPolicyClassName, e); + throw new IllegalArgumentException(exMessage, e); } catch (final NoSuchMethodException e) { - throw new IllegalArgumentException( - "Unable to create EvictionPolicy instance of type " + - evictionPolicyClassName, e); + throw new IllegalArgumentException(exMessage, e); } }
