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.6 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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. import org.apache.tools.ant.Project;
  20. import org.apache.tools.ant.BuildException;
  21. import org.apache.tools.ant.taskdefs.compilers.AptExternalCompilerAdapter;
  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<Option> options = new Vector<Option>();
  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. * Constructor 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. super.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. String exe = getExecutable();
  109. return exe != null ? exe :
  110. JavaEnvUtils.getJdkExecutable(EXECUTABLE_NAME);
  111. }
  112. /**
  113. * Set the compiler.
  114. * This is not allowed and a warning log message is made.
  115. * @param compiler not used.
  116. */
  117. public void setCompiler(String compiler) {
  118. log(ERROR_IGNORING_COMPILER_OPTION, Project.MSG_WARN);
  119. }
  120. /**
  121. * Set the fork attribute.
  122. * Non-forking APT is highly classpath dependent and appears to be too
  123. * brittle to work. The sole reason this attribute is retained
  124. * is the superclass does it
  125. * @param fork if false; warn the option is ignored.
  126. */
  127. public void setFork(boolean fork) {
  128. if (!fork) {
  129. log(WARNING_IGNORING_FORK, Project.MSG_WARN);
  130. }
  131. }
  132. /**
  133. * Get the compiler class name.
  134. * @return the compiler class name.
  135. */
  136. public String getCompiler() {
  137. return super.getCompiler();
  138. }
  139. /**
  140. * Get the compile option for the apt compiler.
  141. * If this is false the "-nocompile" argument will be used.
  142. * @return the value of the compile option.
  143. */
  144. public boolean isCompile() {
  145. return compile;
  146. }
  147. /**
  148. * Set the compile option for the apt compiler.
  149. * Default value is true.
  150. * @param compile if true set the compile option.
  151. */
  152. public void setCompile(boolean compile) {
  153. this.compile = compile;
  154. }
  155. /**
  156. * Get the factory option for the apt compiler.
  157. * If this is non-null the "-factory" argument will be used.
  158. * @return the value of the factory option.
  159. */
  160. public String getFactory() {
  161. return factory;
  162. }
  163. /**
  164. * Set the factory option for the apt compiler.
  165. * Default value is null.
  166. * @param factory the classname of the factory.
  167. */
  168. public void setFactory(String factory) {
  169. this.factory = factory;
  170. }
  171. /**
  172. * Add a reference to a path to the factoryPath attribute.
  173. * @param ref a reference to a path.
  174. */
  175. public void setFactoryPathRef(Reference ref) {
  176. createFactoryPath().setRefid(ref);
  177. }
  178. /**
  179. * Add a path to the factoryPath attribute.
  180. * @return a path to be configured.
  181. */
  182. public Path createFactoryPath() {
  183. if (factoryPath == null) {
  184. factoryPath = new Path(getProject());
  185. }
  186. return factoryPath.createPath();
  187. }
  188. /**
  189. * Get the factory path attribute.
  190. * If this is not null, the "-factorypath" argument will be used.
  191. * The default value is null.
  192. * @return the factory path attribute.
  193. */
  194. public Path getFactoryPath() {
  195. return factoryPath;
  196. }
  197. /**
  198. * Create a nested option.
  199. * @return an option to be configured.
  200. */
  201. public Option createOption() {
  202. Option opt = new Option();
  203. options.add(opt);
  204. return opt;
  205. }
  206. /**
  207. * Get the options to the compiler.
  208. * Each option will use '"-E" name ["=" value]' argument.
  209. * @return the options.
  210. */
  211. public Vector<Option> getOptions() {
  212. return options;
  213. }
  214. /**
  215. * Get the preprocessdir attribute.
  216. * This corresponds to the "-s" argument.
  217. * The default value is null.
  218. * @return the preprocessdir attribute.
  219. */
  220. public File getPreprocessDir() {
  221. return preprocessDir;
  222. }
  223. /**
  224. * Set the preprocessdir attribute.
  225. * @param preprocessDir where to place processor generated source files.
  226. */
  227. public void setPreprocessDir(File preprocessDir) {
  228. this.preprocessDir = preprocessDir;
  229. }
  230. /**
  231. * Do the compilation.
  232. * @throws BuildException on error.
  233. */
  234. public void execute()
  235. throws BuildException {
  236. if (JavaEnvUtils.getJavaVersionNumber() >= 18) {
  237. throw new BuildException("apt does not exist under Java 1.8 and higher");
  238. }
  239. super.execute();
  240. }
  241. }