From f161ff66698bea44784926fc6aa9073d96a00cec Mon Sep 17 00:00:00 2001 From: Gary Gregory Date: Fri, 24 May 2024 09:44:20 -0400 Subject: [PATCH] Simplify exception handling Javadoc --- .../org/apache/commons/lang3/ObjectUtils.java | 8 ++++---- .../apache/commons/lang3/event/EventUtils.java | 13 +++---------- .../apache/commons/lang3/ObjectUtilsTest.java | 7 +++++-- .../commons/lang3/event/EventUtilsTest.java | 16 ++++++++-------- 4 files changed, 20 insertions(+), 24 deletions(-) diff --git a/src/main/java/org/apache/commons/lang3/ObjectUtils.java b/src/main/java/org/apache/commons/lang3/ObjectUtils.java index 8ca591bd9..d75b1f210 100644 --- a/src/main/java/org/apache/commons/lang3/ObjectUtils.java +++ b/src/main/java/org/apache/commons/lang3/ObjectUtils.java @@ -235,8 +235,9 @@ public class ObjectUtils { public static T clone(final T obj) { if (obj instanceof Cloneable) { final Object result; + final Class objClass = obj.getClass(); if (isArray(obj)) { - final Class componentType = obj.getClass().getComponentType(); + final Class componentType = objClass.getComponentType(); if (componentType.isPrimitive()) { int length = Array.getLength(obj); result = Array.newInstance(componentType, length); @@ -248,10 +249,9 @@ public class ObjectUtils { } } else { try { - final Method clone = obj.getClass().getMethod("clone"); - result = clone.invoke(obj); + result = objClass.getMethod("clone").invoke(obj); } catch (final ReflectiveOperationException e) { - throw new CloneFailedException("Exception cloning Cloneable type " + obj.getClass().getName(), e.getCause()); + throw new CloneFailedException("Exception cloning Cloneable type " + objClass.getName(), e); } } return (T) result; diff --git a/src/main/java/org/apache/commons/lang3/event/EventUtils.java b/src/main/java/org/apache/commons/lang3/event/EventUtils.java index 736a4ef5b..0708a5d41 100644 --- a/src/main/java/org/apache/commons/lang3/event/EventUtils.java +++ b/src/main/java/org/apache/commons/lang3/event/EventUtils.java @@ -18,7 +18,6 @@ package org.apache.commons.lang3.event; import java.lang.reflect.InvocationHandler; -import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.lang.reflect.Proxy; import java.util.Arrays; @@ -96,16 +95,10 @@ public class EventUtils { public static void addEventListener(final Object eventSource, final Class listenerType, final L listener) { try { MethodUtils.invokeMethod(eventSource, "add" + listenerType.getSimpleName(), listener); - } catch (final NoSuchMethodException e) { - throw new IllegalArgumentException("Class " + eventSource.getClass().getName() - + " does not have a public add" + listenerType.getSimpleName() + } catch (final ReflectiveOperationException e) { + throw new IllegalArgumentException("Unable to add listener for class " + eventSource.getClass().getName() + + " and public add" + listenerType.getSimpleName() + " method which takes a parameter of type " + listenerType.getName() + "."); - } catch (final IllegalAccessException e) { - throw new IllegalArgumentException("Class " + eventSource.getClass().getName() - + " does not have an accessible add" + listenerType.getSimpleName () - + " method which takes a parameter of type " + listenerType.getName() + "."); - } catch (final InvocationTargetException e) { - throw new IllegalArgumentException("Unable to add listener.", e.getCause()); } } diff --git a/src/test/java/org/apache/commons/lang3/ObjectUtilsTest.java b/src/test/java/org/apache/commons/lang3/ObjectUtilsTest.java index f950ad556..0f6d43212 100644 --- a/src/test/java/org/apache/commons/lang3/ObjectUtilsTest.java +++ b/src/test/java/org/apache/commons/lang3/ObjectUtilsTest.java @@ -286,6 +286,8 @@ public class ObjectUtilsTest extends AbstractLangTest { public void testCloneOfUncloneable() { final UncloneableString string = new UncloneableString("apache"); final CloneFailedException e = assertThrows(CloneFailedException.class, () -> ObjectUtils.clone(string)); + assertNotNull(e); + assertNotNull(e.getCause()); assertEquals(NoSuchMethodException.class, e.getCause().getClass()); } @@ -819,8 +821,9 @@ public class ObjectUtilsTest extends AbstractLangTest { @Test public void testPossibleCloneOfUncloneable() { final UncloneableString string = new UncloneableString("apache"); - final CloneFailedException e = assertThrows(CloneFailedException.class, - () -> ObjectUtils.cloneIfPossible(string)); + final CloneFailedException e = assertThrows(CloneFailedException.class, () -> ObjectUtils.cloneIfPossible(string)); + assertNotNull(e); + assertNotNull(e.getCause()); assertEquals(NoSuchMethodException.class, e.getCause().getClass()); } diff --git a/src/test/java/org/apache/commons/lang3/event/EventUtilsTest.java b/src/test/java/org/apache/commons/lang3/event/EventUtilsTest.java index defdbf924..4a896a2ea 100644 --- a/src/test/java/org/apache/commons/lang3/event/EventUtilsTest.java +++ b/src/test/java/org/apache/commons/lang3/event/EventUtilsTest.java @@ -163,10 +163,10 @@ public class EventUtilsTest extends AbstractLangTest { final PropertyChangeSource src = new PropertyChangeSource(); final EventCountingInvocationHandler handler = new EventCountingInvocationHandler(); final ObjectChangeListener listener = handler.createListener(ObjectChangeListener.class); - final IllegalArgumentException e = - assertThrows(IllegalArgumentException.class, () -> EventUtils.addEventListener(src, ObjectChangeListener.class, listener)); - assertEquals("Class " + src.getClass().getName() + " does not have a public add" + ObjectChangeListener.class.getSimpleName() + " method which takes a parameter of type " + ObjectChangeListener.class.getName() + ".", - e.getMessage()); + final IllegalArgumentException e = assertThrows(IllegalArgumentException.class, + () -> EventUtils.addEventListener(src, ObjectChangeListener.class, listener)); + assertEquals("Unable to add listener for class " + src.getClass().getName() + " and public add" + ObjectChangeListener.class.getSimpleName() + + " method which takes a parameter of type " + ObjectChangeListener.class.getName() + ".", e.getMessage()); } @Test @@ -174,10 +174,10 @@ public class EventUtilsTest extends AbstractLangTest { final PropertyChangeSource src = new PropertyChangeSource(); final EventCountingInvocationHandler handler = new EventCountingInvocationHandler(); final VetoableChangeListener listener = handler.createListener(VetoableChangeListener.class); - final IllegalArgumentException e = - assertThrows(IllegalArgumentException.class, () -> EventUtils.addEventListener(src, VetoableChangeListener.class, listener)); - assertEquals("Class " + src.getClass().getName() + " does not have a public add" + VetoableChangeListener.class.getSimpleName() + " method which takes a parameter of type " + VetoableChangeListener.class.getName() + ".", - e.getMessage()); + final IllegalArgumentException e = assertThrows(IllegalArgumentException.class, + () -> EventUtils.addEventListener(src, VetoableChangeListener.class, listener)); + assertEquals("Unable to add listener for class " + src.getClass().getName() + " and public add" + VetoableChangeListener.class.getSimpleName() + + " method which takes a parameter of type " + VetoableChangeListener.class.getName() + ".", e.getMessage()); } @Test