Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

AjdeCoreBuildManager.java 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  1. /********************************************************************
  2. * Copyright (c) 2007 Contributors. All rights reserved.
  3. * This program and the accompanying materials are made available
  4. * under the terms of the Eclipse Public License v1.0
  5. * which accompanies this distribution and is available at
  6. * http://eclipse.org/legal/epl-v10.html
  7. *
  8. * Contributors: IBM Corporation - initial API and implementation
  9. * Helen Hawkins - initial version (bug 148190)
  10. *******************************************************************/
  11. package org.aspectj.ajde.core.internal;
  12. import java.io.File;
  13. import java.util.ArrayList;
  14. import java.util.Collection;
  15. import java.util.Iterator;
  16. import java.util.List;
  17. import java.util.Map;
  18. import java.util.StringTokenizer;
  19. import org.aspectj.ajde.core.AjCompiler;
  20. import org.aspectj.ajde.core.ICompilerConfiguration;
  21. import org.aspectj.ajde.core.IOutputLocationManager;
  22. import org.aspectj.ajdt.ajc.AjdtCommand;
  23. import org.aspectj.ajdt.ajc.BuildArgParser;
  24. import org.aspectj.ajdt.ajc.ConfigParser;
  25. import org.aspectj.ajdt.internal.core.builder.AjBuildConfig;
  26. import org.aspectj.ajdt.internal.core.builder.AjBuildManager;
  27. import org.aspectj.ajdt.internal.core.builder.AjState;
  28. import org.aspectj.ajdt.internal.core.builder.IncrementalStateManager;
  29. import org.aspectj.asm.AsmManager;
  30. import org.aspectj.bridge.AbortException;
  31. import org.aspectj.bridge.CountingMessageHandler;
  32. import org.aspectj.bridge.IMessage;
  33. import org.aspectj.bridge.IMessageHandler;
  34. import org.aspectj.bridge.ISourceLocation;
  35. import org.aspectj.bridge.Message;
  36. import org.aspectj.bridge.SourceLocation;
  37. import org.aspectj.bridge.context.CompilationAndWeavingContext;
  38. import org.aspectj.org.eclipse.jdt.internal.compiler.impl.CompilerOptions;
  39. import org.aspectj.util.LangUtil;
  40. /**
  41. * Build Manager which drives the build for a given AjCompiler. Tools call build on the AjCompiler which drives this.
  42. */
  43. public class AjdeCoreBuildManager {
  44. private final AjCompiler compiler;
  45. private AjdeCoreBuildNotifierAdapter buildEventNotifier = null;
  46. private final AjBuildManager ajBuildManager;
  47. private final IMessageHandler msgHandlerAdapter;
  48. public AjdeCoreBuildManager(AjCompiler compiler) {
  49. this.compiler = compiler;
  50. this.msgHandlerAdapter = new AjdeCoreMessageHandlerAdapter(compiler.getMessageHandler());
  51. this.ajBuildManager = new AjBuildManager(msgHandlerAdapter);
  52. this.ajBuildManager.environmentSupportsIncrementalCompilation(true);
  53. // this static information needs to be set to ensure
  54. // incremental compilation works correctly
  55. IncrementalStateManager.recordIncrementalStates = true;
  56. IncrementalStateManager.debugIncrementalStates = false;
  57. AsmManager.attemptIncrementalModelRepairs = true;
  58. }
  59. // public AsmManager getStructureModel() {
  60. // return ajBuildManager.
  61. // }
  62. /**
  63. * Execute a full or incremental build
  64. *
  65. * @param fullBuild true if requesting a full build, false if requesting to try an incremental build
  66. */
  67. public void performBuild(boolean fullBuild) {
  68. // If an incremental build is requested, check that we can
  69. if (!fullBuild) {
  70. AjState existingState = IncrementalStateManager.retrieveStateFor(compiler.getId());
  71. if (existingState == null) {
  72. // No existing state so we must do a full build
  73. fullBuild = true;
  74. } else {
  75. AsmManager.setLastActiveStructureModel(existingState.getStructureModel());
  76. // AsmManager.getDefault().setRelationshipMap(existingState.getRelationshipMap());
  77. // AsmManager.getDefault().setHierarchy(existingState.getStructureModel());
  78. }
  79. }
  80. try {
  81. reportProgressBegin();
  82. // record the options passed to the compiler if INFO turned on
  83. if (!msgHandlerAdapter.isIgnoring(IMessage.INFO)) {
  84. handleMessage(new Message(getFormattedOptionsString(), IMessage.INFO, null, null));
  85. }
  86. CompilationAndWeavingContext.reset();
  87. if (fullBuild) { // FULL BUILD
  88. AjBuildConfig buildConfig = generateAjBuildConfig();
  89. if (buildConfig == null) {
  90. return;
  91. }
  92. ajBuildManager.batchBuild(buildConfig, msgHandlerAdapter);
  93. } else { // INCREMENTAL BUILD
  94. // Only rebuild the config object if the configuration has changed
  95. AjBuildConfig buildConfig = null;
  96. ICompilerConfiguration compilerConfig = compiler.getCompilerConfiguration();
  97. int changes = compilerConfig.getConfigurationChanges();
  98. if (changes != ICompilerConfiguration.NO_CHANGES) {
  99. // What configuration changes can we cope with? And besides just repairing the config object
  100. // what does it mean for any existing state that we have?
  101. buildConfig = generateAjBuildConfig();
  102. if (buildConfig == null) {
  103. return;
  104. }
  105. } else {
  106. buildConfig = ajBuildManager.getState().getBuildConfig();
  107. buildConfig.setChanged(changes); // pass it through for the state to use it when making decisions
  108. buildConfig.setModifiedFiles(compilerConfig.getProjectSourceFilesChanged());
  109. buildConfig.setClasspathElementsWithModifiedContents(compilerConfig.getClasspathElementsWithModifiedContents());
  110. compilerConfig.configurationRead();
  111. }
  112. ajBuildManager.incrementalBuild(buildConfig, msgHandlerAdapter);
  113. }
  114. /*
  115. * if (buildFresh) { AjBuildConfig buildConfig = genAjBuildConfig(); if (buildConfig == null) return;
  116. * ajBuildManager.batchBuild(buildConfig,msgHandlerAdapter); } else { AjBuildConfig buildConfig =
  117. * ajBuildManager.getState().getBuildConfig();
  118. *
  119. * ajBuildManager.incrementalBuild(buildConfig,msgHandlerAdapter); }
  120. */
  121. IncrementalStateManager.recordSuccessfulBuild(compiler.getId(), ajBuildManager.getState());
  122. } catch (ConfigParser.ParseException pe) {
  123. handleMessage(new Message("Config file entry invalid, file: " + pe.getFile().getPath() + ", line number: "
  124. + pe.getLine(), IMessage.WARNING, null, null));
  125. } catch (AbortException e) {
  126. final IMessage message = e.getIMessage();
  127. if (message == null) {
  128. handleMessage(new Message(LangUtil.unqualifiedClassName(e) + " thrown: " + e.getMessage(), IMessage.ERROR, e, null));
  129. } else {
  130. handleMessage(new Message(message.getMessage() + "\n" + CompilationAndWeavingContext.getCurrentContext(),
  131. IMessage.ERROR, e, null));
  132. }
  133. } catch (Throwable t) {
  134. handleMessage(new Message("Compile error: " + LangUtil.unqualifiedClassName(t) + " thrown: " + "" + t.getMessage(),
  135. IMessage.ABORT, t, null));
  136. } finally {
  137. compiler.getBuildProgressMonitor().finish(ajBuildManager.wasFullBuild());
  138. }
  139. }
  140. /**
  141. * Starts the various notifiers which are interested in the build progress
  142. */
  143. private void reportProgressBegin() {
  144. compiler.getBuildProgressMonitor().begin();
  145. buildEventNotifier = new AjdeCoreBuildNotifierAdapter(compiler.getBuildProgressMonitor());
  146. ajBuildManager.setProgressListener(buildEventNotifier);
  147. }
  148. private String getFormattedOptionsString() {
  149. ICompilerConfiguration compilerConfig = compiler.getCompilerConfiguration();
  150. return "Building with settings: " + "\n-> output paths: "
  151. + formatCollection(compilerConfig.getOutputLocationManager().getAllOutputLocations()) + "\n-> classpath: "
  152. + compilerConfig.getClasspath() + "\n-> -inpath " + formatCollection(compilerConfig.getInpath()) + "\n-> -outjar "
  153. + formatOptionalString(compilerConfig.getOutJar()) + "\n-> -aspectpath "
  154. + formatCollection(compilerConfig.getAspectPath()) + "\n-> -sourcePathResources "
  155. + formatMap(compilerConfig.getSourcePathResources()) + "\n-> non-standard options: "
  156. + compilerConfig.getNonStandardOptions() + "\n-> javaoptions:" + formatMap(compilerConfig.getJavaOptionsMap());
  157. }
  158. private String formatCollection(Collection options) {
  159. if (options == null)
  160. return "<default>";
  161. if (options.isEmpty())
  162. return "none";
  163. StringBuffer formattedOptions = new StringBuffer();
  164. Iterator it = options.iterator();
  165. while (it.hasNext()) {
  166. String o = it.next().toString();
  167. if (formattedOptions.length() > 0)
  168. formattedOptions.append(", ");
  169. formattedOptions.append(o);
  170. }
  171. return formattedOptions.toString();
  172. }
  173. private String formatMap(Map options) {
  174. if (options == null)
  175. return "<default>";
  176. if (options.isEmpty())
  177. return "none";
  178. return options.toString();
  179. }
  180. private String formatOptionalString(String s) {
  181. if (s == null) {
  182. return "";
  183. } else {
  184. return s;
  185. }
  186. }
  187. /**
  188. * Generate a new AjBuildConfig from the compiler configuration associated with this AjdeCoreBuildManager or from a
  189. * configuration file.
  190. *
  191. * @return null if invalid configuration, corresponding AjBuildConfig otherwise
  192. */
  193. public AjBuildConfig generateAjBuildConfig() {
  194. File configFile = new File(compiler.getId());
  195. ICompilerConfiguration compilerConfig = compiler.getCompilerConfiguration();
  196. CountingMessageHandler handler = CountingMessageHandler.makeCountingMessageHandler(msgHandlerAdapter);
  197. String[] args = null;
  198. // Retrieve the set of files from either an arg file (@filename) or the compiler configuration
  199. if (configFile.exists() && configFile.isFile()) {
  200. args = new String[] { "@" + configFile.getAbsolutePath() };
  201. } else {
  202. List l = compilerConfig.getProjectSourceFiles();
  203. if (l == null) {
  204. return null;
  205. }
  206. args = (String[]) l.toArray(new String[l.size()]);
  207. }
  208. BuildArgParser parser = new BuildArgParser(handler);
  209. AjBuildConfig config = new AjBuildConfig();
  210. parser.populateBuildConfig(config, args, false, configFile);
  211. // Process the CLASSPATH
  212. String propcp = compilerConfig.getClasspath();
  213. if (propcp != null && propcp.length() != 0) {
  214. StringTokenizer st = new StringTokenizer(propcp, File.pathSeparator);
  215. List configClasspath = config.getClasspath();
  216. ArrayList toAdd = new ArrayList();
  217. while (st.hasMoreTokens()) {
  218. String entry = st.nextToken();
  219. if (!configClasspath.contains(entry)) {
  220. toAdd.add(entry);
  221. }
  222. }
  223. if (0 < toAdd.size()) {
  224. ArrayList both = new ArrayList(configClasspath.size() + toAdd.size());
  225. both.addAll(configClasspath);
  226. both.addAll(toAdd);
  227. config.setClasspath(both);
  228. }
  229. }
  230. // Process the OUTJAR
  231. if (config.getOutputJar() == null) {
  232. String outJar = compilerConfig.getOutJar();
  233. if (outJar != null && outJar.length() != 0) {
  234. config.setOutputJar(new File(outJar));
  235. }
  236. }
  237. // Process the OUTPUT LOCATION MANAGER
  238. IOutputLocationManager outputLocationManager = compilerConfig.getOutputLocationManager();
  239. if (config.getCompilationResultDestinationManager() == null && outputLocationManager != null) {
  240. config.setCompilationResultDestinationManager(new OutputLocationAdapter(outputLocationManager));
  241. }
  242. // Process the INPATH
  243. mergeInto(config.getInpath(), compilerConfig.getInpath());
  244. // bug 168840 - calling 'setInPath(..)' creates BinarySourceFiles which
  245. // are used to see if there have been changes in classes on the inpath
  246. if (config.getInpath() != null) {
  247. config.setInPath(config.getInpath());
  248. }
  249. // Process the SOURCE PATH RESOURCES
  250. config.setSourcePathResources(compilerConfig.getSourcePathResources());
  251. // Process the ASPECTPATH
  252. mergeInto(config.getAspectpath(), compilerConfig.getAspectPath());
  253. // Process the JAVA OPTIONS MAP
  254. Map jom = compilerConfig.getJavaOptionsMap();
  255. if (jom != null) {
  256. String version = (String) jom.get(CompilerOptions.OPTION_Compliance);
  257. if (version != null && (version.equals(CompilerOptions.VERSION_1_5) || version.equals(CompilerOptions.VERSION_1_6))) {
  258. config.setBehaveInJava5Way(true);
  259. }
  260. config.getOptions().set(jom);
  261. }
  262. // Process the NON-STANDARD COMPILER OPTIONS
  263. configureNonStandardOptions(config);
  264. compilerConfig.configurationRead();
  265. ISourceLocation location = null;
  266. if (config.getConfigFile() != null) {
  267. location = new SourceLocation(config.getConfigFile(), 0);
  268. }
  269. String message = parser.getOtherMessages(true);
  270. if (null != message) {
  271. IMessage m = new Message(message, IMessage.ERROR, null, location);
  272. handler.handleMessage(m);
  273. }
  274. // always force model generation in AJDE
  275. config.setGenerateModelMode(true);
  276. // always be in incremental mode in AJDE
  277. config.setIncrementalMode(true);
  278. // always force proceedOnError in AJDE
  279. config.setProceedOnError(true);
  280. return config;
  281. }
  282. private void mergeInto(Collection target, Collection source) {
  283. if ((null == target) || (null == source)) {
  284. return;
  285. }
  286. for (Iterator iter = source.iterator(); iter.hasNext();) {
  287. Object next = iter.next();
  288. if (!target.contains(next)) {
  289. target.add(next);
  290. }
  291. }
  292. }
  293. /**
  294. * Helper method for configure build options. This reads all command-line options specified in the non-standard options text
  295. * entry and sets any corresponding unset values in config.
  296. */
  297. private void configureNonStandardOptions(AjBuildConfig config) {
  298. String nonStdOptions = compiler.getCompilerConfiguration().getNonStandardOptions();
  299. if (LangUtil.isEmpty(nonStdOptions)) {
  300. return;
  301. }
  302. // Break a string into a string array of non-standard options.
  303. // Allows for one option to include a ' '. i.e. assuming it has been quoted, it
  304. // won't accidentally get treated as a pair of options (can be needed for xlint props file option)
  305. List tokens = new ArrayList();
  306. int ind = nonStdOptions.indexOf('\"');
  307. int ind2 = nonStdOptions.indexOf('\"', ind + 1);
  308. if ((ind > -1) && (ind2 > -1)) { // dont tokenize within double quotes
  309. String pre = nonStdOptions.substring(0, ind);
  310. String quoted = nonStdOptions.substring(ind + 1, ind2);
  311. String post = nonStdOptions.substring(ind2 + 1, nonStdOptions.length());
  312. tokens.addAll(tokenizeString(pre));
  313. tokens.add(quoted);
  314. tokens.addAll(tokenizeString(post));
  315. } else {
  316. tokens.addAll(tokenizeString(nonStdOptions));
  317. }
  318. String[] args = (String[]) tokens.toArray(new String[] {});
  319. // set the non-standard options in an alternate build config
  320. // (we don't want to lose the settings we already have)
  321. CountingMessageHandler counter = CountingMessageHandler.makeCountingMessageHandler(msgHandlerAdapter);
  322. AjBuildConfig altConfig = AjdtCommand.genBuildConfig(args, counter);
  323. if (counter.hasErrors()) {
  324. return;
  325. }
  326. // copy globals where local is not set
  327. config.installGlobals(altConfig);
  328. }
  329. /** Local helper method for splitting option strings */
  330. private List tokenizeString(String str) {
  331. List tokens = new ArrayList();
  332. StringTokenizer tok = new StringTokenizer(str);
  333. while (tok.hasMoreTokens()) {
  334. tokens.add(tok.nextToken());
  335. }
  336. return tokens;
  337. }
  338. /**
  339. * Helper method to ask the messagehandler to handle the given message
  340. */
  341. private void handleMessage(Message msg) {
  342. compiler.getMessageHandler().handleMessage(msg);
  343. }
  344. public void setCustomMungerFactory(Object o) {
  345. ajBuildManager.setCustomMungerFactory(o);
  346. }
  347. public Object getCustomMungerFactory() {
  348. return ajBuildManager.getCustomMungerFactory();
  349. }
  350. public void cleanupEnvironment() {
  351. ajBuildManager.cleanupEnvironment();
  352. }
  353. public AsmManager getStructureModel() {
  354. return ajBuildManager.getStructureModel();
  355. }
  356. }