Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

AjdeCoreBuildManager.java 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  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.
  42. * Tools call build on the AjCompiler which drives this.
  43. */
  44. public class AjdeCoreBuildManager {
  45. private AjCompiler compiler;
  46. private AjdeCoreBuildNotifierAdapter currNotifier = null;
  47. private AjBuildManager ajBuildManager;
  48. private IMessageHandler msgHandlerAdapter;
  49. public AjdeCoreBuildManager(AjCompiler compiler) {
  50. this.compiler = compiler;
  51. msgHandlerAdapter = new AjdeCoreMessageHandlerAdapter(compiler.getMessageHandler());
  52. ajBuildManager = new AjBuildManager(msgHandlerAdapter);
  53. ajBuildManager.environmentSupportsIncrementalCompilation(true);
  54. // this static information needs to be set to ensure
  55. // incremental compilation works correctly
  56. IncrementalStateManager.recordIncrementalStates=true;
  57. IncrementalStateManager.debugIncrementalStates=false;
  58. AsmManager.attemptIncrementalModelRepairs = true;
  59. }
  60. // XXX hideous, should not be Object
  61. public void setCustomMungerFactory(Object o) {
  62. ajBuildManager.setCustomMungerFactory(o);
  63. }
  64. public Object getCustomMungerFactory() {
  65. return ajBuildManager.getCustomMungerFactory();
  66. }
  67. /**
  68. * @param buildFresh - true if want to force a full build, false otherwise
  69. */
  70. public void doBuild(boolean buildFresh) {
  71. if (!buildFresh) {
  72. buildFresh = updateAsmManagerInformation();
  73. }
  74. try {
  75. startNotifiers();
  76. // record the options passed to the compiler
  77. handleMessage(new Message(getFormattedOptionsString(),IMessage.INFO,null,null));
  78. CompilationAndWeavingContext.reset();
  79. AjBuildConfig buildConfig = genAjBuildConfig();
  80. if (buildConfig == null) return;
  81. if (buildFresh) {
  82. ajBuildManager.batchBuild(buildConfig,msgHandlerAdapter);
  83. } else {
  84. ajBuildManager.incrementalBuild(buildConfig,msgHandlerAdapter);
  85. }
  86. /*
  87. if (buildFresh) {
  88. AjBuildConfig buildConfig = genAjBuildConfig();
  89. if (buildConfig == null) return;
  90. ajBuildManager.batchBuild(buildConfig,msgHandlerAdapter);
  91. } else {
  92. AjBuildConfig buildConfig = ajBuildManager.getState().getBuildConfig();
  93. ajBuildManager.incrementalBuild(buildConfig,msgHandlerAdapter);
  94. }
  95. */
  96. IncrementalStateManager.recordSuccessfulBuild(compiler.getId(),ajBuildManager.getState());
  97. } catch (ConfigParser.ParseException pe) {
  98. handleMessage(new Message("Config file entry invalid, file: " + pe.getFile().getPath()
  99. + ", line number: " + pe.getLine(),IMessage.WARNING,null,null));
  100. } catch (AbortException e) {
  101. final IMessage message = e.getIMessage();
  102. if (message == null) {
  103. handleMessage(new Message(LangUtil.unqualifiedClassName(e) + " thrown: "
  104. + e.getMessage(),IMessage.ERROR,e,null));
  105. } else {
  106. handleMessage(new Message(message.getMessage() + "\n"
  107. + CompilationAndWeavingContext.getCurrentContext(),IMessage.ERROR,e,null));
  108. }
  109. } catch (Throwable t) {
  110. handleMessage(new Message("Compile error: " + LangUtil.unqualifiedClassName(t) + " thrown: " +
  111. "" + t.getMessage(),IMessage.ABORT,t,null));
  112. } finally {
  113. compiler.getBuildProgressMonitor().finish(ajBuildManager.wasFullBuild());
  114. }
  115. }
  116. /**
  117. * Starts the various notifiers which are interested in the build progress
  118. */
  119. private void startNotifiers() {
  120. compiler.getBuildProgressMonitor().begin();
  121. currNotifier = new AjdeCoreBuildNotifierAdapter(compiler.getBuildProgressMonitor());
  122. ajBuildManager.setProgressListener(currNotifier);
  123. }
  124. /**
  125. * Switches the relationshipMap and hierarchy used by AsmManager to be
  126. * the one for the current compiler - this will not be necessary once
  127. * the static nature is removed from the asm.
  128. */
  129. private boolean updateAsmManagerInformation() {
  130. AjState updatedState = IncrementalStateManager.retrieveStateFor(compiler.getId());
  131. if (updatedState == null) {
  132. return true;
  133. } else {
  134. AsmManager.getDefault().setRelationshipMap(updatedState.getRelationshipMap());
  135. AsmManager.getDefault().setHierarchy(updatedState.getStructureModel());
  136. }
  137. return false;
  138. }
  139. // AMC - updated for AspectJ 1.1 options
  140. private String getFormattedOptionsString() {
  141. ICompilerConfiguration compilerConfig = compiler.getCompilerConfiguration();
  142. return "Building with settings: "
  143. + "\n-> output paths: " + formatCollection(compilerConfig.getOutputLocationManager()
  144. .getAllOutputLocations())
  145. + "\n-> classpath: " + compilerConfig.getClasspath()
  146. + "\n-> -inpath " + formatCollection(compilerConfig.getInpath())
  147. + "\n-> -outjar " + formatOptionalString(compilerConfig.getOutJar())
  148. + "\n-> -aspectpath " + formatCollection(compilerConfig.getAspectPath())
  149. + "\n-> -sourcePathResources " + formatMap(compilerConfig.getSourcePathResources())
  150. + "\n-> non-standard options: " + compilerConfig.getNonStandardOptions()
  151. + "\n-> javaoptions:" + formatMap(compilerConfig.getJavaOptionsMap());
  152. }
  153. private String formatCollection( Collection options ) {
  154. if ( options == null ) return "<default>";
  155. if ( options.isEmpty() ) return "none";
  156. StringBuffer formattedOptions = new StringBuffer();
  157. Iterator it = options.iterator();
  158. while (it.hasNext()) {
  159. String o = it.next().toString();
  160. if (formattedOptions.length() > 0) formattedOptions.append(", ");
  161. formattedOptions.append( o );
  162. }
  163. return formattedOptions.toString();
  164. }
  165. private String formatMap( Map options) {
  166. if (options == null) return "<default>";
  167. if (options.isEmpty()) return "none";
  168. return options.toString();
  169. }
  170. private String formatOptionalString( String s ) {
  171. if ( s == null ) { return "" ; }
  172. else { return s; }
  173. }
  174. /**
  175. * Generate a new AjBuildConfig from the compiler configuration
  176. * associated with this AjdeCoreBuildManager
  177. *
  178. * @return null if invalid configuration, corresponding
  179. * AjBuildConfig otherwise
  180. */
  181. public AjBuildConfig genAjBuildConfig() {
  182. File configFile = new File(compiler.getId());
  183. String[] args = null;
  184. if (configFile.exists() && configFile.isFile()) {
  185. args = new String[] { "@" + configFile.getAbsolutePath() };
  186. } else {
  187. List l = compiler.getCompilerConfiguration().getProjectSourceFiles();
  188. if (l == null) return null;
  189. args = new String[l.size()];
  190. int counter = 0;
  191. for (Iterator iter = l.iterator(); iter.hasNext();) {
  192. String element = (String) iter.next();
  193. args[counter] = element;
  194. counter++;
  195. }
  196. }
  197. CountingMessageHandler handler = CountingMessageHandler.makeCountingMessageHandler(
  198. msgHandlerAdapter);
  199. BuildArgParser parser = new BuildArgParser(handler);
  200. AjBuildConfig config = new AjBuildConfig();
  201. parser.populateBuildConfig(config, args, false, configFile);
  202. configureCompilerOptions(config);
  203. ISourceLocation location = null;
  204. if (config.getConfigFile() != null) {
  205. location = new SourceLocation(config.getConfigFile(), 0);
  206. }
  207. String message = parser.getOtherMessages(true);
  208. if (null != message) {
  209. IMessage m = new Message(message, IMessage.ERROR, null, location);
  210. handler.handleMessage(m);
  211. }
  212. // always force model generation in AJDE
  213. config.setGenerateModelMode(true);
  214. // always be in incremental mode in AJDE
  215. config.setIncrementalMode(true);
  216. // always force proceedOnError in AJDE
  217. config.setProceedOnError(true);
  218. return config;
  219. }
  220. /**
  221. * Configure the given AjBuildConfig with the options found in the
  222. * ICompilerConfiguration implementation associated with the AjCompiler
  223. * for this AjdeCoreBuildManager
  224. *
  225. * @param config
  226. */
  227. private void configureCompilerOptions(AjBuildConfig config) {
  228. String propcp = compiler.getCompilerConfiguration().getClasspath();
  229. if (!LangUtil.isEmpty(propcp)) {
  230. StringTokenizer st = new StringTokenizer(propcp, File.pathSeparator);
  231. List configClasspath = config.getClasspath();
  232. ArrayList toAdd = new ArrayList();
  233. while (st.hasMoreTokens()) {
  234. String entry = st.nextToken();
  235. if (!configClasspath.contains(entry)) {
  236. toAdd.add(entry);
  237. }
  238. }
  239. if (0 < toAdd.size()) {
  240. ArrayList both = new ArrayList(configClasspath.size() + toAdd.size());
  241. both.addAll(configClasspath);
  242. both.addAll(toAdd);
  243. config.setClasspath(both);
  244. }
  245. }
  246. // set the outputjar
  247. if (config.getOutputJar() == null) {
  248. String outJar = compiler.getCompilerConfiguration().getOutJar();
  249. if (!LangUtil.isEmpty(outJar)) {
  250. config.setOutputJar(new File( outJar ) );
  251. }
  252. }
  253. // set compilation result destination manager
  254. IOutputLocationManager outputLocationManager = compiler.getCompilerConfiguration().getOutputLocationManager();
  255. if (config.getCompilationResultDestinationManager() == null && outputLocationManager != null) {
  256. config.setCompilationResultDestinationManager(new OutputLocationAdapter(outputLocationManager));
  257. }
  258. join(config.getInpath(),compiler.getCompilerConfiguration().getInpath());
  259. // bug 168840 - calling 'setInPath(..)' creates BinarySourceFiles which
  260. // are used to see if there have been changes in classes on the inpath
  261. if (config.getInpath() != null) config.setInPath(config.getInpath());
  262. config.setSourcePathResources(compiler.getCompilerConfiguration().getSourcePathResources());
  263. join(config.getAspectpath(), compiler.getCompilerConfiguration().getAspectPath());
  264. Map jom = compiler.getCompilerConfiguration().getJavaOptionsMap();
  265. if (jom!=null) {
  266. String version = (String)jom.get(CompilerOptions.OPTION_Compliance);
  267. if (version!=null && ( version.equals(CompilerOptions.VERSION_1_5) || version.equals(CompilerOptions.VERSION_1_6))) {
  268. config.setBehaveInJava5Way(true);
  269. }
  270. config.getOptions().set(jom);
  271. }
  272. configureNonStandardOptions(config);
  273. }
  274. private void join(Collection target, Collection source) {
  275. if ((null == target) || (null == source)) {
  276. return;
  277. }
  278. for (Iterator iter = source.iterator(); iter.hasNext();) {
  279. Object next = iter.next();
  280. if (! target.contains(next)) {
  281. target.add(next);
  282. }
  283. }
  284. }
  285. /**
  286. * Helper method for configure build options. This reads all command-line
  287. * options specified in the non-standard options text entry and sets any
  288. * corresponding unset values in config.
  289. */
  290. private void configureNonStandardOptions(AjBuildConfig config) {
  291. String nonStdOptions = compiler.getCompilerConfiguration().getNonStandardOptions();
  292. if (LangUtil.isEmpty(nonStdOptions)) {
  293. return;
  294. }
  295. // Break a string into a string array of non-standard options.
  296. // Allows for one option to include a ' '. i.e. assuming it has been quoted, it
  297. // won't accidentally get treated as a pair of options (can be needed for xlint props file option)
  298. List tokens = new ArrayList();
  299. int ind = nonStdOptions.indexOf('\"');
  300. int ind2 = nonStdOptions.indexOf('\"',ind+1);
  301. if ((ind > -1) && (ind2 > -1)) { // dont tokenize within double quotes
  302. String pre = nonStdOptions.substring(0,ind);
  303. String quoted = nonStdOptions.substring(ind+1,ind2);
  304. String post = nonStdOptions.substring(ind2+1,nonStdOptions.length());
  305. tokens.addAll(tokenizeString(pre));
  306. tokens.add(quoted);
  307. tokens.addAll(tokenizeString(post));
  308. } else {
  309. tokens.addAll(tokenizeString(nonStdOptions));
  310. }
  311. String[] args = (String[])tokens.toArray(new String[]{});
  312. // set the non-standard options in an alternate build config
  313. // (we don't want to lose the settings we already have)
  314. CountingMessageHandler counter
  315. = CountingMessageHandler.makeCountingMessageHandler(msgHandlerAdapter);
  316. AjBuildConfig altConfig = AjdtCommand.genBuildConfig(args, counter);
  317. if (counter.hasErrors()) {
  318. return;
  319. }
  320. // copy globals where local is not set
  321. config.installGlobals(altConfig);
  322. }
  323. /** Local helper method for splitting option strings */
  324. private List tokenizeString(String str) {
  325. List tokens = new ArrayList();
  326. StringTokenizer tok = new StringTokenizer(str);
  327. while ( tok.hasMoreTokens() ) {
  328. tokens.add(tok.nextToken());
  329. }
  330. return tokens;
  331. }
  332. /**
  333. * Helper method to ask the messagehandler to handle the
  334. * given message
  335. */
  336. private void handleMessage(Message msg) {
  337. compiler.getMessageHandler().handleMessage(msg);
  338. }
  339. }