if (obj == null) {
return null;
}
if (obj instanceof Cloneable) {
Class<?> clazz = obj.getClass ();
Method m;
try {
m = clazz.getMethod("clone", (Class[]) null);
} catch (NoSuchMethodException ex) {
throw new NoSuchMethodError(ex.getMessage());
}
try {
return m.invoke(obj, (Object []) null);
} catch (InvocationTargetException ex) {
Throwable cause = ex.getCause();
if (cause instanceof CloneNotSupportedException) {
throw ((CloneNotSupportedException) cause);
} else {
throw new Error("Unexpected exception", cause);
}
} catch (IllegalAccessException ex) {
throw new IllegalAccessError(ex.getMessage());
}
} else {
throw new CloneNotSupportedException();
}