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 27KB

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