KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > taskdefs > Apt


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */

18 package org.apache.tools.ant.taskdefs;
19
20 import org.apache.tools.ant.Project;
21 import org.apache.tools.ant.BuildException;
22 import org.apache.tools.ant.taskdefs.compilers.AptExternalCompilerAdapter;
23 import org.apache.tools.ant.types.Path;
24 import org.apache.tools.ant.types.Reference;
25 import org.apache.tools.ant.util.JavaEnvUtils;
26
27 import java.util.Vector JavaDoc;
28 import java.io.File JavaDoc;
29
30 /**
31  * Apt Task for running the Annotation processing tool for JDK 1.5. It derives
32  * from the existing Javac task, and forces the compiler based on whether we're
33  * executing internally, or externally.
34  *
35  * @since Ant 1.7
36  */

37
38
39 public class Apt
40         extends Javac {
41     private boolean compile = true;
42     private String JavaDoc factory;
43     private Path factoryPath;
44     private Vector JavaDoc options = new Vector JavaDoc();
45     private File JavaDoc preprocessDir;
46     /** The name of the apt tool. */
47     public static final String JavaDoc EXECUTABLE_NAME = "apt";
48     /** An warning message when ignoring compiler attribute. */
49     public static final String JavaDoc ERROR_IGNORING_COMPILER_OPTION
50         = "Ignoring compiler attribute for the APT task, as it is fixed";
51     /** A warning message if used with java < 1.5. */
52     public static final String JavaDoc ERROR_WRONG_JAVA_VERSION
53         = "Apt task requires Java 1.5+";
54
55     /**
56      * exposed for debug messages
57      */

58     public static final String JavaDoc WARNING_IGNORING_FORK =
59         "Apt only runs in its own JVM; fork=false option ignored";
60
61     /**
62      * The nested option element.
63      */

64     public static final class Option {
65         private String JavaDoc name;
66         private String JavaDoc value;
67
68         /** Constructor for Option */
69         public Option() {
70             //default
71
}
72
73         /**
74          * Get the name attribute.
75          * @return the name attribute.
76          */

77         public String JavaDoc getName() {
78             return name;
79         }
80
81         /**
82          * Set the name attribute.
83          * @param name the name of the option.
84          */

85         public void setName(String JavaDoc name) {
86             this.name = name;
87         }
88
89         /**
90          * Get the value attribute.
91          * @return the value attribute.
92          */

93         public String JavaDoc getValue() {
94             return value;
95         }
96
97         /**
98          * Set the value attribute.
99          * @param value the value of the option.
100          */

101         public void setValue(String JavaDoc value) {
102             this.value = value;
103         }
104     }
105
106     /**
107      * Construtor for Apt task.
108      * This sets the apt compiler adapter as the compiler in the super class.
109      */

110     public Apt() {
111         super();
112         super.setCompiler(AptExternalCompilerAdapter.class.getName());
113         setFork(true);
114     }
115
116     /**
117      * Get the name of the apt executable.
118      *
119      * @return the name of the executable.
120      */

121     public String JavaDoc getAptExecutable() {
122         return JavaEnvUtils.getJdkExecutable(EXECUTABLE_NAME);
123     }
124
125     /**
126      * Set the compiler.
127      * This is not allowed and a warning log message is made.
128      * @param compiler not used.
129      */

130     public void setCompiler(String JavaDoc compiler) {
131         log(ERROR_IGNORING_COMPILER_OPTION, Project.MSG_WARN);
132     }
133
134     /**
135      * Set the fork attribute.
136      * Non-forking APT is highly classpath dependent and appears to be too
137      * brittle to work. The sole reason this attribute is retained
138      * is the superclass does it
139      * @param fork if false; warn the option is ignored.
140      */

141     public void setFork(boolean fork) {
142         if (!fork) {
143             log(WARNING_IGNORING_FORK, Project.MSG_WARN);
144         }
145     }
146
147     /**
148      * Get the compiler class name.
149      * @return the compiler class name.
150      */

151     public String JavaDoc getCompiler() {
152         return super.getCompiler();
153     }
154
155     /**
156      * Get the compile option for the apt compiler.
157      * If this is false the "-nocompile" argument will be used.
158      * @return the value of the compile option.
159      */

160     public boolean isCompile() {
161         return compile;
162     }
163
164     /**
165      * Set the compile option for the apt compiler.
166      * Default value is true.
167      * @param compile if true set the compile option.
168      */

169     public void setCompile(boolean compile) {
170         this.compile = compile;
171     }
172
173     /**
174      * Get the factory option for the apt compiler.
175      * If this is non-null the "-factory" argument will be used.
176      * @return the value of the factory option.
177      */

178     public String JavaDoc getFactory() {
179         return factory;
180     }
181
182     /**
183      * Set the factory option for the apt compiler.
184      * Default value is null.
185      * @param factory the classname of the factory.
186      */

187     public void setFactory(String JavaDoc factory) {
188         this.factory = factory;
189     }
190
191     /**
192      * Add a reference to a path to the factoryPath attribute.
193      * @param ref a reference to a path.
194      */

195     public void setFactoryPathRef(Reference ref) {
196         createFactoryPath().setRefid(ref);
197     }
198
199     /**
200      * Add a path to the factoryPath attribute.
201      * @return a path to be configured.
202      */

203     public Path createFactoryPath() {
204         if (factoryPath == null) {
205             factoryPath = new Path(getProject());
206         }
207         return factoryPath.createPath();
208     }
209
210     /**
211      * Get the factory path attribute.
212      * If this is not null, the "-factorypath" argument will be used.
213      * The default value is null.
214      * @return the factory path attribute.
215      */

216     public Path getFactoryPath() {
217         return factoryPath;
218     }
219
220     /**
221      * Create a nested option.
222      * @return an option to be configured.
223      */

224     public Option createOption() {
225         Option opt = new Option();
226         options.add(opt);
227         return opt;
228     }
229
230     /**
231      * Get the options to the compiler.
232      * Each option will use '"-E" name ["=" value]' argument.
233      * @return the options.
234      */

235     public Vector JavaDoc getOptions() {
236         return options;
237     }
238
239     /**
240      * Get the preprocessdir attribute.
241      * This corresponds to the "-s" argument.
242      * The default value is null.
243      * @return the preprocessdir attribute.
244      */

245     public File JavaDoc getPreprocessDir() {
246         return preprocessDir;
247     }
248
249     /**
250      * Set the preprocessdir attribute.
251      * @param preprocessDir where to place processor generated source files.
252      */

253     public void setPreprocessDir(File JavaDoc preprocessDir) {
254         this.preprocessDir = preprocessDir;
255     }
256
257     /**
258      * Do the compilation.
259      * @throws BuildException on error.
260      */

261     public void execute()
262             throws BuildException {
263         super.execute();
264     }
265 }
266
Popular Tags