1.1 --- a/src/share/classes/com/sun/tools/javadoc/JavadocTool.java Tue Jan 20 17:49:49 2009 +0000 1.2 +++ b/src/share/classes/com/sun/tools/javadoc/JavadocTool.java Tue Jan 20 15:17:45 2009 -0800 1.3 @@ -25,17 +25,29 @@ 1.4 1.5 package com.sun.tools.javadoc; 1.6 1.7 -import java.io.*; 1.8 +import java.io.File; 1.9 +import java.io.IOException; 1.10 +import java.util.Collection; 1.11 +import java.util.EnumSet; 1.12 +import java.util.HashMap; 1.13 +import java.util.Map; 1.14 +import java.util.Set; 1.15 +import javax.tools.JavaFileManager.Location; 1.16 +import javax.tools.JavaFileObject; 1.17 +import javax.tools.StandardJavaFileManager; 1.18 +import javax.tools.StandardLocation; 1.19 1.20 -import java.util.Collection; 1.21 - 1.22 -import com.sun.tools.javac.code.Symbol.*; 1.23 -import com.sun.tools.javac.comp.*; 1.24 -import com.sun.tools.javac.file.Paths; 1.25 +import com.sun.tools.javac.code.Symbol.CompletionFailure; 1.26 +import com.sun.tools.javac.comp.Annotate; 1.27 import com.sun.tools.javac.parser.DocCommentScanner; 1.28 -import com.sun.tools.javac.tree.*; 1.29 -import com.sun.tools.javac.tree.JCTree.*; 1.30 -import com.sun.tools.javac.util.*; 1.31 +import com.sun.tools.javac.tree.JCTree; 1.32 +import com.sun.tools.javac.tree.JCTree.JCClassDecl; 1.33 +import com.sun.tools.javac.tree.JCTree.JCCompilationUnit; 1.34 +import com.sun.tools.javac.util.Abort; 1.35 +import com.sun.tools.javac.util.Context; 1.36 +import com.sun.tools.javac.util.List; 1.37 +import com.sun.tools.javac.util.ListBuffer; 1.38 +import com.sun.tools.javac.util.Position; 1.39 1.40 1.41 /** 1.42 @@ -53,7 +65,6 @@ 1.43 final JavadocClassReader reader; 1.44 final JavadocEnter enter; 1.45 final Annotate annotate; 1.46 - private final Paths paths; 1.47 1.48 /** 1.49 * Construct a new JavaCompiler processor, using appropriately 1.50 @@ -66,7 +77,6 @@ 1.51 reader = JavadocClassReader.instance0(context); 1.52 enter = JavadocEnter.instance0(context); 1.53 annotate = Annotate.instance(context); 1.54 - paths = Paths.instance(context); 1.55 } 1.56 1.57 /** 1.58 @@ -120,7 +130,7 @@ 1.59 boolean quiet) throws IOException { 1.60 docenv = DocEnv.instance(context); 1.61 docenv.showAccess = filter; 1.62 - docenv.quiet = quiet; 1.63 + docenv.quiet = quiet; 1.64 docenv.breakiterator = breakiterator; 1.65 docenv.setLocale(doclocale); 1.66 docenv.setEncoding(encoding); 1.67 @@ -133,12 +143,14 @@ 1.68 ListBuffer<JCCompilationUnit> packTrees = new ListBuffer<JCCompilationUnit>(); 1.69 1.70 try { 1.71 + StandardJavaFileManager fm = (StandardJavaFileManager) docenv.fileManager; 1.72 for (List<String> it = javaNames; it.nonEmpty(); it = it.tail) { 1.73 String name = it.head; 1.74 if (!docClasses && name.endsWith(".java") && new File(name).exists()) { 1.75 + JavaFileObject fo = fm.getJavaFileObjects(name).iterator().next(); 1.76 docenv.notice("main.Loading_source_file", name); 1.77 - JCCompilationUnit tree = parse(name); 1.78 - classTrees.append(tree); 1.79 + JCCompilationUnit tree = parse(fo); 1.80 + classTrees.append(tree); 1.81 } else if (isValidPackageName(name)) { 1.82 names = names.append(name); 1.83 } else if (name.endsWith(".java")) { 1.84 @@ -151,12 +163,14 @@ 1.85 if (!docClasses) { 1.86 // Recursively search given subpackages. If any packages 1.87 //are found, add them to the list. 1.88 - searchSubPackages(subPackages, names, excludedPackages); 1.89 + Map<String,List<JavaFileObject>> packageFiles = 1.90 + searchSubPackages(subPackages, names, excludedPackages); 1.91 1.92 // Parse the packages 1.93 for (List<String> packs = names.toList(); packs.nonEmpty(); packs = packs.tail) { 1.94 // Parse sources ostensibly belonging to package. 1.95 - parsePackageClasses(packs.head, packTrees, excludedPackages); 1.96 + String packageName = packs.head; 1.97 + parsePackageClasses(packageName, packageFiles.get(packageName), packTrees, excludedPackages); 1.98 } 1.99 1.100 if (messager.nerrors() != 0) return null; 1.101 @@ -167,7 +181,8 @@ 1.102 } 1.103 } catch (Abort ex) {} 1.104 1.105 - if (messager.nerrors() != 0) return null; 1.106 + if (messager.nerrors() != 0) 1.107 + return null; 1.108 1.109 if (docClasses) 1.110 return new RootDocImpl(docenv, javaNames, options); 1.111 @@ -185,66 +200,129 @@ 1.112 return isValidClassName(s); 1.113 } 1.114 1.115 - 1.116 - private final static char pathSep = File.pathSeparatorChar; 1.117 - 1.118 /** 1.119 * search all directories in path for subdirectory name. Add all 1.120 * .java files found in such a directory to args. 1.121 */ 1.122 private void parsePackageClasses(String name, 1.123 - ListBuffer<JCCompilationUnit> trees, 1.124 - List<String> excludedPackages) 1.125 - throws IOException { 1.126 + Iterable<JavaFileObject> files, 1.127 + ListBuffer<JCCompilationUnit> trees, 1.128 + List<String> excludedPackages) 1.129 + throws IOException { 1.130 if (excludedPackages.contains(name)) { 1.131 return; 1.132 } 1.133 + 1.134 boolean hasFiles = false; 1.135 docenv.notice("main.Loading_source_files_for_package", name); 1.136 - name = name.replace('.', File.separatorChar); 1.137 - for (File pathname : paths.sourceSearchPath()) { 1.138 - File f = new File(pathname, name); 1.139 - String names[] = f.list(); 1.140 - // if names not null, then found directory with source files 1.141 - if (names != null) { 1.142 - String dir = f.getAbsolutePath(); 1.143 - if (!dir.endsWith(File.separator)) 1.144 - dir = dir + File.separator; 1.145 - for (int j = 0; j < names.length; j++) { 1.146 - if (isValidJavaSourceFile(names[j])) { 1.147 - String fn = dir + names[j]; 1.148 - // messager.notice("main.Loading_source_file", fn); 1.149 - trees.append(parse(fn)); 1.150 - hasFiles = true; 1.151 - } 1.152 + 1.153 + if (files == null) { 1.154 + Location location = docenv.fileManager.hasLocation(StandardLocation.SOURCE_PATH) 1.155 + ? StandardLocation.SOURCE_PATH : StandardLocation.CLASS_PATH; 1.156 + ListBuffer<JavaFileObject> lb = new ListBuffer<JavaFileObject>(); 1.157 + for (JavaFileObject fo: docenv.fileManager.list( 1.158 + location, name, EnumSet.of(JavaFileObject.Kind.SOURCE), false)) { 1.159 + String binaryName = docenv.fileManager.inferBinaryName(location, fo); 1.160 + String simpleName = getSimpleName(binaryName); 1.161 + if (isValidClassName(simpleName)) { 1.162 + lb.append(fo); 1.163 } 1.164 } 1.165 + files = lb.toList(); 1.166 } 1.167 - if (!hasFiles) 1.168 + 1.169 + for (JavaFileObject fo : files) { 1.170 + // messager.notice("main.Loading_source_file", fn); 1.171 + trees.append(parse(fo)); 1.172 + hasFiles = true; 1.173 + } 1.174 + 1.175 + if (!hasFiles) { 1.176 messager.warning(null, "main.no_source_files_for_package", 1.177 - name.replace(File.separatorChar, '.')); 1.178 + name.replace(File.separatorChar, '.')); 1.179 + } 1.180 } 1.181 1.182 /** 1.183 * Recursively search all directories in path for subdirectory name. 1.184 * Add all packages found in such a directory to packages list. 1.185 */ 1.186 + private Map<String,List<JavaFileObject>> searchSubPackages( 1.187 + List<String> subPackages, 1.188 + ListBuffer<String> packages, 1.189 + List<String> excludedPackages) 1.190 + throws IOException { 1.191 + Map<String,List<JavaFileObject>> packageFiles = 1.192 + new HashMap<String,List<JavaFileObject>>(); 1.193 + 1.194 + Map<String,Boolean> includedPackages = new HashMap<String,Boolean>(); 1.195 + includedPackages.put("", true); 1.196 + for (String p: excludedPackages) 1.197 + includedPackages.put(p, false); 1.198 + 1.199 + if (docenv.fileManager.hasLocation(StandardLocation.SOURCE_PATH)) { 1.200 + searchSubPackages(subPackages, 1.201 + includedPackages, 1.202 + packages, packageFiles, 1.203 + StandardLocation.SOURCE_PATH, 1.204 + EnumSet.of(JavaFileObject.Kind.SOURCE)); 1.205 + searchSubPackages(subPackages, 1.206 + includedPackages, 1.207 + packages, packageFiles, 1.208 + StandardLocation.CLASS_PATH, 1.209 + EnumSet.of(JavaFileObject.Kind.CLASS)); 1.210 + } else { 1.211 + searchSubPackages(subPackages, 1.212 + includedPackages, 1.213 + packages, packageFiles, 1.214 + StandardLocation.CLASS_PATH, 1.215 + EnumSet.of(JavaFileObject.Kind.SOURCE, JavaFileObject.Kind.CLASS)); 1.216 + } 1.217 + return packageFiles; 1.218 + } 1.219 + 1.220 private void searchSubPackages(List<String> subPackages, 1.221 - ListBuffer<String> packages, 1.222 - List<String> excludedPackages) { 1.223 - // FIXME: This search path is bogus. 1.224 - // Only the effective source path should be searched for sources. 1.225 - // Only the effective class path should be searched for classes. 1.226 - // Should the bootclasspath/extdirs also be searched for classes? 1.227 - java.util.List<File> pathnames = new java.util.ArrayList<File>(); 1.228 - if (paths.sourcePath() != null) 1.229 - for (File elt : paths.sourcePath()) 1.230 - pathnames.add(elt); 1.231 - for (File elt : paths.userClassPath()) 1.232 - pathnames.add(elt); 1.233 + Map<String,Boolean> includedPackages, 1.234 + ListBuffer<String> packages, 1.235 + Map<String, List<JavaFileObject>> packageFiles, 1.236 + StandardLocation location, Set<JavaFileObject.Kind> kinds) 1.237 + throws IOException { 1.238 + for (String subPackage: subPackages) { 1.239 + if (!isIncluded(subPackage, includedPackages)) 1.240 + continue; 1.241 1.242 - for (String subPackage : subPackages) 1.243 - searchSubPackage(subPackage, packages, excludedPackages, pathnames); 1.244 + for (JavaFileObject fo: docenv.fileManager.list(location, subPackage, kinds, true)) { 1.245 + String binaryName = docenv.fileManager.inferBinaryName(location, fo); 1.246 + String packageName = getPackageName(binaryName); 1.247 + String simpleName = getSimpleName(binaryName); 1.248 + if (isIncluded(packageName, includedPackages) && isValidClassName(simpleName)) { 1.249 + List<JavaFileObject> list = packageFiles.get(packageName); 1.250 + list = (list == null ? List.of(fo) : list.prepend(fo)); 1.251 + packageFiles.put(packageName, list); 1.252 + if (!packages.contains(packageName)) 1.253 + packages.add(packageName); 1.254 + } 1.255 + } 1.256 + } 1.257 + } 1.258 + 1.259 + private String getPackageName(String name) { 1.260 + int lastDot = name.lastIndexOf("."); 1.261 + return (lastDot == -1 ? "" : name.substring(0, lastDot)); 1.262 + } 1.263 + 1.264 + private String getSimpleName(String name) { 1.265 + int lastDot = name.lastIndexOf("."); 1.266 + return (lastDot == -1 ? name : name.substring(lastDot + 1)); 1.267 + } 1.268 + 1.269 + private boolean isIncluded(String packageName, Map<String,Boolean> includedPackages) { 1.270 + Boolean b = includedPackages.get(packageName); 1.271 + if (b == null) { 1.272 + b = isIncluded(getPackageName(packageName), includedPackages); 1.273 + includedPackages.put(packageName, b); 1.274 + } 1.275 + return b; 1.276 } 1.277 1.278 /**