You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Ajc.java 7.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. /* *******************************************************************
  2. * Copyright (c) 2000-2001 Xerox Corporation.
  3. * All rights reserved.
  4. * This program and the accompanying materials are made available
  5. * under the terms of the Eclipse Public License v1.0
  6. * which accompanies this distribution and is available at
  7. * http://www.eclipse.org/legal/epl-v10.html
  8. *
  9. * Contributors:
  10. * Xerox/PARC initial implementation
  11. * ******************************************************************/
  12. package org.aspectj.tools.ant.taskdefs.compilers;
  13. import java.io.File;
  14. import java.io.PrintStream;
  15. import java.util.ArrayList;
  16. import java.util.Arrays;
  17. import java.util.Collections;
  18. import java.util.List;
  19. import org.apache.tools.ant.BuildException;
  20. import org.apache.tools.ant.Project;
  21. import org.apache.tools.ant.taskdefs.Javac;
  22. import org.apache.tools.ant.taskdefs.LogOutputStream;
  23. import org.apache.tools.ant.taskdefs.compilers.DefaultCompilerAdapter;
  24. import org.apache.tools.ant.types.Commandline;
  25. /**
  26. * Ajc uses this as the CompilerAdapter.
  27. *
  28. * This task was developed by the <a href="http://aspectj.org">AspectJ Project</a>
  29. *
  30. * @author <a href="mailto:palm@parc.xerox.com">Jeffrey Palm</a>
  31. * @see org.aspectj.tools.ant.taskdefs.Ajc
  32. */
  33. public class Ajc extends DefaultCompilerAdapter {
  34. /** The value of a compiler success. */
  35. public final static int AJC_COMPILER_SUCCESS = 0;
  36. /** The name of the compiler's main class. */
  37. private final static String MAIN_CLASS_NAME = "org.aspectj.tools.ajc.Main";
  38. /**
  39. * List of arguments allowed only by javac and <b>not</b> ajc.
  40. */
  41. final static List<String> javacOnlyFlags
  42. = finalList(new String[] { "-g:none", "-g:lines",
  43. "-g:vars", "-g:source", "-nowarn"});
  44. final static List<String> javacOnlyArgs
  45. = finalList(new String[] { "-sourcepath",
  46. "-encoding", "-target" });
  47. private static List<String> finalList(String[] args) {
  48. List<String> result = new ArrayList<>(Arrays.asList(args));
  49. return Collections.unmodifiableList(result);
  50. }
  51. /**
  52. * Checks the command line for arguments allowed only in AJC and
  53. * disallowed by AJC and then calls the <code>compile()<code> method.
  54. *
  55. * @return true if a good compile, false otherwise.
  56. * @throws org.apache.tools.ant.BuildException
  57. */
  58. @Override
  59. public boolean execute() throws BuildException {
  60. attributes.log("Using AJC", Project.MSG_VERBOSE);
  61. return compile(addAjcOptions(setupJavacCommand()));
  62. }
  63. /**
  64. * Invokes the AJC compiler using reflection.
  65. *
  66. * @param cline the command line to pass the compiler
  67. * @return true for a good compile (0), false otherwise
  68. * @throws org.apache.tools.ant.BuildException
  69. */
  70. private boolean compile(Commandline cline) throws BuildException {
  71. PrintStream err = System.err;
  72. PrintStream out = System.out;
  73. try {
  74. Class main = Class.forName(MAIN_CLASS_NAME);
  75. if (main == null) {
  76. throw new ClassNotFoundException(MAIN_CLASS_NAME);
  77. }
  78. PrintStream logstr =
  79. new PrintStream(new LogOutputStream(attributes,
  80. Project.MSG_WARN));
  81. System.setOut(logstr);
  82. System.setErr(logstr);
  83. return (Integer) main.getMethod
  84. ("compile", new Class[]{String[].class}).invoke
  85. (main.newInstance(), new Object[]{
  86. removeUnsupported(cline, logstr)
  87. }) == AJC_COMPILER_SUCCESS;
  88. } catch (Exception e) {
  89. if (e instanceof BuildException) {
  90. throw (BuildException)e;
  91. } else {
  92. throw new BuildException("Error starting AJC compiler",
  93. e, location);
  94. }
  95. } finally {
  96. System.setErr(err);
  97. System.setOut(out);
  98. }
  99. }
  100. /**
  101. * Removes unsupported arguments from <code>cline</code>
  102. * issuing warnings for each using <code>log</code>.
  103. *
  104. * @param cline the <code>org.apache.tools.ant.types.Commandline</code> from
  105. * which the argument is removed.
  106. * @return a new <code>java.lang.String</code> array containing all the
  107. * supported arguments found in <code>cline</code>.
  108. * @throws org.apache.tools.ant.BuildException
  109. */
  110. private String[] removeUnsupported(Commandline cline, PrintStream log) {
  111. if (null == log) log = System.err;
  112. String[] args = cline.getCommandline();
  113. List argsList = new ArrayList();
  114. for (int i = 0; i < args.length; i++) {
  115. String arg = args[i];
  116. if (javacOnlyFlags.contains(arg)) {
  117. log.println("ignored by ajc " + arg);
  118. } else if (javacOnlyArgs.contains(arg)) {
  119. i++;
  120. if (i < args.length) {
  121. arg = arg + " " + args[i];
  122. }
  123. log.println("ignored by ajc " + arg);
  124. } else {
  125. argsList.add(args[i]);
  126. }
  127. }
  128. return (String[])argsList.toArray(new String[0]);
  129. }
  130. /**
  131. * Adds arguments that setupJavacCommand() doesn't pick up.
  132. *
  133. * @param cline <code>org.apache.tools.ant.types.Commandline</code> to
  134. * which arguments are added.
  135. * @throws org.apache.tools.ant.BuildException
  136. * @see AjcCompiler#ajcOnlyArgs
  137. * @see org.apache.tools.ant.taskdefs.compilers#DefaultCompilerAdapter.setupJavacCommand()
  138. */
  139. private Commandline addAjcOptions(Commandline cline) throws BuildException {
  140. Javac javac = getJavac();
  141. org.aspectj.tools.ant.taskdefs.Ajc2 ajc = null;
  142. try {
  143. ajc = (org.aspectj.tools.ant.taskdefs.Ajc2)javac;
  144. } catch (ClassCastException cce) {
  145. throw new BuildException(cce+"");
  146. }
  147. if (ajc.getThreads() != null) {
  148. cline.createArgument().setValue("-threads");
  149. cline.createArgument().setValue(ajc.getThreads() + "");
  150. }
  151. if (ajc.getNocomments()) {
  152. cline.createArgument().setValue("-nocomments");
  153. }
  154. if (ajc.getNosymbols()) {
  155. cline.createArgument().setValue("-nosymbols");
  156. }
  157. if (ajc.getPreprocess()) {
  158. cline.createArgument().setValue("-preprocess");
  159. }
  160. if (ajc.getWorkingdir() != null) {
  161. cline.createArgument().setValue("-workingdir");
  162. cline.createArgument().setFile(ajc.getWorkingdir());
  163. }
  164. return cline;
  165. }
  166. /**
  167. * Logs the compilation parameters, adds the files to compile and logs the
  168. * &quot;niceSourceList&quot;
  169. */
  170. @Override
  171. protected void logAndAddFilesToCompile(Commandline cmd) {
  172. // Same behavior as DefaultCompilerAdapter.logAndAddFilesToCompile
  173. attributes.log("Compilation args: " + cmd.toString(), Project.MSG_VERBOSE);
  174. StringBuffer niceSourceList = new StringBuffer("File");
  175. if (compileList.length != 1) {
  176. niceSourceList.append("s");
  177. }
  178. niceSourceList.append(" to be compiled:");
  179. niceSourceList.append(lSep);
  180. for (File file : compileList) {
  181. // DefaultCompilerAdapter only expects .java files but we must deal
  182. // with .lst files also
  183. if (file == null) continue;
  184. String arg = file.getAbsolutePath();
  185. String rest = "";
  186. String name = file.getName();
  187. // For .java files take the default behavior and add that
  188. // file to the command line
  189. if (name.endsWith(".java")) {
  190. cmd.createArgument().setValue(arg);
  191. }
  192. niceSourceList.append(" " + arg + rest + lSep);
  193. }
  194. attributes.log(niceSourceList.toString(), Project.MSG_VERBOSE);
  195. }
  196. }