1 18 19 package org.apache.tools.ant; 20 21 import java.lang.reflect.Method ; 22 import org.apache.tools.ant.dispatch.Dispatchable; 23 import org.apache.tools.ant.dispatch.DispatchUtils; 24 25 31 public class TaskAdapter extends Task implements TypeAdapter { 32 33 34 private Object proxy; 35 36 57 public static void checkTaskClass(final Class taskClass, 58 final Project project) { 59 if (!Dispatchable.class.isAssignableFrom(taskClass)) { 60 try { 63 final Method executeM = taskClass.getMethod("execute", (Class []) null); 64 if (!Void.TYPE.equals(executeM.getReturnType())) { 69 final String message = "return type of execute() should be " 70 + "void but was \"" + executeM.getReturnType() + "\" in " 71 + taskClass; 72 project.log(message, Project.MSG_WARN); 73 } 74 } catch (NoSuchMethodException e) { 75 final String message = "No public execute() in " + taskClass; 76 project.log(message, Project.MSG_ERR); 77 throw new BuildException(message); 78 } catch (LinkageError e) { 79 String message = "Could not load " + taskClass + ": " + e; 80 project.log(message, Project.MSG_ERR); 81 throw new BuildException(message, e); 82 } 83 } 84 } 85 86 92 public void checkProxyClass(Class proxyClass) { 93 checkTaskClass(proxyClass, getProject()); 94 } 95 96 102 public void execute() throws BuildException { 103 try { 104 Method setLocationM = proxy.getClass().getMethod( 105 "setLocation", new Class [] {Location.class}); 106 if (setLocationM != null) { 107 setLocationM.invoke(proxy, new Object [] {getLocation()}); 108 } 109 } catch (NoSuchMethodException e) { 110 } catch (Exception ex) { 113 log("Error setting location in " + proxy.getClass(), 114 Project.MSG_ERR); 115 throw new BuildException(ex); 116 } 117 118 try { 119 Method setProjectM = proxy.getClass().getMethod( 120 "setProject", new Class [] {Project.class}); 121 if (setProjectM != null) { 122 setProjectM.invoke(proxy, new Object [] {getProject()}); 123 } 124 } catch (NoSuchMethodException e) { 125 } catch (Exception ex) { 128 log("Error setting project in " + proxy.getClass(), 129 Project.MSG_ERR); 130 throw new BuildException(ex); 131 } 132 133 try { 134 DispatchUtils.execute(proxy); 135 } catch (BuildException be) { 136 throw be; 137 } catch (Exception ex) { 138 log("Error in " + proxy.getClass(), Project.MSG_VERBOSE); 139 throw new BuildException(ex); 140 } 141 } 142 143 148 public void setProxy(Object o) { 149 this.proxy = o; 150 } 151 152 157 public Object getProxy() { 158 return proxy; 159 } 160 161 } 162 | Popular Tags |