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

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