parent
ccac050601
commit
f161ff6669
|
@ -235,8 +235,9 @@ public class ObjectUtils {
|
||||||
public static <T> T clone(final T obj) {
|
public static <T> T clone(final T obj) {
|
||||||
if (obj instanceof Cloneable) {
|
if (obj instanceof Cloneable) {
|
||||||
final Object result;
|
final Object result;
|
||||||
|
final Class<? extends Object> objClass = obj.getClass();
|
||||||
if (isArray(obj)) {
|
if (isArray(obj)) {
|
||||||
final Class<?> componentType = obj.getClass().getComponentType();
|
final Class<?> componentType = objClass.getComponentType();
|
||||||
if (componentType.isPrimitive()) {
|
if (componentType.isPrimitive()) {
|
||||||
int length = Array.getLength(obj);
|
int length = Array.getLength(obj);
|
||||||
result = Array.newInstance(componentType, length);
|
result = Array.newInstance(componentType, length);
|
||||||
|
@ -248,10 +249,9 @@ public class ObjectUtils {
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
try {
|
try {
|
||||||
final Method clone = obj.getClass().getMethod("clone");
|
result = objClass.getMethod("clone").invoke(obj);
|
||||||
result = clone.invoke(obj);
|
|
||||||
} catch (final ReflectiveOperationException e) {
|
} 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;
|
return (T) result;
|
||||||
|
|
|
@ -18,7 +18,6 @@
|
||||||
package org.apache.commons.lang3.event;
|
package org.apache.commons.lang3.event;
|
||||||
|
|
||||||
import java.lang.reflect.InvocationHandler;
|
import java.lang.reflect.InvocationHandler;
|
||||||
import java.lang.reflect.InvocationTargetException;
|
|
||||||
import java.lang.reflect.Method;
|
import java.lang.reflect.Method;
|
||||||
import java.lang.reflect.Proxy;
|
import java.lang.reflect.Proxy;
|
||||||
import java.util.Arrays;
|
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) {
|
public static <L> void addEventListener(final Object eventSource, final Class<L> listenerType, final L listener) {
|
||||||
try {
|
try {
|
||||||
MethodUtils.invokeMethod(eventSource, "add" + listenerType.getSimpleName(), listener);
|
MethodUtils.invokeMethod(eventSource, "add" + listenerType.getSimpleName(), listener);
|
||||||
} catch (final NoSuchMethodException e) {
|
} catch (final ReflectiveOperationException e) {
|
||||||
throw new IllegalArgumentException("Class " + eventSource.getClass().getName()
|
throw new IllegalArgumentException("Unable to add listener for class " + eventSource.getClass().getName()
|
||||||
+ " does not have a public add" + listenerType.getSimpleName()
|
+ " and public add" + listenerType.getSimpleName()
|
||||||
+ " method which takes a parameter of type " + listenerType.getName() + ".");
|
+ " 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());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -286,6 +286,8 @@ public class ObjectUtilsTest extends AbstractLangTest {
|
||||||
public void testCloneOfUncloneable() {
|
public void testCloneOfUncloneable() {
|
||||||
final UncloneableString string = new UncloneableString("apache");
|
final UncloneableString string = new UncloneableString("apache");
|
||||||
final CloneFailedException e = assertThrows(CloneFailedException.class, () -> ObjectUtils.clone(string));
|
final CloneFailedException e = assertThrows(CloneFailedException.class, () -> ObjectUtils.clone(string));
|
||||||
|
assertNotNull(e);
|
||||||
|
assertNotNull(e.getCause());
|
||||||
assertEquals(NoSuchMethodException.class, e.getCause().getClass());
|
assertEquals(NoSuchMethodException.class, e.getCause().getClass());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -819,8 +821,9 @@ public class ObjectUtilsTest extends AbstractLangTest {
|
||||||
@Test
|
@Test
|
||||||
public void testPossibleCloneOfUncloneable() {
|
public void testPossibleCloneOfUncloneable() {
|
||||||
final UncloneableString string = new UncloneableString("apache");
|
final UncloneableString string = new UncloneableString("apache");
|
||||||
final CloneFailedException e = assertThrows(CloneFailedException.class,
|
final CloneFailedException e = assertThrows(CloneFailedException.class, () -> ObjectUtils.cloneIfPossible(string));
|
||||||
() -> ObjectUtils.cloneIfPossible(string));
|
assertNotNull(e);
|
||||||
|
assertNotNull(e.getCause());
|
||||||
assertEquals(NoSuchMethodException.class, e.getCause().getClass());
|
assertEquals(NoSuchMethodException.class, e.getCause().getClass());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -163,10 +163,10 @@ public class EventUtilsTest extends AbstractLangTest {
|
||||||
final PropertyChangeSource src = new PropertyChangeSource();
|
final PropertyChangeSource src = new PropertyChangeSource();
|
||||||
final EventCountingInvocationHandler handler = new EventCountingInvocationHandler();
|
final EventCountingInvocationHandler handler = new EventCountingInvocationHandler();
|
||||||
final ObjectChangeListener listener = handler.createListener(ObjectChangeListener.class);
|
final ObjectChangeListener listener = handler.createListener(ObjectChangeListener.class);
|
||||||
final IllegalArgumentException e =
|
final IllegalArgumentException e = assertThrows(IllegalArgumentException.class,
|
||||||
assertThrows(IllegalArgumentException.class, () -> EventUtils.addEventListener(src, ObjectChangeListener.class, listener));
|
() -> 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() + ".",
|
assertEquals("Unable to add listener for class " + src.getClass().getName() + " and public add" + ObjectChangeListener.class.getSimpleName()
|
||||||
e.getMessage());
|
+ " method which takes a parameter of type " + ObjectChangeListener.class.getName() + ".", e.getMessage());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -174,10 +174,10 @@ public class EventUtilsTest extends AbstractLangTest {
|
||||||
final PropertyChangeSource src = new PropertyChangeSource();
|
final PropertyChangeSource src = new PropertyChangeSource();
|
||||||
final EventCountingInvocationHandler handler = new EventCountingInvocationHandler();
|
final EventCountingInvocationHandler handler = new EventCountingInvocationHandler();
|
||||||
final VetoableChangeListener listener = handler.createListener(VetoableChangeListener.class);
|
final VetoableChangeListener listener = handler.createListener(VetoableChangeListener.class);
|
||||||
final IllegalArgumentException e =
|
final IllegalArgumentException e = assertThrows(IllegalArgumentException.class,
|
||||||
assertThrows(IllegalArgumentException.class, () -> EventUtils.addEventListener(src, VetoableChangeListener.class, listener));
|
() -> 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() + ".",
|
assertEquals("Unable to add listener for class " + src.getClass().getName() + " and public add" + VetoableChangeListener.class.getSimpleName()
|
||||||
e.getMessage());
|
+ " method which takes a parameter of type " + VetoableChangeListener.class.getName() + ".", e.getMessage());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
|
Loading…
Reference in New Issue