You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

Clone.java 3.4 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /*
  2. * Copyright 2005 The Apache Software Foundation
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. *
  16. */
  17. package org.apache.tools.ant.taskdefs;
  18. import java.lang.reflect.Method;
  19. import org.apache.tools.ant.Task;
  20. import org.apache.tools.ant.Project;
  21. import org.apache.tools.ant.BuildException;
  22. import org.apache.tools.ant.UnknownElement;
  23. import org.apache.tools.ant.RuntimeConfigurable;
  24. /**
  25. * Clone an Object from a reference.
  26. * @since Ant 1.7
  27. */
  28. public class Clone extends UnknownElement {
  29. /** Task name. */
  30. public static final String TASK_NAME = "clone";
  31. /** Clone reference attribute ID. */
  32. public static final String CLONE_REF = "cloneref";
  33. private static final Class[] NO_ARGS = new Class[] {};
  34. /**
  35. * Create a new instance of the Clone task.
  36. */
  37. public Clone() {
  38. super(TASK_NAME);
  39. }
  40. /**
  41. * Creates a named task or data type. If the real object is a task,
  42. * it is configured up to the init() stage.
  43. *
  44. * @param ue The UnknownElement to create the real object for.
  45. * Not used in this implementation.
  46. * @param w The RuntimeConfigurable containing the configuration
  47. * information to pass to the cloned Object.
  48. *
  49. * @return the task or data type represented by the given unknown element.
  50. */
  51. protected Object makeObject(UnknownElement ue, RuntimeConfigurable w) {
  52. String cloneref = (String) (w.getAttributeMap().get(CLONE_REF));
  53. if (cloneref == null) {
  54. throw new BuildException("cloneref attribute not set");
  55. }
  56. Object ob = getProject().getReference(cloneref);
  57. if (ob == null) {
  58. throw new BuildException(
  59. "reference \"" + cloneref + "\" not found");
  60. }
  61. try {
  62. log("Attempting to clone " + ob.toString() + " \""
  63. + cloneref + "\"", Project.MSG_VERBOSE);
  64. Method m = ob.getClass().getMethod("clone", NO_ARGS);
  65. try {
  66. Object bo = m.invoke(ob, NO_ARGS);
  67. if (bo == null) {
  68. throw new BuildException(m.toString() + " returned null");
  69. }
  70. w.removeAttribute(CLONE_REF);
  71. w.setProxy(bo);
  72. w.setElementTag(null);
  73. setRuntimeConfigurableWrapper(w);
  74. if (bo instanceof Task) {
  75. ((Task) bo).setOwningTarget(getOwningTarget());
  76. ((Task) bo).init();
  77. }
  78. return bo;
  79. } catch (Exception e) {
  80. throw new BuildException(e);
  81. }
  82. } catch (NoSuchMethodException e) {
  83. throw new BuildException(
  84. "Unable to locate public clone method for object \""
  85. + cloneref + "\"");
  86. }
  87. }
  88. }