001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018: package org.apache.tools.ant.taskdefs;
019:
020: import org.apache.tools.ant.Project;
021: import org.apache.tools.ant.BuildException;
022: import org.apache.tools.ant.taskdefs.compilers.AptExternalCompilerAdapter;
023: import org.apache.tools.ant.types.Path;
024: import org.apache.tools.ant.types.Reference;
025: import org.apache.tools.ant.util.JavaEnvUtils;
026:
027: import java.util.Vector;
028: import java.io.File;
029:
030: /**
031: * Apt Task for running the Annotation processing tool for JDK 1.5. It derives
032: * from the existing Javac task, and forces the compiler based on whether we're
033: * executing internally, or externally.
034: *
035: * @since Ant 1.7
036: */
037:
038: public class Apt extends Javac {
039: private boolean compile = true;
040: private String factory;
041: private Path factoryPath;
042: private Vector options = new Vector();
043: private File preprocessDir;
044: /** The name of the apt tool. */
045: public static final String EXECUTABLE_NAME = "apt";
046: /** An warning message when ignoring compiler attribute. */
047: public static final String ERROR_IGNORING_COMPILER_OPTION = "Ignoring compiler attribute for the APT task, as it is fixed";
048: /** A warning message if used with java < 1.5. */
049: public static final String ERROR_WRONG_JAVA_VERSION = "Apt task requires Java 1.5+";
050:
051: /**
052: * exposed for debug messages
053: */
054: public static final String WARNING_IGNORING_FORK = "Apt only runs in its own JVM; fork=false option ignored";
055:
056: /**
057: * The nested option element.
058: */
059: public static final class Option {
060: private String name;
061: private String value;
062:
063: /** Constructor for Option */
064: public Option() {
065: //default
066: }
067:
068: /**
069: * Get the name attribute.
070: * @return the name attribute.
071: */
072: public String getName() {
073: return name;
074: }
075:
076: /**
077: * Set the name attribute.
078: * @param name the name of the option.
079: */
080: public void setName(String name) {
081: this .name = name;
082: }
083:
084: /**
085: * Get the value attribute.
086: * @return the value attribute.
087: */
088: public String getValue() {
089: return value;
090: }
091:
092: /**
093: * Set the value attribute.
094: * @param value the value of the option.
095: */
096: public void setValue(String value) {
097: this .value = value;
098: }
099: }
100:
101: /**
102: * Construtor for Apt task.
103: * This sets the apt compiler adapter as the compiler in the super class.
104: */
105: public Apt() {
106: super ();
107: super .setCompiler(AptExternalCompilerAdapter.class.getName());
108: setFork(true);
109: }
110:
111: /**
112: * Get the name of the apt executable.
113: *
114: * @return the name of the executable.
115: */
116: public String getAptExecutable() {
117: return JavaEnvUtils.getJdkExecutable(EXECUTABLE_NAME);
118: }
119:
120: /**
121: * Set the compiler.
122: * This is not allowed and a warning log message is made.
123: * @param compiler not used.
124: */
125: public void setCompiler(String compiler) {
126: log(ERROR_IGNORING_COMPILER_OPTION, Project.MSG_WARN);
127: }
128:
129: /**
130: * Set the fork attribute.
131: * Non-forking APT is highly classpath dependent and appears to be too
132: * brittle to work. The sole reason this attribute is retained
133: * is the superclass does it
134: * @param fork if false; warn the option is ignored.
135: */
136: public void setFork(boolean fork) {
137: if (!fork) {
138: log(WARNING_IGNORING_FORK, Project.MSG_WARN);
139: }
140: }
141:
142: /**
143: * Get the compiler class name.
144: * @return the compiler class name.
145: */
146: public String getCompiler() {
147: return super .getCompiler();
148: }
149:
150: /**
151: * Get the compile option for the apt compiler.
152: * If this is false the "-nocompile" argument will be used.
153: * @return the value of the compile option.
154: */
155: public boolean isCompile() {
156: return compile;
157: }
158:
159: /**
160: * Set the compile option for the apt compiler.
161: * Default value is true.
162: * @param compile if true set the compile option.
163: */
164: public void setCompile(boolean compile) {
165: this .compile = compile;
166: }
167:
168: /**
169: * Get the factory option for the apt compiler.
170: * If this is non-null the "-factory" argument will be used.
171: * @return the value of the factory option.
172: */
173: public String getFactory() {
174: return factory;
175: }
176:
177: /**
178: * Set the factory option for the apt compiler.
179: * Default value is null.
180: * @param factory the classname of the factory.
181: */
182: public void setFactory(String factory) {
183: this .factory = factory;
184: }
185:
186: /**
187: * Add a reference to a path to the factoryPath attribute.
188: * @param ref a reference to a path.
189: */
190: public void setFactoryPathRef(Reference ref) {
191: createFactoryPath().setRefid(ref);
192: }
193:
194: /**
195: * Add a path to the factoryPath attribute.
196: * @return a path to be configured.
197: */
198: public Path createFactoryPath() {
199: if (factoryPath == null) {
200: factoryPath = new Path(getProject());
201: }
202: return factoryPath.createPath();
203: }
204:
205: /**
206: * Get the factory path attribute.
207: * If this is not null, the "-factorypath" argument will be used.
208: * The default value is null.
209: * @return the factory path attribute.
210: */
211: public Path getFactoryPath() {
212: return factoryPath;
213: }
214:
215: /**
216: * Create a nested option.
217: * @return an option to be configured.
218: */
219: public Option createOption() {
220: Option opt = new Option();
221: options.add(opt);
222: return opt;
223: }
224:
225: /**
226: * Get the options to the compiler.
227: * Each option will use '"-E" name ["=" value]' argument.
228: * @return the options.
229: */
230: public Vector getOptions() {
231: return options;
232: }
233:
234: /**
235: * Get the preprocessdir attribute.
236: * This corresponds to the "-s" argument.
237: * The default value is null.
238: * @return the preprocessdir attribute.
239: */
240: public File getPreprocessDir() {
241: return preprocessDir;
242: }
243:
244: /**
245: * Set the preprocessdir attribute.
246: * @param preprocessDir where to place processor generated source files.
247: */
248: public void setPreprocessDir(File preprocessDir) {
249: this .preprocessDir = preprocessDir;
250: }
251:
252: /**
253: * Do the compilation.
254: * @throws BuildException on error.
255: */
256: public void execute() throws BuildException {
257: super.execute();
258: }
259: }
|