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.

Main.java 28KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843
  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 Eclipse Public License v 2.0
  7. * which accompanies this distribution and is available at
  8. * https://www.eclipse.org/org/documents/epl-2.0/EPL-2.0.txt
  9. *
  10. * Contributors:
  11. * Xerox/PARC initial implementation
  12. * Mik Kersten port to AspectJ 1.1+ code base
  13. * ******************************************************************/
  14. package org.aspectj.tools.ajdoc;
  15. import java.io.BufferedReader;
  16. import java.io.File;
  17. import java.io.FileFilter;
  18. import java.io.FileNotFoundException;
  19. import java.io.FileOutputStream;
  20. import java.io.FileReader;
  21. import java.io.FilenameFilter;
  22. import java.io.IOException;
  23. import java.util.ArrayList;
  24. import java.util.Arrays;
  25. import java.util.Collections;
  26. import java.util.Hashtable;
  27. import java.util.LinkedList;
  28. import java.util.List;
  29. import java.util.Locale;
  30. import java.util.StringTokenizer;
  31. import java.util.Vector;
  32. import org.aspectj.asm.AsmManager;
  33. import org.aspectj.bridge.IMessage;
  34. import org.aspectj.bridge.Version;
  35. import org.aspectj.util.FileUtil;
  36. import org.aspectj.util.LangUtil;
  37. /**
  38. * This is an old implementation of ajdoc that does not use an OO style. However, it does the job, and should serve to evolve a
  39. * lightweight ajdoc implementation until we can make a properly extended javadoc implementation.
  40. *
  41. * @author Mik Kersten
  42. */
  43. public class Main implements Config {
  44. private static final String FAIL_MESSAGE = "> compile failed, exiting ajdoc";
  45. /** Command line options. */
  46. static Vector<String> options;
  47. /** Options to pass to ajc. */
  48. static Vector<String> ajcOptions;
  49. /** All of the files to be processed by ajdoc. */
  50. static Vector<String> filenames;
  51. /** List of files to pass to javadoc. */
  52. static Vector<String> fileList;
  53. /** List of packages to pass to javadoc. */
  54. static Vector<String> packageList;
  55. /** Default to package visiblity. */
  56. static String docModifier = "package";
  57. static Vector<String> sourcepath;
  58. static boolean verboseMode = false;
  59. static boolean packageMode = false;
  60. static boolean authorStandardDocletSwitch = false;
  61. static boolean versionStandardDocletSwitch = false;
  62. static File rootDir = null;
  63. static Hashtable declIDTable = new Hashtable();
  64. static String docDir = ".";
  65. private static boolean deleteTempFilesOnExit = true;
  66. private static boolean aborted = false;
  67. private static IMessage[] errors;
  68. private static boolean shownAjdocUsageMessage = false;
  69. // creating a local variable to enable us to create the ajdocworkingdir
  70. // in a local sandbox during testing
  71. private static String outputWorkingDir = Config.WORKING_DIR;
  72. public static void clearState() {
  73. options = new Vector<>();
  74. ajcOptions = new Vector<>();
  75. filenames = new Vector<>();
  76. fileList = new Vector<>();
  77. packageList = new Vector<>();
  78. docModifier = "package";
  79. sourcepath = new Vector<>();
  80. verboseMode = false;
  81. packageMode = false;
  82. rootDir = null;
  83. declIDTable = new Hashtable();
  84. docDir = ".";
  85. aborted = false;
  86. deleteTempFilesOnExit = true;
  87. }
  88. public static void main(String[] args) {
  89. clearState();
  90. // STEP 1: parse the command line and do other global setup
  91. sourcepath.addElement("."); // add the current directory to the classapth
  92. parseCommandLine(args);
  93. rootDir = getRootDir();
  94. File[] inputFiles = new File[filenames.size()];
  95. File[] signatureFiles = new File[filenames.size()];
  96. try {
  97. // create the workingdir if it doesn't exist
  98. if (!(new File(outputWorkingDir).isDirectory())) {
  99. File dir = new File(outputWorkingDir);
  100. dir.mkdir();
  101. if (deleteTempFilesOnExit)
  102. dir.deleteOnExit();
  103. }
  104. for (int i = 0; i < filenames.size(); i++) {
  105. inputFiles[i] = new File(filenames.elementAt(i));
  106. }
  107. // PHASE 0: call ajc
  108. AsmManager model = callAjc(inputFiles);
  109. if (CompilerWrapper.hasErrors()) {
  110. System.out.println(FAIL_MESSAGE);
  111. aborted = true;
  112. errors = CompilerWrapper.getErrors();
  113. return;
  114. }
  115. for (int ii = 0; ii < filenames.size(); ii++) {
  116. signatureFiles[ii] = createSignatureFile(model, inputFiles[ii]);
  117. }
  118. // PHASE 1: generate Signature files (Java with DeclIDs and no bodies).
  119. System.out.println("> Building signature files...");
  120. try {
  121. StubFileGenerator.doFiles(model, declIDTable, inputFiles, signatureFiles);
  122. // Copy package.html and related files over
  123. packageHTML(model, inputFiles);
  124. } catch (DocException d) {
  125. System.err.println(d.getMessage());
  126. return;
  127. }
  128. // PHASE 2: let Javadoc generate HTML (with DeclIDs)
  129. callJavadoc(signatureFiles);
  130. // PHASE 3: add AspectDoc specific stuff to the HTML (and remove the DeclIDS).
  131. decorateHtmlFiles(model, inputFiles);
  132. System.out.println("> Finished.");
  133. } catch (Throwable e) {
  134. handleInternalError(e);
  135. // TODO: Is this really necessary? Why not just re-throw the exception after logging the error message?
  136. // This interrupts tests, making them exit hard, eg. stopping a whole Maven build somewhere in the middle.
  137. exit(-2);
  138. }
  139. }
  140. /**
  141. * Method to copy over any package.html files that may be part of the documentation so that javadoc generates the
  142. * package-summary properly.
  143. */
  144. private static void packageHTML(AsmManager model, File[] inputFiles) throws IOException {
  145. List<String> dirList = new ArrayList<>();
  146. for (File inputFile : inputFiles) {
  147. String packageName = StructureUtil.getPackageDeclarationFromFile(model, inputFile);
  148. // Only copy the package.html file once.
  149. if (dirList.contains(packageName))
  150. continue;
  151. // Check to see if there exist a package.html file for this package.
  152. String dir = inputFile.getAbsolutePath().substring(0, inputFile.getAbsolutePath().lastIndexOf(File.separator));
  153. File input = new File(dir + Config.DIR_SEP_CHAR + "package.html");
  154. File inDir = new File(dir + Config.DIR_SEP_CHAR + "doc-files");
  155. // If it does not exist lets go to the next package.
  156. if (!input.exists()) {
  157. dirList.add(packageName);
  158. continue;
  159. }
  160. String filename = "";
  161. String docFiles = "";
  162. if (packageName != null) {
  163. String pathName = outputWorkingDir + File.separator + packageName.replace('.', File.separatorChar);
  164. File packageDir = new File(pathName);
  165. if (!packageDir.exists()) {
  166. dirList.add(packageName);
  167. continue;
  168. }
  169. packageName = packageName.replace('.', '/'); // !!!
  170. filename = outputWorkingDir + Config.DIR_SEP_CHAR + packageName + Config.DIR_SEP_CHAR + "package.html";
  171. docFiles = rootDir.getAbsolutePath() + Config.DIR_SEP_CHAR + packageName + Config.DIR_SEP_CHAR + "doc-files";
  172. } else {
  173. filename = outputWorkingDir + Config.DIR_SEP_CHAR + "package.html";
  174. docFiles = rootDir.getAbsolutePath() + Config.DIR_SEP_CHAR + "doc-files";
  175. }
  176. File output = new File(filename);
  177. FileUtil.copyFile(input, output);// Copy package.html
  178. // javadoc doesn't do anything with the doc-files folder so
  179. // we'll just copy it directly to the document location.
  180. if (!inDir.exists())
  181. continue;
  182. File outDir = new File(docFiles);
  183. System.out.println("> Copying folder " + outDir);
  184. FileUtil.copyFile(inDir, outDir);// Copy doc-files folder if it exist
  185. }
  186. }
  187. private static AsmManager callAjc(File[] inputFiles) {
  188. ajcOptions.addElement("-noExit");
  189. ajcOptions.addElement("-XjavadocsInModel"); // TODO: wrong option to force model gen
  190. ajcOptions.addElement("-d");
  191. ajcOptions.addElement(rootDir.getAbsolutePath());
  192. String[] argsToCompiler = new String[ajcOptions.size() + inputFiles.length];
  193. int i = 0;
  194. for (; i < ajcOptions.size(); i++) {
  195. argsToCompiler[i] = ajcOptions.elementAt(i);
  196. }
  197. for (File inputFile : inputFiles) {
  198. argsToCompiler[i] = inputFile.getAbsolutePath();
  199. // System.out.println(">> file to ajc: " + inputFiles[j].getAbsolutePath());
  200. i++;
  201. }
  202. System.out.println("> Calling ajc...");
  203. return CompilerWrapper.executeMain(argsToCompiler);
  204. }
  205. private static void callJavadoc(File[] signatureFiles) throws IOException {
  206. System.out.println("> Calling javadoc...");
  207. String[] javadocargs = null;
  208. List<String> files = new ArrayList<>();
  209. if (packageMode) {
  210. int numExtraArgs = 2;
  211. if (authorStandardDocletSwitch)
  212. numExtraArgs++;
  213. if (versionStandardDocletSwitch)
  214. numExtraArgs++;
  215. javadocargs = new String[numExtraArgs + options.size() + packageList.size() + fileList.size()];
  216. javadocargs[0] = "-sourcepath";
  217. javadocargs[1] = outputWorkingDir;
  218. int argIndex = 2;
  219. if (authorStandardDocletSwitch) {
  220. javadocargs[argIndex] = "-author";
  221. argIndex++;
  222. }
  223. if (versionStandardDocletSwitch) {
  224. javadocargs[argIndex] = "-version";
  225. }
  226. // javadocargs[1] = getSourcepathAsString();
  227. for (int k = 0; k < options.size(); k++) {
  228. javadocargs[numExtraArgs + k] = options.elementAt(k);
  229. }
  230. for (int k = 0; k < packageList.size(); k++) {
  231. javadocargs[numExtraArgs + options.size() + k] = packageList.elementAt(k);
  232. }
  233. for (int k = 0; k < fileList.size(); k++) {
  234. javadocargs[numExtraArgs + options.size() + packageList.size() + k] = fileList.elementAt(k);
  235. }
  236. if (LangUtil.is9VMOrGreater()) {
  237. options = new Vector<>();
  238. Collections.addAll(options, javadocargs);
  239. }
  240. } else {
  241. javadocargs = new String[options.size() + signatureFiles.length];
  242. for (int k = 0; k < options.size(); k++) {
  243. javadocargs[k] = options.elementAt(k);
  244. }
  245. for (int k = 0; k < signatureFiles.length; k++) {
  246. javadocargs[options.size() + k] = StructureUtil.translateAjPathName(signatureFiles[k].getCanonicalPath());
  247. }
  248. for (File signatureFile : signatureFiles) {
  249. files.add(StructureUtil.translateAjPathName(signatureFile.getCanonicalPath()));
  250. }
  251. }
  252. // Since JDK 20, the javadoc tool renders the surrounding HTML according to the JVM default locale. Section headers
  253. // such as "Class", "Method Summary" will e.g. in German be named "Klasse", "Methodenübersicht". That would derail
  254. // ajdoc, because it matches on English headers hard-codedly.
  255. //
  256. // The remedy is to temporarily change the JVM default locale while generating javadocs.
  257. Locale defaultLocale = Locale.getDefault();
  258. try {
  259. Locale.setDefault(Locale.ENGLISH);
  260. if (LangUtil.is9VMOrGreater()) {
  261. JavadocRunner.callJavadocViaToolProvider(options, files);
  262. } else {
  263. JavadocRunner.callJavadoc(javadocargs);
  264. }
  265. }
  266. finally {
  267. Locale.setDefault(defaultLocale);
  268. }
  269. }
  270. /**
  271. * We start with the known HTML files (the ones that correspond directly to the input files.) As we go along, we may learn that
  272. * Javadoc split one .java file into multiple .html files to handle inner classes or local classes. The html file decorator
  273. * picks that up.
  274. */
  275. private static void decorateHtmlFiles(AsmManager model, File[] inputFiles) throws IOException {
  276. System.out.println("> Decorating html files...");
  277. HtmlDecorator.decorateHTMLFromInputFiles(model, declIDTable, rootDir, inputFiles, docModifier);
  278. System.out.println("> Removing generated tags...");
  279. removeDeclIDsFromFile("index-all.html", true);
  280. removeDeclIDsFromFile("serialized-form.html", true);
  281. if (packageList.size() > 0) {
  282. for (int p = 0; p < packageList.size(); p++) {
  283. removeDeclIDsFromFile(packageList.elementAt(p).replace('.', '/') + Config.DIR_SEP_CHAR
  284. + "package-summary.html", true);
  285. }
  286. } else {
  287. File[] files = rootDir.listFiles();
  288. if (files == null) {
  289. System.err.println("Destination directory is not a directory: " + rootDir.toString());
  290. return;
  291. }
  292. files = FileUtil.listFiles(rootDir, new FileFilter() {
  293. @Override
  294. public boolean accept(File f) {
  295. return f.getName().equals("package-summary.html");
  296. }
  297. });
  298. for (File file : files) {
  299. removeDeclIDsFromFile(file.getAbsolutePath(), false);
  300. }
  301. }
  302. }
  303. private static void removeDeclIDsFromFile(String filename, boolean relativePath) {
  304. // Remove the decl ids from "index-all.html"
  305. File indexFile;
  306. if (relativePath) {
  307. indexFile = new File(docDir + Config.DIR_SEP_CHAR + filename);
  308. } else {
  309. indexFile = new File(filename);
  310. }
  311. try {
  312. if (indexFile.exists()) {
  313. BufferedReader indexFileReader = new BufferedReader(new FileReader(indexFile));
  314. // StringBuffer greatly reduces the time it takes to remove generated tags
  315. StringBuilder indexFileBuffer = new StringBuilder((int) indexFile.length());
  316. String line = indexFileReader.readLine();
  317. while (line != null) {
  318. int indexStart = line.indexOf(Config.DECL_ID_STRING);
  319. int indexEnd = line.indexOf(Config.DECL_ID_TERMINATOR);
  320. if (indexStart != -1 && indexEnd != -1) {
  321. line = line.substring(0, indexStart) + line.substring(indexEnd + Config.DECL_ID_TERMINATOR.length());
  322. }
  323. indexFileBuffer.append(line);
  324. line = indexFileReader.readLine();
  325. }
  326. FileOutputStream fos = new FileOutputStream(indexFile);
  327. fos.write(indexFileBuffer.toString().getBytes());
  328. indexFileReader.close();
  329. fos.close();
  330. }
  331. } catch (IOException ioe) {
  332. // be siltent
  333. }
  334. }
  335. static Vector<String> getSourcePath() {
  336. Vector<String> sourcePath = new Vector<>();
  337. boolean found = false;
  338. for (int i = 0; i < options.size(); i++) {
  339. String currOption = options.elementAt(i);
  340. if (found && !currOption.startsWith("-")) {
  341. sourcePath.add(currOption);
  342. }
  343. if (currOption.equals("-sourcepath")) {
  344. found = true;
  345. }
  346. }
  347. return sourcePath;
  348. }
  349. static File getRootDir() {
  350. File rootDir = new File(".");
  351. for (int i = 0; i < options.size(); i++) {
  352. if (options.elementAt(i).equals("-d")) {
  353. rootDir = new File(options.elementAt(i + 1));
  354. if (!rootDir.exists()) {
  355. rootDir.mkdir();
  356. // System.out.println( "Destination directory not found: " +
  357. // (String)options.elementAt(i+1) );
  358. // System.exit( -1 );
  359. }
  360. }
  361. }
  362. return rootDir;
  363. }
  364. static File createSignatureFile(AsmManager model, File inputFile) throws IOException {
  365. String packageName = StructureUtil.getPackageDeclarationFromFile(model, inputFile);
  366. String filename = "";
  367. if (packageName != null) {
  368. String pathName = outputWorkingDir + '/' + packageName.replace('.', '/');
  369. File packageDir = new File(pathName);
  370. if (!packageDir.exists()) {
  371. packageDir.mkdirs();
  372. if (deleteTempFilesOnExit)
  373. packageDir.deleteOnExit();
  374. }
  375. // verifyPackageDirExists(packageName, null);
  376. packageName = packageName.replace('.', '/'); // !!!
  377. filename = outputWorkingDir + Config.DIR_SEP_CHAR + packageName + Config.DIR_SEP_CHAR + inputFile.getName();
  378. } else {
  379. filename = outputWorkingDir + Config.DIR_SEP_CHAR + inputFile.getName();
  380. }
  381. File signatureFile = new File(filename);
  382. if (deleteTempFilesOnExit)
  383. signatureFile.deleteOnExit();
  384. return signatureFile;
  385. }
  386. // static void verifyPackageDirExists( String packageName, String offset ) {
  387. // System.err.println(">>> name: " + packageName + ", offset: " + offset);
  388. // if ( packageName.indexOf( "." ) != -1 ) {
  389. // File tempFile = new File("c:/aspectj-test/d1/d2/d3");
  390. // tempFile.mkdirs();
  391. // String currPkgDir = packageName.substring( 0, packageName.indexOf( "." ) );
  392. // String remainingPkg = packageName.substring( packageName.indexOf( "." )+1 );
  393. // String filePath = null;
  394. // if ( offset != null ) {
  395. // filePath = Config.WORKING_DIR + Config.DIR_SEP_CHAR +
  396. // offset + Config.DIR_SEP_CHAR + currPkgDir ;
  397. // }
  398. // else {
  399. // filePath = Config.WORKING_DIR + Config.DIR_SEP_CHAR + currPkgDir;
  400. // }
  401. // File packageDir = new File( filePath );
  402. // if ( !packageDir.exists() ) {
  403. // packageDir.mkdir();
  404. // if (deleteTempFilesOnExit) packageDir.deleteOnExit();
  405. // }
  406. // if ( remainingPkg != "" ) {
  407. // verifyPackageDirExists( remainingPkg, currPkgDir );
  408. // }
  409. // }
  410. // else {
  411. // String filePath = null;
  412. // if ( offset != null ) {
  413. // filePath = Config.WORKING_DIR + Config.DIR_SEP_CHAR + offset + Config.DIR_SEP_CHAR + packageName;
  414. // }
  415. // else {
  416. // filePath = Config.WORKING_DIR + Config.DIR_SEP_CHAR + packageName;
  417. // }
  418. // File packageDir = new File( filePath );
  419. // if ( !packageDir.exists() ) {
  420. // packageDir.mkdir();
  421. // if (deleteTempFilesOnExit) packageDir.deleteOnExit();
  422. // }
  423. // }
  424. // }
  425. /**
  426. * Can read Eclipse-generated single-line arg
  427. */
  428. static void parseCommandLine(String[] args) {
  429. if (args.length == 0) {
  430. displayHelpAndExit(null);
  431. } else if (args.length == 1 && args[0].startsWith("@")) {
  432. String argFile = args[0].substring(1);
  433. System.out.println("> Using arg file: " + argFile);
  434. BufferedReader br;
  435. try {
  436. br = new BufferedReader(new FileReader(argFile));
  437. String line = "";
  438. line = br.readLine();
  439. StringTokenizer st = new StringTokenizer(line, " ");
  440. List<String> argList = new ArrayList<>();
  441. while (st.hasMoreElements()) {
  442. argList.add(st.nextToken());
  443. }
  444. // System.err.println(argList);
  445. args = new String[argList.size()];
  446. int counter = 0;
  447. for (String s : argList) {
  448. args[counter] = s;
  449. counter++;
  450. }
  451. } catch (FileNotFoundException e) {
  452. System.err.println("> could not read arg file: " + argFile);
  453. e.printStackTrace();
  454. } catch (IOException ioe) {
  455. System.err.println("> could not read arg file: " + argFile);
  456. ioe.printStackTrace();
  457. }
  458. }
  459. List<String> vargs = new LinkedList<>(Arrays.asList(args));
  460. vargs.add("-Xset:minimalModel=false");
  461. parseArgs(vargs, new File(".")); // !!!
  462. if (filenames.size() == 0) {
  463. displayHelpAndExit("ajdoc: No packages or classes specified");
  464. }
  465. }
  466. static void setSourcepath(String arg) {
  467. sourcepath.clear();
  468. arg = arg + File.pathSeparator; // makes things easier for ourselves
  469. StringTokenizer tokenizer = new StringTokenizer(arg, File.pathSeparator);
  470. while (tokenizer.hasMoreElements()) {
  471. sourcepath.addElement(tokenizer.nextToken());
  472. }
  473. }
  474. static String getSourcepathAsString() {
  475. String cPath = "";
  476. for (int i = 0; i < sourcepath.size(); i++) {
  477. cPath += sourcepath.elementAt(i) + Config.DIR_SEP_CHAR + outputWorkingDir;
  478. if (i != sourcepath.size() - 1) {
  479. cPath += File.pathSeparator;
  480. }
  481. }
  482. return cPath;
  483. }
  484. static void parseArgs(List vargs, File currentWorkingDir) {
  485. boolean addNextAsOption = false;
  486. boolean addNextAsArgFile = false;
  487. boolean addNextToAJCOptions = false;
  488. boolean addNextAsDocDir = false;
  489. boolean addNextAsClasspath = false;
  490. boolean ignoreArg = false; // used for discrepancy betwen class/sourcepath in ajc/javadoc
  491. boolean addNextAsSourcePath = false;
  492. if (vargs.size() == 0) {
  493. displayHelpAndExit(null);
  494. }
  495. for (Object varg : vargs) {
  496. String arg = (String) varg;
  497. ignoreArg = false;
  498. if (addNextAsDocDir) {
  499. docDir = arg;
  500. addNextAsDocDir = false;
  501. }
  502. if (addNextAsClasspath) {
  503. addNextAsClasspath = false;
  504. }
  505. if (addNextAsSourcePath) {
  506. setSourcepath(arg);
  507. addNextAsSourcePath = false;
  508. ignoreArg = true;
  509. }
  510. if (arg.startsWith("@")) {
  511. expandAtSignFile(arg.substring(1), currentWorkingDir);
  512. } else if (arg.equals("-argfile")) {
  513. addNextAsArgFile = true;
  514. } else if (addNextAsArgFile) {
  515. expandAtSignFile(arg, currentWorkingDir);
  516. addNextAsArgFile = false;
  517. } else if (arg.equals("-d")) {
  518. addNextAsOption = true;
  519. options.addElement(arg);
  520. addNextAsDocDir = true;
  521. } else if (arg.equals("-bootclasspath")) {
  522. addNextAsOption = true;
  523. addNextToAJCOptions = true;
  524. options.addElement(arg);
  525. ajcOptions.addElement(arg);
  526. } else if (arg.equals("-source")) {
  527. addNextAsOption = true;
  528. addNextToAJCOptions = true;
  529. addNextAsClasspath = true;
  530. options.addElement(arg);
  531. ajcOptions.addElement(arg);
  532. } else if (arg.equals("-classpath")) {
  533. addNextAsOption = true;
  534. addNextToAJCOptions = true;
  535. addNextAsClasspath = true;
  536. options.addElement(arg);
  537. ajcOptions.addElement(arg);
  538. } else if (arg.equals("-encoding")) {
  539. addNextAsOption = true;
  540. addNextToAJCOptions = false;
  541. options.addElement(arg);
  542. } else if (arg.equals("-docencoding")) {
  543. addNextAsOption = true;
  544. addNextToAJCOptions = false;
  545. options.addElement(arg);
  546. } else if (arg.equals("-charset")) {
  547. addNextAsOption = true;
  548. addNextToAJCOptions = false;
  549. options.addElement(arg);
  550. } else if (arg.equals("-sourcepath")) {
  551. addNextAsSourcePath = true;
  552. // options.addElement( arg );
  553. // ajcOptions.addElement( arg );
  554. } else if (arg.equals("-link")) {
  555. addNextAsOption = true;
  556. options.addElement(arg);
  557. } else if (arg.equals("-bottom")) {
  558. addNextAsOption = true;
  559. options.addElement(arg);
  560. } else if (arg.equals("-windowtitle")) {
  561. addNextAsOption = true;
  562. options.addElement(arg);
  563. } else if (arg.equals("-XajdocDebug")) {
  564. deleteTempFilesOnExit = false;
  565. } else if (arg.equals("-use")) {
  566. System.out.println("> Ignoring unsupported option: -use");
  567. } else if (arg.equals("-splitindex")) {
  568. // passed to javadoc
  569. } else if (arg.startsWith("-") || addNextAsOption || addNextToAJCOptions) {
  570. if (arg.equals("-private")) {
  571. docModifier = "private";
  572. } else if (arg.equals("-package")) {
  573. docModifier = "package";
  574. } else if (arg.equals("-protected")) {
  575. docModifier = "protected";
  576. } else if (arg.equals("-public")) {
  577. docModifier = "public";
  578. } else if (arg.equals("-verbose")) {
  579. verboseMode = true;
  580. } else if (arg.equals("-author")) {
  581. authorStandardDocletSwitch = true;
  582. } else if (arg.equals("-version")) {
  583. versionStandardDocletSwitch = true;
  584. } else if (arg.equals("-v")) {
  585. System.out.println(getVersion());
  586. exit(0);
  587. } else if (arg.equals("-help")) {
  588. displayHelpAndExit(null);
  589. } else if (arg.equals("-doclet") || arg.equals("-docletpath")) {
  590. System.out.println("The doclet and docletpath options are not currently supported \n"
  591. + "since ajdoc makes assumptions about the behavior of the standard \n"
  592. + "doclet. If you would find this option useful please email us at: \n"
  593. + " \n"
  594. + " aspectj-dev@eclipse.org \n"
  595. + " \n");
  596. exit(0);
  597. } else if (arg.equals("-nonavbar") || arg.equals("-noindex")) {
  598. // pass through
  599. // System.err.println("> ignoring unsupported option: " + arg);
  600. } else if (addNextToAJCOptions || addNextAsOption) {
  601. // deal with these two options together even though effectively
  602. // just falling through if addNextAsOption is true. Otherwise
  603. // will have to ensure check "addNextToAJCOptions" before
  604. // "addNextAsOption" so as the options are added to the
  605. // correct lists.
  606. if (addNextToAJCOptions) {
  607. ajcOptions.addElement(arg);
  608. if (!arg.startsWith("-")) {
  609. addNextToAJCOptions = false;
  610. }
  611. if (!addNextAsOption) {
  612. continue;
  613. }
  614. }
  615. } else if (arg.startsWith("-")) {
  616. ajcOptions.addElement(arg);
  617. addNextToAJCOptions = true;
  618. continue;
  619. } else {
  620. System.err.println("> unrecognized argument: " + arg);
  621. displayHelpAndExit(null);
  622. }
  623. options.addElement(arg);
  624. addNextAsOption = false;
  625. } else {
  626. // check if this is a file or a package
  627. // System.err.println(">>>>>>>> " + );
  628. // String entryName = arg.substring(arg.lastIndexOf(File.separator)+1);
  629. if (FileUtil.hasSourceSuffix(arg) || arg.endsWith(".lst") && arg != null) {
  630. File f = new File(arg);
  631. if (f.isAbsolute()) {
  632. filenames.addElement(arg);
  633. } else {
  634. filenames.addElement(currentWorkingDir + Config.DIR_SEP_CHAR + arg);
  635. }
  636. fileList.addElement(arg);
  637. }
  638. // PACKAGE MODE STUFF
  639. else if (!ignoreArg) {
  640. packageMode = true;
  641. packageList.addElement(arg);
  642. arg = arg.replace('.', '/'); // !!!
  643. // do this for every item in the classpath
  644. for (int c = 0; c < sourcepath.size(); c++) {
  645. String path = sourcepath.elementAt(c) + Config.DIR_SEP_CHAR + arg;
  646. File pkg = new File(path);
  647. if (pkg.isDirectory()) {
  648. String[] files = pkg.list(new FilenameFilter() {
  649. @Override
  650. public boolean accept(File dir, String name) {
  651. int index1 = name.lastIndexOf(".");
  652. int index2 = name.length();
  653. if ((index1 >= 0 && index2 >= 0)
  654. && (name.substring(index1, index2).equals(".java") || name.substring(index1, index2)
  655. .equals(".aj"))) {
  656. return true;
  657. } else {
  658. return false;
  659. }
  660. }
  661. });
  662. for (String file : files) {
  663. filenames.addElement(sourcepath.elementAt(c) + Config.DIR_SEP_CHAR + arg
  664. + Config.DIR_SEP_CHAR + file);
  665. }
  666. } else if (c == sourcepath.size()) { // last element on classpath
  667. System.out.println("ajdoc: No package, class, or source file " + "found named " + arg + ".");
  668. } else {
  669. // didn't find it on that element of the classpath but that's ok
  670. }
  671. }
  672. }
  673. }
  674. }
  675. // set the default visibility as an option to javadoc option
  676. if (!options.contains("-private") && !options.contains("-package") && !options.contains("-protected")
  677. && !options.contains("-public")) {
  678. options.addElement("-package");
  679. }
  680. }
  681. static void expandAtSignFile(String filename, File currentWorkingDir) {
  682. List<String> result = new LinkedList<>();
  683. File atFile = qualifiedFile(filename, currentWorkingDir);
  684. String atFileParent = atFile.getParent();
  685. File myWorkingDir = null;
  686. if (atFileParent != null)
  687. myWorkingDir = new File(atFileParent);
  688. try {
  689. BufferedReader stream = new BufferedReader(new FileReader(atFile));
  690. String line = null;
  691. while ((line = stream.readLine()) != null) {
  692. // strip out any comments of the form # to end of line
  693. int commentStart = line.indexOf("//");
  694. if (commentStart != -1) {
  695. line = line.substring(0, commentStart);
  696. }
  697. // remove extra whitespace that might have crept in
  698. line = line.trim();
  699. // ignore blank lines
  700. if (line.length() == 0)
  701. continue;
  702. result.add(line);
  703. }
  704. stream.close();
  705. } catch (IOException e) {
  706. System.err.println("Error while reading the @ file " + atFile.getPath() + ".\n" + e);
  707. System.exit(-1);
  708. }
  709. parseArgs(result, myWorkingDir);
  710. }
  711. static File qualifiedFile(String name, File currentWorkingDir) {
  712. name = name.replace('/', File.separatorChar);
  713. File file = new File(name);
  714. if (!file.isAbsolute() && currentWorkingDir != null) {
  715. file = new File(currentWorkingDir, name);
  716. }
  717. return file;
  718. }
  719. static void displayHelpAndExit(String message) {
  720. shownAjdocUsageMessage = true;
  721. if (message != null) {
  722. System.err.println(message);
  723. System.err.println();
  724. System.err.println(Config.USAGE);
  725. } else {
  726. System.out.println(Config.USAGE);
  727. exit(0);
  728. }
  729. }
  730. static protected void exit(int value) {
  731. System.out.flush();
  732. System.err.flush();
  733. System.exit(value);
  734. }
  735. /* This section of code handles errors that occur during compilation */
  736. static final String internalErrorMessage = " \n"
  737. + "If this has not already been logged as a bug raised please raise \n"
  738. + "a new AspectJ bug at https://github.com/eclipse-aspectj/aspectj/issues \n"
  739. + "including the text below. To make the bug a priority, please also \n"
  740. + "include a test program that can reproduce this problem.\n ";
  741. static public void handleInternalError(Throwable uncaughtThrowable) {
  742. System.err.println("An internal error occured in ajdoc");
  743. System.err.println(internalErrorMessage);
  744. System.err.println(uncaughtThrowable.toString());
  745. uncaughtThrowable.printStackTrace();
  746. System.err.println();
  747. }
  748. static String getVersion() {
  749. return "ajdoc version " + Version.getText();
  750. }
  751. public static boolean hasAborted() {
  752. return aborted;
  753. }
  754. public static IMessage[] getErrors() {
  755. return errors;
  756. }
  757. public static boolean hasShownAjdocUsageMessage() {
  758. return shownAjdocUsageMessage;
  759. }
  760. /**
  761. * Sets the output working dir to be &lt;fullyQualifiedOutputDir&gt;\ajdocworkingdir. Useful in testing to redirect the ajdocworkingdir
  762. * to the sandbox
  763. */
  764. public static void setOutputWorkingDir(String fullyQulifiedOutputDir) {
  765. if (fullyQulifiedOutputDir == null) {
  766. resetOutputWorkingDir();
  767. } else {
  768. outputWorkingDir = fullyQulifiedOutputDir + File.separatorChar + Config.WORKING_DIR;
  769. }
  770. }
  771. /**
  772. * Resets the output working dir to be the default which is &lt;the current directory&gt;\ajdocworkingdir
  773. */
  774. public static void resetOutputWorkingDir() {
  775. outputWorkingDir = Config.WORKING_DIR;
  776. }
  777. }