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 33KB

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