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.

AjdtCommand.java 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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 Common Public License v1.0
  6. * which accompanies this distribution and is available at
  7. * http://www.eclipse.org/legal/cpl-v10.html
  8. *
  9. * Contributors:
  10. * Xerox/PARC initial implementation
  11. * ******************************************************************/
  12. package org.aspectj.ajdt.ajc;
  13. import org.aspectj.ajdt.internal.core.builder.AjBuildConfig;
  14. import org.aspectj.ajdt.internal.core.builder.AjBuildManager;
  15. import org.aspectj.bridge.*;
  16. import org.aspectj.util.LangUtil;
  17. import org.eclipse.jdt.internal.core.builder.MissingSourceFileException;
  18. import java.util.Hashtable;
  19. import java.util.Iterator;
  20. import java.util.List;
  21. /**
  22. * ICommand adapter for the AspectJ eclipse-based compiler.
  23. */
  24. public class AjdtCommand implements ICommand {
  25. /** Message String for any AbortException thrown from ICommand API's */
  26. public static final String ABORT_MESSAGE = "ABORT";
  27. private boolean canRepeatCommand = true;
  28. AjBuildManager buildManager = null;
  29. String[] savedArgs = null;
  30. /**
  31. * Run AspectJ compiler, wrapping any exceptions thrown as
  32. * ABORT messages (containing ABORT_MESSAGE String).
  33. * @param args the String[] for the compiler
  34. * @param handler the IMessageHandler for any messages
  35. * @see org.aspectj.bridge.ICommand#runCommand(String[], IMessageHandler)
  36. * @return false if command failed
  37. */
  38. public boolean runCommand(String[] args, IMessageHandler handler) {
  39. try {
  40. buildManager = new AjBuildManager(handler);
  41. savedArgs = new String[args.length];
  42. System.arraycopy(args, 0, savedArgs, 0, savedArgs.length);
  43. CountingMessageHandler counter = new CountingMessageHandler(handler);
  44. AjBuildConfig config = genBuildConfig(savedArgs, counter);
  45. return (!counter.hasErrors()
  46. && buildManager.batchBuild(config, counter)
  47. && !counter.hasErrors());
  48. } catch (AbortException ae) {
  49. if (ae.isSilent()) {
  50. throw ae;
  51. } else {
  52. MessageUtil.abort(handler, ABORT_MESSAGE, ae);
  53. return false;
  54. }
  55. } catch (MissingSourceFileException t) { // XXX special handling - here only?
  56. MessageUtil.error(handler, t.getMessage());
  57. return false;
  58. } catch (Throwable t) {
  59. //System.err.println("caught: " + t);
  60. MessageUtil.abort(handler, ABORT_MESSAGE, t);
  61. return false;
  62. }
  63. }
  64. /**
  65. * Run AspectJ compiler, wrapping any exceptions thrown as
  66. * ABORT messages (containing ABORT_MESSAGE String).
  67. * @see org.aspectj.bridge.ICommand#repeatCommand(IMessageHandler)
  68. * @return false if command failed
  69. */
  70. public boolean repeatCommand(IMessageHandler handler) {
  71. if (null == buildManager) {
  72. MessageUtil.abort(handler, "repeatCommand called before runCommand");
  73. return false;
  74. }
  75. try {
  76. //buildManager.setMessageHandler(handler);
  77. CountingMessageHandler counter = new CountingMessageHandler(handler);
  78. // regenerate configuration b/c world might have changed (?)
  79. AjBuildConfig config = genBuildConfig(savedArgs, counter);
  80. return (!counter.hasErrors()
  81. && buildManager.incrementalBuild(config, handler)
  82. && !counter.hasErrors());
  83. } catch (MissingSourceFileException t) {
  84. return false; // already converted to error
  85. } catch (Throwable t) {
  86. MessageUtil.abort(handler, ABORT_MESSAGE, t);
  87. return false;
  88. }
  89. }
  90. /** @throws AbortException.ABORT on error after logging message */
  91. AjBuildConfig genBuildConfig(String[] args, IMessageHandler handler) {
  92. BuildArgParser parser = new BuildArgParser();
  93. AjBuildConfig config = parser.genBuildConfig(args, handler);
  94. String message = parser.getOtherMessages(true);
  95. if (null != message) {
  96. IMessage.Kind kind = inferKind(message);
  97. handler.handleMessage(new Message(message, kind, null, null));
  98. throw new AbortException(); // XXX tangled - assumes handler prints?
  99. }
  100. return config;
  101. }
  102. /** @return IMessage.WARNING unless message contains error or info */
  103. protected IMessage.Kind inferKind(String message) { // XXX dubious
  104. if (-1 == message.indexOf("error")) {
  105. return IMessage.ERROR;
  106. } else if (-1 == message.indexOf("info")) {
  107. return IMessage.INFO;
  108. } else {
  109. return IMessage.WARNING;
  110. }
  111. }
  112. }