Path: blob/aarch64-shenandoah-jdk8u272-b10/langtools/src/share/classes/com/sun/tools/javadoc/RootDocImpl.java
38899 views
/*1* Copyright (c) 1997, 2016, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package com.sun.tools.javadoc;2627import java.io.IOException;28import java.util.Collection;29import java.util.Locale;3031import javax.tools.JavaFileManager;32import javax.tools.JavaFileObject;33import javax.tools.StandardJavaFileManager;3435import com.sun.javadoc.*;36import com.sun.tools.javac.tree.JCTree.JCClassDecl;37import com.sun.tools.javac.util.List;38import com.sun.tools.javac.util.ListBuffer;39import com.sun.tools.javac.util.Position;4041/**42* This class holds the information from one run of javadoc.43* Particularly the packages, classes and options specified44* by the user.45*46* <p><b>This is NOT part of any supported API.47* If you write code that depends on this, you do so at your own risk.48* This code and its internal interfaces are subject to change or49* deletion without notice.</b>50*51* @since 1.252* @author Robert Field53* @author Atul M Dambalkar54* @author Neal Gafter (rewrite)55*/56public class RootDocImpl extends DocImpl implements RootDoc {5758/**59* list of classes specified on the command line.60*/61private List<ClassDocImpl> cmdLineClasses;6263/**64* list of packages specified on the command line.65*/66private List<PackageDocImpl> cmdLinePackages;6768/**69* a collection of all options.70*/71private List<String[]> options;7273/**74* Constructor used when reading source files.75*76* @param env the documentation environment, state for this javadoc run77* @param classes list of classes specified on the commandline78* @param packages list of package names specified on the commandline79* @param options list of options80*/81public RootDocImpl(DocEnv env, List<JCClassDecl> classes, List<String> packages, List<String[]> options) {82super(env, null);83this.options = options;84setPackages(env, packages);85setClasses(env, classes);86}8788/**89* Constructor used when reading class files.90*91* @param env the documentation environment, state for this javadoc run92* @param classes list of class names specified on the commandline93* @param options list of options94*/95public RootDocImpl(DocEnv env, List<String> classes, List<String[]> options) {96super(env, null);97this.options = options;98cmdLinePackages = List.nil();99ListBuffer<ClassDocImpl> classList = new ListBuffer<ClassDocImpl>();100for (String className : classes) {101ClassDocImpl c = env.loadClass(className);102if (c == null)103env.error(null, "javadoc.class_not_found", className);104else105classList = classList.append(c);106}107cmdLineClasses = classList.toList();108}109110/**111* Initialize classes information. Those classes are input from112* command line.113*114* @param env the compilation environment115* @param classes a list of ClassDeclaration116*/117private void setClasses(DocEnv env, List<JCClassDecl> classes) {118ListBuffer<ClassDocImpl> result = new ListBuffer<ClassDocImpl>();119for (JCClassDecl def : classes) {120//### Do we want modifier check here?121if (env.shouldDocument(def.sym)) {122ClassDocImpl cd = env.getClassDoc(def.sym);123if (cd != null) {124cd.isIncluded = true;125result.append(cd);126} //else System.out.println(" (classdoc is null)");//DEBUG127} //else System.out.println(" (env.shouldDocument() returned false)");//DEBUG128}129cmdLineClasses = result.toList();130}131132/**133* Initialize packages information.134*135* @param env the compilation environment136* @param packages a list of package names (String)137*/138private void setPackages(DocEnv env, List<String> packages) {139ListBuffer<PackageDocImpl> packlist = new ListBuffer<PackageDocImpl>();140for (String name : packages) {141PackageDocImpl pkg = env.lookupPackage(name);142if (pkg != null) {143pkg.isIncluded = true;144packlist.append(pkg);145} else {146env.warning(null, "main.no_source_files_for_package", name);147}148}149cmdLinePackages = packlist.toList();150}151152/**153* Command line options.154*155* <pre>156* For example, given:157* javadoc -foo this that -bar other ...158*159* This method will return:160* options()[0][0] = "-foo"161* options()[0][1] = "this"162* options()[0][2] = "that"163* options()[1][0] = "-bar"164* options()[1][1] = "other"165* </pre>166*167* @return an array of arrays of String.168*/169public String[][] options() {170return options.toArray(new String[options.length()][]);171}172173/**174* Packages specified on the command line.175*/176public PackageDoc[] specifiedPackages() {177return (PackageDoc[])cmdLinePackages178.toArray(new PackageDocImpl[cmdLinePackages.length()]);179}180181/**182* Classes and interfaces specified on the command line.183*/184public ClassDoc[] specifiedClasses() {185ListBuffer<ClassDocImpl> classesToDocument = new ListBuffer<ClassDocImpl>();186for (ClassDocImpl cd : cmdLineClasses) {187cd.addAllClasses(classesToDocument, true);188}189return (ClassDoc[])classesToDocument.toArray(new ClassDocImpl[classesToDocument.length()]);190}191192/**193* Return all classes and interfaces (including those inside194* packages) to be documented.195*/196public ClassDoc[] classes() {197ListBuffer<ClassDocImpl> classesToDocument = new ListBuffer<ClassDocImpl>();198for (ClassDocImpl cd : cmdLineClasses) {199cd.addAllClasses(classesToDocument, true);200}201for (PackageDocImpl pd : cmdLinePackages) {202pd.addAllClassesTo(classesToDocument);203}204return classesToDocument.toArray(new ClassDocImpl[classesToDocument.length()]);205}206207/**208* Return a ClassDoc for the specified class/interface name209*210* @param qualifiedName qualified class name211* (i.e. includes package name).212*213* @return a ClassDocImpl holding the specified class, null if214* this class is not referenced.215*/216public ClassDoc classNamed(String qualifiedName) {217return env.lookupClass(qualifiedName);218}219220/**221* Return a PackageDoc for the specified package name222*223* @param name package name224*225* @return a PackageDoc holding the specified package, null if226* this package is not referenced.227*/228public PackageDoc packageNamed(String name) {229return env.lookupPackage(name);230}231232/**233* Return the name of this Doc item.234*235* @return the string <code>"*RootDocImpl*"</code>.236*/237public String name() {238return "*RootDocImpl*";239}240241/**242* Return the name of this Doc item.243*244* @return the string <code>"*RootDocImpl*"</code>.245*/246public String qualifiedName() {247return "*RootDocImpl*";248}249250/**251* Return true if this Doc is include in the active set.252* RootDocImpl isn't even a program entity so it is always false.253*/254public boolean isIncluded() {255return false;256}257258/**259* Print error message, increment error count.260*261* @param msg message to print262*/263public void printError(String msg) {264env.printError(msg);265}266267/**268* Print error message, increment error count.269*270* @param msg message to print271*/272public void printError(SourcePosition pos, String msg) {273env.printError(pos, msg);274}275276/**277* Print warning message, increment warning count.278*279* @param msg message to print280*/281public void printWarning(String msg) {282env.printWarning(msg);283}284285/**286* Print warning message, increment warning count.287*288* @param msg message to print289*/290public void printWarning(SourcePosition pos, String msg) {291env.printWarning(pos, msg);292}293294/**295* Print a message.296*297* @param msg message to print298*/299public void printNotice(String msg) {300env.printNotice(msg);301}302303/**304* Print a message.305*306* @param msg message to print307*/308public void printNotice(SourcePosition pos, String msg) {309env.printNotice(pos, msg);310}311312/**313* Return the path of the overview file and null if it does not exist.314* @return the path of the overview file and null if it does not exist.315*/316private JavaFileObject getOverviewPath() {317for (String[] opt : options) {318if (opt[0].equals("-overview")) {319if (env.fileManager instanceof StandardJavaFileManager) {320StandardJavaFileManager fm = (StandardJavaFileManager) env.fileManager;321return fm.getJavaFileObjects(opt[1]).iterator().next();322}323}324}325return null;326}327328/**329* Do lazy initialization of "documentation" string.330*/331@Override332protected String documentation() {333if (documentation == null) {334JavaFileObject overviewPath = getOverviewPath();335if (overviewPath == null) {336// no doc file to be had337documentation = "";338} else {339// read from file340try {341documentation = readHTMLDocumentation(342overviewPath.openInputStream(),343overviewPath);344} catch (IOException exc) {345documentation = "";346env.error(null, "javadoc.File_Read_Error", overviewPath.getName());347}348}349}350return documentation;351}352353/**354* Return the source position of the entity, or null if355* no position is available.356*/357@Override358public SourcePosition position() {359JavaFileObject path;360return ((path = getOverviewPath()) == null) ?361null :362SourcePositionImpl.make(path, Position.NOPOS, null);363}364365/**366* Return the locale provided by the user or the default locale value.367*/368public Locale getLocale() {369return env.doclocale.locale;370}371372/**373* Return the current file manager.374*/375public JavaFileManager getFileManager() {376return env.fileManager;377}378379public void initDocLint(Collection<String> opts, Collection<String> customTagNames) {380env.initDoclint(opts, customTagNames);381}382383public JavaScriptScanner initJavaScriptScanner(boolean allowScriptInComments) {384return env.initJavaScriptScanner(allowScriptInComments);385}386387public boolean isFunctionalInterface(AnnotationDesc annotationDesc) {388return annotationDesc.annotationType().qualifiedName().equals(389env.syms.functionalInterfaceType.toString()) && env.source.allowLambda();390}391392public boolean showTagMessages() {393return env.showTagMessages();394}395}396397398