1
|
|
/*
|
2
|
|
* The Apache Software License, Version 1.1
|
3
|
|
*
|
4
|
|
* Copyright (c) 2000-2002 The Apache Software Foundation. All rights
|
5
|
|
* reserved.
|
6
|
|
*
|
7
|
|
* Redistribution and use in source and binary forms, with or without
|
8
|
|
* modification, are permitted provided that the following conditions
|
9
|
|
* are met:
|
10
|
|
*
|
11
|
|
* 1. Redistributions of source code must retain the above copyright
|
12
|
|
* notice, this list of conditions and the following disclaimer.
|
13
|
|
*
|
14
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
15
|
|
* notice, this list of conditions and the following disclaimer in
|
16
|
|
* the documentation and/or other materials provided with the
|
17
|
|
* distribution.
|
18
|
|
*
|
19
|
|
* 3. The end-user documentation included with the redistribution, if
|
20
|
|
* any, must include the following acknowlegement:
|
21
|
|
* "This product includes software developed by the
|
22
|
|
* Apache Software Foundation (http://www.apache.org/)."
|
23
|
|
* Alternately, this acknowlegement may appear in the software itself,
|
24
|
|
* if and wherever such third-party acknowlegements normally appear.
|
25
|
|
*
|
26
|
|
* 4. The names "Ant" and "Apache Software
|
27
|
|
* Foundation" must not be used to endorse or promote products derived
|
28
|
|
* from this software without prior written permission. For written
|
29
|
|
* permission, please contact apache@apache.org.
|
30
|
|
*
|
31
|
|
* 5. Products derived from this software may not be called "Apache"
|
32
|
|
* nor may "Apache" appear in their names without prior written
|
33
|
|
* permission of the Apache Group.
|
34
|
|
*
|
35
|
|
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
|
36
|
|
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
37
|
|
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
38
|
|
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
|
39
|
|
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
40
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
41
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
|
42
|
|
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
43
|
|
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
44
|
|
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
|
45
|
|
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
46
|
|
* SUCH DAMAGE.
|
47
|
|
* ====================================================================
|
48
|
|
*
|
49
|
|
* This software consists of voluntary contributions made by many
|
50
|
|
* individuals on behalf of the Apache Software Foundation. For more
|
51
|
|
* information on the Apache Software Foundation, please see
|
52
|
|
* <http://www.apache.org/>.
|
53
|
|
*/
|
54
|
|
|
55
|
|
package org.apache.tools.ant;
|
56
|
|
|
57
|
|
import java.lang.reflect.Method;
|
58
|
|
|
59
|
|
/**
|
60
|
|
* Uses introspection to "adapt" an arbitrary Bean which doesn't
|
61
|
|
* itself extend Task, but still contains an execute method and optionally
|
62
|
|
* a setProject method.
|
63
|
|
*
|
64
|
|
* @author costin@dnt.ro
|
65
|
|
*/
|
66
|
|
public class TaskAdapter extends Task {
|
67
|
|
|
68
|
|
/** Object to act as a proxy for. */
|
69
|
|
private Object proxy;
|
70
|
|
|
71
|
|
/**
|
72
|
|
* Checks whether or not a class is suitable to be adapted by TaskAdapter.
|
73
|
|
*
|
74
|
|
* This only checks conditions which are additionally required for
|
75
|
|
* tasks adapted by TaskAdapter. Thus, this method should be called by
|
76
|
|
* Project.checkTaskClass.
|
77
|
|
*
|
78
|
|
* Throws a BuildException and logs as Project.MSG_ERR for
|
79
|
|
* conditions that will cause the task execution to fail.
|
80
|
|
* Logs other suspicious conditions with Project.MSG_WARN.
|
81
|
|
*
|
82
|
|
* @param taskClass Class to test for suitability.
|
83
|
|
* Must not be <code>null</code>.
|
84
|
|
* @param project Project to log warnings/errors to.
|
85
|
|
* Must not be <code>null</code>.
|
86
|
|
*
|
87
|
|
* @see Project#checkTaskClass(Class)
|
88
|
|
*/
|
89
|
106
|
public static void checkTaskClass(final Class taskClass,
|
90
|
|
final Project project) {
|
91
|
|
// don't have to check for interface, since then
|
92
|
|
// taskClass would be abstract too.
|
93
|
106
|
try {
|
94
|
106
|
final Method executeM = taskClass.getMethod("execute", null);
|
95
|
|
// don't have to check for public, since
|
96
|
|
// getMethod finds public method only.
|
97
|
|
// don't have to check for abstract, since then
|
98
|
|
// taskClass would be abstract too.
|
99
|
103
|
if (!Void.TYPE.equals(executeM.getReturnType())) {
|
100
|
1
|
final String message = "return type of execute() should be "
|
101
|
|
+ "void but was \"" + executeM.getReturnType() + "\" in "
|
102
|
|
+ taskClass;
|
103
|
1
|
project.log(message, Project.MSG_WARN);
|
104
|
|
}
|
105
|
|
} catch (NoSuchMethodException e) {
|
106
|
3
|
final String message = "No public execute() in " + taskClass;
|
107
|
3
|
project.log(message, Project.MSG_ERR);
|
108
|
3
|
throw new BuildException(message);
|
109
|
|
}
|
110
|
|
}
|
111
|
|
|
112
|
|
/**
|
113
|
|
* Executes the proxied task.
|
114
|
|
*
|
115
|
|
* @exception BuildException if the project could not be set
|
116
|
|
* or the method could not be executed.
|
117
|
|
*/
|
118
|
66
|
public void execute() throws BuildException {
|
119
|
66
|
Method setProjectM = null;
|
120
|
66
|
try {
|
121
|
66
|
Class c = proxy.getClass();
|
122
|
66
|
setProjectM =
|
123
|
|
c.getMethod("setProject", new Class[] {Project.class});
|
124
|
66
|
if (setProjectM != null) {
|
125
|
66
|
setProjectM.invoke(proxy, new Object[] {getProject()});
|
126
|
|
}
|
127
|
|
} catch (NoSuchMethodException e) {
|
128
|
|
// ignore this if the class being used as a task does not have
|
129
|
|
// a set project method.
|
130
|
|
} catch (Exception ex) {
|
131
|
0
|
log("Error setting project in " + proxy.getClass(),
|
132
|
|
Project.MSG_ERR);
|
133
|
0
|
throw new BuildException(ex);
|
134
|
|
}
|
135
|
|
|
136
|
|
|
137
|
66
|
Method executeM = null;
|
138
|
66
|
try {
|
139
|
66
|
Class c = proxy.getClass();
|
140
|
66
|
executeM = c.getMethod("execute", new Class[0]);
|
141
|
66
|
if (executeM == null) {
|
142
|
0
|
log("No public execute() in " + proxy.getClass(),
|
143
|
|
Project.MSG_ERR);
|
144
|
0
|
throw new BuildException("No public execute() in "
|
145
|
|
+ proxy.getClass());
|
146
|
|
}
|
147
|
66
|
executeM.invoke(proxy, null);
|
148
|
57
|
return;
|
149
|
|
} catch (java.lang.reflect.InvocationTargetException ie) {
|
150
|
9
|
log("Error in " + proxy.getClass(), Project.MSG_ERR);
|
151
|
9
|
Throwable t = ie.getTargetException();
|
152
|
9
|
if (t instanceof BuildException) {
|
153
|
9
|
throw ((BuildException) t);
|
154
|
|
} else {
|
155
|
0
|
throw new BuildException(t);
|
156
|
|
}
|
157
|
|
} catch (Exception ex) {
|
158
|
0
|
log("Error in " + proxy.getClass(), Project.MSG_ERR);
|
159
|
0
|
throw new BuildException(ex);
|
160
|
|
}
|
161
|
|
|
162
|
|
}
|
163
|
|
|
164
|
|
/**
|
165
|
|
* Sets the target object to proxy for.
|
166
|
|
*
|
167
|
|
* @param o The target object. Must not be <code>null</code>.
|
168
|
|
*/
|
169
|
66
|
public void setProxy(Object o) {
|
170
|
66
|
this.proxy = o;
|
171
|
|
}
|
172
|
|
|
173
|
|
/**
|
174
|
|
* Returns the target object being proxied.
|
175
|
|
*
|
176
|
|
* @return the target proxy object
|
177
|
|
*/
|
178
|
258
|
public Object getProxy() {
|
179
|
258
|
return this.proxy ;
|
180
|
|
}
|
181
|
|
|
182
|
|
}
|
183
|
|
|