📄 main.java
字号:
for (Iterator it = packageAndClasses.iterator(); it.hasNext();) { String classOrPackage = (String) it.next(); boolean foundSourceFile = false; if (classOrPackage.endsWith(".java")) { for (Iterator pit = option_sourcepath.iterator(); pit.hasNext() && !foundSourceFile; ) { File sourceDir = (File)pit.next(); File sourceFile = new File(sourceDir, classOrPackage); if (sourceFile.exists() && !sourceFile.isDirectory()) { rootDoc.addSpecifiedSourceFile(sourceFile); foundSourceFile = true; break; } } if (!foundSourceFile) { File sourceFile = new File(classOrPackage); if (sourceFile.exists() && !sourceFile.isDirectory()) { rootDoc.addSpecifiedSourceFile(sourceFile); foundSourceFile = true; } } } if (!foundSourceFile) { //--- Check for illegal name if (classOrPackage.startsWith(".") || classOrPackage.endsWith(".") || classOrPackage.indexOf("..") > 0 || !checkCharSet(classOrPackage, "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890_.")) { throw new ParseException("Illegal class or package name '" + classOrPackage + "'"); } //--- Assemble absolute path to package String classOrPackageRelPath = classOrPackage.replace('.', File.separatorChar); //--- Create one file object each for a possible package directory // and a possible class file, and find out if they exist. List packageDirs = rootDoc.findSourceFiles(classOrPackageRelPath); List sourceFiles = rootDoc.findSourceFiles(classOrPackageRelPath + ".java"); boolean packageDirExists = !packageDirs.isEmpty(); boolean sourceFileExists = !sourceFiles.isEmpty(); //--- Complain if neither exists: not found if (!packageDirExists && !sourceFileExists) { reporter.printError("Class or package " + classOrPackage + " not found."); return false; } //--- Complain if both exist: ambigious else if (packageDirExists && sourceFileExists) { reporter.printError("Ambigious class/package name " + classOrPackage + "."); return false; } //--- Otherwise, if the package directory exists, it is a package else if (packageDirExists) { Iterator packageDirIt = packageDirs.iterator(); boolean packageDirFound = false; while (packageDirIt.hasNext()) { File packageDir = (File)packageDirIt.next(); if (packageDir.isDirectory()) { rootDoc.addSpecifiedPackageName(classOrPackage); packageDirFound = true; break; } } if (!packageDirFound) { reporter.printError("No suitable file or directory found for" + classOrPackage); return false; } } //--- Otherwise, emit error message else { reporter.printError("No sources files found for package " + classOrPackage); } } } //--- Complain if no packages or classes specified if (option_help) { usage(); return true; } //--- Validate custom options passed on command line // by asking the Doclet if they are OK. String[][] customOptionArr = (String[][]) options .toArray(new String[0][0]); if (validOptionsMethod != null && !((Boolean) validOptionsMethod.invoke(null, new Object[] { customOptionArr, reporter })).booleanValue()) { // Not ok: shutdown system. reporter.printNotice(STRING_TRY_GJDOC_HELP); return false; } if (!rootDoc.hasSpecifiedPackagesOrClasses()) { reporter.printError("No packages or classes specified."); reporter.printNotice(STRING_TRY_GJDOC_HELP); return false; } rootDoc.setOptions(customOptionArr); rootDoc.build(); //--- Bail out if no classes found if (0 == rootDoc.classes().length && 0 == rootDoc.specifiedPackages().length && 0 == rootDoc.specifiedClasses().length) { reporter.printError("No packages or classes found(!)."); return false; } //--- Our work is done, tidy up memory System.gc(); System.gc(); //--- Set flag indicating Phase II of documentation generation docletRunning = true; //--- Invoke the start method on the Doclet: produce output reporter.printNotice("Running doclet..."); TemporaryStore tstore = new TemporaryStore(Main.rootDoc); Thread.currentThread().setContextClassLoader(docletClass.getClassLoader()); if (null != startTempMethod) { startTempMethod.invoke(null, new Object[] { tstore }); } else { startMethod.invoke(null, new Object[] { tstore.getAndClear() }); } //--- Let the user know how many warnings/errors occured if (reporter.getWarningCount() > 0) { reporter.printNotice(reporter.getWarningCount() + " warnings"); } if (reporter.getErrorCount() > 0) { reporter.printNotice(reporter.getErrorCount() + " errors"); } System.gc(); //--- Done. return true; } catch (Exception e) { e.printStackTrace(); return false; } } private void addFoundPackages(String subpackage, Set foundPackages) { if (foundPackages.isEmpty()) { reporter.printWarning("No classes found under subpackage " + subpackage); } else { boolean onePackageAdded = false; for (Iterator rit = foundPackages.iterator(); rit.hasNext();) { String foundPackage = (String)rit.next(); boolean excludeThisPackage = false; for (Iterator eit = option_exclude.iterator(); eit.hasNext();) { String excludePackage = (String)eit.next(); if (foundPackage.equals(excludePackage) || foundPackage.startsWith(excludePackage + ":")) { excludeThisPackage = true; break; } } if (!excludeThisPackage) { rootDoc.addSpecifiedPackageName(foundPackage); onePackageAdded = true; } } if (!onePackageAdded) { if (null != subpackage) { reporter.printWarning("No non-excluded classes found under subpackage " + subpackage); } else { reporter.printWarning("No non-excluded classes found."); } } } } /** * Verify that the given file is a valid Java source file and that * it specifies the given package. */ private boolean isValidJavaFile(File file, String expectedPackage) { try { InputStream in = new BufferedInputStream(new FileInputStream(file)); int ch, prevChar = 0; final int STATE_DEFAULT = 0; final int STATE_COMMENT = 1; final int STATE_LINE_COMMENT = 2; int state = STATE_DEFAULT; StringBuffer word = new StringBuffer(); int wordIndex = 0; while ((ch = in.read()) >= 0) { String completeWord = null; switch (state) { case STATE_COMMENT: if (prevChar == '*' && ch == '/') { state = STATE_DEFAULT; } break; case STATE_LINE_COMMENT: if (ch == '\n') { state = STATE_DEFAULT; } break; case STATE_DEFAULT: if (prevChar == '/' && ch == '*') { word.deleteCharAt(word.length() - 1); if (word.length() > 0) { completeWord = word.toString(); word.setLength(0); } state = STATE_COMMENT; } else if (prevChar == '/' && ch == '/') { word.deleteCharAt(word.length() - 1); if (word.length() > 0) { completeWord = word.toString(); word.setLength(0); } state = STATE_LINE_COMMENT; } else if (" \t\r\n".indexOf(ch) >= 0) { if (word.length() > 0) { completeWord = word.toString(); word.setLength(0); } } else if (1 == wordIndex && ';' == ch) { if (word.length() > 0) { completeWord = word.toString(); word.setLength(0); } else { // empty package name in source file: "package ;" -> invalid source file in.close(); return false; } } else { word.append((char)ch); } break; } if (null != completeWord) { if (0 == wordIndex && !"package".equals(completeWord)) { in.close(); return "".equals(expectedPackage); } else if (1 == wordIndex) { in.close(); return expectedPackage.equals(completeWord); } ++ wordIndex; } prevChar = ch; } // no package or class found before end-of-file -> invalid source file in.close(); return false; } catch (IOException e) { reporter.printWarning("Could not examine file " + file + ": " + e); return false; } } /** * Recursively try to locate valid Java packages under the given * package specified by its name and its directory. Add the names * of all valid packages to the result list. */ private void findPackages(String subpackage, File packageDir, Set result) { File[] files = packageDir.listFiles(); if (null != files) { for (int i=0; i<files.length; ++i) { File file = files[i]; if (!file.isDirectory() && file.getName().endsWith(".java")) { if (isValidJavaFile(file, subpackage)) { if ("".equals(subpackage)) { result.add(null); } else { result.add(subpackage); } break; } } } for (int i=0; i<files.length; ++i) { File file = files[i]; if (file.isDirectory()) { String newSubpackage; if (null != subpackage && subpackage.length() > 0) { newSubpackage = subpackage + "." + file.getName(); } else { newSubpackage = file.getName(); } findPackages(newSubpackage, file, result); } } } } /** * */ private static boolean validOptions(String options[][], DocErrorReporter reporter) { boolean foundDocletOption = false; for (int i = 0; i < options.length; i++) { String[] opt = options[i]; if (opt[0].equalsIgnoreCase("-doclet")) { if (foundDocletOption) { reporter.printError("Only one -doclet option allowed."); return false; } else { foundDocletOption = true; } } } return true; } /** * Main entry point. This is the method called when gjdoc is invoked from the * command line. * * @param args * command line arguments */ public static void main(String[] args) { try { //--- Remember current time for profiling purposes Timer.setStartTime(); //--- Handle control to the Singleton instance of this class int result = instance.start(args); if (result < 0) { // fatal error System.exit(5); } else if (result > 0) { // errors encountered System.exit(1); } else { // success System.exit(0); } } catch (Exception e) { //--- unexpected error e.printStackTrace(); System.exit(1); } } /** * Parses command line arguments and subsequentially handles control to the * startDoclet() method * * @param args The command line parameters. */ public static int execute(String[] args) { try { int result = instance.start(args); if (result < 0) { // fatal error return 5; } else if (result > 0) { // errors encountered return 1; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -