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

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