Search in sources :

Example 1 with CloneFailedException

use of org.apache.commons.lang3.exception.CloneFailedException in project commons-lang by apache.

the class ObjectUtils method clone.

// cloning
// -----------------------------------------------------------------------
/**
 * <p>Clone an object.</p>
 *
 * @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());
            }
        }
        // OK because input is of type T
        @SuppressWarnings("unchecked") final T checked = (T) result;
        return checked;
    }
    return null;
}
Also used : Method(java.lang.reflect.Method) CloneFailedException(org.apache.commons.lang3.exception.CloneFailedException) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 2 with CloneFailedException

use of org.apache.commons.lang3.exception.CloneFailedException in project xwiki-platform by xwiki.

the class ExecutionContextCopier method copy.

@Override
public ExecutionContext copy(ExecutionContext originalExecutionContext) throws CloneFailedException {
    try {
        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);
    }
}
Also used : ExecutionContext(org.xwiki.context.ExecutionContext) XWikiContext(com.xpn.xwiki.XWikiContext) CloneFailedException(org.apache.commons.lang3.exception.CloneFailedException) CloneFailedException(org.apache.commons.lang3.exception.CloneFailedException)

Aggregations

CloneFailedException (org.apache.commons.lang3.exception.CloneFailedException)2 XWikiContext (com.xpn.xwiki.XWikiContext)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Method (java.lang.reflect.Method)1 ExecutionContext (org.xwiki.context.ExecutionContext)1