Simplify exception handling

Javadoc
This commit is contained in:
Gary Gregory 2024-05-24 09:44:20 -04:00
parent ccac050601
commit f161ff6669
4 changed files with 20 additions and 24 deletions

View File

@ -235,8 +235,9 @@ public class ObjectUtils {
public static <T> T clone(final T obj) {
if (obj instanceof Cloneable) {
final Object result;
final Class<? extends Object> 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;

View File

@ -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 <L> void addEventListener(final Object eventSource, final Class<L> 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());
}
}

View File

@ -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());
}

View File

@ -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