Example usage for org.apache.commons.lang3.exception CloneFailedException CloneFailedException

List of usage examples for org.apache.commons.lang3.exception CloneFailedException CloneFailedException

Introduction

In this page you can find the example usage for org.apache.commons.lang3.exception CloneFailedException CloneFailedException.

Prototype

public CloneFailedException(final String message, final Throwable cause) 

Source Link

Document

Constructs a CloneFailedException.

Usage

From source file:com.mawujun.util.ObjectUtils.java

/**
 * <p>Clone an object.</p>/*from w  ww  . j  a  v a2 s.  c o m*/
 *
 * @param <T> the type of the object
 * @param obj  the object to clone, null returns null
 * @return the clone if the object implements {@link Cloneable} otherwise {@code null}
 * @throws CloneFailedException if the object is cloneable and the clone operation fails
 * @since 3.0
 */
public static <T> T clone(final T obj) {
    if (obj instanceof Cloneable) {
        final Object result;
        if (obj.getClass().isArray()) {
            final Class<?> componentType = obj.getClass().getComponentType();
            if (!componentType.isPrimitive()) {
                result = ((Object[]) obj).clone();
            } else {
                int length = Array.getLength(obj);
                result = Array.newInstance(componentType, length);
                while (length-- > 0) {
                    Array.set(result, length, Array.get(obj, length));
                }
            }
        } else {
            try {
                final Method clone = obj.getClass().getMethod("clone");
                result = clone.invoke(obj);
            } catch (final NoSuchMethodException e) {
                throw new CloneFailedException(
                        "Cloneable type " + obj.getClass().getName() + " has no clone method", e);
            } catch (final IllegalAccessException e) {
                throw new CloneFailedException("Cannot clone Cloneable type " + obj.getClass().getName(), e);
            } catch (final InvocationTargetException e) {
                throw new CloneFailedException("Exception cloning Cloneable type " + obj.getClass().getName(),
                        e.getCause());
            }
        }
        @SuppressWarnings("unchecked")
        final T checked = (T) result;
        return checked;
    }

    return null;
}

From source file:org.apache.flink.api.java.typeutils.runtime.kryo.KryoSerializer.java

private ExecutionConfig.SerializableSerializer<? extends Serializer<?>> deepCopySerializer(
        ExecutionConfig.SerializableSerializer<? extends Serializer<?>> original) {
    try {/* ww w.  ja  v a 2s .  c o m*/
        return InstantiationUtil.clone(original, Thread.currentThread().getContextClassLoader());
    } catch (IOException | ClassNotFoundException ex) {
        throw new CloneFailedException("Could not clone serializer instance of class " + original.getClass(),
                ex);
    }
}

From source file:org.xwiki.contrib.jobmacro.internal.context.ExecutionContextCopier.java

@Override
public ExecutionContext copy(ExecutionContext originalExecutionContext) throws CloneFailedException {
    try {/*from ww  w. j  a  v a 2s . co m*/
        ExecutionContext clonedExecutionContext = this.executionContextManager.clone(originalExecutionContext);

        // XWikiContext
        // The above clone just creates and initializes an empty XWiki Context, so it needs special handling.
        XWikiContext xwikiContext = (XWikiContext) originalExecutionContext
                .getProperty(XWikiContext.EXECUTIONCONTEXT_KEY);
        XWikiContext clonedXWikiContext = xwikiContextCloner.copy(xwikiContext);
        clonedExecutionContext.setProperty(XWikiContext.EXECUTIONCONTEXT_KEY, clonedXWikiContext);

        // VelocityContext
        // Reset the VelocityContext from the EC by removing it and calling the Velocity ECInitializer which is
        // normally called by the execution of the ECInitializers by ECManager.clone(). This ensures a clean new
        // VC is created. It'll get filled when VelocityContextManager.getVelocityContext() is called by whatever
        // code need the VC.
        clonedExecutionContext.removeProperty(VelocityExecutionContextInitializer.VELOCITY_CONTEXT_ID);
        this.velocityExecutionContextInitializer.initialize(clonedExecutionContext);

        return clonedExecutionContext;
    } catch (Exception e) {
        throw new CloneFailedException(String.format("Failed to clone [%s]", originalExecutionContext), e);
    }
}