Path: blob/aarch64-shenandoah-jdk8u272-b10/langtools/src/share/classes/com/sun/tools/javadoc/PackageDocImpl.java
38899 views
/*1* Copyright (c) 1997, 2013, 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.io.InputStream;2930import javax.tools.FileObject;3132import com.sun.javadoc.*;33import com.sun.source.util.TreePath;34import com.sun.tools.javac.code.Attribute;35import com.sun.tools.javac.code.Scope;36import com.sun.tools.javac.code.Symbol.ClassSymbol;37import com.sun.tools.javac.code.Symbol.PackageSymbol;38import com.sun.tools.javac.tree.JCTree;39import com.sun.tools.javac.tree.JCTree.JCCompilationUnit;40import com.sun.tools.javac.util.List;41import com.sun.tools.javac.util.ListBuffer;42import com.sun.tools.javac.util.Name;43import com.sun.tools.javac.util.Position;4445/**46* Represents a java package. Provides access to information47* about the package, the package's comment and tags, and the48* classes in the package.49*50* <p><b>This is NOT part of any supported API.51* If you write code that depends on this, you do so at your own risk.52* This code and its internal interfaces are subject to change or53* deletion without notice.</b>54*55* @since 1.256* @author Kaiyang Liu (original)57* @author Robert Field (rewrite)58* @author Neal Gafter (rewrite)59* @author Scott Seligman (package-info.java)60*/6162public class PackageDocImpl extends DocImpl implements PackageDoc {6364protected PackageSymbol sym;65private JCCompilationUnit tree = null; // for source position6667public FileObject docPath = null;68private boolean foundDoc; // found a doc comment in either69// package.html or package-info.java7071boolean isIncluded = false; // Set in RootDocImpl.72public boolean setDocPath = false; //Flag to avoid setting doc path multiple times.7374/**75* Constructor76*/77public PackageDocImpl(DocEnv env, PackageSymbol sym) {78this(env, sym, null);79}8081/**82* Constructor83*/84public PackageDocImpl(DocEnv env, PackageSymbol sym, TreePath treePath) {85super(env, treePath);86this.sym = sym;87this.tree = (treePath == null) ? null : (JCCompilationUnit) treePath.getCompilationUnit();88foundDoc = (documentation != null);89}9091void setTree(JCTree tree) {92this.tree = (JCCompilationUnit) tree;93}9495public void setTreePath(TreePath treePath) {96super.setTreePath(treePath);97checkDoc();98}99100/**101* Do lazy initialization of "documentation" string.102*/103protected String documentation() {104if (documentation != null)105return documentation;106if (docPath != null) {107// read from file108try {109InputStream s = docPath.openInputStream();110documentation = readHTMLDocumentation(s, docPath);111} catch (IOException exc) {112documentation = "";113env.error(null, "javadoc.File_Read_Error", docPath.getName());114}115} else {116// no doc file to be had117documentation = "";118}119return documentation;120}121122/**123* Cache of all classes contained in this package, including124* member classes of those classes, and their member classes, etc.125* Includes only those classes at the specified protection level126* and weaker.127*/128private List<ClassDocImpl> allClassesFiltered = null;129130/**131* Cache of all classes contained in this package, including132* member classes of those classes, and their member classes, etc.133*/134private List<ClassDocImpl> allClasses = null;135136/**137* Return a list of all classes contained in this package, including138* member classes of those classes, and their member classes, etc.139*/140private List<ClassDocImpl> getClasses(boolean filtered) {141if (allClasses != null && !filtered) {142return allClasses;143}144if (allClassesFiltered != null && filtered) {145return allClassesFiltered;146}147ListBuffer<ClassDocImpl> classes = new ListBuffer<ClassDocImpl>();148for (Scope.Entry e = sym.members().elems; e != null; e = e.sibling) {149if (e.sym != null) {150ClassSymbol s = (ClassSymbol)e.sym;151ClassDocImpl c = env.getClassDoc(s);152if (c != null && !c.isSynthetic())153c.addAllClasses(classes, filtered);154}155}156if (filtered)157return allClassesFiltered = classes.toList();158else159return allClasses = classes.toList();160}161162/**163* Add all included classes (including Exceptions and Errors)164* and interfaces.165*/166public void addAllClassesTo(ListBuffer<ClassDocImpl> list) {167list.appendList(getClasses(true));168}169170/**171* Get all classes (including Exceptions and Errors)172* and interfaces.173* @since J2SE1.4.174*175* @return all classes and interfaces in this package, filtered to include176* only the included classes if filter==true.177*/178public ClassDoc[] allClasses(boolean filter) {179List<ClassDocImpl> classes = getClasses(filter);180return classes.toArray(new ClassDocImpl[classes.length()]);181}182183/**184* Get all included classes (including Exceptions and Errors)185* and interfaces. Same as allClasses(true).186*187* @return all included classes and interfaces in this package.188*/189public ClassDoc[] allClasses() {190return allClasses(true);191}192193/**194* Get ordinary classes (that is, exclude exceptions, errors,195* enums, interfaces, and annotation types) in this package.196*197* @return included ordinary classes in this package.198*/199public ClassDoc[] ordinaryClasses() {200ListBuffer<ClassDocImpl> ret = new ListBuffer<ClassDocImpl>();201for (ClassDocImpl c : getClasses(true)) {202if (c.isOrdinaryClass()) {203ret.append(c);204}205}206return ret.toArray(new ClassDocImpl[ret.length()]);207}208209/**210* Get Exception classes in this package.211*212* @return included Exceptions in this package.213*/214public ClassDoc[] exceptions() {215ListBuffer<ClassDocImpl> ret = new ListBuffer<ClassDocImpl>();216for (ClassDocImpl c : getClasses(true)) {217if (c.isException()) {218ret.append(c);219}220}221return ret.toArray(new ClassDocImpl[ret.length()]);222}223224/**225* Get Error classes in this package.226*227* @return included Errors in this package.228*/229public ClassDoc[] errors() {230ListBuffer<ClassDocImpl> ret = new ListBuffer<ClassDocImpl>();231for (ClassDocImpl c : getClasses(true)) {232if (c.isError()) {233ret.append(c);234}235}236return ret.toArray(new ClassDocImpl[ret.length()]);237}238239/**240* Get included enum types in this package.241*242* @return included enum types in this package.243*/244public ClassDoc[] enums() {245ListBuffer<ClassDocImpl> ret = new ListBuffer<ClassDocImpl>();246for (ClassDocImpl c : getClasses(true)) {247if (c.isEnum()) {248ret.append(c);249}250}251return ret.toArray(new ClassDocImpl[ret.length()]);252}253254/**255* Get included interfaces in this package, omitting annotation types.256*257* @return included interfaces in this package.258*/259public ClassDoc[] interfaces() {260ListBuffer<ClassDocImpl> ret = new ListBuffer<ClassDocImpl>();261for (ClassDocImpl c : getClasses(true)) {262if (c.isInterface()) {263ret.append(c);264}265}266return ret.toArray(new ClassDocImpl[ret.length()]);267}268269/**270* Get included annotation types in this package.271*272* @return included annotation types in this package.273*/274public AnnotationTypeDoc[] annotationTypes() {275ListBuffer<AnnotationTypeDocImpl> ret =276new ListBuffer<AnnotationTypeDocImpl>();277for (ClassDocImpl c : getClasses(true)) {278if (c.isAnnotationType()) {279ret.append((AnnotationTypeDocImpl)c);280}281}282return ret.toArray(new AnnotationTypeDocImpl[ret.length()]);283}284285/**286* Get the annotations of this package.287* Return an empty array if there are none.288*/289public AnnotationDesc[] annotations() {290AnnotationDesc res[] = new AnnotationDesc[sym.getRawAttributes().length()];291int i = 0;292for (Attribute.Compound a : sym.getRawAttributes()) {293res[i++] = new AnnotationDescImpl(env, a);294}295return res;296}297298299/**300* Lookup for a class within this package.301*302* @return ClassDocImpl of found class, or null if not found.303*/304public ClassDoc findClass(String className) {305final boolean filtered = true;306for (ClassDocImpl c : getClasses(filtered)) {307if (c.name().equals(className)) {308return c;309}310}311return null;312}313314/**315* Return true if this package is included in the active set.316*/317public boolean isIncluded() {318return isIncluded;319}320321/**322* Get package name.323*324* Note that we do not provide a means of obtaining the simple325* name of a package -- package names are always returned in their326* uniquely qualified form.327*/328public String name() {329return qualifiedName();330}331332/**333* Get package name.334*/335public String qualifiedName() {336if (qualifiedName == null) {337Name fullname = sym.getQualifiedName();338// Some bogus tests depend on the interned "" being returned.339// See 6457276.340qualifiedName = fullname.isEmpty() ? "" : fullname.toString();341}342return qualifiedName;343}344345private String qualifiedName;346347/**348* set doc path for an unzipped directory349*/350public void setDocPath(FileObject path) {351setDocPath = true;352if (path == null)353return;354if (!path.equals(docPath)) {355docPath = path;356checkDoc();357}358}359360// Has checkDoc() sounded off yet?361private boolean checkDocWarningEmitted = false;362363/**364* Invoked when a source of package doc comments is located.365* Emits a diagnostic if this is the second one.366*/367private void checkDoc() {368if (foundDoc) {369if (!checkDocWarningEmitted) {370env.warning(null, "javadoc.Multiple_package_comments", name());371checkDocWarningEmitted = true;372}373} else {374foundDoc = true;375}376}377378/**379* Return the source position of the entity, or null if380* no position is available.381*/382public SourcePosition position() {383return (tree != null)384? SourcePositionImpl.make(tree.sourcefile, tree.pos, tree.lineMap)385: SourcePositionImpl.make(docPath, Position.NOPOS, null);386}387}388389390