Uploaded image for project: 'Commons Lang'
  1. Commons Lang
  2. LANG-576

Add methods for cloneables to ObjectUtils

    XMLWordPrintableJSON

    Details

    • Type: New Feature
    • Status: Closed
    • Priority: Minor
    • Resolution: Fixed
    • Affects Version/s: None
    • Fix Version/s: 3.0
    • Component/s: lang.*
    • Labels:
      None

      Description

      Object.clone is declared protected, which makes it impossible to write code like:

      if (obj instanceof Cloneable) {
        Object clone = obj.clone();
        ...
      }
      

      Following two methods will help in such a situation:

          /**
           * Clone an object.
           * 
           * @param <T> the type of the object
           * @param o the object to clone
           * @return the clone if the object implements {@link Cloneable} otherwise <code>null</code>
           * @throws CloneFailedException if the object is cloneable and the clone operation fails
           * @since 3.0
           */
          public static <T> T clone(final T o) {
              if (o instanceof Cloneable) {
                  try {
                      final Method clone = o.getClass().getMethod("clone", (Class[])null);
                      @SuppressWarnings("unchecked")
                      final T result = (T)clone.invoke(o, (Object[])null);
                      return result;
                  } catch (final NoSuchMethodException e) {
                      throw new CloneFailedException("Cloneable type has no clone method", e);
                  } catch (final IllegalAccessException e) {
                      throw new CloneFailedException("Cannot clone Cloneable type", e);
                  } catch (final InvocationTargetException e) {
                      throw new CloneFailedException("Exception cloning Cloneable type", e.getCause());
                  }
              }
      
              return null;
          }
      
          /**
           * Clone an object if possible.
           * 
           * @param <T> the type of the object
           * @param o the object to clone
           * @return the clone if the object implements {@link Cloneable} otherwise the object itself
           * @throws CloneFailedException if the object is cloneable and the clone operation fails
           * @since 3.0
           */
          public static <T> T cloneIfPossible(final T o) {
              final T clone = clone(o);
              return clone == null ? o : clone;
          }
      

      Comments? Note, that the code currently introduces also a new CloneFailedException. Use another existing one?

      Unit tests will be provided also.

        Attachments

        1. CloneableTest.diff
          4 kB
          Joerg Schaible
        2. Cloneable.diff
          5 kB
          Joerg Schaible

          Issue Links

            Activity

              People

              • Assignee:
                joehni Joerg Schaible
                Reporter:
                joehni Joerg Schaible
              • Votes:
                0 Vote for this issue
                Watchers:
                0 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved: