public static Object tryClone ( Object toBeCloned ) { if ( toBeCloned == null ) { return null ; } Object clonedObj = null ; if ( toBeCloned instanceof Cloneable ) { try { Method method = Object . class . getDeclaredMethod ( "clone" ) ; method . setAccessible ( true ) ; clonedObj = method . invoke ( toBeCloned ) ; } catch ( Exception e ) { if ( e instanceof CloneNotSupportedException ) { clonedObj = toBeCloned ; } else { throw e instanceof RuntimeException ? ( RuntimeException ) e : new RuntimeException ( e ) ; } } } else { clonedObj = toBeCloned ; } return clonedObj ; }
Try cloning an object .