PageRenderTime 20ms CodeModel.GetById 17ms app.highlight 2ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/src/org/apache/http/client/utils/CloneUtils.java

http://github.com/onedanshow/Screen-Courter
Java | 80 lines | 37 code | 6 blank | 37 comment | 6 complexity | aa4c2b13ef01677132d46ff84de989df MD5 | raw file
Possible License(s): GPL-3.0
 1/*
 2 * $HeadURL$
 3 * $Revision$
 4 * $Date$
 5 *
 6 * ====================================================================
 7 *
 8 *  Licensed to the Apache Software Foundation (ASF) under one or more
 9 *  contributor license agreements.  See the NOTICE file distributed with
10 *  this work for additional information regarding copyright ownership.
11 *  The ASF licenses this file to You under the Apache License, Version 2.0
12 *  (the "License"); you may not use this file except in compliance with
13 *  the License.  You may obtain a copy of the License at
14 *
15 *      http://www.apache.org/licenses/LICENSE-2.0
16 *
17 *  Unless required by applicable law or agreed to in writing, software
18 *  distributed under the License is distributed on an "AS IS" BASIS,
19 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20 *  See the License for the specific language governing permissions and
21 *  limitations under the License.
22 * ====================================================================
23 *
24 * This software consists of voluntary contributions made by many
25 * individuals on behalf of the Apache Software Foundation.  For more
26 * information on the Apache Software Foundation, please see
27 * <http://www.apache.org/>.
28 *
29 */
30package org.apache.http.client.utils;
31
32import java.lang.reflect.InvocationTargetException;
33import java.lang.reflect.Method;
34
35import org.apache.http.annotation.Immutable;
36
37/**
38 * A collection of utilities to workaround limitations of Java clone framework.
39 *
40 * @since 4.0
41 */
42@Immutable
43public class CloneUtils {
44
45    public static Object clone(final Object obj) throws CloneNotSupportedException {
46        if (obj == null) {
47            return null;
48        }
49        if (obj instanceof Cloneable) {
50            Class<?> clazz = obj.getClass ();
51            Method m;
52            try {
53                m = clazz.getMethod("clone", (Class[]) null);
54            } catch (NoSuchMethodException ex) {
55                throw new NoSuchMethodError(ex.getMessage());
56            }
57            try {
58                return m.invoke(obj, (Object []) null);
59            } catch (InvocationTargetException ex) {
60                Throwable cause = ex.getCause();
61                if (cause instanceof CloneNotSupportedException) {
62                    throw ((CloneNotSupportedException) cause);
63                } else {
64                    throw new Error("Unexpected exception", cause);
65                }
66            } catch (IllegalAccessException ex) {
67                throw new IllegalAccessError(ex.getMessage());
68            }
69        } else {
70            throw new CloneNotSupportedException();
71        }
72    }
73
74    /**
75     * This class should not be instantiated.
76     */
77    private CloneUtils() {
78    }
79
80}