aoqi@0: /* aoqi@0: * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved. aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. aoqi@0: * aoqi@0: * This code is free software; you can redistribute it and/or modify it aoqi@0: * under the terms of the GNU General Public License version 2 only, as aoqi@0: * published by the Free Software Foundation. Oracle designates this aoqi@0: * particular file as subject to the "Classpath" exception as provided aoqi@0: * by Oracle in the LICENSE file that accompanied this code. aoqi@0: * aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that aoqi@0: * accompanied this code). aoqi@0: * aoqi@0: * You should have received a copy of the GNU General Public License version aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation, aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. aoqi@0: * aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA aoqi@0: * or visit www.oracle.com if you need additional information or have any aoqi@0: * questions. aoqi@0: */ aoqi@0: aoqi@0: package com.sun.tools.javadoc; aoqi@0: aoqi@0: import java.io.IOException; aoqi@0: import java.util.Collection; aoqi@0: import java.util.Locale; aoqi@0: aoqi@0: import javax.tools.JavaFileManager; aoqi@0: import javax.tools.JavaFileObject; aoqi@0: import javax.tools.StandardJavaFileManager; aoqi@0: aoqi@0: import com.sun.javadoc.*; aoqi@0: import com.sun.tools.javac.tree.JCTree.JCClassDecl; aoqi@0: import com.sun.tools.javac.util.List; aoqi@0: import com.sun.tools.javac.util.ListBuffer; aoqi@0: import com.sun.tools.javac.util.Position; aoqi@0: aoqi@0: /** aoqi@0: * This class holds the information from one run of javadoc. aoqi@0: * Particularly the packages, classes and options specified aoqi@0: * by the user. aoqi@0: * aoqi@0: *

This is NOT part of any supported API. aoqi@0: * If you write code that depends on this, you do so at your own risk. aoqi@0: * This code and its internal interfaces are subject to change or aoqi@0: * deletion without notice. aoqi@0: * aoqi@0: * @since 1.2 aoqi@0: * @author Robert Field aoqi@0: * @author Atul M Dambalkar aoqi@0: * @author Neal Gafter (rewrite) aoqi@0: */ aoqi@0: public class RootDocImpl extends DocImpl implements RootDoc { aoqi@0: aoqi@0: /** aoqi@0: * list of classes specified on the command line. aoqi@0: */ aoqi@0: private List cmdLineClasses; aoqi@0: aoqi@0: /** aoqi@0: * list of packages specified on the command line. aoqi@0: */ aoqi@0: private List cmdLinePackages; aoqi@0: aoqi@0: /** aoqi@0: * a collection of all options. aoqi@0: */ aoqi@0: private List options; aoqi@0: aoqi@0: /** aoqi@0: * Constructor used when reading source files. aoqi@0: * aoqi@0: * @param env the documentation environment, state for this javadoc run aoqi@0: * @param classes list of classes specified on the commandline aoqi@0: * @param packages list of package names specified on the commandline aoqi@0: * @param options list of options aoqi@0: */ aoqi@0: public RootDocImpl(DocEnv env, List classes, List packages, List options) { aoqi@0: super(env, null); aoqi@0: this.options = options; aoqi@0: setPackages(env, packages); aoqi@0: setClasses(env, classes); aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Constructor used when reading class files. aoqi@0: * aoqi@0: * @param env the documentation environment, state for this javadoc run aoqi@0: * @param classes list of class names specified on the commandline aoqi@0: * @param options list of options aoqi@0: */ aoqi@0: public RootDocImpl(DocEnv env, List classes, List options) { aoqi@0: super(env, null); aoqi@0: this.options = options; aoqi@0: cmdLinePackages = List.nil(); aoqi@0: ListBuffer classList = new ListBuffer(); aoqi@0: for (String className : classes) { aoqi@0: ClassDocImpl c = env.loadClass(className); aoqi@0: if (c == null) aoqi@0: env.error(null, "javadoc.class_not_found", className); aoqi@0: else aoqi@0: classList = classList.append(c); aoqi@0: } aoqi@0: cmdLineClasses = classList.toList(); aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Initialize classes information. Those classes are input from aoqi@0: * command line. aoqi@0: * aoqi@0: * @param env the compilation environment aoqi@0: * @param classes a list of ClassDeclaration aoqi@0: */ aoqi@0: private void setClasses(DocEnv env, List classes) { aoqi@0: ListBuffer result = new ListBuffer(); aoqi@0: for (JCClassDecl def : classes) { aoqi@0: //### Do we want modifier check here? aoqi@0: if (env.shouldDocument(def.sym)) { aoqi@0: ClassDocImpl cd = env.getClassDoc(def.sym); aoqi@0: if (cd != null) { aoqi@0: cd.isIncluded = true; aoqi@0: result.append(cd); aoqi@0: } //else System.out.println(" (classdoc is null)");//DEBUG aoqi@0: } //else System.out.println(" (env.shouldDocument() returned false)");//DEBUG aoqi@0: } aoqi@0: cmdLineClasses = result.toList(); aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Initialize packages information. aoqi@0: * aoqi@0: * @param env the compilation environment aoqi@0: * @param packages a list of package names (String) aoqi@0: */ aoqi@0: private void setPackages(DocEnv env, List packages) { aoqi@0: ListBuffer packlist = new ListBuffer(); aoqi@0: for (String name : packages) { aoqi@0: PackageDocImpl pkg = env.lookupPackage(name); aoqi@0: if (pkg != null) { aoqi@0: pkg.isIncluded = true; aoqi@0: packlist.append(pkg); aoqi@0: } else { aoqi@0: env.warning(null, "main.no_source_files_for_package", name); aoqi@0: } aoqi@0: } aoqi@0: cmdLinePackages = packlist.toList(); aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Command line options. aoqi@0: * aoqi@0: *

aoqi@0:      * For example, given:
aoqi@0:      *     javadoc -foo this that -bar other ...
aoqi@0:      *
aoqi@0:      * This method will return:
aoqi@0:      *      options()[0][0] = "-foo"
aoqi@0:      *      options()[0][1] = "this"
aoqi@0:      *      options()[0][2] = "that"
aoqi@0:      *      options()[1][0] = "-bar"
aoqi@0:      *      options()[1][1] = "other"
aoqi@0:      * 
aoqi@0: * aoqi@0: * @return an array of arrays of String. aoqi@0: */ aoqi@0: public String[][] options() { aoqi@0: return options.toArray(new String[options.length()][]); aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Packages specified on the command line. aoqi@0: */ aoqi@0: public PackageDoc[] specifiedPackages() { aoqi@0: return (PackageDoc[])cmdLinePackages aoqi@0: .toArray(new PackageDocImpl[cmdLinePackages.length()]); aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Classes and interfaces specified on the command line. aoqi@0: */ aoqi@0: public ClassDoc[] specifiedClasses() { aoqi@0: ListBuffer classesToDocument = new ListBuffer(); aoqi@0: for (ClassDocImpl cd : cmdLineClasses) { aoqi@0: cd.addAllClasses(classesToDocument, true); aoqi@0: } aoqi@0: return (ClassDoc[])classesToDocument.toArray(new ClassDocImpl[classesToDocument.length()]); aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Return all classes and interfaces (including those inside aoqi@0: * packages) to be documented. aoqi@0: */ aoqi@0: public ClassDoc[] classes() { aoqi@0: ListBuffer classesToDocument = new ListBuffer(); aoqi@0: for (ClassDocImpl cd : cmdLineClasses) { aoqi@0: cd.addAllClasses(classesToDocument, true); aoqi@0: } aoqi@0: for (PackageDocImpl pd : cmdLinePackages) { aoqi@0: pd.addAllClassesTo(classesToDocument); aoqi@0: } aoqi@0: return classesToDocument.toArray(new ClassDocImpl[classesToDocument.length()]); aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Return a ClassDoc for the specified class/interface name aoqi@0: * aoqi@0: * @param qualifiedName qualified class name aoqi@0: * (i.e. includes package name). aoqi@0: * aoqi@0: * @return a ClassDocImpl holding the specified class, null if aoqi@0: * this class is not referenced. aoqi@0: */ aoqi@0: public ClassDoc classNamed(String qualifiedName) { aoqi@0: return env.lookupClass(qualifiedName); aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Return a PackageDoc for the specified package name aoqi@0: * aoqi@0: * @param name package name aoqi@0: * aoqi@0: * @return a PackageDoc holding the specified package, null if aoqi@0: * this package is not referenced. aoqi@0: */ aoqi@0: public PackageDoc packageNamed(String name) { aoqi@0: return env.lookupPackage(name); aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Return the name of this Doc item. aoqi@0: * aoqi@0: * @return the string "*RootDocImpl*". aoqi@0: */ aoqi@0: public String name() { aoqi@0: return "*RootDocImpl*"; aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Return the name of this Doc item. aoqi@0: * aoqi@0: * @return the string "*RootDocImpl*". aoqi@0: */ aoqi@0: public String qualifiedName() { aoqi@0: return "*RootDocImpl*"; aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Return true if this Doc is include in the active set. aoqi@0: * RootDocImpl isn't even a program entity so it is always false. aoqi@0: */ aoqi@0: public boolean isIncluded() { aoqi@0: return false; aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Print error message, increment error count. aoqi@0: * aoqi@0: * @param msg message to print aoqi@0: */ aoqi@0: public void printError(String msg) { aoqi@0: env.printError(msg); aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Print error message, increment error count. aoqi@0: * aoqi@0: * @param msg message to print aoqi@0: */ aoqi@0: public void printError(SourcePosition pos, String msg) { aoqi@0: env.printError(pos, msg); aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Print warning message, increment warning count. aoqi@0: * aoqi@0: * @param msg message to print aoqi@0: */ aoqi@0: public void printWarning(String msg) { aoqi@0: env.printWarning(msg); aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Print warning message, increment warning count. aoqi@0: * aoqi@0: * @param msg message to print aoqi@0: */ aoqi@0: public void printWarning(SourcePosition pos, String msg) { aoqi@0: env.printWarning(pos, msg); aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Print a message. aoqi@0: * aoqi@0: * @param msg message to print aoqi@0: */ aoqi@0: public void printNotice(String msg) { aoqi@0: env.printNotice(msg); aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Print a message. aoqi@0: * aoqi@0: * @param msg message to print aoqi@0: */ aoqi@0: public void printNotice(SourcePosition pos, String msg) { aoqi@0: env.printNotice(pos, msg); aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Return the path of the overview file and null if it does not exist. aoqi@0: * @return the path of the overview file and null if it does not exist. aoqi@0: */ aoqi@0: private JavaFileObject getOverviewPath() { aoqi@0: for (String[] opt : options) { aoqi@0: if (opt[0].equals("-overview")) { aoqi@0: if (env.fileManager instanceof StandardJavaFileManager) { aoqi@0: StandardJavaFileManager fm = (StandardJavaFileManager) env.fileManager; aoqi@0: return fm.getJavaFileObjects(opt[1]).iterator().next(); aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: return null; aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Do lazy initialization of "documentation" string. aoqi@0: */ aoqi@0: @Override aoqi@0: protected String documentation() { aoqi@0: if (documentation == null) { aoqi@0: JavaFileObject overviewPath = getOverviewPath(); aoqi@0: if (overviewPath == null) { aoqi@0: // no doc file to be had aoqi@0: documentation = ""; aoqi@0: } else { aoqi@0: // read from file aoqi@0: try { aoqi@0: documentation = readHTMLDocumentation( aoqi@0: overviewPath.openInputStream(), aoqi@0: overviewPath); aoqi@0: } catch (IOException exc) { aoqi@0: documentation = ""; aoqi@0: env.error(null, "javadoc.File_Read_Error", overviewPath.getName()); aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: return documentation; aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Return the source position of the entity, or null if aoqi@0: * no position is available. aoqi@0: */ aoqi@0: @Override aoqi@0: public SourcePosition position() { aoqi@0: JavaFileObject path; aoqi@0: return ((path = getOverviewPath()) == null) ? aoqi@0: null : aoqi@0: SourcePositionImpl.make(path, Position.NOPOS, null); aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Return the locale provided by the user or the default locale value. aoqi@0: */ aoqi@0: public Locale getLocale() { aoqi@0: return env.doclocale.locale; aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Return the current file manager. aoqi@0: */ aoqi@0: public JavaFileManager getFileManager() { aoqi@0: return env.fileManager; aoqi@0: } aoqi@0: aoqi@0: public void initDocLint(Collection opts, Collection customTagNames) { aoqi@0: env.initDoclint(opts, customTagNames); aoqi@0: } aoqi@0: aoqi@0: public boolean isFunctionalInterface(AnnotationDesc annotationDesc) { aoqi@0: return annotationDesc.annotationType().qualifiedName().equals( aoqi@0: env.syms.functionalInterfaceType.toString()) && env.source.allowLambda(); aoqi@0: } aoqi@0: aoqi@0: public boolean showTagMessages() { aoqi@0: return env.showTagMessages(); aoqi@0: } aoqi@0: }