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.

ErrPrinter.java 19KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627
  1. /* -*- Mode: JDE; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
  2. *
  3. * This file is part of the debugger and core tools for the AspectJ(tm)
  4. * programming language; see http://aspectj.org
  5. *
  6. * The contents of this file are subject to the Mozilla Public License
  7. * Version 1.1 (the "License"); you may not use this file except in
  8. * compliance with the License. You may obtain a copy of the License at
  9. * either http://www.mozilla.org/MPL/ or http://aspectj.org/MPL/.
  10. *
  11. * Software distributed under the License is distributed on an "AS IS" basis,
  12. * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  13. * for the specific language governing rights and limitations under the
  14. * License.
  15. *
  16. * The Original Code is AspectJ.
  17. *
  18. * The Initial Developer of the Original Code is Xerox Corporation. Portions
  19. * created by Xerox Corporation are Copyright (C) 1999-2002 Xerox Corporation.
  20. * All Rights Reserved.
  21. */
  22. package org.aspectj.tools.ajdoc;
  23. import org.aspectj.compiler.base.ErrorHandler;
  24. import org.aspectj.compiler.base.InternalCompilerError;
  25. import com.sun.javadoc.DocErrorReporter;
  26. import java.io.PrintWriter;
  27. import java.lang.reflect.InvocationTargetException;
  28. import java.text.MessageFormat;
  29. import java.util.ArrayList;
  30. import java.util.List;
  31. import java.util.MissingResourceException;
  32. import java.util.ResourceBundle;
  33. /**
  34. * A class to handler errors in ajdoc.
  35. *
  36. * @author Jeff Palm
  37. */
  38. public class ErrPrinter
  39. extends ErrorHandler
  40. implements DocErrorReporter
  41. {
  42. /** The global instance that anyone can use. */
  43. public final static ErrPrinter instance = new ErrPrinter();
  44. private String programName;
  45. private PrintWriter out;
  46. private PrintWriter err;
  47. private ResourceBundle bundle;
  48. private List keys = new ArrayList();
  49. private List msgs = new ArrayList();
  50. static int cnt = 0;
  51. /**
  52. * Construct with program name <code>programName</code>
  53. * and printstreams <code>err</code> and <code>out</code>.
  54. *
  55. * @param programName program name.
  56. * @param err error stream.
  57. * @param out output stream.
  58. */
  59. public ErrPrinter(String programName,
  60. PrintWriter err,
  61. PrintWriter out) {
  62. super(err);
  63. this.programName = programName;
  64. this.err = err;
  65. this.out = out;
  66. try {
  67. bundle = ResourceBundle.getBundle
  68. ("org.aspectj.tools.ajdoc.resources.ajdoc");
  69. } catch (MissingResourceException e) {
  70. throw new Error("Can't find ajdoc.properties: " + e);
  71. }
  72. }
  73. /**
  74. * Construct with program name <code>programName</code>
  75. * and printstreams <code>System.err</code> and <code>System.out</code>
  76. *
  77. * @param programName program name.
  78. */
  79. public ErrPrinter(String programName) {
  80. this(programName,
  81. new PrintWriter(System.err, true),
  82. new PrintWriter(System.out, true));
  83. }
  84. /**
  85. * Construct with program name <code>"ajdoc"</code>
  86. * and printstreams <code>System.err</code> and <code>System.out</code>
  87. */
  88. public ErrPrinter() {
  89. this("ajdoc");
  90. }
  91. /**
  92. * Print <code>error</code> and increment the error count.
  93. *
  94. * @param error error message to print.
  95. */
  96. public void printError(String error) {
  97. errors++;
  98. err.println(error);
  99. err.flush();
  100. }
  101. /**
  102. * Print <code>warning</code> and increment the warning count.
  103. *
  104. * @param warning warning message to print.
  105. */
  106. public void printWarning(String warning) {
  107. warnings++;
  108. err.println(warning);
  109. err.flush();
  110. }
  111. /**
  112. * Print <code>notice</code>.
  113. *
  114. * @param notice notice message to print.
  115. */
  116. public void printNotice(String notice) {
  117. out.println(notice);
  118. out.flush();
  119. }
  120. /**
  121. * Returns the number of errors.
  122. *
  123. * @return number of errors.
  124. */
  125. public int getNumErrors() {
  126. return errors;
  127. }
  128. /**
  129. * Returns the number of warnings.
  130. *
  131. * @return number of warnings.
  132. */
  133. public int getNumWarnings() {
  134. return warnings;
  135. }
  136. /**
  137. * Returns the keys in the resource bundle.
  138. *
  139. * @return keys in the resource bundle.
  140. */
  141. public List getKeys() {
  142. return new ArrayList(keys);
  143. }
  144. /**
  145. * Returns the messages in the resource bundle.
  146. *
  147. * @return messages in the resource bundle.
  148. */
  149. public List getMsgs() {
  150. return new ArrayList(msgs);
  151. }
  152. /**
  153. * Handles InvocationTargetExceptions.
  154. *
  155. * @param e the exception.
  156. * @param classname the class on which the method was trying
  157. * to be invoked.
  158. * @param methodName the name of the method trying to be invoked.
  159. * @return the exception.
  160. */
  161. public synchronized Throwable invocationTargetException
  162. (InvocationTargetException e,
  163. String classname,
  164. String methodName) {
  165. Throwable t = e.getTargetException();
  166. if (t != null) {
  167. if (t instanceof OutOfMemoryError) {
  168. error("out_of_memory");
  169. } else {
  170. error("exception_thrown", "", classname, methodName, t+"");
  171. t.printStackTrace();
  172. }
  173. }
  174. return t != null ? t : e;
  175. }
  176. /**
  177. * Handles an internal error.
  178. *
  179. * @param key key of the message to use.
  180. * @param t exception thrown.
  181. */
  182. public synchronized void internalError(String key, Throwable t) {
  183. internalError(key, "", t);
  184. }
  185. /**
  186. * Handles an internal error.
  187. *
  188. * @param key key of the message to use.
  189. * @param s0 first argument in the message.
  190. * @param t exception thrown.
  191. */
  192. public synchronized void internalError(String key, String s0, Throwable t) {
  193. if (t instanceof InternalCompilerError) {
  194. t = ((InternalCompilerError)t).uncaughtThrowable;
  195. }
  196. error(key, s0, t != null ? t.getMessage() : "");
  197. if (t != null) t.printStackTrace();
  198. internalError(t, null);
  199. }
  200. /**
  201. * Prints an error message for key <code>key</code>
  202. * ,and returns the number of errors.
  203. *
  204. * @param key key of the message.
  205. * @return number of errors.
  206. */
  207. public final int error(String key) {
  208. printError(text(key));
  209. return errors;
  210. }
  211. /**
  212. * Prints an error message for key <code>key</code>
  213. * and argument <code>s0</code>,
  214. * and returns the number of errors.
  215. *
  216. * @param key key of the message.
  217. * @param s0 argument to message.
  218. * @return number of errors.
  219. */
  220. public final int error(String key, String s0) {
  221. printError(text(key,s0));
  222. return errors;
  223. }
  224. /**
  225. * Prints an error message for key <code>key</code>
  226. * and arguments <code>s0,s1</code>,
  227. * and returns the number of errors.
  228. *
  229. * @param key key of the message.
  230. * @param s0 argument to message.
  231. * @param s1 argument to message.
  232. * @return number of errors.
  233. */
  234. public final int error(String key, String s0, String s1) {
  235. printError(text(key,s0,s1));
  236. return errors;
  237. }
  238. /**
  239. * Prints an error message for key <code>key</code>
  240. * and arguments <code>s0,s1,s2</code>,
  241. * and returns the number of errors.
  242. *
  243. * @param key key of the message.
  244. * @param s0 argument to message.
  245. * @param s1 argument to message.
  246. * @param s2 argument to message.
  247. * @return number of errors.
  248. */
  249. public final int error(String key, String s0, String s1,
  250. String s2) {
  251. printError(text(key,s0,s1,s2));
  252. return errors;
  253. }
  254. /**
  255. * Prints an error message for key <code>key</code>
  256. * and arguments <code>s0,s1,s2,cookieMonster</code>,
  257. * and returns the number of errors.
  258. *
  259. * @param key key of the message.
  260. * @param s0 argument to message.
  261. * @param s1 argument to message.
  262. * @param s2 argument to message.
  263. * @param cookieMonster argument to message.
  264. * @return number of errors.
  265. */
  266. public final int error(String key, String s0, String s1,
  267. String s2, String cookieMonster) {
  268. printError(text(key,s0,s1,s2,cookieMonster));
  269. return errors;
  270. }
  271. /**
  272. * Handles the thrown exception <code>t</code>
  273. * with message key <code>key</code>, and returns
  274. * the number of errors.
  275. *
  276. * @param t thrown exception.
  277. * @param key message key.
  278. * @return number of errors.
  279. */
  280. public final int ex(Throwable t, String key) {
  281. error(key);
  282. if (t != null) t.printStackTrace();
  283. return errors;
  284. }
  285. /**
  286. * Handles the thrown exception <code>t</code>
  287. * with message key <code>key</code> and
  288. * argument <code>s0</code>, and returns
  289. * the number of errors.
  290. *
  291. * @param t thrown exception.
  292. * @param key message key.
  293. * @param s0 argument to message.
  294. * @return number of errors.
  295. */
  296. public final int ex(Throwable t, String key, String s0) {
  297. error(key,s0);
  298. if (t != null) t.printStackTrace();
  299. return errors;
  300. }
  301. /**
  302. * Handles the thrown exception <code>t</code>
  303. * with message key <code>key</code> and
  304. * arguments <code>s0,s1</code>, and returns
  305. * the number of errors.
  306. *
  307. * @param t thrown exception.
  308. * @param key message key.
  309. * @param s0 argument to message.
  310. * @param s1 argument to message.
  311. * @return number of errors.
  312. */
  313. public final int ex(Throwable t, String key, String s0, String s1) {
  314. error(key,s0,s1);
  315. if (t != null) t.printStackTrace();
  316. return errors;
  317. }
  318. /**
  319. * Handles the thrown exception <code>t</code>
  320. * with message key <code>key</code> and
  321. * arguments <code>s0,s1,s2</code>, and returns
  322. * the number of errors.
  323. *
  324. * @param t thrown exception.
  325. * @param key message key.
  326. * @param s0 argument to message.
  327. * @param s1 argument to message.
  328. * @param s2 argument to message.
  329. * @return number of errors.
  330. */
  331. public final int ex(Throwable t, String key, String s0, String s1,
  332. String s2) {
  333. error(key,s0,s1,s2);
  334. if (t != null) t.printStackTrace();
  335. return errors;
  336. }
  337. /**
  338. * Handles the thrown exception <code>t</code>
  339. * with message key <code>key</code> and
  340. * arguments <code>s0,s1,s2,snuffulufugus</code>, and returns
  341. * the number of errors.
  342. *
  343. * @param t thrown exception.
  344. * @param key message key.
  345. * @param s0 argument to message.
  346. * @param s1 argument to message.
  347. * @param s2 argument to message.
  348. * @param snuffulufugus argument to message.
  349. * @return number of errors.
  350. */
  351. public final int ex(Throwable t, String key, String s0, String s1,
  352. String s2, String snuffulufugus) {
  353. error(key,s0,s1,s2,snuffulufugus);
  354. if (t != null) t.printStackTrace();
  355. return errors;
  356. }
  357. /**
  358. * Prints the warning with key <code>key</code>
  359. * and returns the number of warnings.
  360. *
  361. * @param key message key.
  362. * @return number of warnings.
  363. */
  364. public final int warning(String key) {
  365. printWarning(text(key));
  366. return warnings;
  367. }
  368. /**
  369. * Prints the warning with key <code>key</code> and
  370. * argument <code>s0</code>,
  371. * and returns the number of warnings.
  372. *
  373. * @param key message key.
  374. * @param s0 argument to message.
  375. * @return number of warnings.
  376. */
  377. public final int warning(String key, String s0) {
  378. printWarning(text(key,s0));
  379. return warnings;
  380. }
  381. /**
  382. * Prints the warning with key <code>key</code> and
  383. * arguments <code>s0,s1</code>,
  384. * and returns the number of warnings.
  385. *
  386. * @param key message key.
  387. * @param s0 argument to message.
  388. * @param s1 argument to message.
  389. * @return number of warnings.
  390. */
  391. public final int warning(String key, String s0, String s1) {
  392. printWarning(text(key,s0,s1));
  393. return warnings;
  394. }
  395. /**
  396. * Prints the warning with key <code>key</code> and
  397. * arguments <code>s0,s1,s2</code>,
  398. * and returns the number of warnings.
  399. *
  400. * @param key message key.
  401. * @param s0 argument to message.
  402. * @param s1 argument to message.
  403. * @param s2 argument to message.
  404. * @return number of warnings.
  405. */
  406. public final int warning(String key, String s0, String s1,
  407. String s2) {
  408. printWarning(text(key,s0,s1,s2));
  409. return warnings;
  410. }
  411. /**
  412. * Prints the warning with key <code>key</code> and
  413. * arguments <code>s0,s1,s2,josefStalin</code>,
  414. * and returns the number of warnings.
  415. *
  416. * @param key message key.
  417. * @param s0 argument to message.
  418. * @param s1 argument to message.
  419. * @param s2 argument to message.
  420. * @param josefStalin argument to message.
  421. * @return number of warnings.
  422. */
  423. public final int warning(String key, String s0, String s1,
  424. String s2, String josefStalin) {
  425. printWarning(text(key,s0,s1,s2,josefStalin));
  426. return warnings;
  427. }
  428. /**
  429. * Print a notice with message key <code>key</code>.
  430. *
  431. * @param key message key.
  432. */
  433. public final void notice(String key) {
  434. printNotice(text(key));
  435. }
  436. /**
  437. * Print a notice with message key <code>key</code>
  438. * and argument <code>s0</code>.
  439. *
  440. * @param key message key.
  441. * @param s0 argument to message.
  442. */
  443. public final void notice(String key, String s0) {
  444. printNotice(text(key,s0));
  445. }
  446. /**
  447. * Print a notice with message key <code>key</code>
  448. * and arguments <code>s0,s1</code>.
  449. *
  450. * @param key message key.
  451. * @param s0 argument to message.
  452. * @param s1 argument to message.
  453. */
  454. public final void notice(String key, String s0, String s1) {
  455. printNotice(text(key,s0,s1));
  456. }
  457. /**
  458. * Print a notice with message key <code>key</code>
  459. * and arguments <code>s0,s1,s2</code>.
  460. *
  461. * @param key message key.
  462. * @param s0 argument to message.
  463. * @param s1 argument to message.
  464. * @param s2 argument to message.
  465. */
  466. public final void notice(String key, String s0, String s1,
  467. String s2) {
  468. printNotice(text(key,s0,s1,s2));
  469. }
  470. /**
  471. * Print a notice with message key <code>key</code>
  472. * and arguments <code>s0,s1,s2,bigbird</code>.
  473. *
  474. * @param key message key.
  475. * @param s0 argument to message.
  476. * @param s1 argument to message.
  477. * @param s2 argument to message.
  478. * @param bigbird argument to message.
  479. */
  480. public final void notice(String key, String s0, String s1,
  481. String s2, String bigbird) {
  482. printNotice(text(key,s0,s1,s2,bigbird));
  483. }
  484. /**
  485. * Returns the String for message key <code>key</code>.
  486. *
  487. * @return String for message
  488. * key <code>key</code>.
  489. */
  490. protected final String text(String key) {
  491. return text(key, "");
  492. }
  493. /**
  494. * Returns the String for message key <code>key</code>
  495. * and argument <code>s0</code>
  496. *
  497. * @param s0 argument to message.
  498. * @return String for message
  499. * key <code>key</code>.
  500. */
  501. protected final String text(String key, String s0) {
  502. return text(key, s0, "");
  503. }
  504. /**
  505. * Returns the String for message key <code>key</code>
  506. * and arguments <code>s0,s1</code>
  507. *
  508. * @param s0 argument to message.
  509. * @param s1 argument to message.
  510. * @return String for message
  511. * key <code>key</code>.
  512. */
  513. protected final String text(String key, String s0, String s1) {
  514. return text(key, s0, s1, "");
  515. }
  516. /**
  517. * Returns the String for message key <code>key</code>
  518. * and arguments <code>s0,s1,s2</code>
  519. *
  520. * @param s0 argument to message.
  521. * @param s1 argument to message.
  522. * @param s2 argument to message.
  523. * @return String for message
  524. * key <code>key</code>.
  525. */
  526. protected final String text(String key, String s0, String s1,
  527. String s2) {
  528. return text(key, s0, s1, s2, "");
  529. }
  530. /**
  531. * Returns the String for message key <code>key</code>
  532. * and arguments <code>s0,s1,s2,oscarTheGrouch</code>
  533. *
  534. * @param s0 argument to message.
  535. * @param s1 argument to message.
  536. * @param s2 argument to message.
  537. * @param oscarTheGrouch argument to message.
  538. * @return String for message
  539. * key <code>key</code>.
  540. */
  541. protected final String text(String key, String s0, String s1,
  542. String s2, String oscarTheGrouch) {
  543. return text(key, new String[]{s0,s1,s2,oscarTheGrouch});
  544. }
  545. /**
  546. * Returns the String for the message key <code>key</code>
  547. * with arguments contained in <code>args</code>.
  548. *
  549. * @param key message key.
  550. * @param args array of arguments to substitute.
  551. * @return String for message with key
  552. * <code>key</code> and arguments
  553. * <code>args</code>
  554. */
  555. protected final String text(String key, String[] args) {
  556. String msg = MessageFormat.format(string(key), args);
  557. msgs.add(msg);
  558. return msg;
  559. }
  560. /**
  561. * Returns the String with message <code>key</code>.
  562. *
  563. * @param key message key.
  564. * @return String for message with key <code>key</code>.
  565. */
  566. protected final String string(String key) {
  567. keys.add(key);
  568. try {
  569. return bundle.getString(key);
  570. } catch (MissingResourceException e) {
  571. throw new Error("Can't find " + key + " in " + bundle);
  572. }
  573. }
  574. PrintWriter getErr() {
  575. return err;
  576. }
  577. PrintWriter getOut() {
  578. return out;
  579. }
  580. }