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.

TestUtil.java 27KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788
  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.testing.util;
  13. import java.io.BufferedReader;
  14. import java.io.ByteArrayOutputStream;
  15. import java.io.DataInputStream;
  16. import java.io.File;
  17. import java.io.FileInputStream;
  18. import java.io.IOException;
  19. import java.io.InputStream;
  20. import java.io.PrintStream;
  21. import java.io.PrintWriter;
  22. import java.io.StringReader;
  23. import java.io.StringWriter;
  24. import java.lang.reflect.InvocationTargetException;
  25. import java.lang.reflect.Method;
  26. import java.lang.reflect.Modifier;
  27. import java.util.ArrayList;
  28. import java.util.Arrays;
  29. import java.util.Collection;
  30. import java.util.Enumeration;
  31. import java.util.HashSet;
  32. import java.util.List;
  33. import java.util.Set;
  34. import jdiff.text.FileLine;
  35. import jdiff.util.Diff;
  36. import jdiff.util.DiffNormalOutput;
  37. import junit.framework.Assert;
  38. import junit.framework.Test;
  39. import junit.framework.TestCase;
  40. import junit.framework.TestResult;
  41. import junit.framework.TestSuite;
  42. import junit.runner.TestCaseClassLoader;
  43. import org.aspectj.bridge.IMessageHandler;
  44. import org.aspectj.bridge.MessageUtil;
  45. import org.aspectj.util.FileUtil;
  46. import org.aspectj.util.LangUtil;
  47. import org.aspectj.util.Reflection;
  48. /**
  49. * Things that junit should perhaps have, but doesn't. Note the file-comparison
  50. * methods require JDiff to run, but JDiff types are not required to resolve
  51. * this class. Also, the bytecode weaver is required to compare class files, but
  52. * not to compare other files.
  53. */
  54. public final class TestUtil {
  55. private TestUtil() {
  56. super();
  57. }
  58. // ---- arrays
  59. public static void assertArrayEquals(String msg, Object[] expected,
  60. Object[] found) {
  61. TestCase.assertEquals(msg, Arrays.asList(expected), Arrays
  62. .asList(found));
  63. }
  64. // ---- unordered
  65. public static void assertSetEquals(Collection expected, Collection found) {
  66. assertSetEquals(null, expected, found);
  67. }
  68. public static void assertSetEquals(String msg, Object[] expected,
  69. Object[] found) {
  70. assertSetEquals(msg, Arrays.asList(expected), Arrays.asList(found));
  71. }
  72. public static void assertSetEquals(String msg, Collection expected,
  73. Collection found) {
  74. msg = (msg == null) ? "" : msg + ": ";
  75. Set results1 = new HashSet(found);
  76. results1.removeAll(expected);
  77. Set results2 = new HashSet(expected);
  78. results2.removeAll(found);
  79. if (results1.isEmpty()) {
  80. TestCase.assertTrue(msg + "Expected but didn't find: "
  81. + results2.toString(), results2.isEmpty());
  82. } else if (results2.isEmpty()) {
  83. TestCase.assertTrue(msg + "Didn't expect: " + results1.toString(),
  84. results1.isEmpty());
  85. } else {
  86. TestCase.assertTrue(msg + "Expected but didn't find: "
  87. + results2.toString() + "\nDidn't expect: "
  88. + results1.toString(), false);
  89. }
  90. }
  91. // ---- objects
  92. public static void assertCommutativeEquals(Object a, Object b,
  93. boolean should) {
  94. TestCase.assertEquals(a + " equals " + b, should, a.equals(b));
  95. TestCase.assertEquals(b + " equals " + a, should, b.equals(a));
  96. assertHashEquals(a, b, should);
  97. }
  98. private static void assertHashEquals(Object s, Object t, boolean should) {
  99. if (should) {
  100. TestCase.assertTrue(s + " does not hash to same as " + t, s
  101. .hashCode() == t.hashCode());
  102. } else {
  103. if (s.hashCode() == t.hashCode()) {
  104. System.err.println("warning: hash collision with hash = "
  105. + t.hashCode());
  106. System.err.println(" for " + s);
  107. System.err.println(" and " + t);
  108. }
  109. }
  110. }
  111. // -- reflective stuff
  112. public static void runMain(String classPath, String className) {
  113. runMethod(classPath, className, "main", new Object[] { new String[0] });
  114. }
  115. public static Object runMethod(String classPath, String className,
  116. String methodName, Object[] args) {
  117. classPath += File.pathSeparator + System.getProperty("java.class.path");
  118. ClassLoader loader = new TestCaseClassLoader(classPath);
  119. Class c = null;
  120. try {
  121. c = loader.loadClass(className);
  122. } catch (ClassNotFoundException e) {
  123. Assert.assertTrue("unexpected exception: " + e, false);
  124. }
  125. return Reflection.invokestaticN(c, methodName, args);
  126. }
  127. /**
  128. * Checks that two multi-line strings have the same value. Each line is
  129. * trimmed before comparision Produces an error on the particular line of
  130. * conflict
  131. */
  132. public static void assertMultiLineStringEquals(String message, String s1,
  133. String s2) {
  134. try {
  135. BufferedReader r1 = new BufferedReader(new StringReader(s1));
  136. BufferedReader r2 = new BufferedReader(new StringReader(s2));
  137. List lines = new ArrayList();
  138. String l1, l2;
  139. int index = 1;
  140. while (true) {
  141. l1 = readNonBlankLine(r1);
  142. l2 = readNonBlankLine(r2);
  143. if (l1 == null || l2 == null)
  144. break;
  145. if (l1.equals(l2)) {
  146. lines.add(l1);
  147. } else {
  148. showContext(lines);
  149. Assert.assertEquals(message + "(line " + index + ")", l1,
  150. l2);
  151. }
  152. index++;
  153. }
  154. if (l1 != null)
  155. showContext(lines);
  156. Assert.assertTrue(message + ": unexpected " + l1, l1 == null);
  157. if (l2 != null)
  158. showContext(lines);
  159. Assert.assertTrue(message + ": unexpected " + l2, l2 == null);
  160. } catch (IOException ioe) {
  161. Assert.assertTrue(message + ": caught " + ioe.getMessage(), false);
  162. }
  163. }
  164. private static void showContext(List lines) {
  165. int n = lines.size();
  166. for (int i = Math.max(0, n - 8); i < n; i++) {
  167. System.err.println(lines.get(i));
  168. }
  169. }
  170. private static String readNonBlankLine(BufferedReader r) throws IOException {
  171. String l = r.readLine();
  172. if (l == null)
  173. return null;
  174. l = l.trim();
  175. // comment to include comments when reading
  176. int commentLoc = l.indexOf("//");
  177. if (-1 != commentLoc) {
  178. l = l.substring(0, commentLoc).trim();
  179. }
  180. if ("".equals(l))
  181. return readNonBlankLine(r);
  182. return l;
  183. }
  184. /**
  185. * If there is an expected dir, expect each file in its subtree to match a
  186. * corresponding actual file in the base directory. This does NOT check that
  187. * all actual files have corresponding expected files. This ignores
  188. * directory paths containing "CVS".
  189. *
  190. * @param handler
  191. * the IMessageHandler sink for error messages
  192. * @param expectedBaseDir
  193. * the File path to the directory containing expected files, all
  194. * of which are compared with any corresponding actual files
  195. * @param actualBaseDir
  196. * the File path to the base directory from which to find any
  197. * actual files corresponding to expected files.
  198. * @return true if all files in the expectedBaseDir directory tree have
  199. * matching files in the actualBaseDir directory tree.
  200. */
  201. public static boolean sameDirectoryContents(final IMessageHandler handler,
  202. final File expectedBaseDir, final File actualBaseDir,
  203. final boolean fastFail) {
  204. LangUtil.throwIaxIfNull(handler, "handler");
  205. if (!FileUtil.canReadDir(expectedBaseDir)) {
  206. MessageUtil.fail(handler, " expected dir not found: "
  207. + expectedBaseDir);
  208. return false;
  209. }
  210. if (!FileUtil.canReadDir(actualBaseDir)) {
  211. MessageUtil
  212. .fail(handler, " actual dir not found: " + actualBaseDir);
  213. return false;
  214. }
  215. String[] paths = FileUtil.listFiles(expectedBaseDir);
  216. boolean result = true;
  217. for (int i = 0; i < paths.length; i++) {
  218. if (-1 != paths[i].indexOf("CVS")) {
  219. continue;
  220. }
  221. if (!sameFiles(handler, expectedBaseDir, actualBaseDir, paths[i])
  222. && result) {
  223. result = false;
  224. if (fastFail) {
  225. break;
  226. }
  227. }
  228. }
  229. return result;
  230. }
  231. // ------------ File-comparison utilities (XXX need their own class...)
  232. /**
  233. * Test interface to compare two files, line by line, and report differences
  234. * as one FAIL message if a handler is supplied. This preprocesses .class
  235. * files by disassembling.
  236. *
  237. * @param handler
  238. * the IMessageHandler for any FAIL messages (null to ignore)
  239. * @param expectedFile
  240. * the File path to the canonical file
  241. * @param actualFile
  242. * the File path to the actual file, if any
  243. * @return true if the input files are the same, based on per-line
  244. * comparisons
  245. */
  246. public static boolean sameFiles(IMessageHandler handler, File expectedFile,
  247. File actualFile) {
  248. return doSameFile(handler, null, null, expectedFile, actualFile);
  249. }
  250. /**
  251. * Test interface to compare two files, line by line, and report differences
  252. * as one FAIL message if a handler is supplied. This preprocesses .class
  253. * files by disassembling. This method assumes that the files are at the
  254. * same offset from two respective base directories.
  255. *
  256. * @param handler
  257. * the IMessageHandler for any FAIL messages (null to ignore)
  258. * @param expectedBaseDir
  259. * the File path to the canonical file base directory
  260. * @param actualBaseDir
  261. * the File path to the actual file base directory
  262. * @param path
  263. * the String path offset from the base directories
  264. * @return true if the input files are the same, based on per-line
  265. * comparisons
  266. */
  267. public static boolean sameFiles(IMessageHandler handler,
  268. File expectedBaseDir, File actualBaseDir, String path) {
  269. File actualFile = new File(actualBaseDir, path);
  270. File expectedFile = new File(expectedBaseDir, path);
  271. return doSameFile(handler, expectedBaseDir, actualBaseDir,
  272. expectedFile, actualFile);
  273. }
  274. /**
  275. * This does the work, selecting a lineator subclass and converting public
  276. * API's to JDiff APIs for comparison. Currently, all jdiff interfaces are
  277. * method-local, so this class will load without it; if we do use it, we can
  278. * avoid the duplication.
  279. */
  280. private static boolean doSameFile(IMessageHandler handler,
  281. File expectedBaseDir, File actualBaseDir, File expectedFile,
  282. File actualFile) {
  283. String path = expectedFile.getPath();
  284. // XXX permit user to specify lineator
  285. ILineator lineator = Lineator.TEXT;
  286. if (path.endsWith(".class")) {
  287. if (ClassLineator.haveDisassembler()) {
  288. lineator = Lineator.CLASS;
  289. } else {
  290. MessageUtil.abort(handler,
  291. "skipping - dissassembler not available");
  292. return false;
  293. }
  294. }
  295. CanonicalLine[] actualLines = null;
  296. CanonicalLine[] expectedLines = null;
  297. try {
  298. actualLines = lineator.getLines(handler, actualFile, actualBaseDir);
  299. expectedLines = lineator.getLines(handler, expectedFile,
  300. expectedBaseDir);
  301. } catch (IOException e) {
  302. MessageUtil.fail(handler, "rendering lines ", e);
  303. return false;
  304. }
  305. if (!LangUtil.isEmpty(actualLines) && !LangUtil.isEmpty(expectedLines)) {
  306. // here's the transmutation back to jdiff - extract if publishing
  307. // JDiff
  308. CanonicalLine[][] clines = new CanonicalLine[][] { expectedLines,
  309. actualLines };
  310. FileLine[][] flines = new FileLine[2][];
  311. for (int i = 0; i < clines.length; i++) {
  312. CanonicalLine[] cline = clines[i];
  313. FileLine[] fline = new FileLine[cline.length];
  314. for (int j = 0; j < fline.length; j++) {
  315. fline[j] = new FileLine(cline[j].canonical, cline[j].line);
  316. }
  317. flines[i] = fline;
  318. }
  319. Diff.change edits = new Diff(flines[0], flines[1]).diff_2(false);
  320. if ((null == edits) || (0 == (edits.inserted + edits.deleted))) {
  321. // XXX confirm with jdiff that null means no edits
  322. return true;
  323. } else {
  324. // String m = render(handler, edits, flines[0], flines[1]);
  325. StringWriter writer = new StringWriter();
  326. DiffNormalOutput out = new DiffNormalOutput(flines[0],
  327. flines[1]);
  328. out.setOut(writer);
  329. out.setLineSeparator(LangUtil.EOL);
  330. try {
  331. out.writeScript(edits);
  332. } catch (IOException e) {
  333. MessageUtil.fail(handler, "rendering edits", e);
  334. } finally {
  335. if (null != writer) {
  336. try {
  337. writer.close();
  338. } catch (IOException e) {
  339. MessageUtil.fail(handler,
  340. "closing after rendering edits", e);
  341. }
  342. }
  343. }
  344. String message = "diff between " + path + " in expected dir "
  345. + expectedBaseDir + " and actual dir " + actualBaseDir
  346. + LangUtil.EOL + writer.toString();
  347. MessageUtil.fail(handler, message);
  348. }
  349. }
  350. return false;
  351. }
  352. /**
  353. * TODO move to testing-utils for use by loadtime5, others.
  354. *
  355. * @param sink
  356. * @param sourceName
  357. */
  358. public static void loadTestsReflectively(TestSuite sink, String sourceName, boolean ignoreError) {
  359. Throwable thrown = null;
  360. try {
  361. ClassLoader loader = sink.getClass().getClassLoader();
  362. Class sourceClass = loader.loadClass(sourceName);
  363. if (!Modifier.isPublic(sourceClass.getModifiers())) {
  364. errorSuite(sink, sourceName, "not public class");
  365. return;
  366. }
  367. Method suiteMethod = sourceClass.getMethod("suite", new Class[0]);
  368. int mods = suiteMethod.getModifiers();
  369. if (!Modifier.isStatic(mods) || !Modifier.isPublic(mods)) {
  370. errorSuite(sink, sourceName, "not static method");
  371. return;
  372. }
  373. if (!Modifier.isPublic(mods)) {
  374. errorSuite(sink, sourceName, "not public method");
  375. return;
  376. }
  377. if (!Test.class.isAssignableFrom(suiteMethod.getReturnType())) {
  378. errorSuite(sink, sourceName, "suite() does not return Test");
  379. return;
  380. }
  381. Object result = suiteMethod.invoke(null, new Object[0]);
  382. Test test = (Test) result;
  383. if (!(test instanceof TestSuite)) {
  384. sink.addTest(test);
  385. } else {
  386. TestSuite source = (TestSuite) test;
  387. Enumeration tests = source.tests();
  388. while (tests.hasMoreElements()) {
  389. sink.addTest((Test) tests.nextElement());
  390. }
  391. }
  392. } catch (ClassNotFoundException e) {
  393. thrown = e;
  394. } catch (SecurityException e) {
  395. thrown = e;
  396. } catch (NoSuchMethodException e) {
  397. thrown = e;
  398. } catch (IllegalArgumentException e) {
  399. thrown = e;
  400. } catch (IllegalAccessException e) {
  401. thrown = e;
  402. } catch (InvocationTargetException e) {
  403. thrown = e;
  404. }
  405. if (null != thrown) {
  406. if (ignoreError) {
  407. System.err.println("Error loading " + sourceName);
  408. thrown.printStackTrace(System.err);
  409. } else {
  410. errorSuite(sink, sourceName, thrown);
  411. }
  412. }
  413. }
  414. private static void errorSuite(TestSuite sink, String sourceName,
  415. Throwable thrown) {
  416. sink.addTest(new ErrorTest(sourceName, thrown));
  417. }
  418. private static void errorSuite(TestSuite sink, String sourceName, String err) {
  419. String message = "bad " + sourceName + ": " + err;
  420. sink.addTest(new ErrorTest(message));
  421. }
  422. /**
  423. * Junit test failure, e.g., to report suite initialization errors at test time.
  424. */
  425. public static class ErrorTest implements Test {
  426. private final Throwable thrown;
  427. public ErrorTest(Throwable thrown) {
  428. this.thrown = thrown;
  429. }
  430. public ErrorTest(String message) {
  431. this.thrown = new Error(message);
  432. }
  433. public ErrorTest(String message, Throwable thrown) {
  434. this(new TestError(message, thrown));
  435. }
  436. public int countTestCases() {
  437. return 1;
  438. }
  439. public void run(TestResult result) {
  440. result.startTest(this);
  441. result.addError(this, thrown);
  442. }
  443. }
  444. /**
  445. * Nested exception - remove when using 1.4 or later.
  446. */
  447. public static class TestError extends Error {
  448. private Throwable thrown;
  449. public TestError(String message) {
  450. super(message);
  451. }
  452. public TestError(String message, Throwable thrown) {
  453. super(message);
  454. this.thrown = thrown;
  455. }
  456. public Throwable getCause() {
  457. return thrown;
  458. }
  459. public void printStackTrace() {
  460. printStackTrace(System.err);
  461. }
  462. public void printStackTrace(PrintStream ps) {
  463. printStackTrace(new PrintWriter(ps));
  464. }
  465. public void printStackTrace(PrintWriter pw) {
  466. super.printStackTrace(pw);
  467. if (null != thrown) {
  468. pw.print("Caused by: ");
  469. thrown.printStackTrace(pw);
  470. }
  471. }
  472. }
  473. /** component that reduces file to CanonicalLine[] */
  474. public static interface ILineator {
  475. /** Lineator suitable for text files */
  476. public static final ILineator TEXT = new TextLineator();
  477. /** Lineator suitable for class files (disassembles first) */
  478. public static final ILineator CLASS = new ClassLineator();
  479. /**
  480. * Reduce file to CanonicalLine[].
  481. *
  482. * @param handler
  483. * the IMessageHandler for errors (may be null)
  484. * @param file
  485. * the File to render
  486. * @param basedir
  487. * the File for the base directory (may be null)
  488. * @return CanonicalLine[] of lines - not null, but perhaps empty
  489. */
  490. CanonicalLine[] getLines(IMessageHandler handler, File file,
  491. File basedir) throws IOException;
  492. }
  493. /** alias for jdiff FileLine to avoid client binding */
  494. public static class CanonicalLine {
  495. public static final CanonicalLine[] NO_LINES = new CanonicalLine[0];
  496. /** canonical variant of line for comparison */
  497. public final String canonical;
  498. /** actual line, for logging */
  499. public final String line;
  500. public CanonicalLine(String canonical, String line) {
  501. this.canonical = canonical;
  502. this.line = line;
  503. }
  504. public String toString() {
  505. return line;
  506. }
  507. }
  508. private abstract static class Lineator implements ILineator {
  509. /**
  510. * Reduce file to CanonicalLine[].
  511. *
  512. * @param handler
  513. * the IMessageHandler for errors (may be null)
  514. * @param file
  515. * the File to render
  516. * @param basedir
  517. * the File for the base directory (may be null)
  518. */
  519. public CanonicalLine[] getLines(IMessageHandler handler, File file,
  520. File basedir) throws IOException {
  521. if (!file.canRead() || !file.isFile()) {
  522. MessageUtil.error(handler, "not readable file: " + basedir
  523. + " - " + file);
  524. return null;
  525. }
  526. // capture file as FileLine[]
  527. InputStream in = null;
  528. /* String path = */FileUtil.normalizedPath(file, basedir);
  529. LineStream capture = new LineStream();
  530. try {
  531. lineate(capture, handler, basedir, file);
  532. } catch (IOException e) {
  533. MessageUtil
  534. .fail(handler,
  535. "NormalizedCompareFiles IOException reading "
  536. + file, e);
  537. return null;
  538. } finally {
  539. if (null != in) {
  540. try {
  541. in.close();
  542. } catch (IOException e) {
  543. } // ignore
  544. }
  545. capture.flush();
  546. capture.close();
  547. }
  548. String missed = capture.getMissed();
  549. if (!LangUtil.isEmpty(missed)) {
  550. MessageUtil.warn(handler,
  551. "NormalizedCompareFiles missed input: " + missed);
  552. return null;
  553. } else {
  554. String[] lines = capture.getLines();
  555. CanonicalLine[] result = new CanonicalLine[lines.length];
  556. for (int i = 0; i < lines.length; i++) {
  557. result[i] = new CanonicalLine(lines[i], lines[i]);
  558. }
  559. return result;
  560. }
  561. }
  562. protected abstract void lineate(PrintStream sink,
  563. IMessageHandler handler, File basedir, File file)
  564. throws IOException;
  565. }
  566. private static class TextLineator extends Lineator {
  567. protected void lineate(PrintStream sink, IMessageHandler handler,
  568. File basedir, File file) throws IOException {
  569. InputStream in = null;
  570. try {
  571. in = new FileInputStream(file);
  572. FileUtil.copyStream(new DataInputStream(in), sink);
  573. } finally {
  574. try {
  575. in.close();
  576. } catch (IOException e) {
  577. } // ignore
  578. }
  579. }
  580. }
  581. public static class ClassLineator extends Lineator {
  582. protected void lineate(PrintStream sink, IMessageHandler handler,
  583. File basedir, File file) throws IOException {
  584. String name = FileUtil.fileToClassName(basedir, file);
  585. // XXX re-enable preflight?
  586. // if ((null != basedir) && (path.length()-6 != name.length())) {
  587. // MessageUtil.error(handler, "unexpected class name \""
  588. // + name + "\" for path " + path);
  589. // return null;
  590. // }
  591. disassemble(handler, basedir, name, sink);
  592. }
  593. public static boolean haveDisassembler() {
  594. try {
  595. return (null != Class
  596. .forName("org.aspectj.weaver.bcel.LazyClassGen"));
  597. } catch (ClassNotFoundException e) {
  598. // XXX fix
  599. // System.err.println(e.getMessage());
  600. // e.printStackTrace(System.err);
  601. return false;
  602. }
  603. }
  604. /** XXX dependency on bcweaver/bcel */
  605. private static void disassemble(IMessageHandler handler, File basedir,
  606. String name, PrintStream out) throws IOException {
  607. // LazyClassGen.disassemble(FileUtil.normalizedPath(basedir), name,
  608. // capture);
  609. Throwable thrown = null;
  610. String basedirPath = FileUtil.normalizedPath(basedir);
  611. // XXX use reflection utilities to invoke dissassembler?
  612. try {
  613. // XXX need test to detect when this is refactored
  614. Class c = Class.forName("org.aspectj.weaver.bcel.LazyClassGen");
  615. Method m = c.getMethod("disassemble", new Class[] {
  616. String.class, String.class, PrintStream.class });
  617. m.invoke(null, new Object[] { basedirPath, name, out });
  618. } catch (ClassNotFoundException e) {
  619. thrown = e;
  620. } catch (NoSuchMethodException e) {
  621. thrown = e;
  622. } catch (IllegalAccessException e) {
  623. thrown = e;
  624. } catch (InvocationTargetException e) {
  625. Throwable t = e.getTargetException();
  626. if (t instanceof IOException) {
  627. throw (IOException) t;
  628. }
  629. thrown = t;
  630. }
  631. if (null != thrown) {
  632. MessageUtil.fail(handler, "disassembling " + name + " path: "
  633. + basedirPath, thrown);
  634. }
  635. }
  636. }
  637. /**
  638. * Capture PrintStream output to String[] (delimiting component String on
  639. * println()), also showing any missed text.
  640. */
  641. public static class LineStream extends PrintStream {
  642. StringBuffer sb = new StringBuffer();
  643. ByteArrayOutputStream missed;
  644. ArrayList sink;
  645. public LineStream() {
  646. super(new ByteArrayOutputStream());
  647. this.sink = new ArrayList();
  648. missed = (ByteArrayOutputStream) out;
  649. }
  650. /** @return any text not captured by our overrides */
  651. public String getMissed() {
  652. return missed.toString();
  653. }
  654. /** clear captured lines (but not missed text) */
  655. public void clear() {
  656. sink.clear();
  657. }
  658. /**
  659. * Get String[] of lines printed, delimited by println(..) calls.
  660. *
  661. * @return lines printed, exclusive of any not yet terminated by newline
  662. */
  663. public String[] getLines() {
  664. return (String[]) sink.toArray(new String[0]);
  665. }
  666. // ---------- PrintStream overrides
  667. public void println(Object x) {
  668. println(x.toString());
  669. }
  670. public void print(Object obj) {
  671. print(obj.toString());
  672. }
  673. public void println(char c) {
  674. sb.append(c);
  675. println();
  676. }
  677. public void println(char[] c) {
  678. sb.append(c);
  679. println();
  680. }
  681. public void print(char c) {
  682. sb.append(c);
  683. }
  684. public void print(char[] c) {
  685. sb.append(c);
  686. }
  687. public void println(String s) {
  688. print(s);
  689. println();
  690. }
  691. public void print(String s) {
  692. sb.append(s);
  693. }
  694. public void println() {
  695. String line = sb.toString();
  696. sink.add(line);
  697. sb.setLength(0);
  698. }
  699. }
  700. }