mobile/android/thirdparty/ch/boye/httpclientandroidlib/client/utils/CloneUtils.java

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/mobile/android/thirdparty/ch/boye/httpclientandroidlib/client/utils/CloneUtils.java	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,80 @@
     1.4 +/*
     1.5 + * $HeadURL$
     1.6 + * $Revision$
     1.7 + * $Date$
     1.8 + *
     1.9 + * ====================================================================
    1.10 + *
    1.11 + *  Licensed to the Apache Software Foundation (ASF) under one or more
    1.12 + *  contributor license agreements.  See the NOTICE file distributed with
    1.13 + *  this work for additional information regarding copyright ownership.
    1.14 + *  The ASF licenses this file to You under the Apache License, Version 2.0
    1.15 + *  (the "License"); you may not use this file except in compliance with
    1.16 + *  the License.  You may obtain a copy of the License at
    1.17 + *
    1.18 + *      http://www.apache.org/licenses/LICENSE-2.0
    1.19 + *
    1.20 + *  Unless required by applicable law or agreed to in writing, software
    1.21 + *  distributed under the License is distributed on an "AS IS" BASIS,
    1.22 + *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    1.23 + *  See the License for the specific language governing permissions and
    1.24 + *  limitations under the License.
    1.25 + * ====================================================================
    1.26 + *
    1.27 + * This software consists of voluntary contributions made by many
    1.28 + * individuals on behalf of the Apache Software Foundation.  For more
    1.29 + * information on the Apache Software Foundation, please see
    1.30 + * <http://www.apache.org/>.
    1.31 + *
    1.32 + */
    1.33 +package ch.boye.httpclientandroidlib.client.utils;
    1.34 +
    1.35 +import java.lang.reflect.InvocationTargetException;
    1.36 +import java.lang.reflect.Method;
    1.37 +
    1.38 +import ch.boye.httpclientandroidlib.annotation.Immutable;
    1.39 +
    1.40 +/**
    1.41 + * A collection of utilities to workaround limitations of Java clone framework.
    1.42 + *
    1.43 + * @since 4.0
    1.44 + */
    1.45 +@Immutable
    1.46 +public class CloneUtils {
    1.47 +
    1.48 +    public static Object clone(final Object obj) throws CloneNotSupportedException {
    1.49 +        if (obj == null) {
    1.50 +            return null;
    1.51 +        }
    1.52 +        if (obj instanceof Cloneable) {
    1.53 +            Class<?> clazz = obj.getClass ();
    1.54 +            Method m;
    1.55 +            try {
    1.56 +                m = clazz.getMethod("clone", (Class[]) null);
    1.57 +            } catch (NoSuchMethodException ex) {
    1.58 +                throw new NoSuchMethodError(ex.getMessage());
    1.59 +            }
    1.60 +            try {
    1.61 +                return m.invoke(obj, (Object []) null);
    1.62 +            } catch (InvocationTargetException ex) {
    1.63 +                Throwable cause = ex.getCause();
    1.64 +                if (cause instanceof CloneNotSupportedException) {
    1.65 +                    throw ((CloneNotSupportedException) cause);
    1.66 +                } else {
    1.67 +                    throw new Error("Unexpected exception", cause);
    1.68 +                }
    1.69 +            } catch (IllegalAccessException ex) {
    1.70 +                throw new IllegalAccessError(ex.getMessage());
    1.71 +            }
    1.72 +        } else {
    1.73 +            throw new CloneNotSupportedException();
    1.74 +        }
    1.75 +    }
    1.76 +
    1.77 +    /**
    1.78 +     * This class should not be instantiated.
    1.79 +     */
    1.80 +    private CloneUtils() {
    1.81 +    }
    1.82 +
    1.83 +}

mercurial