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.

BuildArgParser.java 34KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929
  1. /* *******************************************************************
  2. * Copyright (c) 2002 Palo Alto Research Center, Incorporated (PARC).
  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. * PARC initial implementation
  11. * ******************************************************************/
  12. package org.aspectj.ajdt.ajc;
  13. import org.aspectj.ajdt.internal.compiler.lookup.EclipseSourceLocation;
  14. import org.aspectj.ajdt.internal.core.builder.AjBuildConfig;
  15. import org.aspectj.bridge.*;
  16. import org.aspectj.org.eclipse.jdt.core.compiler.CategorizedProblem;
  17. import org.aspectj.org.eclipse.jdt.internal.compiler.apt.dispatch.AptProblem;
  18. import org.aspectj.org.eclipse.jdt.internal.compiler.batch.FileSystem;
  19. import org.aspectj.org.eclipse.jdt.internal.compiler.batch.Main;
  20. import org.aspectj.org.eclipse.jdt.internal.compiler.classfmt.ClassFileConstants;
  21. import org.aspectj.org.eclipse.jdt.internal.compiler.env.IModule;
  22. import org.aspectj.org.eclipse.jdt.internal.compiler.impl.CompilerOptions;
  23. import org.aspectj.util.FileUtil;
  24. import org.aspectj.util.LangUtil;
  25. import org.aspectj.weaver.Constants;
  26. import org.aspectj.weaver.Dump;
  27. import org.aspectj.weaver.WeaverMessages;
  28. import java.io.File;
  29. import java.io.IOException;
  30. import java.io.PrintWriter;
  31. import java.io.StringWriter;
  32. import java.util.*;
  33. @SuppressWarnings("unchecked")
  34. public class BuildArgParser extends Main {
  35. private static final String BUNDLE_NAME = "org.aspectj.ajdt.ajc.messages";
  36. private static boolean LOADED_BUNDLE = false;
  37. static {
  38. Main.bundleName = BUNDLE_NAME;
  39. ResourceBundleFactory.getBundle(Locale.getDefault());
  40. if (!LOADED_BUNDLE) {
  41. LOADED_BUNDLE = true;
  42. }
  43. }
  44. /** to initialize super's PrintWriter but refer to underlying StringWriter */
  45. private static class StringPrintWriter extends PrintWriter {
  46. public final StringWriter stringWriter;
  47. StringPrintWriter(StringWriter sw) {
  48. super(sw);
  49. this.stringWriter = sw;
  50. }
  51. }
  52. /** @return multi-line String usage for the compiler */
  53. public static String getUsage() {
  54. return _bind("misc.usage", new String[] { _bind("compiler.name", (String[]) null) });
  55. }
  56. public static String getXOptionUsage() {
  57. return _bind("xoption.usage", new String[] { _bind("compiler.name", (String[]) null) });
  58. }
  59. /**
  60. * StringWriter sink for some errors. This only captures errors not handled by any IMessageHandler parameter and only when no
  61. * PrintWriter is set in the constructor. XXX This relies on (Sun's) implementation of StringWriter, which returns the actual
  62. * (not copy) internal StringBuffer.
  63. */
  64. private final StringBuffer errorSink;
  65. private IMessageHandler handler;
  66. /**
  67. * Overrides super's bundle.
  68. */
  69. public BuildArgParser(PrintWriter writer, IMessageHandler handler) {
  70. super(writer, writer, false, null, null);
  71. if (writer instanceof StringPrintWriter) {
  72. errorSink = ((StringPrintWriter) writer).stringWriter.getBuffer();
  73. } else {
  74. errorSink = null;
  75. }
  76. this.handler = handler;
  77. }
  78. /** Set up to capture messages using getOtherMessages(boolean) */
  79. public BuildArgParser(IMessageHandler handler) {
  80. this(new StringPrintWriter(new StringWriter()), handler);
  81. }
  82. /**
  83. * Generate build configuration for the input args, passing to handler any error messages.
  84. *
  85. * @param args the String[] arguments for the build configuration
  86. * @return AjBuildConfig per args, which will be invalid unless there are no handler errors.
  87. */
  88. public AjBuildConfig genBuildConfig(String[] args) {
  89. final AjBuildConfig config = new AjBuildConfig(this);
  90. populateBuildConfig(config, args, true, null);
  91. return config;
  92. }
  93. /**
  94. * Generate build configuration for the input arguments, passing to handler any error messages.
  95. *
  96. * @param args the String[] arguments for the build configuration
  97. * @param setClasspath determines if the classpath should be parsed and set on the build configuration
  98. * @param configFile can be null
  99. * @return AjBuildConfig per arguments, which will be invalid unless there are no handler errors.
  100. */
  101. public AjBuildConfig populateBuildConfig(AjBuildConfig buildConfig, String[] args, boolean setClasspath, File configFile) {
  102. Dump.saveCommandLine(args);
  103. buildConfig.setConfigFile(configFile);
  104. try {
  105. // sets filenames to be non-null in order to make sure that file parameters are ignored
  106. super.filenames = new String[] { "" };
  107. AjcConfigParser parser = new AjcConfigParser(buildConfig, handler);
  108. parser.parseCommandLine(args);
  109. boolean swi = buildConfig.getShowWeavingInformation();
  110. // Now jump through firey hoops to turn them on/off
  111. if (handler instanceof CountingMessageHandler) {
  112. IMessageHandler delegate = ((CountingMessageHandler) handler).delegate;
  113. if (swi) {
  114. delegate.dontIgnore(IMessage.WEAVEINFO);
  115. } else {
  116. delegate.ignore(IMessage.WEAVEINFO);
  117. }
  118. }
  119. boolean incrementalMode = buildConfig.isIncrementalMode() || buildConfig.isIncrementalFileMode();
  120. List<File> xmlfileList = new ArrayList<File>();
  121. xmlfileList.addAll(parser.getXmlFiles());
  122. List<File> fileList = new ArrayList<File>();
  123. List<File> files = parser.getFiles();
  124. if (!LangUtil.isEmpty(files)) {
  125. if (incrementalMode) {
  126. MessageUtil.error(handler, "incremental mode only handles source files using -sourceroots");
  127. } else {
  128. fileList.addAll(files);
  129. }
  130. }
  131. List<String> javaArgList = new ArrayList<String>();
  132. // disable all special eclipse warnings by default - why???
  133. // ??? might want to instead override getDefaultOptions()
  134. javaArgList.add("-warn:none");
  135. // these next four lines are some nonsense to fool the eclipse batch compiler
  136. // without these it will go searching for reasonable values from properties
  137. // TODO fix org.eclipse.jdt.internal.compiler.batch.Main so this hack isn't needed
  138. javaArgList.add("-classpath");
  139. javaArgList.add(parser.classpath == null ? System.getProperty("user.dir") : parser.classpath);
  140. // javaArgList.add("-bootclasspath");
  141. // javaArgList.add(parser.bootclasspath == null ? System.getProperty("user.dir") : parser.bootclasspath);
  142. javaArgList.addAll(parser.getUnparsedArgs());
  143. super.configure(javaArgList.toArray(new String[javaArgList.size()]));
  144. if (parser.getModuleInfoArgument() != null) {
  145. IModule moduleDesc = super.getModuleDesc(parser.getModuleInfoArgument());
  146. buildConfig.setModuleDesc(moduleDesc);
  147. }
  148. if (!proceed) {
  149. buildConfig.doNotProceed();
  150. return buildConfig;
  151. }
  152. if (buildConfig.getSourceRoots() != null) {
  153. for (Iterator i = buildConfig.getSourceRoots().iterator(); i.hasNext();) {
  154. fileList.addAll(collectSourceRootFiles((File) i.next()));
  155. }
  156. }
  157. buildConfig.setXmlFiles(xmlfileList);
  158. buildConfig.setFiles(fileList);
  159. if (destinationPath != null) { // XXX ?? unparsed but set?
  160. buildConfig.setOutputDir(new File(destinationPath));
  161. }
  162. if (setClasspath) {
  163. // This computed classpaths will be missing aspectpaths, inpaths, add those first
  164. buildConfig.setClasspath(getClasspath(parser));
  165. buildConfig.setModulepath(getModulepath(parser));
  166. buildConfig.setModulepathClasspathEntries(handleModulepath(parser.modulepath));
  167. buildConfig.setModulesourcepath(getModulesourcepath(parser));
  168. buildConfig.setModulesourcepathClasspathEntries(handleModuleSourcepath(parser.modulesourcepath));
  169. buildConfig.setBootclasspath(getBootclasspath(parser));
  170. // TODO other paths (module/module source)
  171. }
  172. if (incrementalMode && (0 == buildConfig.getSourceRoots().size())) {
  173. MessageUtil.error(handler, "specify a source root when in incremental mode");
  174. }
  175. /*
  176. * Ensure we don't overwrite injars, inpath or aspectpath with outjar bug-71339
  177. */
  178. File outjar = buildConfig.getOutputJar();
  179. if (outjar != null) {
  180. /* Search injars */
  181. for (Iterator i = buildConfig.getInJars().iterator(); i.hasNext();) {
  182. File injar = (File) i.next();
  183. if (injar.equals(outjar)) {
  184. String message = WeaverMessages.format(WeaverMessages.OUTJAR_IN_INPUT_PATH);
  185. MessageUtil.error(handler, message);
  186. }
  187. }
  188. /* Search inpath */
  189. for (Iterator i = buildConfig.getInpath().iterator(); i.hasNext();) {
  190. File inPathElement = (File) i.next();
  191. if (!inPathElement.isDirectory() && inPathElement.equals(outjar)) {
  192. String message = WeaverMessages.format(WeaverMessages.OUTJAR_IN_INPUT_PATH);
  193. MessageUtil.error(handler, message);
  194. }
  195. }
  196. /* Search aspectpath */
  197. for (File pathElement: buildConfig.getAspectpath()) {
  198. if (!pathElement.isDirectory() && pathElement.equals(outjar)) {
  199. String message = WeaverMessages.format(WeaverMessages.OUTJAR_IN_INPUT_PATH);
  200. MessageUtil.error(handler, message);
  201. }
  202. }
  203. }
  204. setDebugOptions();
  205. buildConfig.getOptions().set(options);
  206. } catch (IllegalArgumentException iae) {
  207. ISourceLocation location = null;
  208. if (buildConfig.getConfigFile() != null) {
  209. location = new SourceLocation(buildConfig.getConfigFile(), 0);
  210. }
  211. IMessage m = new Message(iae.getMessage(), IMessage.ERROR, null, location);
  212. handler.handleMessage(m);
  213. }
  214. return buildConfig;
  215. }
  216. private void augmentCheckedClasspaths(List<File> extraPathEntries, String encoding) {
  217. if (extraPathEntries.size() == 0) {
  218. return;
  219. }
  220. ArrayList<String> asList = toArrayList(extraPathEntries);
  221. List<FileSystem.Classpath> newClasspathEntries = handleClasspath(asList, encoding);
  222. FileSystem.Classpath[] newCheckedClasspaths = new FileSystem.Classpath[checkedClasspaths.length + newClasspathEntries.size()];
  223. System.arraycopy(checkedClasspaths, 0, newCheckedClasspaths, 0, checkedClasspaths.length);
  224. for (int i = 0; i < newClasspathEntries.size();i++) {
  225. newCheckedClasspaths[i + checkedClasspaths.length] = newClasspathEntries.get(i);
  226. }
  227. checkedClasspaths = newCheckedClasspaths;
  228. }
  229. private ArrayList<String> toArrayList(java.util.List<File> files) {
  230. ArrayList<String> arrayList = new ArrayList<String>();
  231. for (File file: files) {
  232. arrayList.add(file.getAbsolutePath());
  233. }
  234. return arrayList;
  235. }
  236. public void printVersion() {
  237. final String version = bind("misc.version", //$NON-NLS-1$
  238. new String[] { bind("compiler.name"), //$NON-NLS-1$
  239. Version.text + " - Built: " + Version.time_text, bind("compiler.version"), //$NON-NLS-1$
  240. bind("compiler.copyright") //$NON-NLS-1$
  241. });
  242. System.out.println(version);
  243. }
  244. @Override
  245. public void addExtraProblems(CategorizedProblem problem) {
  246. super.addExtraProblems(problem);
  247. if (problem instanceof AptProblem) {
  248. handler.handleMessage(newAptMessage((AptProblem)problem));
  249. }
  250. }
  251. private IMessage newAptMessage(AptProblem problem) {
  252. String message = problem.getMessage();
  253. boolean isError = problem.isError();
  254. if (problem._referenceContext != null) {
  255. return new Message(message,
  256. new EclipseSourceLocation(problem._referenceContext.compilationResult(), problem.getSourceStart(), problem.getSourceEnd()),
  257. isError);
  258. } else {
  259. return new Message(message, null, isError);
  260. }
  261. }
  262. @Override
  263. public void initializeAnnotationProcessorManager() {
  264. if (this.compilerOptions.complianceLevel < ClassFileConstants.JDK1_6 || !this.compilerOptions.processAnnotations)
  265. return;
  266. super.initializeAnnotationProcessorManager();
  267. }
  268. @Override
  269. public void printUsage() {
  270. System.out.println(getUsage());
  271. System.out.flush();
  272. }
  273. /**
  274. * Get messages not dumped to handler or any PrintWriter.
  275. *
  276. * @param flush if true, empty errors
  277. * @return null if none, String otherwise
  278. * @see BuildArgParser()
  279. */
  280. public String getOtherMessages(boolean flush) {
  281. if (null == errorSink) {
  282. return null;
  283. }
  284. String result = errorSink.toString().trim();
  285. if (0 == result.length()) {
  286. result = null;
  287. }
  288. if (flush) {
  289. errorSink.setLength(0);
  290. }
  291. return result;
  292. }
  293. private void setDebugOptions() {
  294. options.put(CompilerOptions.OPTION_LocalVariableAttribute, CompilerOptions.GENERATE);
  295. options.put(CompilerOptions.OPTION_LineNumberAttribute, CompilerOptions.GENERATE);
  296. options.put(CompilerOptions.OPTION_SourceFileAttribute, CompilerOptions.GENERATE);
  297. }
  298. private Collection collectSourceRootFiles(File dir) {
  299. return Arrays.asList(FileUtil.listFiles(dir, FileUtil.aspectjSourceFileFilter));
  300. }
  301. public List<String> getBootclasspath(AjcConfigParser parser) {
  302. List<String> ret = new ArrayList<String>();
  303. if (parser.bootclasspath == null) {
  304. if (LangUtil.is19VMOrGreater()) {
  305. addClasspath(LangUtil.getJrtFsFilePath(),ret);
  306. } else {
  307. addClasspath(System.getProperty("sun.boot.class.path", ""), ret);
  308. }
  309. } else {
  310. addClasspath(parser.bootclasspath, ret);
  311. }
  312. return ret;
  313. }
  314. public List<String> getModulepath(AjcConfigParser parser) {
  315. List<String> ret = new ArrayList<String>();
  316. addClasspath(parser.modulepath, ret);
  317. return ret;
  318. }
  319. public List<String> getModulesourcepath(AjcConfigParser parser) {
  320. List<String> ret = new ArrayList<String>();
  321. addClasspath(parser.modulesourcepath, ret);
  322. return ret;
  323. }
  324. public ArrayList<FileSystem.Classpath> handleClasspath(ArrayList<String> classpaths, String customEncoding) {
  325. return super.handleClasspath(classpaths, customEncoding);
  326. }
  327. /**
  328. * If the classpath is not set, we use the environment's java.class.path, but remove the aspectjtools.jar entry from that list
  329. * in order to prevent wierd bootstrap issues (refer to bug#39959).
  330. */
  331. public List getClasspath(AjcConfigParser parser) {
  332. List ret = new ArrayList();
  333. // if (parser.bootclasspath == null) {
  334. // addClasspath(System.getProperty("sun.boot.class.path", ""), ret);
  335. // } else {
  336. // addClasspath(parser.bootclasspath, ret);
  337. // }
  338. String extdirs = parser.extdirs;
  339. if (extdirs == null) {
  340. extdirs = System.getProperty("java.ext.dirs", "");
  341. }
  342. addExtDirs(extdirs, ret);
  343. if (parser.classpath == null) {
  344. addClasspath(System.getProperty("java.class.path", ""), ret);
  345. List fixedList = new ArrayList();
  346. for (Iterator it = ret.iterator(); it.hasNext();) {
  347. String entry = (String) it.next();
  348. if (!entry.endsWith("aspectjtools.jar")) {
  349. fixedList.add(entry);
  350. }
  351. }
  352. ret = fixedList;
  353. } else {
  354. addClasspath(parser.classpath, ret);
  355. }
  356. // ??? eclipse seems to put outdir on the classpath
  357. // ??? we're brave and believe we don't need it
  358. return ret;
  359. }
  360. private void addExtDirs(String extdirs, List classpathCollector) {
  361. StringTokenizer tokenizer = new StringTokenizer(extdirs, File.pathSeparator);
  362. while (tokenizer.hasMoreTokens()) {
  363. // classpathCollector.add(tokenizer.nextToken());
  364. File dirFile = new File(tokenizer.nextToken());
  365. if (dirFile.canRead() && dirFile.isDirectory()) {
  366. File[] files = dirFile.listFiles(FileUtil.ZIP_FILTER);
  367. for (int i = 0; i < files.length; i++) {
  368. classpathCollector.add(files[i].getAbsolutePath());
  369. }
  370. } else {
  371. // XXX alert on invalid -extdirs entries
  372. }
  373. }
  374. }
  375. private void addClasspath(String classpath, List<String> classpathCollector) {
  376. if (classpath == null) {
  377. return;
  378. }
  379. StringTokenizer tokenizer = new StringTokenizer(classpath, File.pathSeparator);
  380. while (tokenizer.hasMoreTokens()) {
  381. classpathCollector.add(tokenizer.nextToken());
  382. }
  383. }
  384. private class AjcConfigParser extends ConfigParser {
  385. private String bootclasspath = null;
  386. private String classpath = null;
  387. private String modulepath = null;
  388. private String modulesourcepath = null;
  389. private String extdirs = null;
  390. private List unparsedArgs = new ArrayList();
  391. private AjBuildConfig buildConfig;
  392. private IMessageHandler handler;
  393. private String moduleInfoArgument;
  394. public AjcConfigParser(AjBuildConfig buildConfig, IMessageHandler handler) {
  395. this.buildConfig = buildConfig;
  396. this.handler = handler;
  397. }
  398. public List getUnparsedArgs() {
  399. return unparsedArgs;
  400. }
  401. public String getModuleInfoArgument() {
  402. return this.moduleInfoArgument;
  403. }
  404. /**
  405. * Extract AspectJ-specific options (except for argfiles). Caller should warn when sourceroots is empty but in incremental
  406. * mode. Signals warnings or errors through handler set in constructor.
  407. */
  408. public void parseOption(String arg, LinkedList<Arg> args) { // XXX use ListIterator.remove()
  409. int nextArgIndex = args.indexOf(arg) + 1; // XXX assumes unique
  410. // trim arg?
  411. buildConfig.setXlazyTjp(true); // now default - MINOR could be pushed down and made default at a lower level
  412. if (LangUtil.isEmpty(arg)) {
  413. showWarning("empty arg found");
  414. } else if (arg.endsWith("module-info.java")) {
  415. moduleInfoArgument = arg;
  416. } else if (arg.equals("-inpath")) {
  417. if (args.size() > nextArgIndex) {
  418. // buildConfig.getAjOptions().put(AjCompilerOptions.OPTION_Inpath, CompilerOptions.PRESERVE);
  419. StringTokenizer st = new StringTokenizer(((ConfigParser.Arg) args.get(nextArgIndex)).getValue(),
  420. File.pathSeparator);
  421. boolean inpathChange = false;
  422. while (st.hasMoreTokens()) {
  423. String filename = st.nextToken();
  424. File file = makeFile(filename);
  425. if (FileUtil.isZipFile(file)) {
  426. buildConfig.addToInpath(file);
  427. inpathChange = true;
  428. } else {
  429. if (file.isDirectory()) {
  430. buildConfig.addToInpath(file);
  431. inpathChange = true;
  432. } else {
  433. showWarning("skipping missing, empty or corrupt inpath entry: " + filename);
  434. }
  435. }
  436. }
  437. if (inpathChange) {
  438. buildConfig.processInPath();
  439. }
  440. args.remove(args.get(nextArgIndex));
  441. }
  442. } else if (arg.equals("-injars")) {
  443. if (args.size() > nextArgIndex) {
  444. // buildConfig.getAjOptions().put(AjCompilerOptions.OPTION_InJARs, CompilerOptions.PRESERVE);
  445. StringTokenizer st = new StringTokenizer(((ConfigParser.Arg) args.get(nextArgIndex)).getValue(),
  446. File.pathSeparator);
  447. while (st.hasMoreTokens()) {
  448. String filename = st.nextToken();
  449. File jarFile = makeFile(filename);
  450. if (FileUtil.isZipFile(jarFile)) {
  451. buildConfig.getInJars().add(jarFile);
  452. } else {
  453. File dirFile = makeFile(filename);
  454. if (dirFile.isDirectory()) {
  455. buildConfig.getInJars().add(dirFile);
  456. } else {
  457. showWarning("skipping missing, empty or corrupt injar: " + filename);
  458. }
  459. }
  460. }
  461. args.remove(args.get(nextArgIndex));
  462. }
  463. } else if (arg.equals("-aspectpath")) {
  464. if (args.size() > nextArgIndex) {
  465. StringTokenizer st = new StringTokenizer(((ConfigParser.Arg) args.get(nextArgIndex)).getValue(),
  466. File.pathSeparator);
  467. while (st.hasMoreTokens()) {
  468. String filename = st.nextToken();
  469. File jarFile = makeFile(filename);
  470. if (FileUtil.isZipFile(jarFile) || jarFile.isDirectory()) {
  471. // buildConfig.getAspectpath().add(jarFile);
  472. buildConfig.addToAspectpath(jarFile);
  473. } else {
  474. showWarning("skipping missing, empty or corrupt aspectpath entry: " + filename);
  475. }
  476. }
  477. args.remove(args.get(nextArgIndex));
  478. }
  479. } else if (arg.equals("-makeAjReflectable")) {
  480. buildConfig.setMakeReflectable(true);
  481. } else if (arg.equals("-sourceroots")) {
  482. if (args.size() > nextArgIndex) {
  483. List<File> sourceRoots = new ArrayList<File>();
  484. StringTokenizer st = new StringTokenizer(((ConfigParser.Arg) args.get(nextArgIndex)).getValue(),
  485. File.pathSeparator);
  486. while (st.hasMoreTokens()) {
  487. File f = makeFile(st.nextToken());
  488. if (f.isDirectory() && f.canRead()) {
  489. sourceRoots.add(f);
  490. } else {
  491. showError("bad sourceroot: " + f);
  492. }
  493. }
  494. if (0 < sourceRoots.size()) {
  495. buildConfig.setSourceRoots(sourceRoots);
  496. }
  497. args.remove(args.get(nextArgIndex));
  498. } else {
  499. showError("-sourceroots requires list of directories");
  500. }
  501. } else if (arg.equals("-outjar")) {
  502. if (args.size() > nextArgIndex) {
  503. // buildConfig.getAjOptions().put(AjCompilerOptions.OPTION_OutJAR, CompilerOptions.GENERATE);
  504. File jarFile = makeFile(((ConfigParser.Arg) args.get(nextArgIndex)).getValue());
  505. if (!jarFile.isDirectory()) {
  506. try {
  507. if (!jarFile.exists()) {
  508. jarFile.createNewFile();
  509. }
  510. buildConfig.setOutputJar(jarFile);
  511. } catch (IOException ioe) {
  512. showError("unable to create outjar file: " + jarFile);
  513. }
  514. } else {
  515. showError("invalid -outjar file: " + jarFile);
  516. }
  517. args.remove(args.get(nextArgIndex));
  518. } else {
  519. showError("-outjar requires jar path argument");
  520. }
  521. } else if (arg.equals("-outxml")) {
  522. buildConfig.setOutxmlName(org.aspectj.bridge.Constants.AOP_AJC_XML);
  523. } else if (arg.equals("-outxmlfile")) {
  524. if (args.size() > nextArgIndex) {
  525. String name = ((ConfigParser.Arg) args.get(nextArgIndex)).getValue();
  526. buildConfig.setOutxmlName(name);
  527. args.remove(args.get(nextArgIndex));
  528. } else {
  529. showError("-outxmlfile requires file name argument");
  530. }
  531. } else if (arg.equals("-log")) {
  532. // remove it as it's already been handled in org.aspectj.tools.ajc.Main
  533. args.remove(args.get(nextArgIndex));
  534. } else if (arg.equals("-messageHolder")) {
  535. // remove it as it's already been handled in org.aspectj.tools.ajc.Main
  536. args.remove(args.get(nextArgIndex));
  537. } else if (arg.equals("-incremental")) {
  538. buildConfig.setIncrementalMode(true);
  539. } else if (arg.equals("-XincrementalFile")) {
  540. if (args.size() > nextArgIndex) {
  541. File file = makeFile(((ConfigParser.Arg) args.get(nextArgIndex)).getValue());
  542. buildConfig.setIncrementalFile(file);
  543. if (!file.canRead()) {
  544. showError("bad -XincrementalFile : " + file);
  545. // if not created before recompile test, stop after first compile
  546. }
  547. args.remove(args.get(nextArgIndex));
  548. } else {
  549. showError("-XincrementalFile requires file argument");
  550. }
  551. } else if (arg.equals("-crossrefs")) {
  552. buildConfig.setGenerateCrossRefsMode(true);
  553. buildConfig.setGenerateModelMode(true);
  554. } else if (arg.startsWith("-checkRuntimeVersion:")) {
  555. String lcArg = arg.toLowerCase();
  556. if (lcArg.endsWith(":false")) {
  557. buildConfig.setCheckRuntimeVersion(false);
  558. } else if (lcArg.endsWith(":true")) {
  559. buildConfig.setCheckRuntimeVersion(true);
  560. } else {
  561. showError("bad value for -checkRuntimeVersion option, must be true or false");
  562. }
  563. } else if (arg.equals("-emacssym")) {
  564. buildConfig.setEmacsSymMode(true);
  565. buildConfig.setGenerateModelMode(true);
  566. } else if (arg.equals("-XjavadocsInModel")) {
  567. buildConfig.setGenerateModelMode(true);
  568. buildConfig.setGenerateJavadocsInModelMode(true);
  569. } else if (arg.equals("-Xdev:NoAtAspectJProcessing")) {
  570. buildConfig.setNoAtAspectJAnnotationProcessing(true);
  571. } else if (arg.equals("-XaddSerialVersionUID")) {
  572. buildConfig.setAddSerialVerUID(true);
  573. } else if (arg.equals("-xmlConfigured")) {
  574. buildConfig.setXmlConfigured(true);
  575. } else if (arg.equals("-Xdev:Pinpoint")) {
  576. buildConfig.setXdevPinpointMode(true);
  577. } else if (arg.startsWith("-Xjoinpoints:")) {
  578. buildConfig.setXJoinpoints(arg.substring(13));
  579. } else if (arg.equals("-noWeave") || arg.equals("-XnoWeave")) {
  580. showWarning("the noweave option is no longer required and is being ignored");
  581. } else if (arg.equals("-XterminateAfterCompilation")) {
  582. buildConfig.setTerminateAfterCompilation(true);
  583. } else if (arg.equals("-XserializableAspects")) {
  584. buildConfig.setXserializableAspects(true);
  585. } else if (arg.equals("-XlazyTjp")) {
  586. // do nothing as this is now on by default
  587. showWarning("-XlazyTjp should no longer be used, build tjps lazily is now the default");
  588. } else if (arg.startsWith("-Xreweavable")) {
  589. showWarning("-Xreweavable is on by default");
  590. if (arg.endsWith(":compress")) {
  591. showWarning("-Xreweavable:compress is no longer available - reweavable is now default");
  592. }
  593. } else if (arg.startsWith("-Xset:")) {
  594. buildConfig.setXconfigurationInfo(arg.substring(6));
  595. } else if (arg.startsWith("-aspectj.pushin=")) {
  596. // a little dirty but this should never be used in the IDE
  597. try {
  598. System.setProperty("aspectj.pushin", arg.substring(16));
  599. } catch (Exception e) {
  600. e.printStackTrace();
  601. }
  602. } else if (arg.startsWith("-XnotReweavable")) {
  603. buildConfig.setXnotReweavable(true);
  604. } else if (arg.equals("-XnoInline")) {
  605. buildConfig.setXnoInline(true);
  606. } else if (arg.equals("-XhasMember")) {
  607. buildConfig.setXHasMemberSupport(true);
  608. } else if (arg.startsWith("-showWeaveInfo")) {
  609. buildConfig.setShowWeavingInformation(true);
  610. } else if (arg.equals("-Xlintfile")) {
  611. if (args.size() > nextArgIndex) {
  612. File lintSpecFile = makeFile(((ConfigParser.Arg) args.get(nextArgIndex)).getValue());
  613. // XXX relax restriction on props file suffix?
  614. if (lintSpecFile.canRead() && lintSpecFile.getName().endsWith(".properties")) {
  615. buildConfig.setLintSpecFile(lintSpecFile);
  616. } else {
  617. showError("bad -Xlintfile file: " + lintSpecFile);
  618. buildConfig.setLintSpecFile(null);
  619. }
  620. args.remove(args.get(nextArgIndex));
  621. } else {
  622. showError("-Xlintfile requires .properties file argument");
  623. }
  624. } else if (arg.equals("-Xlint")) {
  625. // buildConfig.getAjOptions().put(
  626. // AjCompilerOptions.OPTION_Xlint,
  627. // CompilerOptions.GENERATE);
  628. buildConfig.setLintMode(AjBuildConfig.AJLINT_DEFAULT);
  629. } else if (arg.startsWith("-Xlint:")) {
  630. if (7 < arg.length()) {
  631. buildConfig.setLintMode(arg.substring(7));
  632. } else {
  633. showError("invalid lint option " + arg);
  634. }
  635. } else if (arg.equals("-bootclasspath")) {
  636. if (args.size() > nextArgIndex) {
  637. String bcpArg = ((ConfigParser.Arg) args.get(nextArgIndex)).getValue();
  638. StringBuffer bcp = new StringBuffer();
  639. StringTokenizer strTok = new StringTokenizer(bcpArg, File.pathSeparator);
  640. while (strTok.hasMoreTokens()) {
  641. bcp.append(makeFile(strTok.nextToken()));
  642. if (strTok.hasMoreTokens()) {
  643. bcp.append(File.pathSeparator);
  644. }
  645. }
  646. bootclasspath = bcp.toString();
  647. args.remove(args.get(nextArgIndex));
  648. } else {
  649. showError("-bootclasspath requires classpath entries");
  650. }
  651. } else if (arg.equals("-classpath") || arg.equals("-cp")) {
  652. if (args.size() > nextArgIndex) {
  653. String cpArg = ((ConfigParser.Arg) args.get(nextArgIndex)).getValue();
  654. StringBuffer cp = new StringBuffer();
  655. StringTokenizer strTok = new StringTokenizer(cpArg, File.pathSeparator);
  656. while (strTok.hasMoreTokens()) {
  657. cp.append(makeFile(strTok.nextToken()));
  658. if (strTok.hasMoreTokens()) {
  659. cp.append(File.pathSeparator);
  660. }
  661. }
  662. classpath = cp.toString();
  663. Arg pathArg = args.get(nextArgIndex);
  664. unparsedArgs.add("-classpath");
  665. unparsedArgs.add(pathArg.getValue());
  666. args.remove(pathArg);
  667. } else {
  668. showError("-classpath requires classpath entries");
  669. }
  670. } else if (arg.equals("--module-path") || arg.equals("-p")) {
  671. if (args.size() > nextArgIndex) {
  672. String mpArg = ((ConfigParser.Arg) args.get(nextArgIndex)).getValue();
  673. modulepath = mpArg;
  674. // StringBuffer mp = new StringBuffer();
  675. // StringTokenizer strTok = new StringTokenizer(mpArg, File.pathSeparator);
  676. // while (strTok.hasMoreTokens()) {
  677. // mp.append(makeFile(strTok.nextToken()));
  678. // if (strTok.hasMoreTokens()) {
  679. // mp.append(File.pathSeparator);
  680. // }
  681. // }
  682. // modulepath = mp.toString();
  683. args.remove(args.get(nextArgIndex));
  684. } else {
  685. showError("--module-path requires modulepath entries");
  686. }
  687. } else if (arg.equals("--module-source-path") || arg.equals("-p")) {
  688. if (args.size() > nextArgIndex) {
  689. String mspArg = ((ConfigParser.Arg) args.get(nextArgIndex)).getValue();
  690. modulesourcepath = mspArg;
  691. // StringBuffer mp = new StringBuffer();
  692. // StringTokenizer strTok = new StringTokenizer(mpArg, File.pathSeparator);
  693. // while (strTok.hasMoreTokens()) {
  694. // mp.append(makeFile(strTok.nextToken()));
  695. // if (strTok.hasMoreTokens()) {
  696. // mp.append(File.pathSeparator);
  697. // }
  698. // }
  699. // modulepath = mp.toString();
  700. args.remove(args.get(nextArgIndex));
  701. } else {
  702. showError("--module-source-path requires modulepath entries");
  703. }
  704. } else if (arg.equals("-extdirs")) {
  705. if (args.size() > nextArgIndex) {
  706. String extdirsArg = ((ConfigParser.Arg) args.get(nextArgIndex)).getValue();
  707. StringBuffer ed = new StringBuffer();
  708. StringTokenizer strTok = new StringTokenizer(extdirsArg, File.pathSeparator);
  709. while (strTok.hasMoreTokens()) {
  710. ed.append(makeFile(strTok.nextToken()));
  711. if (strTok.hasMoreTokens()) {
  712. ed.append(File.pathSeparator);
  713. }
  714. }
  715. extdirs = ed.toString();
  716. args.remove(args.get(nextArgIndex));
  717. } else {
  718. showError("-extdirs requires list of external directories");
  719. }
  720. // error on directory unless -d, -{boot}classpath, or -extdirs
  721. } else if (arg.equals("-d")) {
  722. dirLookahead(arg, args, nextArgIndex);
  723. // } else if (arg.equals("-classpath")) {
  724. // dirLookahead(arg, args, nextArgIndex);
  725. // } else if (arg.equals("-bootclasspath")) {
  726. // dirLookahead(arg, args, nextArgIndex);
  727. // } else if (arg.equals("-extdirs")) {
  728. // dirLookahead(arg, args, nextArgIndex);
  729. } else if (arg.equals("-proceedOnError")) {
  730. buildConfig.setProceedOnError(true);
  731. } else if (arg.equals("-processorpath")) { // -processorpath <directories and ZIP archives separated by pathseporator
  732. addPairToUnparsed(args, arg, nextArgIndex, "-processorpath requires list of external directories or zip archives");
  733. } else if (arg.equals("-processor")) { // -processor <class1[,class2,...]>
  734. addPairToUnparsed(args, arg, nextArgIndex, "-processor requires list of processors' classes");
  735. } else if (arg.equals("-s")) { // -s <dir> destination directory for generated source files
  736. addPairToUnparsed(args, arg, nextArgIndex, "-s requires directory");
  737. } else if (arg.equals("-classNames")) { // -classNames <className1[,className2,...]>
  738. addPairToUnparsed(args, arg, nextArgIndex, "-classNames requires list of classes");
  739. } else if (new File(arg).isDirectory()) {
  740. showError("dir arg not permitted: " + arg);
  741. } else if (arg.startsWith("-Xajruntimetarget")) {
  742. if (arg.endsWith(":1.2")) {
  743. buildConfig.setTargetAspectjRuntimeLevel(Constants.RUNTIME_LEVEL_12);
  744. } else if (arg.endsWith(":1.5")) {
  745. buildConfig.setTargetAspectjRuntimeLevel(Constants.RUNTIME_LEVEL_15);
  746. } else {
  747. showError("-Xajruntimetarget:<level> only supports a target level of 1.2 or 1.5");
  748. }
  749. } else if (arg.equals("-timers")) {
  750. buildConfig.setTiming(true);
  751. // swallow - it is dealt with in Main.runMain()
  752. } else if (arg.equals("-1.5")) {
  753. buildConfig.setBehaveInJava5Way(true);
  754. unparsedArgs.add("-1.5");
  755. // this would enable the '-source 1.5' to do the same as '-1.5' but doesnt sound quite right as
  756. // as an option right now as it doesnt mean we support 1.5 source code - people will get confused...
  757. } else if (arg.equals("-1.6")) {
  758. buildConfig.setBehaveInJava5Way(true);
  759. unparsedArgs.add("-1.6");
  760. } else if (arg.equals("-1.7")) {
  761. buildConfig.setBehaveInJava5Way(true);
  762. unparsedArgs.add("-1.7");
  763. } else if (arg.equals("-1.8")) {
  764. buildConfig.setBehaveInJava5Way(true);
  765. unparsedArgs.add("-1.8");
  766. } else if (arg.equals("-1.9")) {
  767. buildConfig.setBehaveInJava5Way(true);
  768. unparsedArgs.add("-1.9");
  769. } else if (arg.equals("-source")) {
  770. if (args.size() > nextArgIndex) {
  771. String level = ((ConfigParser.Arg) args.get(nextArgIndex)).getValue();
  772. if (level.equals("1.5") || level.equals("5") || level.equals("1.6") || level.equals("6") || level.equals("1.7")
  773. || level.equals("7") || level.equals("8") || level.equals("1.8") || level.equals("9") || level.equals("1.9")) {
  774. buildConfig.setBehaveInJava5Way(true);
  775. }
  776. unparsedArgs.add("-source");
  777. unparsedArgs.add(level);
  778. args.remove(args.get(nextArgIndex));
  779. }
  780. } else {
  781. // argfile, @file parsed by superclass
  782. // no eclipse options parsed:
  783. // -d args, -help (handled),
  784. // -classpath, -target, -1.3, -1.4, -source [1.3|1.4]
  785. // -nowarn, -warn:[...], -deprecation, -noImportError,
  786. // -g:[...], -preserveAllLocals,
  787. // -referenceInfo, -encoding, -verbose, -log, -time
  788. // -noExit, -repeat
  789. // (Actually, -noExit grabbed by Main)
  790. unparsedArgs.add(arg);
  791. }
  792. }
  793. protected void dirLookahead(String arg, LinkedList argList, int nextArgIndex) {
  794. unparsedArgs.add(arg);
  795. ConfigParser.Arg next = (ConfigParser.Arg) argList.get(nextArgIndex);
  796. String value = next.getValue();
  797. if (!LangUtil.isEmpty(value)) {
  798. if (new File(value).isDirectory()) {
  799. unparsedArgs.add(value);
  800. argList.remove(next);
  801. return;
  802. }
  803. }
  804. }
  805. public void showError(String message) {
  806. ISourceLocation location = null;
  807. if (buildConfig.getConfigFile() != null) {
  808. location = new SourceLocation(buildConfig.getConfigFile(), 0);
  809. }
  810. IMessage errorMessage = new Message(CONFIG_MSG + message, IMessage.ERROR, null, location);
  811. handler.handleMessage(errorMessage);
  812. // MessageUtil.error(handler, CONFIG_MSG + message);
  813. }
  814. protected void showWarning(String message) {
  815. ISourceLocation location = null;
  816. if (buildConfig.getConfigFile() != null) {
  817. location = new SourceLocation(buildConfig.getConfigFile(), 0);
  818. }
  819. IMessage errorMessage = new Message(CONFIG_MSG + message, IMessage.WARNING, null, location);
  820. handler.handleMessage(errorMessage);
  821. // MessageUtil.warn(handler, message);
  822. }
  823. protected File makeFile(File dir, String name) {
  824. name = name.replace('/', File.separatorChar);
  825. File ret = new File(name);
  826. if (dir == null || ret.isAbsolute()) {
  827. return ret;
  828. }
  829. try {
  830. dir = dir.getCanonicalFile();
  831. } catch (IOException ioe) {
  832. }
  833. return new File(dir, name);
  834. }
  835. private void addPairToUnparsed(LinkedList<Arg> args, String arg, int nextArgIndex, String errorMessage) {
  836. if (args.size() <= nextArgIndex) {
  837. showError(errorMessage);
  838. return;
  839. }
  840. final Arg nextArg = args.get(nextArgIndex);
  841. args.remove(nextArg);
  842. unparsedArgs.add(arg);
  843. unparsedArgs.add(nextArg.getValue());
  844. }
  845. private int indexOf(LinkedList<Arg> args, String arg) {
  846. int index = 0;
  847. for (Arg argument : args) {
  848. if (arg.equals(argument.getValue())) {
  849. return index;
  850. }
  851. index++;
  852. }
  853. return -1;
  854. }
  855. }
  856. }