/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

  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. */
  30. package org.apache.http.client.utils;
  31. import java.lang.reflect.InvocationTargetException;
  32. import java.lang.reflect.Method;
  33. import org.apache.http.annotation.Immutable;
  34. /**
  35. * A collection of utilities to workaround limitations of Java clone framework.
  36. *
  37. * @since 4.0
  38. */
  39. @Immutable
  40. public class CloneUtils {
  41. public static Object clone(final Object obj) throws CloneNotSupportedException {
  42. if (obj == null) {
  43. return null;
  44. }
  45. if (obj instanceof Cloneable) {
  46. Class<?> clazz = obj.getClass ();
  47. Method m;
  48. try {
  49. m = clazz.getMethod("clone", (Class[]) null);
  50. } catch (NoSuchMethodException ex) {
  51. throw new NoSuchMethodError(ex.getMessage());
  52. }
  53. try {
  54. return m.invoke(obj, (Object []) null);
  55. } catch (InvocationTargetException ex) {
  56. Throwable cause = ex.getCause();
  57. if (cause instanceof CloneNotSupportedException) {
  58. throw ((CloneNotSupportedException) cause);
  59. } else {
  60. throw new Error("Unexpected exception", cause);
  61. }
  62. } catch (IllegalAccessException ex) {
  63. throw new IllegalAccessError(ex.getMessage());
  64. }
  65. } else {
  66. throw new CloneNotSupportedException();
  67. }
  68. }
  69. /**
  70. * This class should not be instantiated.
  71. */
  72. private CloneUtils() {
  73. }
  74. }