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.

CompilerAdapter.java 25KB

21 years ago
21 years ago
21 years ago
21 years ago
21 years ago
21 years ago
21 years ago
21 years ago
21 years ago
21 years ago
21 years ago
21 years ago
21 years ago
20 years ago
20 years ago
21 years ago
21 years ago
21 years ago
21 years ago
21 years ago
21 years ago
21 years ago
21 years ago
21 years ago
21 years ago
21 years ago
21 years ago
21 years ago
21 years ago
21 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657
  1. /* *******************************************************************
  2. * Copyright (c) 2002 Palo Alto Research Center, Incorporated (PARC),
  3. * 2003 Contributors.
  4. * All rights reserved.
  5. * This program and the accompanying materials are made available
  6. * under the terms of the Eclipse Public License v1.0
  7. * which accompanies this distribution and is available at
  8. * http://www.eclipse.org/legal/epl-v10.html
  9. *
  10. * Contributors:
  11. * Xerox/PARC initial implementation
  12. * AMC 01.20.2003 extended to support new AspectJ 1.1 options,
  13. * bugzilla #29769
  14. * ******************************************************************/
  15. package org.aspectj.ajde.internal;
  16. import java.io.File;
  17. import java.util.*;
  18. import org.aspectj.ajde.*;
  19. import org.aspectj.ajdt.ajc.*;
  20. import org.aspectj.ajdt.internal.core.builder.*;
  21. import org.aspectj.bridge.*;
  22. import org.aspectj.bridge.context.CompilationAndWeavingContext;
  23. import org.aspectj.util.LangUtil;
  24. //import org.eclipse.core.runtime.OperationCanceledException;
  25. import org.aspectj.org.eclipse.jdt.internal.compiler.impl.CompilerOptions;
  26. public class CompilerAdapter {
  27. private static final Set DEFAULT__AJDE_WARNINGS;
  28. static {
  29. DEFAULT__AJDE_WARNINGS = new HashSet();
  30. DEFAULT__AJDE_WARNINGS.add(BuildOptionsAdapter.WARN_ASSERT_IDENITIFIER);
  31. DEFAULT__AJDE_WARNINGS.add(BuildOptionsAdapter.WARN_CONSTRUCTOR_NAME);
  32. DEFAULT__AJDE_WARNINGS.add(BuildOptionsAdapter.WARN_DEPRECATION);
  33. DEFAULT__AJDE_WARNINGS.add(BuildOptionsAdapter.WARN_MASKED_CATCH_BLOCKS);
  34. DEFAULT__AJDE_WARNINGS.add(BuildOptionsAdapter.WARN_PACKAGE_DEFAULT_METHOD);
  35. DEFAULT__AJDE_WARNINGS.add(BuildOptionsAdapter.WARN_UNUSED_IMPORTS);
  36. // DEFAULT__AJDE_WARNINGS.put(BuildOptionsAdapter.WARN_);
  37. // DEFAULT__AJDE_WARNINGS.put(BuildOptionsAdapter.WARN_);
  38. }
  39. // private Map optionsMap;
  40. private AjBuildManager buildManager = null;
  41. private IMessageHandler messageHandler = null;
  42. private BuildNotifierAdapter currNotifier = null;
  43. private boolean initialized = false;
  44. private boolean structureDirty = true;
  45. private boolean showInfoMessages = false;
  46. // set to false in incremental mode to re-do initial build
  47. private boolean nextBuild = false;
  48. public CompilerAdapter() {
  49. super();
  50. }
  51. public void showInfoMessages(boolean show) { // XXX surface in GUI
  52. showInfoMessages = show;
  53. }
  54. public boolean getShowInfoMessages() {
  55. return showInfoMessages;
  56. }
  57. public void nextBuildFresh() {
  58. if (nextBuild) {
  59. nextBuild = false;
  60. }
  61. }
  62. public void requestCompileExit() {
  63. if (currNotifier != null) {
  64. currNotifier.cancelBuild();
  65. } else {
  66. signalText("unable to cancel build process");
  67. }
  68. }
  69. public boolean isStructureDirty() {
  70. return structureDirty;
  71. }
  72. public void setStructureDirty(boolean structureDirty) {
  73. this.structureDirty = structureDirty;
  74. }
  75. public boolean compile(String configFile, BuildProgressMonitor progressMonitor, boolean buildModel) {
  76. if (configFile == null) {
  77. Ajde.getDefault().getErrorHandler().handleError("Tried to build null config file.");
  78. }
  79. init();
  80. try {
  81. CompilationAndWeavingContext.reset();
  82. AjBuildConfig buildConfig = genBuildConfig(configFile);
  83. if (buildConfig == null) {
  84. return false;
  85. }
  86. buildConfig.setGenerateModelMode(buildModel);
  87. currNotifier = new BuildNotifierAdapter(progressMonitor, buildManager);
  88. buildManager.setProgressListener(currNotifier);
  89. String rtInfo = buildManager.checkRtJar(buildConfig); // !!! will get called twice
  90. if (rtInfo != null) {
  91. Ajde.getDefault().getErrorHandler().handleWarning(
  92. "AspectJ Runtime error: " + rtInfo
  93. + " Please place a valid aspectjrt.jar on the classpath.");
  94. return false;
  95. }
  96. boolean incrementalEnabled =
  97. buildConfig.isIncrementalMode()
  98. || buildConfig.isIncrementalFileMode();
  99. boolean successfulBuild;
  100. if (incrementalEnabled && nextBuild) {
  101. successfulBuild = buildManager.incrementalBuild(buildConfig, messageHandler);
  102. } else {
  103. if (incrementalEnabled) {
  104. nextBuild = incrementalEnabled;
  105. }
  106. successfulBuild = buildManager.batchBuild(buildConfig, messageHandler);
  107. }
  108. IncrementalStateManager.recordSuccessfulBuild(configFile,buildManager.getState());
  109. return successfulBuild;
  110. // } catch (OperationCanceledException ce) {
  111. // Ajde.getDefault().getErrorHandler().handleWarning(
  112. // "build cancelled by user");
  113. // return false;
  114. } catch (AbortException e) {
  115. final IMessage message = e.getIMessage();
  116. if (message == null) {
  117. signalThrown(e);
  118. } else {
  119. String messageText = message.getMessage() + "\n" + CompilationAndWeavingContext.getCurrentContext();
  120. Ajde.getDefault().getErrorHandler().handleError(messageText, message.getThrown());
  121. }
  122. return false;
  123. } catch (Throwable t) {
  124. signalThrown(t);
  125. return false;
  126. }
  127. }
  128. /**
  129. * Generate AjBuildConfig from the local configFile parameter
  130. * plus global project and build options.
  131. * Errors signalled using signal... methods.
  132. * @param configFile
  133. * @return null if invalid configuration,
  134. * corresponding AjBuildConfig otherwise
  135. */
  136. public AjBuildConfig genBuildConfig(String configFilePath) {
  137. init();
  138. File configFile = new File(configFilePath);
  139. if (!configFile.exists()) {
  140. Ajde.getDefault().getErrorHandler().handleError(
  141. "Config file \"" + configFile + "\" does not exist."
  142. );
  143. return null;
  144. }
  145. String[] args = new String[] { "@" + configFile.getAbsolutePath() };
  146. CountingMessageHandler handler
  147. = CountingMessageHandler.makeCountingMessageHandler(messageHandler);
  148. BuildArgParser parser = new BuildArgParser(handler);
  149. AjBuildConfig config = new AjBuildConfig();
  150. parser.populateBuildConfig(config, args, false, configFile);
  151. configureBuildOptions(config,Ajde.getDefault().getBuildManager().getBuildOptions(),handler);
  152. configureProjectOptions(config, Ajde.getDefault().getProjectProperties()); // !!! not what the API intended
  153. // // -- get globals, treat as defaults used if no local values
  154. // AjBuildConfig global = new AjBuildConfig();
  155. // // AMC refactored into two methods to populate buildConfig from buildOptions and
  156. // // project properties - bugzilla 29769.
  157. // BuildOptionsAdapter buildOptions
  158. // = Ajde.getDefault().getBuildManager().getBuildOptions();
  159. // if (!configureBuildOptions(/* global */ config, buildOptions, handler)) {
  160. // return null;
  161. // }
  162. // ProjectPropertiesAdapter projectOptions =
  163. // Ajde.getDefault().getProjectProperties();
  164. // configureProjectOptions(global, projectOptions);
  165. // config.installGlobals(global);
  166. ISourceLocation location = null;
  167. if (config.getConfigFile() != null) {
  168. location = new SourceLocation(config.getConfigFile(), 0);
  169. }
  170. String message = parser.getOtherMessages(true);
  171. if (null != message) {
  172. IMessage m = new Message(message, IMessage.ERROR, null, location);
  173. handler.handleMessage(m);
  174. }
  175. // always force model generation in AJDE
  176. config.setGenerateModelMode(true);
  177. if (Ajde.getDefault().getBuildManager().getBuildOptions().getJavaOptionsMap() != null) {
  178. config.getOptions().set(Ajde.getDefault().getBuildManager().getBuildOptions().getJavaOptionsMap());
  179. }
  180. return config;
  181. // return fixupBuildConfig(config);
  182. }
  183. // /**
  184. // * Fix up build configuration just before using to compile.
  185. // * This should be delegated to a BuildAdapter callback (XXX)
  186. // * for implementation-specific value checks
  187. // * (e.g., to force use of project classpath rather
  188. // * than local config classpath).
  189. // * This implementation does no checks and returns local.
  190. // * @param local the AjBuildConfig generated to validate
  191. // * @param global
  192. // * @param buildOptions
  193. // * @param projectOptions
  194. // * @return null if unable to fix problems or fixed AjBuildConfig if no errors
  195. // *
  196. // */
  197. // protected AjBuildConfig fixupBuildConfig(AjBuildConfig local) {
  198. // if (Ajde.getDefault().getBuildManager().getBuildOptions().getJavaOptionsMap() != null) {
  199. // local.getJavaOptions().putAll(Ajde.getDefault().getBuildManager().getBuildOptions().getJavaOptionsMap());
  200. // }
  201. // return local;
  202. // }
  203. // /** signal error text to user */
  204. // protected void signalError(String text) {
  205. // }
  206. // /** signal warning text to user */
  207. // protected void signalWarning(String text) {
  208. //
  209. // }
  210. /** signal text to user */
  211. protected void signalText(String text) {
  212. Ajde.getDefault().getIdeUIAdapter().displayStatusInformation(text);
  213. }
  214. /** signal Throwable to user (summary in GUI, trace to stdout). */
  215. protected void signalThrown(Throwable t) { // nothing to error handler?
  216. String text = LangUtil.unqualifiedClassName(t)
  217. + " thrown: "
  218. + t.getMessage();
  219. Ajde.getDefault().getErrorHandler().handleError(text, t);
  220. }
  221. /**
  222. * Populate options in a build configuration, using the Ajde BuildOptionsAdapter.
  223. * Added by AMC 01.20.2003, bugzilla #29769
  224. */
  225. private static boolean configureBuildOptions( AjBuildConfig config, BuildOptionsAdapter options, IMessageHandler handler) {
  226. LangUtil.throwIaxIfNull(options, "options");
  227. LangUtil.throwIaxIfNull(config, "config");
  228. Map optionsToSet = new HashMap();
  229. LangUtil.throwIaxIfNull(optionsToSet, "javaOptions");
  230. if (options.getSourceCompatibilityLevel() != null && options.getSourceCompatibilityLevel().equals(CompilerOptions.VERSION_1_5)) {
  231. optionsToSet.put(CompilerOptions.OPTION_Compliance, CompilerOptions.VERSION_1_5);
  232. optionsToSet.put(CompilerOptions.OPTION_Source, CompilerOptions.VERSION_1_5);
  233. } else if (options.getSourceCompatibilityLevel() != null && options.getSourceCompatibilityLevel().equals(CompilerOptions.VERSION_1_4)) {
  234. optionsToSet.put(CompilerOptions.OPTION_Compliance, CompilerOptions.VERSION_1_4);
  235. optionsToSet.put(CompilerOptions.OPTION_Source, CompilerOptions.VERSION_1_4);
  236. }
  237. String enc = options.getCharacterEncoding();
  238. if (!LangUtil.isEmpty(enc)) {
  239. optionsToSet.put(CompilerOptions.OPTION_Encoding, enc );
  240. }
  241. String compliance = options.getComplianceLevel();
  242. if (!LangUtil.isEmpty(compliance)) {
  243. String version = CompilerOptions.VERSION_1_4;
  244. if ( compliance.equals( BuildOptionsAdapter.VERSION_13 ) ) {
  245. version = CompilerOptions.VERSION_1_3;
  246. }
  247. optionsToSet.put(CompilerOptions.OPTION_Compliance, version );
  248. optionsToSet.put(CompilerOptions.OPTION_Source, version );
  249. }
  250. String sourceLevel = options.getSourceCompatibilityLevel();
  251. if (!LangUtil.isEmpty(sourceLevel)) {
  252. String slVersion = CompilerOptions.VERSION_1_4;
  253. if ( sourceLevel.equals( BuildOptionsAdapter.VERSION_13 ) ) {
  254. slVersion = CompilerOptions.VERSION_1_3;
  255. }
  256. // never set a lower source level than compliance level
  257. // Mik: prepended with 1.5 check
  258. if (sourceLevel.equals(CompilerOptions.VERSION_1_5)) {
  259. optionsToSet.put(CompilerOptions.OPTION_Source, CompilerOptions.VERSION_1_5);
  260. } else {
  261. if (optionsToSet.containsKey(CompilerOptions.OPTION_Compliance)) {
  262. String setCompliance = (String) optionsToSet.get(CompilerOptions.OPTION_Compliance);
  263. if ( ! (setCompliance.equals(CompilerOptions.VERSION_1_4 )
  264. && slVersion.equals(CompilerOptions.VERSION_1_3)) ) {
  265. optionsToSet.put(CompilerOptions.OPTION_Source, slVersion);
  266. }
  267. }
  268. }
  269. }
  270. Set warnings = options.getWarnings();
  271. if (!LangUtil.isEmpty(warnings)) {
  272. // turn off all warnings
  273. disableWarnings( optionsToSet );
  274. // then selectively enable those in the set
  275. enableWarnings( optionsToSet, warnings );
  276. } else if (warnings == null) {
  277. // set default warnings on...
  278. enableWarnings( optionsToSet, DEFAULT__AJDE_WARNINGS);
  279. }
  280. Set debugOptions = options.getDebugLevel();
  281. if (!LangUtil.isEmpty(debugOptions)) {
  282. // default is all options on, so just need to selectively
  283. // disable
  284. boolean sourceLine = false;
  285. boolean varAttr = false;
  286. boolean lineNo = false;
  287. Iterator it = debugOptions.iterator();
  288. while (it.hasNext()){
  289. String debug = (String) it.next();
  290. if ( debug.equals( BuildOptionsAdapter.DEBUG_ALL )) {
  291. sourceLine = true;
  292. varAttr = true;
  293. lineNo = true;
  294. } else if ( debug.equals( BuildOptionsAdapter.DEBUG_LINES )) {
  295. lineNo = true;
  296. } else if ( debug.equals( BuildOptionsAdapter.DEBUG_SOURCE )) {
  297. sourceLine = true;
  298. } else if ( debug.equals( BuildOptionsAdapter.DEBUG_VARS)) {
  299. varAttr = true;
  300. }
  301. }
  302. if (sourceLine) optionsToSet.put(CompilerOptions.OPTION_SourceFileAttribute,
  303. CompilerOptions.GENERATE);
  304. if (varAttr) optionsToSet.put(CompilerOptions.OPTION_LocalVariableAttribute,
  305. CompilerOptions.GENERATE);
  306. if (lineNo) optionsToSet.put(CompilerOptions.OPTION_LineNumberAttribute,
  307. CompilerOptions.GENERATE);
  308. }
  309. //XXX we can't turn off import errors in 3.0 stream
  310. // if ( options.getNoImportError() ) {
  311. // javaOptions.put( CompilerOptions.OPTION_ReportInvalidImport,
  312. // CompilerOptions.WARNING);
  313. // }
  314. if ( options.getPreserveAllLocals() ) {
  315. optionsToSet.put( CompilerOptions.OPTION_PreserveUnusedLocal,
  316. CompilerOptions.PRESERVE);
  317. }
  318. if ( !config.isIncrementalMode()
  319. && options.getIncrementalMode() ) {
  320. config.setIncrementalMode(true);
  321. }
  322. Map jom = options.getJavaOptionsMap();
  323. if (jom!=null) {
  324. String version = (String)jom.get(CompilerOptions.OPTION_Compliance);
  325. if (version!=null && version.equals(CompilerOptions.VERSION_1_5)) {
  326. config.setBehaveInJava5Way(true);
  327. }
  328. }
  329. config.getOptions().set(optionsToSet);
  330. String toAdd = options.getNonStandardOptions();
  331. return LangUtil.isEmpty(toAdd)
  332. ? true
  333. : configureNonStandardOptions( config, toAdd, handler );
  334. // ignored: lenient, porting, preprocess, strict, usejavac, workingdir
  335. }
  336. /**
  337. * Helper method for configureBuildOptions
  338. */
  339. private static void disableWarnings( Map options ) {
  340. options.put(
  341. CompilerOptions.OPTION_ReportOverridingPackageDefaultMethod,
  342. CompilerOptions.IGNORE);
  343. options.put(
  344. CompilerOptions.OPTION_ReportMethodWithConstructorName,
  345. CompilerOptions.IGNORE);
  346. options.put(
  347. CompilerOptions.OPTION_ReportDeprecation,
  348. CompilerOptions.IGNORE);
  349. options.put(
  350. CompilerOptions.OPTION_ReportHiddenCatchBlock,
  351. CompilerOptions.IGNORE);
  352. options.put(
  353. CompilerOptions.OPTION_ReportUnusedLocal,
  354. CompilerOptions.IGNORE);
  355. options.put(
  356. CompilerOptions.OPTION_ReportUnusedParameter,
  357. CompilerOptions.IGNORE);
  358. options.put(
  359. CompilerOptions.OPTION_ReportSyntheticAccessEmulation,
  360. CompilerOptions.IGNORE);
  361. options.put(
  362. CompilerOptions.OPTION_ReportNonExternalizedStringLiteral,
  363. CompilerOptions.IGNORE);
  364. options.put(
  365. CompilerOptions.OPTION_ReportAssertIdentifier,
  366. CompilerOptions.IGNORE);
  367. options.put(
  368. CompilerOptions.OPTION_ReportUnusedImport,
  369. CompilerOptions.IGNORE);
  370. }
  371. /**
  372. * Helper method for configureBuildOptions
  373. */
  374. private static void enableWarnings( Map options, Set warnings ) {
  375. Iterator it = warnings.iterator();
  376. while (it.hasNext() ) {
  377. String thisWarning = (String) it.next();
  378. if ( thisWarning.equals( BuildOptionsAdapter.WARN_ASSERT_IDENITIFIER )) {
  379. options.put( CompilerOptions.OPTION_ReportAssertIdentifier,
  380. CompilerOptions.WARNING );
  381. } else if ( thisWarning.equals( BuildOptionsAdapter.WARN_CONSTRUCTOR_NAME )) {
  382. options.put( CompilerOptions.OPTION_ReportMethodWithConstructorName,
  383. CompilerOptions.WARNING );
  384. } else if ( thisWarning.equals( BuildOptionsAdapter.WARN_DEPRECATION )) {
  385. options.put( CompilerOptions.OPTION_ReportDeprecation,
  386. CompilerOptions.WARNING );
  387. } else if ( thisWarning.equals( BuildOptionsAdapter.WARN_MASKED_CATCH_BLOCKS )) {
  388. options.put( CompilerOptions.OPTION_ReportHiddenCatchBlock,
  389. CompilerOptions.WARNING );
  390. } else if ( thisWarning.equals( BuildOptionsAdapter.WARN_PACKAGE_DEFAULT_METHOD )) {
  391. options.put( CompilerOptions.OPTION_ReportOverridingPackageDefaultMethod,
  392. CompilerOptions.WARNING );
  393. } else if ( thisWarning.equals( BuildOptionsAdapter.WARN_SYNTHETIC_ACCESS )) {
  394. options.put( CompilerOptions.OPTION_ReportSyntheticAccessEmulation,
  395. CompilerOptions.WARNING );
  396. } else if ( thisWarning.equals( BuildOptionsAdapter.WARN_UNUSED_ARGUMENTS )) {
  397. options.put( CompilerOptions.OPTION_ReportUnusedParameter,
  398. CompilerOptions.WARNING );
  399. } else if ( thisWarning.equals( BuildOptionsAdapter.WARN_UNUSED_IMPORTS )) {
  400. options.put( CompilerOptions.OPTION_ReportUnusedImport,
  401. CompilerOptions.WARNING );
  402. } else if ( thisWarning.equals( BuildOptionsAdapter.WARN_UNUSED_LOCALS )) {
  403. options.put( CompilerOptions.OPTION_ReportUnusedLocal,
  404. CompilerOptions.WARNING );
  405. } else if ( thisWarning.equals( BuildOptionsAdapter.WARN_NLS )) {
  406. options.put( CompilerOptions.OPTION_ReportNonExternalizedStringLiteral,
  407. CompilerOptions.WARNING );
  408. }
  409. }
  410. }
  411. /** Local helper method for splitting option strings */
  412. private static List tokenizeString(String str) {
  413. List tokens = new ArrayList();
  414. StringTokenizer tok = new StringTokenizer(str);
  415. while ( tok.hasMoreTokens() ) {
  416. tokens.add(tok.nextToken());
  417. }
  418. return tokens;
  419. }
  420. /**
  421. * Helper method for configure build options.
  422. * This reads all command-line options specified
  423. * in the non-standard options text entry and
  424. * sets any corresponding unset values in config.
  425. * @return false if config failed
  426. */
  427. private static boolean configureNonStandardOptions(
  428. AjBuildConfig config,
  429. String nonStdOptions,
  430. IMessageHandler messageHandler ) {
  431. if (LangUtil.isEmpty(nonStdOptions)) {
  432. return true;
  433. }
  434. // Break a string into a string array of non-standard options.
  435. // Allows for one option to include a ' '. i.e. assuming it has been quoted, it
  436. // won't accidentally get treated as a pair of options (can be needed for xlint props file option)
  437. List tokens = new ArrayList();
  438. int ind = nonStdOptions.indexOf('\"');
  439. int ind2 = nonStdOptions.indexOf('\"',ind+1);
  440. if ((ind > -1) && (ind2 > -1)) { // dont tokenize within double quotes
  441. String pre = nonStdOptions.substring(0,ind);
  442. String quoted = nonStdOptions.substring(ind+1,ind2);
  443. String post = nonStdOptions.substring(ind2+1,nonStdOptions.length());
  444. tokens.addAll(tokenizeString(pre));
  445. tokens.add(quoted);
  446. tokens.addAll(tokenizeString(post));
  447. } else {
  448. tokens.addAll(tokenizeString(nonStdOptions));
  449. }
  450. String[] args = (String[])tokens.toArray(new String[]{});
  451. // set the non-standard options in an alternate build config
  452. // (we don't want to lose the settings we already have)
  453. CountingMessageHandler counter
  454. = CountingMessageHandler.makeCountingMessageHandler(messageHandler);
  455. AjBuildConfig altConfig = AjdtCommand.genBuildConfig(args, counter);
  456. if (counter.hasErrors()) {
  457. return false;
  458. }
  459. // copy globals where local is not set
  460. config.installGlobals(altConfig);
  461. return true;
  462. }
  463. /**
  464. * Add new options from the ProjectPropertiesAdapter to the configuration.
  465. * <ul>
  466. * <li>New list entries are added if not duplicates in,
  467. * for classpath, aspectpath, injars, inpath and sourceroots</li>
  468. * <li>Set only one new entry for output dir or output jar
  469. * only if there is no output dir/jar entry in the config</li>
  470. * </ul>
  471. * Subsequent changes to the ProjectPropertiesAdapter will not affect
  472. * the configuration.
  473. * <p>Added by AMC 01.20.2003, bugzilla #29769
  474. */
  475. private void configureProjectOptions( AjBuildConfig config, ProjectPropertiesAdapter properties ) {
  476. // XXX no error handling in copying project properties
  477. // Handle regular classpath
  478. String propcp = properties.getClasspath();
  479. if (!LangUtil.isEmpty(propcp)) {
  480. StringTokenizer st = new StringTokenizer(propcp, File.pathSeparator);
  481. List configClasspath = config.getClasspath();
  482. ArrayList toAdd = new ArrayList();
  483. while (st.hasMoreTokens()) {
  484. String entry = st.nextToken();
  485. if (!configClasspath.contains(entry)) {
  486. toAdd.add(entry);
  487. }
  488. }
  489. if (0 < toAdd.size()) {
  490. ArrayList both = new ArrayList(configClasspath.size() + toAdd.size());
  491. both.addAll(configClasspath);
  492. both.addAll(toAdd);
  493. config.setClasspath(both);
  494. Ajde.getDefault().logEvent("building with classpath: " + both);
  495. }
  496. }
  497. // Handle boot classpath
  498. propcp = properties.getBootClasspath();
  499. if (!LangUtil.isEmpty(propcp)) {
  500. StringTokenizer st = new StringTokenizer(propcp, File.pathSeparator);
  501. List configClasspath = config.getBootclasspath();
  502. ArrayList toAdd = new ArrayList();
  503. while (st.hasMoreTokens()) {
  504. String entry = st.nextToken();
  505. if (!configClasspath.contains(entry)) {
  506. toAdd.add(entry);
  507. }
  508. }
  509. if (0 < toAdd.size()) {
  510. ArrayList both = new ArrayList(configClasspath.size() + toAdd.size());
  511. both.addAll(configClasspath);
  512. both.addAll(toAdd);
  513. config.setBootclasspath(both);
  514. Ajde.getDefault().logEvent("building with boot classpath: " + both);
  515. }
  516. }
  517. // set outputdir and outputjar only if both not set
  518. if ((null == config.getOutputDir() && (null == config.getOutputJar()))) {
  519. String outPath = properties.getOutputPath();
  520. if (!LangUtil.isEmpty(outPath)) {
  521. config.setOutputDir(new File(outPath));
  522. }
  523. String outJar = properties.getOutJar();
  524. if (!LangUtil.isEmpty(outJar)) {
  525. config.setOutputJar(new File( outJar ) );
  526. }
  527. }
  528. // set compilation result destination manager if not set
  529. OutputLocationManager outputLocationManager = properties.getOutputLocationManager();
  530. if (config.getCompilationResultDestinationManager() == null &&
  531. outputLocationManager != null) {
  532. config.setCompilationResultDestinationManager(new OutputLocationAdapter(outputLocationManager));
  533. }
  534. join(config.getSourceRoots(), properties.getSourceRoots());
  535. join(config.getInJars(), properties.getInJars());
  536. join(config.getInpath(),properties.getInpath());
  537. config.setSourcePathResources(properties.getSourcePathResources());
  538. join(config.getAspectpath(), properties.getAspectPath());
  539. }
  540. void join(Collection target, Collection source) { // XXX dup Util
  541. if ((null == target) || (null == source)) {
  542. return;
  543. }
  544. for (Iterator iter = source.iterator(); iter.hasNext();) {
  545. Object next = iter.next();
  546. if (! target.contains(next)) {
  547. target.add(next);
  548. }
  549. }
  550. }
  551. private void init() {
  552. if (!initialized) { // XXX plug into AJDE initialization
  553. // Ajde.getDefault().setErrorHandler(new DebugErrorHandler());
  554. if (Ajde.getDefault().getMessageHandler() != null) {
  555. this.messageHandler = Ajde.getDefault().getMessageHandler();
  556. } else {
  557. this.messageHandler = new MessageHandlerAdapter();
  558. }
  559. buildManager = new AjBuildManager(messageHandler);
  560. buildManager.environmentSupportsIncrementalCompilation(true);
  561. // XXX need to remove the properties file each time!
  562. initialized = true;
  563. }
  564. }
  565. class MessageHandlerAdapter extends MessageHandler {
  566. private TaskListManager taskListManager;
  567. public MessageHandlerAdapter() {
  568. this.taskListManager = Ajde.getDefault().getTaskListManager();
  569. }
  570. public boolean handleMessage(IMessage message) throws AbortException {
  571. IMessage.Kind kind = message.getKind();
  572. if (kind == IMessage.ABORT) return handleAbort(message);
  573. if (isIgnoring(kind)
  574. || (!showInfoMessages && IMessage.INFO.equals(kind))) {
  575. return true;
  576. }
  577. taskListManager.addSourcelineTask(message);
  578. return super.handleMessage(message); // also store...
  579. }
  580. private boolean handleAbort(IMessage abortMessage) {
  581. throw new AbortException(abortMessage);
  582. }
  583. }
  584. public void setState(AjState buildState) {
  585. buildManager.setState(buildState);
  586. buildManager.setStructureModel(buildState.getStructureModel());
  587. }
  588. public IMessageHandler getMessageHandler() {
  589. return messageHandler;
  590. }
  591. public boolean wasFullBuild() {
  592. return buildManager.wasFullBuild();
  593. }
  594. }