Java Utililty Methods Object Clone

List of utility methods to do Object Clone

Description

The list of methods to do Object Clone are organized into topic(s).

Method

Eclone(E object)
clone
Class<?> c = object.getClass();
try {
    return (E) c.getMethod("clone").invoke(object);
} catch (Exception e) {
return null;
Objectclone(Object a)
clone
if (a instanceof double[]) {
    return ((double[]) a).clone();
if (a instanceof float[]) {
    return ((float[]) a).clone();
if (a instanceof long[]) {
    return ((long[]) a).clone();
...
Objectclone(Object original)
Returns a clone of the given original Object
if (!(original instanceof Cloneable))
    throw new CloneNotSupportedException();
try {
    return original.getClass().getMethod("clone").invoke(original);
} catch (Exception e) {
    throw new CloneNotSupportedException();
Objectclone(Object value)
clone
Object rval = null;
if (value instanceof Cloneable) {
    try {
        rval = value.getClass().getMethod("clone").invoke(value);
    } catch (final Exception e) {
        rval = e;
if (rval == null || rval instanceof Exception) {
    if (value == null || value instanceof String || value instanceof Number || value instanceof Boolean) {
        rval = value;
    } else {
        throw new RuntimeException(new CloneNotSupportedException(
                (rval instanceof Exception ? ((Exception) rval).getMessage() : "")));
return rval;
Stringclone(String name, String type, int pos)
clone
StringBuffer fieldName = new StringBuffer();
toIdentifier(name, fieldName);
String javaFieldName = getFieldName(pos, name, 0);
if (type.startsWith("custom:")) {
    String javaType = type.substring(7);
    return "rec." + javaFieldName + " = ((" + javaType + ")" + javaFieldName + ".clone());";
} else {
    return "rec." + javaFieldName + " = " + javaFieldName;
...
Tclone(T array)
Clones the given array object.
Class<?> arrayType = array.getClass();
if (!arrayType.isArray()) {
    throw new IllegalArgumentException("specified object is not an array");
Class<?> componentType = arrayType.getComponentType();
Object ret;
if (!componentType.isPrimitive()) {
    ret = ((Object[]) array).clone();
...
Objectclone_obj_array(Object source)
clonobarray
if (source instanceof float[]) {
    return ((float[]) source).clone();
} else {
    if (source instanceof short[]) {
        return ((short[]) source).clone();
    } else {
        if (source instanceof byte[]) {
            return ((byte[]) source).clone();
...
ClasscloneClass(Class clazz)
clone Class
Class<?> newClazz = null;
try {
    newClazz = Class.forName(clazz.getName());
} catch (ClassNotFoundException e) {
    e.printStackTrace();
return newClazz;
TcloneCloneNotSupportedObject(final T obj, final boolean deep)
clone Clone Not Supported Object
if (obj instanceof String) {
    return obj;
} else if (obj instanceof Byte) {
    return (T) new Byte((Byte) obj);
} else if (obj instanceof Short) {
    return (T) new Short((Short) obj);
} else if (obj instanceof Integer) {
    return (T) new Integer((Integer) obj);
...
ShortcloneShort(Short sOriginal)
Echtes Klonen von Short.
if (sOriginal == null) {
    return null;
} else {
    return new Short(sOriginal.shortValue());