Mon, 16 Jun 2008 13:28:00 -0700
6714364: refactor javac File handling code into new javac.file package
Reviewed-by: mcimadamore
duke@1 | 1 | /* |
duke@1 | 2 | * Copyright 2001-2006 Sun Microsystems, Inc. All Rights Reserved. |
duke@1 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
duke@1 | 4 | * |
duke@1 | 5 | * This code is free software; you can redistribute it and/or modify it |
duke@1 | 6 | * under the terms of the GNU General Public License version 2 only, as |
duke@1 | 7 | * published by the Free Software Foundation. Sun designates this |
duke@1 | 8 | * particular file as subject to the "Classpath" exception as provided |
duke@1 | 9 | * by Sun in the LICENSE file that accompanied this code. |
duke@1 | 10 | * |
duke@1 | 11 | * This code is distributed in the hope that it will be useful, but WITHOUT |
duke@1 | 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
duke@1 | 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
duke@1 | 14 | * version 2 for more details (a copy is included in the LICENSE file that |
duke@1 | 15 | * accompanied this code). |
duke@1 | 16 | * |
duke@1 | 17 | * You should have received a copy of the GNU General Public License version |
duke@1 | 18 | * 2 along with this work; if not, write to the Free Software Foundation, |
duke@1 | 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
duke@1 | 20 | * |
duke@1 | 21 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
duke@1 | 22 | * CA 95054 USA or visit www.sun.com if you need additional information or |
duke@1 | 23 | * have any questions. |
duke@1 | 24 | */ |
duke@1 | 25 | |
duke@1 | 26 | package com.sun.tools.javadoc; |
duke@1 | 27 | |
duke@1 | 28 | import java.io.*; |
duke@1 | 29 | |
duke@1 | 30 | import java.util.Collection; |
duke@1 | 31 | |
duke@1 | 32 | import com.sun.tools.javac.code.Symbol.*; |
duke@1 | 33 | import com.sun.tools.javac.comp.*; |
jjg@50 | 34 | import com.sun.tools.javac.file.Paths; |
duke@1 | 35 | import com.sun.tools.javac.parser.DocCommentScanner; |
duke@1 | 36 | import com.sun.tools.javac.tree.*; |
duke@1 | 37 | import com.sun.tools.javac.tree.JCTree.*; |
duke@1 | 38 | import com.sun.tools.javac.util.*; |
duke@1 | 39 | |
duke@1 | 40 | |
duke@1 | 41 | /** |
duke@1 | 42 | * This class could be the main entry point for Javadoc when Javadoc is used as a |
duke@1 | 43 | * component in a larger software system. It provides operations to |
duke@1 | 44 | * construct a new javadoc processor, and to run it on a set of source |
duke@1 | 45 | * files. |
duke@1 | 46 | * @author Neal Gafter |
duke@1 | 47 | */ |
duke@1 | 48 | public class JavadocTool extends com.sun.tools.javac.main.JavaCompiler { |
duke@1 | 49 | DocEnv docenv; |
duke@1 | 50 | |
duke@1 | 51 | final Context context; |
duke@1 | 52 | final Messager messager; |
duke@1 | 53 | final JavadocClassReader reader; |
duke@1 | 54 | final JavadocEnter enter; |
duke@1 | 55 | final Annotate annotate; |
duke@1 | 56 | private final Paths paths; |
duke@1 | 57 | |
duke@1 | 58 | /** |
duke@1 | 59 | * Construct a new JavaCompiler processor, using appropriately |
duke@1 | 60 | * extended phases of the underlying compiler. |
duke@1 | 61 | */ |
duke@1 | 62 | protected JavadocTool(Context context) { |
duke@1 | 63 | super(context); |
duke@1 | 64 | this.context = context; |
duke@1 | 65 | messager = Messager.instance0(context); |
duke@1 | 66 | reader = JavadocClassReader.instance0(context); |
duke@1 | 67 | enter = JavadocEnter.instance0(context); |
duke@1 | 68 | annotate = Annotate.instance(context); |
duke@1 | 69 | paths = Paths.instance(context); |
duke@1 | 70 | } |
duke@1 | 71 | |
duke@1 | 72 | /** |
duke@1 | 73 | * For javadoc, the parser needs to keep comments. Overrides method from JavaCompiler. |
duke@1 | 74 | */ |
duke@1 | 75 | protected boolean keepComments() { |
duke@1 | 76 | return true; |
duke@1 | 77 | } |
duke@1 | 78 | |
duke@1 | 79 | /** |
duke@1 | 80 | * Construct a new javadoc tool. |
duke@1 | 81 | */ |
duke@1 | 82 | public static JavadocTool make0(Context context) { |
duke@1 | 83 | Messager messager = null; |
duke@1 | 84 | try { |
duke@1 | 85 | // force the use of Javadoc's class reader |
duke@1 | 86 | JavadocClassReader.preRegister(context); |
duke@1 | 87 | |
duke@1 | 88 | // force the use of Javadoc's own enter phase |
duke@1 | 89 | JavadocEnter.preRegister(context); |
duke@1 | 90 | |
duke@1 | 91 | // force the use of Javadoc's own member enter phase |
duke@1 | 92 | JavadocMemberEnter.preRegister(context); |
duke@1 | 93 | |
duke@1 | 94 | // force the use of Javadoc's own todo phase |
duke@1 | 95 | JavadocTodo.preRegister(context); |
duke@1 | 96 | |
duke@1 | 97 | // force the use of Messager as a Log |
duke@1 | 98 | messager = Messager.instance0(context); |
duke@1 | 99 | |
duke@1 | 100 | // force the use of the scanner that captures Javadoc comments |
duke@1 | 101 | DocCommentScanner.Factory.preRegister(context); |
duke@1 | 102 | |
duke@1 | 103 | return new JavadocTool(context); |
duke@1 | 104 | } catch (CompletionFailure ex) { |
duke@1 | 105 | messager.error(Position.NOPOS, ex.getMessage()); |
duke@1 | 106 | return null; |
duke@1 | 107 | } |
duke@1 | 108 | } |
duke@1 | 109 | |
duke@1 | 110 | public RootDocImpl getRootDocImpl(String doclocale, |
duke@1 | 111 | String encoding, |
duke@1 | 112 | ModifierFilter filter, |
duke@1 | 113 | List<String> javaNames, |
duke@1 | 114 | List<String[]> options, |
duke@1 | 115 | boolean breakiterator, |
duke@1 | 116 | List<String> subPackages, |
duke@1 | 117 | List<String> excludedPackages, |
duke@1 | 118 | boolean docClasses, |
duke@1 | 119 | boolean legacyDoclet, |
duke@1 | 120 | boolean quiet) throws IOException { |
duke@1 | 121 | docenv = DocEnv.instance(context); |
duke@1 | 122 | docenv.showAccess = filter; |
duke@1 | 123 | docenv.quiet = quiet; |
duke@1 | 124 | docenv.breakiterator = breakiterator; |
duke@1 | 125 | docenv.setLocale(doclocale); |
duke@1 | 126 | docenv.setEncoding(encoding); |
duke@1 | 127 | docenv.docClasses = docClasses; |
duke@1 | 128 | docenv.legacyDoclet = legacyDoclet; |
duke@1 | 129 | reader.sourceCompleter = docClasses ? null : this; |
duke@1 | 130 | |
duke@1 | 131 | ListBuffer<String> names = new ListBuffer<String>(); |
duke@1 | 132 | ListBuffer<JCCompilationUnit> classTrees = new ListBuffer<JCCompilationUnit>(); |
duke@1 | 133 | ListBuffer<JCCompilationUnit> packTrees = new ListBuffer<JCCompilationUnit>(); |
duke@1 | 134 | |
duke@1 | 135 | try { |
duke@1 | 136 | for (List<String> it = javaNames; it.nonEmpty(); it = it.tail) { |
duke@1 | 137 | String name = it.head; |
duke@1 | 138 | if (!docClasses && name.endsWith(".java") && new File(name).exists()) { |
duke@1 | 139 | docenv.notice("main.Loading_source_file", name); |
duke@1 | 140 | JCCompilationUnit tree = parse(name); |
duke@1 | 141 | classTrees.append(tree); |
duke@1 | 142 | } else if (isValidPackageName(name)) { |
duke@1 | 143 | names = names.append(name); |
duke@1 | 144 | } else if (name.endsWith(".java")) { |
jjg@50 | 145 | docenv.error(null, "main.file_not_found", name); |
duke@1 | 146 | } else { |
duke@1 | 147 | docenv.error(null, "main.illegal_package_name", name); |
duke@1 | 148 | } |
duke@1 | 149 | } |
duke@1 | 150 | |
duke@1 | 151 | if (!docClasses) { |
duke@1 | 152 | // Recursively search given subpackages. If any packages |
duke@1 | 153 | //are found, add them to the list. |
duke@1 | 154 | searchSubPackages(subPackages, names, excludedPackages); |
duke@1 | 155 | |
duke@1 | 156 | // Parse the packages |
duke@1 | 157 | for (List<String> packs = names.toList(); packs.nonEmpty(); packs = packs.tail) { |
duke@1 | 158 | // Parse sources ostensibly belonging to package. |
duke@1 | 159 | parsePackageClasses(packs.head, packTrees, excludedPackages); |
duke@1 | 160 | } |
duke@1 | 161 | |
duke@1 | 162 | if (messager.nerrors() != 0) return null; |
duke@1 | 163 | |
duke@1 | 164 | // Enter symbols for all files |
duke@1 | 165 | docenv.notice("main.Building_tree"); |
duke@1 | 166 | enter.main(classTrees.toList().appendList(packTrees.toList())); |
duke@1 | 167 | } |
duke@1 | 168 | } catch (Abort ex) {} |
duke@1 | 169 | |
duke@1 | 170 | if (messager.nerrors() != 0) return null; |
duke@1 | 171 | |
duke@1 | 172 | if (docClasses) |
duke@1 | 173 | return new RootDocImpl(docenv, javaNames, options); |
duke@1 | 174 | else |
duke@1 | 175 | return new RootDocImpl(docenv, listClasses(classTrees.toList()), names.toList(), options); |
duke@1 | 176 | } |
duke@1 | 177 | |
duke@1 | 178 | /** Is the given string a valid package name? */ |
duke@1 | 179 | boolean isValidPackageName(String s) { |
duke@1 | 180 | int index; |
duke@1 | 181 | while ((index = s.indexOf('.')) != -1) { |
duke@1 | 182 | if (!isValidClassName(s.substring(0, index))) return false; |
duke@1 | 183 | s = s.substring(index+1); |
duke@1 | 184 | } |
duke@1 | 185 | return isValidClassName(s); |
duke@1 | 186 | } |
duke@1 | 187 | |
duke@1 | 188 | |
duke@1 | 189 | private final static char pathSep = File.pathSeparatorChar; |
duke@1 | 190 | |
duke@1 | 191 | /** |
duke@1 | 192 | * search all directories in path for subdirectory name. Add all |
duke@1 | 193 | * .java files found in such a directory to args. |
duke@1 | 194 | */ |
duke@1 | 195 | private void parsePackageClasses(String name, |
duke@1 | 196 | ListBuffer<JCCompilationUnit> trees, |
duke@1 | 197 | List<String> excludedPackages) |
duke@1 | 198 | throws IOException { |
duke@1 | 199 | if (excludedPackages.contains(name)) { |
duke@1 | 200 | return; |
duke@1 | 201 | } |
duke@1 | 202 | boolean hasFiles = false; |
duke@1 | 203 | docenv.notice("main.Loading_source_files_for_package", name); |
duke@1 | 204 | name = name.replace('.', File.separatorChar); |
duke@1 | 205 | for (File pathname : paths.sourceSearchPath()) { |
duke@1 | 206 | File f = new File(pathname, name); |
duke@1 | 207 | String names[] = f.list(); |
duke@1 | 208 | // if names not null, then found directory with source files |
duke@1 | 209 | if (names != null) { |
duke@1 | 210 | String dir = f.getAbsolutePath(); |
duke@1 | 211 | if (!dir.endsWith(File.separator)) |
duke@1 | 212 | dir = dir + File.separator; |
duke@1 | 213 | for (int j = 0; j < names.length; j++) { |
duke@1 | 214 | if (isValidJavaSourceFile(names[j])) { |
duke@1 | 215 | String fn = dir + names[j]; |
duke@1 | 216 | // messager.notice("main.Loading_source_file", fn); |
duke@1 | 217 | trees.append(parse(fn)); |
duke@1 | 218 | hasFiles = true; |
duke@1 | 219 | } |
duke@1 | 220 | } |
duke@1 | 221 | } |
duke@1 | 222 | } |
duke@1 | 223 | if (!hasFiles) |
duke@1 | 224 | messager.warning(null, "main.no_source_files_for_package", |
duke@1 | 225 | name.replace(File.separatorChar, '.')); |
duke@1 | 226 | } |
duke@1 | 227 | |
duke@1 | 228 | /** |
duke@1 | 229 | * Recursively search all directories in path for subdirectory name. |
duke@1 | 230 | * Add all packages found in such a directory to packages list. |
duke@1 | 231 | */ |
duke@1 | 232 | private void searchSubPackages(List<String> subPackages, |
duke@1 | 233 | ListBuffer<String> packages, |
duke@1 | 234 | List<String> excludedPackages) { |
duke@1 | 235 | // FIXME: This search path is bogus. |
duke@1 | 236 | // Only the effective source path should be searched for sources. |
duke@1 | 237 | // Only the effective class path should be searched for classes. |
duke@1 | 238 | // Should the bootclasspath/extdirs also be searched for classes? |
duke@1 | 239 | java.util.List<File> pathnames = new java.util.ArrayList<File>(); |
duke@1 | 240 | if (paths.sourcePath() != null) |
duke@1 | 241 | for (File elt : paths.sourcePath()) |
duke@1 | 242 | pathnames.add(elt); |
duke@1 | 243 | for (File elt : paths.userClassPath()) |
duke@1 | 244 | pathnames.add(elt); |
duke@1 | 245 | |
duke@1 | 246 | for (String subPackage : subPackages) |
duke@1 | 247 | searchSubPackage(subPackage, packages, excludedPackages, pathnames); |
duke@1 | 248 | } |
duke@1 | 249 | |
duke@1 | 250 | /** |
duke@1 | 251 | * Recursively search all directories in path for subdirectory name. |
duke@1 | 252 | * Add all packages found in such a directory to packages list. |
duke@1 | 253 | */ |
duke@1 | 254 | private void searchSubPackage(String packageName, |
duke@1 | 255 | ListBuffer<String> packages, |
duke@1 | 256 | List<String> excludedPackages, |
duke@1 | 257 | Collection<File> pathnames) { |
duke@1 | 258 | if (excludedPackages.contains(packageName)) |
duke@1 | 259 | return; |
duke@1 | 260 | |
duke@1 | 261 | String packageFilename = packageName.replace('.', File.separatorChar); |
duke@1 | 262 | boolean addedPackage = false; |
duke@1 | 263 | for (File pathname : pathnames) { |
duke@1 | 264 | File f = new File(pathname, packageFilename); |
duke@1 | 265 | String filenames[] = f.list(); |
duke@1 | 266 | // if filenames not null, then found directory |
duke@1 | 267 | if (filenames != null) { |
duke@1 | 268 | for (String filename : filenames) { |
duke@1 | 269 | if (!addedPackage |
duke@1 | 270 | && (isValidJavaSourceFile(filename) || |
duke@1 | 271 | isValidJavaClassFile(filename)) |
duke@1 | 272 | && !packages.contains(packageName)) { |
duke@1 | 273 | packages.append(packageName); |
duke@1 | 274 | addedPackage = true; |
duke@1 | 275 | } else if (isValidClassName(filename) && |
duke@1 | 276 | (new File(f, filename)).isDirectory()) { |
duke@1 | 277 | searchSubPackage(packageName + "." + filename, |
duke@1 | 278 | packages, excludedPackages, pathnames); |
duke@1 | 279 | } |
duke@1 | 280 | } |
duke@1 | 281 | } |
duke@1 | 282 | } |
duke@1 | 283 | } |
duke@1 | 284 | |
duke@1 | 285 | /** |
duke@1 | 286 | * Return true if given file name is a valid class file name. |
duke@1 | 287 | * @param file the name of the file to check. |
duke@1 | 288 | * @return true if given file name is a valid class file name |
duke@1 | 289 | * and false otherwise. |
duke@1 | 290 | */ |
duke@1 | 291 | private static boolean isValidJavaClassFile(String file) { |
duke@1 | 292 | if (!file.endsWith(".class")) return false; |
duke@1 | 293 | String clazzName = file.substring(0, file.length() - ".class".length()); |
duke@1 | 294 | return isValidClassName(clazzName); |
duke@1 | 295 | } |
duke@1 | 296 | |
duke@1 | 297 | /** |
duke@1 | 298 | * Return true if given file name is a valid Java source file name. |
duke@1 | 299 | * @param file the name of the file to check. |
duke@1 | 300 | * @return true if given file name is a valid Java source file name |
duke@1 | 301 | * and false otherwise. |
duke@1 | 302 | */ |
duke@1 | 303 | private static boolean isValidJavaSourceFile(String file) { |
duke@1 | 304 | if (!file.endsWith(".java")) return false; |
duke@1 | 305 | String clazzName = file.substring(0, file.length() - ".java".length()); |
duke@1 | 306 | return isValidClassName(clazzName); |
duke@1 | 307 | } |
duke@1 | 308 | |
duke@1 | 309 | /** Are surrogates supported? |
duke@1 | 310 | */ |
duke@1 | 311 | final static boolean surrogatesSupported = surrogatesSupported(); |
duke@1 | 312 | private static boolean surrogatesSupported() { |
duke@1 | 313 | try { |
duke@1 | 314 | boolean b = Character.isHighSurrogate('a'); |
duke@1 | 315 | return true; |
duke@1 | 316 | } catch (NoSuchMethodError ex) { |
duke@1 | 317 | return false; |
duke@1 | 318 | } |
duke@1 | 319 | } |
duke@1 | 320 | |
duke@1 | 321 | /** |
duke@1 | 322 | * Return true if given file name is a valid class name |
duke@1 | 323 | * (including "package-info"). |
duke@1 | 324 | * @param clazzname the name of the class to check. |
duke@1 | 325 | * @return true if given class name is a valid class name |
duke@1 | 326 | * and false otherwise. |
duke@1 | 327 | */ |
duke@1 | 328 | public static boolean isValidClassName(String s) { |
duke@1 | 329 | if (s.length() < 1) return false; |
duke@1 | 330 | if (s.equals("package-info")) return true; |
duke@1 | 331 | if (surrogatesSupported) { |
duke@1 | 332 | int cp = s.codePointAt(0); |
duke@1 | 333 | if (!Character.isJavaIdentifierStart(cp)) |
duke@1 | 334 | return false; |
duke@1 | 335 | for (int j=Character.charCount(cp); j<s.length(); j+=Character.charCount(cp)) { |
duke@1 | 336 | cp = s.codePointAt(j); |
duke@1 | 337 | if (!Character.isJavaIdentifierPart(cp)) |
duke@1 | 338 | return false; |
duke@1 | 339 | } |
duke@1 | 340 | } else { |
duke@1 | 341 | if (!Character.isJavaIdentifierStart(s.charAt(0))) |
duke@1 | 342 | return false; |
duke@1 | 343 | for (int j=1; j<s.length(); j++) |
duke@1 | 344 | if (!Character.isJavaIdentifierPart(s.charAt(j))) |
duke@1 | 345 | return false; |
duke@1 | 346 | } |
duke@1 | 347 | return true; |
duke@1 | 348 | } |
duke@1 | 349 | |
duke@1 | 350 | /** |
duke@1 | 351 | * From a list of top level trees, return the list of contained class definitions |
duke@1 | 352 | */ |
duke@1 | 353 | List<JCClassDecl> listClasses(List<JCCompilationUnit> trees) { |
duke@1 | 354 | ListBuffer<JCClassDecl> result = new ListBuffer<JCClassDecl>(); |
duke@1 | 355 | for (JCCompilationUnit t : trees) { |
duke@1 | 356 | for (JCTree def : t.defs) { |
duke@1 | 357 | if (def.getTag() == JCTree.CLASSDEF) |
duke@1 | 358 | result.append((JCClassDecl)def); |
duke@1 | 359 | } |
duke@1 | 360 | } |
duke@1 | 361 | return result.toList(); |
duke@1 | 362 | } |
duke@1 | 363 | |
duke@1 | 364 | } |