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.

Apt.java 7.4 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. /*
  2. * Copyright 2002-2005 The Apache Software Foundation
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. *
  16. */
  17. package org.apache.tools.ant.taskdefs;
  18. import org.apache.tools.ant.Project;
  19. import org.apache.tools.ant.BuildException;
  20. import org.apache.tools.ant.taskdefs.compilers.AptExternalCompilerAdapter;
  21. import org.apache.tools.ant.taskdefs.compilers.AptCompilerAdapter;
  22. import org.apache.tools.ant.types.Path;
  23. import org.apache.tools.ant.types.Reference;
  24. import org.apache.tools.ant.util.JavaEnvUtils;
  25. import java.util.Vector;
  26. import java.io.File;
  27. /**
  28. * Apt Task for running the Annotation processing tool for JDK 1.5. It derives
  29. * from the existing Javac task, and forces the compiler based on whether we're
  30. * executing internally, or externally.
  31. *
  32. * @since Ant 1.7
  33. */
  34. public class Apt
  35. extends Javac {
  36. private boolean compile = true;
  37. private String factory;
  38. private Path factoryPath;
  39. private Vector options = new Vector();
  40. private File preprocessDir;
  41. /** The name of the apt tool. */
  42. public static final String EXECUTABLE_NAME = "apt";
  43. /** An warning message when ignoring compiler attribute. */
  44. public static final String ERROR_IGNORING_COMPILER_OPTION
  45. = "Ignoring compiler attribute for the APT task, as it is fixed";
  46. /** A warning message if used with java < 1.5. */
  47. public static final String ERROR_WRONG_JAVA_VERSION
  48. = "Apt task requires Java 1.5+";
  49. /**
  50. * exposed for debug messages
  51. */
  52. public static final String WARNING_IGNORING_FORK =
  53. "Apt only runs in its own JVM; fork=false option ignored";
  54. /**
  55. * The nested option element.
  56. */
  57. public static final class Option {
  58. private String name;
  59. private String value;
  60. /** Constructor for Option */
  61. public Option() {
  62. //default
  63. }
  64. /**
  65. * Get the name attribute.
  66. * @return the name attribute.
  67. */
  68. public String getName() {
  69. return name;
  70. }
  71. /**
  72. * Set the name attribute.
  73. * @param name the name of the option.
  74. */
  75. public void setName(String name) {
  76. this.name = name;
  77. }
  78. /**
  79. * Get the value attribute.
  80. * @return the value attribute.
  81. */
  82. public String getValue() {
  83. return value;
  84. }
  85. /**
  86. * Set the value attribute.
  87. * @param value the value of the option.
  88. */
  89. public void setValue(String value) {
  90. this.value = value;
  91. }
  92. }
  93. /**
  94. * Construtor for Apt task.
  95. * This sets the apt compiler adapter as the compiler in the super class.
  96. */
  97. public Apt() {
  98. super();
  99. super.setCompiler(AptExternalCompilerAdapter.class.getName());
  100. setFork(true);
  101. }
  102. /**
  103. * Get the name of the apt executable.
  104. *
  105. * @return the name of the executable.
  106. */
  107. public String getAptExecutable() {
  108. return JavaEnvUtils.getJdkExecutable(EXECUTABLE_NAME);
  109. }
  110. /**
  111. * Set the compiler.
  112. * This is not allowed and a warning log message is made.
  113. * @param compiler not used.
  114. */
  115. public void setCompiler(String compiler) {
  116. log(ERROR_IGNORING_COMPILER_OPTION, Project.MSG_WARN);
  117. }
  118. /**
  119. * Set the fork attribute.
  120. * Non-forking APT is highly classpath dependent and appears to be too
  121. * brittle to work. The sole reason this attribute is retained
  122. * is the superclass does it
  123. * @param fork if false; warn the option is ignored.
  124. */
  125. public void setFork(boolean fork) {
  126. if (!fork) {
  127. log(WARNING_IGNORING_FORK,Project.MSG_WARN);
  128. }
  129. }
  130. /**
  131. * Get the compiler class name.
  132. * @return the compiler class name.
  133. */
  134. public String getCompiler() {
  135. return super.getCompiler();
  136. }
  137. /**
  138. * Get the compile option for the apt compiler.
  139. * If this is false the "-nocompile" argument will be used.
  140. * @return the value of the compile option.
  141. */
  142. public boolean isCompile() {
  143. return compile;
  144. }
  145. /**
  146. * Set the compile option for the apt compiler.
  147. * Default value is true.
  148. * @param compile if true set the compile option.
  149. */
  150. public void setCompile(boolean compile) {
  151. this.compile = compile;
  152. }
  153. /**
  154. * Get the factory option for the apt compiler.
  155. * If this is non-null the "-factory" argument will be used.
  156. * @return the value of the factory option.
  157. */
  158. public String getFactory() {
  159. return factory;
  160. }
  161. /**
  162. * Set the factory option for the apt compiler.
  163. * Default value is null.
  164. * @param factory the classname of the factory.
  165. */
  166. public void setFactory(String factory) {
  167. this.factory = factory;
  168. }
  169. /**
  170. * Add a reference to a path to the factoryPath attribute.
  171. * @param ref a reference to a path.
  172. */
  173. public void setFactoryPathRef(Reference ref) {
  174. createFactoryPath().setRefid(ref);
  175. }
  176. /**
  177. * Add a path to the factoryPath attribute.
  178. * @return a path to be configured.
  179. */
  180. public Path createFactoryPath() {
  181. if (factoryPath == null) {
  182. factoryPath = new Path(getProject());
  183. }
  184. return factoryPath.createPath();
  185. }
  186. /**
  187. * Get the factory path attribute.
  188. * If this is not null, the "-factorypath" argument will be used.
  189. * The default value is null.
  190. * @return the factory path attribute.
  191. */
  192. public Path getFactoryPath() {
  193. return factoryPath;
  194. }
  195. /**
  196. * Create a nested option.
  197. * @return an option to be configured.
  198. */
  199. public Option createOption() {
  200. Option opt = new Option();
  201. options.add(opt);
  202. return opt;
  203. }
  204. /**
  205. * Get the options to the compiler.
  206. * Each option will use '"-E" name ["=" value]' argument.
  207. * @return the options.
  208. */
  209. public Vector getOptions() {
  210. return options;
  211. }
  212. /**
  213. * Get the preprocessdir attribute.
  214. * This corresponds to the "-s" argument.
  215. * The default value is null.
  216. * @return the preprocessdir attribute.
  217. */
  218. public File getPreprocessDir() {
  219. return preprocessDir;
  220. }
  221. /**
  222. * Set the preprocessdir attribute.
  223. * @param preprocessDir where to place processor generated source files.
  224. */
  225. public void setPreprocessDir(File preprocessDir) {
  226. this.preprocessDir = preprocessDir;
  227. }
  228. /**
  229. * Do the compilation.
  230. * @throws BuildException on error.
  231. */
  232. public void execute()
  233. throws BuildException {
  234. if (!JavaEnvUtils.isJavaVersion(JavaEnvUtils.JAVA_1_5)) {
  235. throw new BuildException(ERROR_WRONG_JAVA_VERSION);
  236. }
  237. super.execute();
  238. }
  239. }