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.

AbstractTask.java 2.7 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. package org.apache.ant;
  2. import java.io.*;
  3. import java.util.*;
  4. import java.lang.reflect.*;
  5. import java.beans.*;
  6. /**
  7. * Superclass of all Tasks. All tasks extend from this.
  8. *
  9. * @author James Duncan Davidson (duncan@apache.org)
  10. */
  11. public abstract class AbstractTask {
  12. // -----------------------------------------------------------------
  13. // PROTECTED DATA MEMBERS
  14. // -----------------------------------------------------------------
  15. /**
  16. *
  17. */
  18. protected Project project;
  19. // -----------------------------------------------------------------
  20. // ABSTRACT PUBLIC METHODS
  21. // -----------------------------------------------------------------
  22. /**
  23. *
  24. */
  25. public abstract boolean execute() throws AntException;
  26. // -----------------------------------------------------------------
  27. // PUBLIC METHODS
  28. // -----------------------------------------------------------------
  29. /**
  30. * Used by the system to set the attributes which then get reflected
  31. * into the particular implementation class
  32. */
  33. public void setAttributes(Hashtable attributes) {
  34. Class clazz = this.getClass();
  35. BeanInfo bi;
  36. try {
  37. bi = Introspector.getBeanInfo(clazz);
  38. } catch (IntrospectionException ie) {
  39. System.out.println("Can't reflect on: " + clazz);
  40. // XXX exception out
  41. return;
  42. }
  43. PropertyDescriptor[] pda = bi.getPropertyDescriptors();
  44. for (int i = 0; i < pda.length; i++) {
  45. PropertyDescriptor pd = pda[i];
  46. String property = pd.getName();
  47. Object o = attributes.get(property);
  48. if (o != null) {
  49. String value = (String)o;
  50. Method setMethod = pd.getWriteMethod();
  51. if (setMethod != null) {
  52. Class[] ma = setMethod.getParameterTypes();
  53. if (ma.length == 1) {
  54. Class c = ma[0];
  55. if (c.getName().equals("java.lang.String")) {
  56. try {
  57. setMethod.invoke(this, new String[] {value});
  58. } catch (Exception e) {
  59. // XXX bad bad bad -- narrow to exact exceptions
  60. System.out.println("OUCH: " + e);
  61. // XXX exception out.
  62. }
  63. }
  64. }
  65. }
  66. }
  67. }
  68. }
  69. /**
  70. * Used by system to set the project.
  71. */
  72. public void setProject(Project project) {
  73. this.project = project;
  74. }
  75. }