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.

Ajdoc.java 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  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.CompilerErrors;
  24. import org.aspectj.compiler.base.ExitRequestException;
  25. import org.aspectj.compiler.base.InternalCompilerError;
  26. import com.sun.javadoc.RootDoc;
  27. import java.io.FileNotFoundException;
  28. import java.io.IOException;
  29. import java.io.PrintWriter;
  30. import java.util.ArrayList;
  31. import java.util.List;
  32. /**
  33. * Front-end for ajdoc.
  34. *
  35. */
  36. public class Ajdoc extends AjdocCompiler {
  37. private final static int VERBOSE = 1;
  38. private final static int WARNINGS = 4;
  39. private DocletProxy docletProxy;
  40. private RootDocMaker rootDocMaker;
  41. private String source = null;
  42. private String extdirs = null;
  43. private String locale = null;
  44. private String encoding = null;
  45. private String sourcepath = null;
  46. private String classpath = null;
  47. private String bootclasspath = null;
  48. private int verbosity = WARNINGS;
  49. private List filenamesAndPackages = new ArrayList();
  50. private List options = new ArrayList();
  51. public Ajdoc(String programName) {
  52. super(programName);
  53. }
  54. public Ajdoc() {
  55. this("ajdoc");
  56. }
  57. /**
  58. * Programmatic entry into this compiler that
  59. * uses the error printer to catch internal errors.
  60. *
  61. * @param args Command line arguments.
  62. * @return <code>0</code> for a successful document.
  63. */
  64. public int execute(String[] args) {
  65. try {
  66. return doc(args) && err.getNumErrors() == 0 ? 0 : 1;
  67. } catch (ExitRequestException exit) {
  68. return exit.getValue();
  69. } catch (CompilerErrors err) {
  70. return err.errors; // report error already printed by ajc
  71. } catch (InternalCompilerError error) { // cf ajc.Main
  72. handleInternalError(error.uncaughtThrowable);
  73. error.showWhere(new PrintWriter(System.err));
  74. return -2;
  75. } catch (Exception e) {
  76. e.printStackTrace(System.err);
  77. err.internalError("internal_msg", e);
  78. return 1;
  79. }
  80. }
  81. /** copied from ajc.Main */
  82. public void handleInternalError(Throwable uncaughtThrowable) {
  83. System.err.println("An internal error occured in Ajdoc invocation of AspectJ-"
  84. +getCompiler().getVersion());
  85. System.err.println(uncaughtThrowable.toString());
  86. uncaughtThrowable.printStackTrace(System.err);
  87. System.err.println();
  88. }
  89. /**
  90. * Programmatic entry into this compiler that
  91. * doesn't use the error printer to catch internal errors.
  92. *
  93. * @param args Command line arguments.
  94. * @return <code>true</code> for a succesful run.
  95. */
  96. public boolean doc(String[] args) {
  97. long start = System.currentTimeMillis();
  98. if (args == null) {
  99. err.error("internal_error", "Arguments cannot be null");
  100. return false;
  101. }
  102. try {
  103. args = expandAndCreateDoclet(args);
  104. } catch (FileNotFoundException e) {
  105. err.error("file_not_found_exception", e.getMessage());
  106. return false;
  107. } catch (IOException e) {
  108. err.error("io_exception", e.getMessage());
  109. return false;
  110. }
  111. for (int i = 0; i < args.length;) {
  112. String arg = args[i++];
  113. if (arg.equals("-overview")) {
  114. set(args, i++);
  115. } else if (arg.equals("-public")) {
  116. set(arg);
  117. if (filter != null) {
  118. err.error("argument_already_seen", arg);
  119. } else {
  120. setFilter(AccessChecker.PUBLIC, arg);
  121. }
  122. } else if (arg.equals("-protected")) {
  123. set(arg);
  124. if (filter != null) {
  125. err.error("argument_already_seen", arg);
  126. } else {
  127. setFilter(AccessChecker.PROTECTED, arg);
  128. }
  129. } else if (arg.equals("-package")) {
  130. set(arg);
  131. if (filter != null) {
  132. err.error("argument_already_seen", arg);
  133. } else {
  134. setFilter(AccessChecker.PACKAGE, arg);
  135. }
  136. } else if (arg.equals("-private")) {
  137. set(arg);
  138. if (filter != null) {
  139. err.error("argument_already_seen", arg);
  140. } else {
  141. setFilter(AccessChecker.PRIVATE, arg);
  142. }
  143. } else if (arg.equals("-help")) {
  144. usage(0);
  145. } else if (arg.equals("-sourcepath")) {
  146. if (sourcepath != null) {
  147. usage("argument_already_seen", arg);
  148. }
  149. sourcepath = set(args, i++);
  150. }else if (arg.equals("-classpath")) {
  151. if (classpath != null) {
  152. usage("argument_already_seen", arg);
  153. }
  154. classpath = set(args, i++);
  155. }else if (arg.equals("-bootclasspath")) {
  156. if (bootclasspath != null) {
  157. usage("argument_already_seen", arg);
  158. }
  159. bootclasspath = set(args, i++);
  160. }else if (arg.equals("-source")) {
  161. if (source != null) {
  162. usage("argument_already_seen", arg);
  163. }
  164. source = set(args, i++);
  165. } else if (arg.equals("-extdirs")) {
  166. if (extdirs != null) {
  167. usage("argument_already_seen", arg);
  168. }
  169. extdirs = set(args, i++);
  170. } else if (arg.equals("-verbose")) {
  171. set(arg);
  172. verbosity |= VERBOSE;
  173. } else if (arg.equals("-locale")) {
  174. if (locale != null) {
  175. usage("argument_already_seen", arg);
  176. }
  177. set(args, i++);
  178. } else if (arg.equals("-encoding")) {
  179. if (encoding != null) {
  180. usage("argument_already_seen", arg);
  181. }
  182. encoding = set(args, i++);
  183. } else if (arg.equals("-compiler")) {
  184. err.warning("usage_help", "-compiler option ignored");
  185. } else if (arg.equals("-debug")) {
  186. err.warning("usage_help", "-debug option disabled");
  187. } else if (arg.startsWith("-J")) { // todo unsupported?
  188. if (arg.length() == 2) continue;
  189. String rest = arg.substring(2);
  190. int ieq = rest.indexOf('=');
  191. String key, val;
  192. if (ieq != -1) {
  193. key = rest.substring(0, ieq);
  194. val = rest.substring(ieq+1);
  195. } else {
  196. key = rest;
  197. val = "";
  198. }
  199. System.setProperty(key, val);
  200. } else if (arg.startsWith("-")) {
  201. int optionLength = docletProxy.optionLength(arg);
  202. if (optionLength < 0) {
  203. exit();
  204. } else if (optionLength == 0) {
  205. usage("invalid_flag", arg);
  206. } else if (optionLength > args.length) {
  207. usage("requires_argument", arg, optionLength+"");
  208. } else {
  209. List iargs = new ArrayList();
  210. iargs.add(arg);
  211. for (int j = 0; j < optionLength-1; j++) {
  212. iargs.add(args[i++]);
  213. }
  214. set((String[])iargs.toArray(new String[iargs.size()]));
  215. }
  216. } else {
  217. filenamesAndPackages.add(arg);
  218. }
  219. }
  220. if (locale == null) {
  221. locale = "";
  222. }
  223. if (sourcepath == null) {
  224. sourcepath = ".";
  225. }
  226. try {
  227. if (!docletProxy.validOptions(options, err)) {
  228. exit(1);
  229. }
  230. } catch (IOException e) {
  231. e.printStackTrace(System.err);
  232. err.internalError("internal_msg", e);
  233. return false;
  234. }
  235. if (filenamesAndPackages.size() < 1) {
  236. usage("No_packages_or_classes_specified");
  237. return false;
  238. }
  239. RootDoc rootDoc = null;
  240. boolean good = true;
  241. try {
  242. rootDoc = makeRootDoc(sourcepath,
  243. classpath,
  244. bootclasspath,
  245. extdirs,
  246. verbosity,
  247. encoding,
  248. locale,
  249. source,
  250. filenamesAndPackages,
  251. options,
  252. err,
  253. programName,
  254. getFilter());
  255. } catch (CannotMakeRootDocException e) {
  256. err.error("cant_create_root_doc_ex", "AjdocCompiler", e.getMessage());
  257. exit(1);
  258. good = false;
  259. }
  260. good &= rootDoc != null;
  261. if (good) {
  262. err.notice("generating_docs");
  263. try {
  264. good &= docletProxy.start(rootDoc);
  265. } catch (IOException e) {
  266. e.printStackTrace(System.err);
  267. err.internalError("internal_msg", e);
  268. return false;
  269. }
  270. }
  271. if ((verbosity & VERBOSE) != 0) {
  272. err.notice("done_in", Long.toString(System.currentTimeMillis()-start));
  273. }
  274. return good;
  275. }
  276. private void usage(String key, String s0, String s1) {
  277. err.error(key, s0, s1);
  278. usage(1);
  279. }
  280. private void usage(String key, String s0) {
  281. usage(key,s0,"");
  282. }
  283. private void usage(String key) {
  284. usage(key,"");
  285. }
  286. private void usage() {
  287. err.notice("usage_help", programName);
  288. if (docletProxy != null) { docletProxy.optionLength("-help"); }
  289. }
  290. private void usage(int exit) {
  291. usage();
  292. exit(exit);
  293. }
  294. protected String[] expandAndCreateDoclet(String[] args) throws IOException {
  295. List list = new ArrayList();
  296. docletProxy = DocletProxy.DEFAULT;
  297. for (int i = 0; i < args.length;) {
  298. String arg = args[i++];
  299. if (arg == null || arg.length() < 1) continue;
  300. if (arg.charAt(0) == '@') {
  301. expandAtFile(arg.substring(1), list);
  302. } else if (arg.equals("-argfile")) {
  303. if (check(args, i)) {
  304. expandAtFile(args[i++], list);
  305. }
  306. } else if (arg.equals("-doclet")) {
  307. err.warning("usage_help", "-doclet option ignored");
  308. } else if (arg.equals("-docletpath")) {
  309. err.warning("usage_help", "-docletpath option ignored");
  310. } else if (arg.equals("-standard")) {
  311. docletProxy = DocletProxy.STANDARD;
  312. } else {
  313. list.add(arg);
  314. }
  315. }
  316. return (String[])list.toArray(new String[list.size()]);
  317. }
  318. private static boolean check(String[] args, int i) {
  319. return i >= 0 && i < args.length;
  320. }
  321. private String set(String[] args, int i) {
  322. String arg = null;
  323. if (check(args, i)) {
  324. arg = args[i];
  325. set(args[i-1], arg);
  326. } else {
  327. err.internalError("internal_error",
  328. new ArrayIndexOutOfBoundsException(i));
  329. }
  330. return arg;
  331. }
  332. private void set(String opt) {
  333. set(new String[]{opt});
  334. }
  335. private void set(String opt, String arg) {
  336. set(new String[]{opt, arg});
  337. }
  338. private void set(String[] opts) {
  339. options.add(opts);
  340. }
  341. protected void internalCompile(List filenames) {
  342. super.internalCompile(filenames);
  343. }
  344. private final void exit() {
  345. exit(0);
  346. }
  347. private void exit(int exit) {
  348. throw new ExitRequestException(exit);
  349. }
  350. private static String classname(Object o) {
  351. return o != null ? o.getClass().getName() : "null";
  352. }
  353. }