Java tutorial
//package com.java2s; import java.lang.reflect.*; import java.util.*; public class Main { public static <C> C cloneObject(C original) { try { C clone = (C) original.getClass().newInstance(); for (Field field : getAllFieldsValues(original.getClass())) { field.setAccessible(true); if (field.get(original) == null || Modifier.isFinal(field.getModifiers())) { continue; } if (field.getType().isPrimitive() || field.getType().equals(String.class) || field.getType().getSuperclass().equals(Number.class) || field.getType().equals(Boolean.class)) { field.set(clone, field.get(original)); } else { Object childObj = field.get(original); if (childObj == original) { field.set(clone, clone); } else { field.set(clone, cloneObject(field.get(original))); } } } return clone; } catch (Exception e) { return null; } } public static <T extends Object> T newInstance(Class<T> cl, Object... args) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException { Constructor<T> constructor = cl.getDeclaredConstructor(new Class[0]); boolean accessible = constructor.isAccessible(); constructor.setAccessible(true); T t = constructor.newInstance(args); constructor.setAccessible(accessible); return t; } /** * Return a list of all fields (whatever access status, and on whatever * superclass they were defined) that can be found on this class. * This is like a union of {@link Class#getDeclaredFields()} which * ignores and super-classes, and {@link Class#getFields()} which ignored * non-public fields * * @param clazz The class to introspect * @return The complete list of fields */ public static Field[] getAllFieldsValues(Class<?> clazz) { List<Class<?>> classes = getAllSuperclasses(clazz); classes.add(clazz); return getAllFields(classes); } /** * Return a List of super-classes for the given class. * * @param clazz the class to look up * @return the List of super-classes in order going up from this one */ public static List<Class<?>> getAllSuperclasses(Class<?> clazz) { List<Class<?>> classes = new ArrayList<Class<?>>(); Class<?> superclass = clazz.getSuperclass(); while (superclass != null) { classes.add(superclass); superclass = superclass.getSuperclass(); } return classes; } /** * Return a list of all fields (whatever access status, and on whatever * superclass they were defined) that can be found on this class. * This is like a union of {@link Class#getDeclaredFields()} which * ignores and super-classes, and {@link Class#getFields()} which ignored * non-public fields * * @param clazz The class to introspect * @return The complete list of fields */ public static Field[] getAllFields(Class<?> clazz) { List<Class<?>> classes = getAllSuperclasses(clazz); classes.add(clazz); return getAllFields(classes); } /** * As {@link #getAllFields(Class)} but acts on a list of {@link Class}s and * uses only {@link Class#getDeclaredFields()}. * * @param classes The list of classes to reflect on * @return The complete list of fields */ private static Field[] getAllFields(List<Class<?>> classes) { Set<Field> fields = new HashSet<Field>(); for (Class<?> clazz : classes) { fields.addAll(Arrays.asList(clazz.getDeclaredFields())); } return fields.toArray(new Field[fields.size()]); } }