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.

CompileCommand.java 9.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  1. /* *******************************************************************
  2. * Copyright (c) 2003 Contributors.
  3. * All rights reserved.
  4. * This program and the accompanying materials are made available
  5. * under the terms of the Eclipse Public License v1.0
  6. * which accompanies this distribution and is available at
  7. * http://www.eclipse.org/legal/epl-v10.html
  8. *
  9. * Contributors:
  10. * Wes Isberg initial implementation
  11. * Helen Hawkins Converted to new interface (bug 148190)
  12. * ******************************************************************/
  13. package org.aspectj.testing.ajde;
  14. import java.io.File;
  15. import java.io.FileOutputStream;
  16. import java.io.IOException;
  17. import java.io.OutputStream;
  18. import java.io.PrintStream;
  19. import java.lang.reflect.InvocationHandler;
  20. import java.lang.reflect.Method;
  21. import java.lang.reflect.Proxy;
  22. import java.util.Collections;
  23. import java.util.List;
  24. import java.util.Map;
  25. import java.util.Set;
  26. import org.aspectj.ajde.core.AjCompiler;
  27. import org.aspectj.ajde.core.IBuildMessageHandler;
  28. import org.aspectj.ajde.core.IBuildProgressMonitor;
  29. import org.aspectj.ajde.core.ICompilerConfiguration;
  30. import org.aspectj.ajde.core.IOutputLocationManager;
  31. import org.aspectj.ajde.core.JavaOptions;
  32. import org.aspectj.bridge.AbortException;
  33. import org.aspectj.bridge.ICommand;
  34. import org.aspectj.bridge.IMessage;
  35. import org.aspectj.bridge.IMessage.Kind;
  36. import org.aspectj.bridge.IMessageHandler;
  37. import org.aspectj.bridge.MessageHandler;
  38. import org.aspectj.testing.harness.bridge.Globals;
  39. import org.aspectj.util.FileUtil;
  40. /**
  41. * This re-uses the same config file to setup ajde so that recompiles appear to be of the same configuration.
  42. *
  43. * @since Java 1.3 (uses dynamic proxies)
  44. */
  45. public class CompileCommand implements ICommand {
  46. // time out waiting for build at three minutes
  47. long MAX_TIME = 180 * 1000;
  48. // this proxy ignores calls
  49. InvocationHandler proxy = new VoidInvocationHandler();
  50. InvocationHandler loggingProxy = new LoggingInvocationHandler();
  51. MyMessageHandler myHandler = new MyMessageHandler();
  52. long endTime;
  53. boolean buildNextFresh;
  54. File tempDir;
  55. private AjCompiler compiler;
  56. /**
  57. * Clients call this before repeatCommand as a one-shot request for a full rebuild of the same configuration. (Requires a
  58. * downcast from ICommand to CompileCommand.)
  59. */
  60. public void buildNextFresh() {
  61. buildNextFresh = true;
  62. }
  63. // --------- ICommand interface
  64. public boolean runCommand(String[] args, IMessageHandler handler) {
  65. setup(args);
  66. myHandler.start();
  67. long startTime = System.currentTimeMillis();
  68. try {
  69. compiler.buildFresh();
  70. } finally {
  71. runCommandCleanup();
  72. }
  73. return !myHandler.hasError();
  74. }
  75. public boolean repeatCommand(IMessageHandler handler) {
  76. myHandler.start();
  77. long startTime = System.currentTimeMillis();
  78. // System.err.println("recompiling...");
  79. if (buildNextFresh) {
  80. buildNextFresh = false;
  81. compiler.buildFresh();
  82. } else {
  83. compiler.build();
  84. }
  85. return !myHandler.hasError();
  86. }
  87. void runCommandCleanup() {
  88. if (null != tempDir) {
  89. FileUtil.deleteContents(tempDir);
  90. tempDir.delete();
  91. }
  92. }
  93. // set by build progress monitor when done
  94. void setEndTime(long endTime) {
  95. this.endTime = endTime;
  96. }
  97. private void setup(String[] args) {
  98. File config = writeConfig(args);
  99. if (null == config) {
  100. throw new Error("unable to write config file");
  101. }
  102. IBuildProgressMonitor buildProgressMonitor = new MyBuildProgressMonitor();
  103. String classesDir = "../testing/bin/classes";
  104. for (int i = 0; i < args.length; i++) {
  105. if ("-d".equals(args[i]) && ((1 + i) < args.length)) {
  106. classesDir = args[1 + i];
  107. break;
  108. }
  109. }
  110. MyCompilerConfig compilerConfig = new MyCompilerConfig();
  111. compiler = new AjCompiler("blah", compilerConfig, buildProgressMonitor, myHandler);
  112. }
  113. private File writeConfig(String[] args) {
  114. tempDir = FileUtil.getTempDir("CompileCommand");
  115. File result = new File(tempDir, "config.lst");
  116. OutputStream out = null;
  117. try {
  118. out = new FileOutputStream(result);
  119. PrintStream outs = new PrintStream(out, true);
  120. for (int i = 0; i < args.length; i++) {
  121. outs.println(args[i]);
  122. }
  123. return result;
  124. } catch (IOException e) {
  125. return null;
  126. } finally {
  127. try {
  128. out.close();
  129. } catch (IOException e) {
  130. }
  131. }
  132. }
  133. // private Object makeLoggingProxy(Class interfac) {
  134. // return Proxy.newProxyInstance(
  135. // interfac.getClassLoader(),
  136. // new Class[] { interfac },
  137. // loggingProxy);
  138. // }
  139. private Object makeProxy(Class interfac) {
  140. return Proxy.newProxyInstance(interfac.getClassLoader(), new Class[] { interfac }, proxy);
  141. }
  142. }
  143. class MyMessageHandler implements IBuildMessageHandler {
  144. boolean hasError;
  145. boolean hasWarning;
  146. private MessageHandler messageHandler = new MessageHandler(false);
  147. public boolean handleMessage(IMessage message) throws AbortException {
  148. maintainHasWarning(message.getKind());
  149. return messageHandler.handleMessage(message);
  150. }
  151. private void maintainHasWarning(IMessage.Kind kind) {
  152. if (!hasError) {
  153. if (IMessage.ERROR.isSameOrLessThan(kind)) {
  154. hasError = true;
  155. hasWarning = true;
  156. }
  157. }
  158. if (!hasWarning && IMessage.WARNING.isSameOrLessThan(kind)) {
  159. hasWarning = true;
  160. }
  161. }
  162. public boolean hasWarning() {
  163. return hasWarning;
  164. }
  165. public boolean hasError() {
  166. return hasError;
  167. }
  168. public void start() {
  169. hasWarning = false;
  170. hasError = false;
  171. messageHandler.init(true);
  172. }
  173. public void dontIgnore(Kind kind) {
  174. messageHandler.dontIgnore(kind);
  175. }
  176. public void ignore(Kind kind) {
  177. messageHandler.ignore(kind);
  178. }
  179. public boolean isIgnoring(Kind kind) {
  180. return messageHandler.isIgnoring(kind);
  181. }
  182. }
  183. class MyBuildProgressMonitor implements IBuildProgressMonitor {
  184. public void begin() {
  185. }
  186. public void finish(boolean wasFullBuild) {
  187. }
  188. public boolean isCancelRequested() {
  189. return false;
  190. }
  191. public void setProgress(double percentDone) {
  192. }
  193. public void setProgressText(String text) {
  194. }
  195. }
  196. class VoidInvocationHandler implements InvocationHandler {
  197. public Object invoke(Object me, Method method, Object[] args) throws Throwable {
  198. // System.err.println("Proxying"
  199. // // don't call toString on self b/c proxied
  200. // // + " me=" + me.getClass().getName()
  201. // + " method=" + method
  202. // + " args=" + (LangUtil.isEmpty(args)
  203. // ? "[]" : Arrays.asList(args).toString()));
  204. return null;
  205. }
  206. }
  207. class LoggingInvocationHandler implements InvocationHandler {
  208. public Object invoke(Object me, Method method, Object[] args) throws Throwable {
  209. System.err.println("Proxying " + render(method, args));
  210. return null;
  211. }
  212. public static String render(Class c) {
  213. if (null == c) {
  214. return "(Class) null";
  215. }
  216. String result = c.getName();
  217. if (result.startsWith("java")) {
  218. int loc = result.lastIndexOf(".");
  219. if (-1 != loc) {
  220. result = result.substring(loc + 1);
  221. }
  222. }
  223. return result;
  224. }
  225. public static String render(Method method, Object[] args) {
  226. StringBuffer sb = new StringBuffer();
  227. sb.append(render(method.getReturnType()));
  228. sb.append(" ");
  229. sb.append(method.getName());
  230. sb.append("(");
  231. Class[] parmTypes = method.getParameterTypes();
  232. int parmTypesLength = (null == parmTypes ? 0 : parmTypes.length);
  233. int argsLength = (null == args ? 0 : args.length);
  234. boolean doType = (parmTypesLength == argsLength);
  235. for (int i = 0; i < argsLength; i++) {
  236. if (i > 0) {
  237. sb.append(", ");
  238. }
  239. if (doType) {
  240. sb.append("(");
  241. sb.append(render(parmTypes[i]));
  242. sb.append(") ");
  243. }
  244. if (null == args[i]) {
  245. sb.append("null");
  246. } else { // also don't recurse into proxied toString?
  247. sb.append(args[i].toString());
  248. }
  249. }
  250. sb.append(")");
  251. return sb.toString();
  252. }
  253. }
  254. class MyCompilerConfig implements ICompilerConfiguration {
  255. private Set inpath;
  256. private Set aspectPath;
  257. private String outJar;
  258. private IOutputLocationManager locationMgr;
  259. public Set getAspectPath() {
  260. return aspectPath;
  261. }
  262. public void setAspectPath(Set path) {
  263. aspectPath = path;
  264. }
  265. public String getClasspath() {
  266. return Globals.S_aspectjrt_jar;
  267. }
  268. public Set getInpath() {
  269. return inpath;
  270. }
  271. public void setInpath(Set input) {
  272. inpath = input;
  273. }
  274. public Map getJavaOptionsMap() {
  275. return JavaOptions.getDefaultJavaOptions();
  276. }
  277. public List getProjectXmlConfigFiles() {
  278. return Collections.EMPTY_LIST;
  279. }
  280. public String getOutJar() {
  281. return outJar;
  282. }
  283. public void configurationRead() {
  284. }
  285. public void setOutJar(String input) {
  286. outJar = input;
  287. }
  288. public IOutputLocationManager getOutputLocationManager() {
  289. if (locationMgr == null) {
  290. locationMgr = new MyOutputLocationManager();
  291. }
  292. return locationMgr;
  293. }
  294. public String getNonStandardOptions() {
  295. return null;
  296. }
  297. public List getProjectSourceFiles() {
  298. return null;
  299. }
  300. public List getProjectSourceFilesChanged() {
  301. return null;
  302. }
  303. public Map getSourcePathResources() {
  304. return null;
  305. }
  306. public int getConfigurationChanges() {
  307. return ICompilerConfiguration.EVERYTHING;
  308. }
  309. public List getClasspathElementsWithModifiedContents() {
  310. return null;
  311. }
  312. public String getProjectEncoding() {
  313. return null;
  314. }
  315. public String getProcessor() {
  316. return null;
  317. }
  318. public String getProcessorPath() {
  319. return null;
  320. }
  321. }
  322. class MyOutputLocationManager implements IOutputLocationManager {
  323. public List getAllOutputLocations() {
  324. return null;
  325. }
  326. public File getDefaultOutputLocation() {
  327. return null;
  328. }
  329. public File getOutputLocationForClass(File compilationUnit) {
  330. return null;
  331. }
  332. public File getOutputLocationForResource(File resource) {
  333. return null;
  334. }
  335. public String getUniqueIdentifier() {
  336. return null;
  337. }
  338. public Map getInpathMap() {
  339. return Collections.EMPTY_MAP;
  340. }
  341. public String getSourceFolderForFile(File sourceFile) {
  342. return null;
  343. }
  344. public void reportFileWrite(String outputfile, int filetype) {
  345. }
  346. public void reportFileRemove(String outputfile, int filetype) {
  347. }
  348. public int discoverChangesSince(File dir, long buildtime) {
  349. // TODO Auto-generated method stub
  350. return 0;
  351. }
  352. public String getProjectEncoding() {
  353. return null;
  354. }
  355. }