Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

IncCompilerRun.java 19KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504
  1. /* *******************************************************************
  2. * Copyright (c) 1999-2001 Xerox Corporation,
  3. * 2002 Palo Alto Research Center, Incorporated (PARC).
  4. * All rights reserved.
  5. * This program and the accompanying materials are made available
  6. * under the terms of the Common Public License v1.0
  7. * which accompanies this distribution and is available at
  8. * http://www.eclipse.org/legal/cpl-v10.html
  9. *
  10. * Contributors:
  11. * Xerox/PARC initial implementation
  12. * ******************************************************************/
  13. package org.aspectj.testing.harness.bridge;
  14. import java.io.File;
  15. import java.io.FileFilter;
  16. import java.io.IOException;
  17. //import java.util.*;
  18. import java.util.ArrayList;
  19. import java.util.Arrays;
  20. import java.util.List;
  21. //import java.util.Collections;
  22. //import java.util.List;
  23. import org.aspectj.ajde.ui.StructureModelUtil;
  24. import org.aspectj.ajde.ui.StructureModelUtil.ModelIncorrectException;
  25. import org.aspectj.bridge.ICommand;
  26. //import org.aspectj.bridge.IMessage;
  27. import org.aspectj.bridge.MessageUtil;
  28. import org.aspectj.testing.ajde.CompileCommand;
  29. import org.aspectj.testing.run.IRunIterator;
  30. import org.aspectj.testing.run.IRunStatus;
  31. import org.aspectj.testing.run.WrappedRunIterator;
  32. import org.aspectj.testing.xml.SoftMessage;
  33. import org.aspectj.testing.xml.XMLWriter;
  34. import org.aspectj.util.FileUtil;
  35. import org.aspectj.util.LangUtil;
  36. /**
  37. * An incremental compiler run takes an existing compiler commmand
  38. * from the sandbox, updates the staging directory, and recompiles.
  39. * The staging directory is updated by prefix/suffix rules applied
  40. * to files found below Sandbox.testBaseSrcDir.
  41. * Files with suffix .{tag}.java are owned by this run
  42. * and are copied to the staging directory
  43. * unless they are prefixed "delete.", in which case the
  44. * corresponding file is deleted. Any "owned" file is passed to
  45. * the compiler as the list of changed files.
  46. * The files entry contains the expected files recompiled. XXX underinclusive
  47. * XXX prefer messages for expected files?
  48. * XXX later: also support specified paths, etc.
  49. */
  50. public class IncCompilerRun implements IAjcRun {
  51. final Spec spec; // nonfinal later to make re-runnable
  52. Sandbox sandbox;
  53. /**
  54. * @param handler must not be null, but may be reused in the same thread
  55. */
  56. public IncCompilerRun(Spec spec) {
  57. LangUtil.throwIaxIfNull(spec, "spec");
  58. this.spec = spec;
  59. }
  60. /**
  61. * Initialize this from the sandbox, using compiler and changedFiles.
  62. * @param sandbox the Sandbox setup for this test, including copying
  63. * any changed files, etc.
  64. * @see org.aspectj.testing.harness.bridge.AjcTest.IAjcRun#setup(File, File)
  65. * @throws AbortException containing IOException or IllegalArgumentException
  66. * if the staging operations fail
  67. */
  68. public boolean setupAjcRun(Sandbox sandbox, Validator validator) {
  69. LangUtil.throwIaxIfNull(validator, "validator");
  70. if (!validator.nullcheck(sandbox, "sandbox")
  71. || !validator.nullcheck(spec, "spec")
  72. || !validator.nullcheck(spec.tag, "fileSuffix")) {
  73. return false;
  74. }
  75. File srcDir = sandbox.getTestBaseSrcDir(this);
  76. File destDir = sandbox.stagingDir;
  77. if (!validator.canReadDir(srcDir, "testBaseSrcDir")
  78. || !validator.canReadDir(destDir, "stagingDir")) {
  79. return false;
  80. }
  81. this.sandbox = sandbox;
  82. return doStaging(validator);
  83. }
  84. /**
  85. * Handle copying and deleting of files per tag.
  86. * This returns false unless
  87. * (1) tag is "same", or
  88. * (2) some file was copied or deleted successfully
  89. * and there were no failures copying or deleting files.
  90. * @return true if staging completed successfully
  91. */
  92. boolean doStaging(final Validator validator) {
  93. if ("same".equals(spec.tag)) {
  94. return true;
  95. }
  96. boolean result = false;
  97. try {
  98. final String toSuffix = ".java";
  99. final String fromSuffix = "." + spec.tag + toSuffix;
  100. // copy our tagged generation of files to the staging directory,
  101. // deleting any with ChangedFilesCollector.DELETE_SUFFIX
  102. // sigh - delay until after last last-mod-time
  103. intHolder holder = new intHolder();
  104. List copied = new ArrayList();
  105. doStaging(validator,".java",holder,copied);
  106. doStaging(validator,".jar",holder,copied);
  107. doStaging(validator,".class",holder,copied);
  108. doStaging(validator,".properties",holder,copied); // arbitrary resource extension
  109. doStaging(validator,".xml",holder,copied); // arbitrary resource extension
  110. if ((0 == holder.numCopies) && (0 == holder.numDeletes)) {
  111. validator.fail("no files changed??");
  112. } else {
  113. result = (0 == holder.numFails);
  114. }
  115. if (0 < copied.size()) {
  116. File[] files = (File[]) copied.toArray(new File[0]);
  117. FileUtil.sleepPastFinalModifiedTime(files);
  118. }
  119. } catch (NullPointerException npe) {
  120. validator.fail("staging - input", npe);
  121. } catch (IOException e) {
  122. validator.fail("staging - operations", e);
  123. }
  124. return result;
  125. }
  126. private void doStaging(final Validator validator, final String toSuffix,
  127. final intHolder holder,final List copied)
  128. throws IOException
  129. {
  130. final String fromSuffix = "." + spec.tag + toSuffix;
  131. final String clip = ".delete" + toSuffix;
  132. FileFilter deleteOrCount = new FileFilter() {
  133. /** do copy unless file should be deleted */
  134. public boolean accept(File file) {
  135. boolean doCopy = true;
  136. String path = file.getAbsolutePath();
  137. if (!path.endsWith(clip)) {
  138. holder.numCopies++;
  139. validator.info("copying file: " + path);
  140. copied.add(file);
  141. } else {
  142. doCopy = false;
  143. path = path.substring(0, path.length()-clip.length()) + toSuffix;
  144. File toDelete = new File(path);
  145. if (toDelete.delete()) {
  146. validator.info("deleted file: " + path);
  147. holder.numDeletes++;
  148. } else {
  149. validator.fail("unable to delete file: " + path);
  150. holder.numFails++;
  151. }
  152. }
  153. return doCopy;
  154. }
  155. };
  156. File srcDir = sandbox.getTestBaseSrcDir(this);
  157. File destDir = sandbox.stagingDir;
  158. FileUtil.copyDir(srcDir, destDir, fromSuffix, toSuffix, deleteOrCount);
  159. }
  160. private static class intHolder {
  161. int numCopies;
  162. int numDeletes;
  163. int numFails;
  164. }
  165. /**
  166. * @see org.aspectj.testing.run.IRun#run(IRunStatus)
  167. */
  168. public boolean run(IRunStatus status) {
  169. ICommand compiler = sandbox.getCommand(this);
  170. if (null == compiler) {
  171. MessageUtil.abort(status, "null compiler");
  172. }
  173. // // This is a list of expected classes (in File-normal form
  174. // // relative to base class/src dir, without .class suffix
  175. // // -- like "org/aspectj/tools/ajc/Main")
  176. // // A preliminary list is generated in doStaging.
  177. // ArrayList expectedClasses = doStaging(status);
  178. // if (null == expectedClasses) {
  179. // return false;
  180. // }
  181. //
  182. // // now add any (additional) expected-class entries listed in the spec
  183. // // normalize to a similar file path (and do info messages for redundancies).
  184. //
  185. // List alsoChanged = spec.getPathsAsFile(sandbox.stagingDir);
  186. // for (Iterator iter = alsoChanged.iterator(); iter.hasNext();) {
  187. // File f = (File) iter.next();
  188. //
  189. // if (expectedClasses.contains(f)) {
  190. // // XXX remove old comment changed.contains() works b/c getPathsAsFile producing both File
  191. // // normalizes the paths, and File.equals(..) compares these lexically
  192. // String s = "specification of changed file redundant with tagged file: ";
  193. // MessageUtil.info(status, s + f);
  194. // } else {
  195. // expectedClasses.add(f);
  196. // }
  197. // }
  198. //
  199. // // now can create handler, use it for reporting
  200. // List errors = spec.getMessages(IMessage.ERROR);
  201. // List warnings = spec.getMessages(IMessage.WARNING);
  202. // AjcMessageHandler handler = new AjcMessageHandler(errors, warnings, expectedClasses);
  203. // same DirChanges handling for JavaRun, CompilerRun, IncCompilerRun
  204. // XXX around advice or template method/class
  205. DirChanges dirChanges = null;
  206. if (!LangUtil.isEmpty(spec.dirChanges)) {
  207. LangUtil.throwIaxIfFalse(1 == spec.dirChanges.size(), "expecting only 1 dirChanges");
  208. dirChanges = new DirChanges((DirChanges.Spec) spec.dirChanges.get(0));
  209. if (!dirChanges.start(status, sandbox.classesDir)) {
  210. return false; // setup failed
  211. }
  212. }
  213. AjcMessageHandler handler = new AjcMessageHandler(spec.getMessages());
  214. boolean handlerResult = false;
  215. boolean commandResult = false;
  216. boolean result = false;
  217. boolean report = false;
  218. try {
  219. handler.init();
  220. if (spec.fresh) {
  221. if (compiler instanceof CompileCommand) { // urk
  222. ((CompileCommand) compiler).buildNextFresh();
  223. } else {
  224. MessageUtil.info(handler, "fresh not supported by compiler: " + compiler);
  225. }
  226. }
  227. // final long startTime = System.currentTimeMillis();
  228. commandResult = compiler.repeatCommand(handler);
  229. if (!spec.checkModel.equals("")) {
  230. StructureModelUtil.checkModel(spec.checkModel);
  231. }
  232. // XXX disabled LangUtil.throwIaxIfNotAllAssignable(actualRecompiled, File.class, "recompiled");
  233. report = true;
  234. // handler does not verify sandbox...
  235. handlerResult = handler.passed();
  236. if (!handlerResult) {
  237. result = false;
  238. } else {
  239. result = (commandResult == handler.expectingCommandTrue());
  240. if (! result) {
  241. String m = commandResult
  242. ? "incremental compile command did not return false as expected"
  243. : "incremental compile command returned false unexpectedly";
  244. MessageUtil.fail(status, m);
  245. } else if (null != dirChanges) {
  246. result = dirChanges.end(status, sandbox.testBaseDir);
  247. }
  248. }
  249. } catch (ModelIncorrectException e) {
  250. MessageUtil.fail(status,e.getMessage());
  251. } finally {
  252. if (!result || spec.runtime.isVerbose()) { // more debugging context in case of failure
  253. MessageUtil.info(handler, "spec: " + spec.toLongString());
  254. MessageUtil.info(handler, "sandbox: " + sandbox);
  255. String[] classes = FileUtil.listFiles(sandbox.classesDir);
  256. MessageUtil.info(handler, "sandbox.classes: " + Arrays.asList(classes));
  257. }
  258. // XXX weak - actual messages not reported in real-time, no fast-fail
  259. if (report) {
  260. handler.report(status);
  261. }
  262. }
  263. return result;
  264. }
  265. // private boolean hasFile(ArrayList changed, File f) {
  266. // return changed.contains(f); // d
  267. // }
  268. public String toString() {
  269. return "" + spec;
  270. // return "IncCompilerRun(" + spec + ")"; // XXX
  271. }
  272. /**
  273. * initializer/factory for IncCompilerRun.
  274. */
  275. public static class Spec extends AbstractRunSpec {
  276. public static final String XMLNAME = "inc-compile";
  277. protected boolean fresh;
  278. protected ArrayList classesAdded;
  279. protected ArrayList classesRemoved;
  280. protected ArrayList classesUpdated;
  281. protected String checkModel;
  282. /**
  283. * skip description, skip sourceLocation,
  284. * do keywords, skip options, do paths as classes, do comment,
  285. * skip staging (always true), skip badInput (irrelevant)
  286. * do dirChanges, do messages but skip children.
  287. */
  288. // private static final XMLNames NAMES = new XMLNames(XMLNames.DEFAULT,
  289. // "", "", null, "", "classes", null, "", "", false, false, true);
  290. //
  291. /** identifies files this run owns, so {name}.{tag}.java maps to {name}.java */
  292. String tag;
  293. public Spec() {
  294. super(XMLNAME);
  295. setStaging(true);
  296. classesAdded = new ArrayList();
  297. classesRemoved = new ArrayList();
  298. classesUpdated = new ArrayList();
  299. checkModel="";
  300. }
  301. protected void initClone(Spec spec)
  302. throws CloneNotSupportedException {
  303. super.initClone(spec);
  304. spec.fresh = fresh;
  305. spec.tag = tag;
  306. spec.classesAdded.clear();
  307. spec.classesAdded.addAll(classesAdded);
  308. spec.classesRemoved.clear();
  309. spec.classesRemoved.addAll(classesRemoved);
  310. spec.classesUpdated.clear();
  311. spec.classesUpdated.addAll(classesUpdated);
  312. }
  313. public Object clone() throws CloneNotSupportedException {
  314. Spec result = new Spec();
  315. initClone(result);
  316. return result;
  317. }
  318. public void setFresh(boolean fresh) {
  319. this.fresh = fresh;
  320. }
  321. public void setTag(String input) {
  322. tag = input;
  323. }
  324. public void setCheckModel(String thingsToCheck) {
  325. this.checkModel=thingsToCheck;
  326. }
  327. public String toString() {
  328. return "IncCompile.Spec(" + tag + ", " + super.toString() + ",["+checkModel+"])";
  329. }
  330. /** override to set dirToken to Sandbox.CLASSES and default suffix to ".class" */
  331. public void addDirChanges(DirChanges.Spec spec) { // XXX copy/paste of CompilerRun.Spec...
  332. if (null == spec) {
  333. return;
  334. }
  335. spec.setDirToken(Sandbox.CLASSES_DIR);
  336. spec.setDefaultSuffix(".class");
  337. super.addDirChanges(spec);
  338. }
  339. /** @return a IncCompilerRun with this as spec if setup completes successfully. */
  340. public IRunIterator makeRunIterator(Sandbox sandbox, Validator validator) {
  341. IncCompilerRun run = new IncCompilerRun(this);
  342. if (run.setupAjcRun(sandbox, validator)) {
  343. // XXX need name
  344. return new WrappedRunIterator(this, run);
  345. }
  346. return null;
  347. }
  348. /**
  349. * Write this out as a compile element as defined in
  350. * AjcSpecXmlReader.DOCTYPE.
  351. * @see AjcSpecXmlReader#DOCTYPE
  352. * @see IXmlWritable#writeXml(XMLWriter)
  353. */
  354. public void writeXml(XMLWriter out) {
  355. String attr = XMLWriter.makeAttribute("tag", tag);
  356. out.startElement(xmlElementName, attr, false);
  357. if (fresh) {
  358. out.printAttribute("fresh", "true");
  359. }
  360. super.writeAttributes(out);
  361. out.endAttributes();
  362. if (!LangUtil.isEmpty(dirChanges)) {
  363. DirChanges.Spec.writeXml(out, dirChanges);
  364. }
  365. SoftMessage.writeXml(out, getMessages());
  366. out.endElement(xmlElementName);
  367. }
  368. public void setClassesAdded(String items) {
  369. addItems(classesAdded, items);
  370. }
  371. public void setClassesUpdated(String items) {
  372. addItems(classesUpdated, items);
  373. }
  374. public void setClassesRemoved(String items) {
  375. addItems(classesRemoved, items);
  376. }
  377. private void addItems(ArrayList list, String items) {
  378. if (null != items) {
  379. String[] classes = XMLWriter.unflattenList(items);
  380. if (!LangUtil.isEmpty(classes)) {
  381. for (int i = 0; i < classes.length; i++) {
  382. if (!LangUtil.isEmpty(classes[i])) {
  383. list.add(classes[i]);
  384. }
  385. }
  386. }
  387. }
  388. }
  389. } // class IncCompilerRun.Spec
  390. }
  391. // // XXX replaced with method-local class - revisit if useful
  392. //
  393. // /**
  394. // * This class collects the list of all changed files and
  395. // * deletes the corresponding file for those prefixed "delete."
  396. // */
  397. // static class ChangedFilesCollector implements FileFilter {
  398. // static final String DELETE_SUFFIX = ".delete.java";
  399. // static final String REPLACE_SUFFIX = ".java";
  400. // final ArrayList changed;
  401. // final Validator validator;
  402. // /** need this to generate paths by clipping */
  403. // final File destDir;
  404. //
  405. // /** @param changed the sink for all files changed (full paths) */
  406. // public ChangedFilesCollector(ArrayList changed, File destDir, Validator validator) {
  407. // LangUtil.throwIaxIfNull(validator, "ChangedFilesCollector - handler");
  408. // this.changed = changed;
  409. // this.validator = validator;
  410. // this.destDir = destDir;
  411. // }
  412. //
  413. // /**
  414. // * This converts the input File to normal String path form
  415. // * (without any source suffix) and adds it to the list changed.
  416. // * If the name of the file is suffixed ".delete..", then
  417. // * delete the corresponding file, and return false (no copy).
  418. // * Return true otherwise (copy file).
  419. // * @see java.io.FileFilter#accept(File)
  420. // */
  421. // public boolean accept(File file) {
  422. // final String aname = file.getAbsolutePath();
  423. // String name = file.getName();
  424. // boolean doCopy = true;
  425. // boolean failed = false;
  426. // if (name.endsWith(DELETE_SUFFIX)) {
  427. // name = name.substring(0,name.length()-DELETE_SUFFIX.length());
  428. // file = file.getParentFile();
  429. // file = new File(file, name + REPLACE_SUFFIX);
  430. // if (!file.canWrite()) {
  431. // validator.fail("file to delete is not writable: " + file);
  432. // failed = true;
  433. // } else if (!file.delete()) {
  434. // validator.fail("unable to delete file: " + file);
  435. // failed = true;
  436. // }
  437. // doCopy = false;
  438. // }
  439. // if (!failed && doCopy) {
  440. // int clip = FileUtil.sourceSuffixLength(file);
  441. // if (-1 != clip) {
  442. // name.substring(0, name.length()-clip);
  443. // }
  444. // if (null != destDir) {
  445. // String path = destDir.getPath();
  446. // if (!LangUtil.isEmpty(path)) {
  447. // // XXX incomplete
  448. // if (name.startsWith(path)) {
  449. // } else {
  450. // int loc = name.lastIndexOf(path);
  451. // if (-1 == loc) { // sigh
  452. //
  453. // } else {
  454. //
  455. // }
  456. // }
  457. // }
  458. // }
  459. // name = FileUtil.weakNormalize(name);
  460. // changed.add(file);
  461. // }
  462. // return doCopy;
  463. // }
  464. // };