Path: blob/aarch64-shenandoah-jdk8u272-b10/langtools/src/share/classes/com/sun/tools/javadoc/DocImpl.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.DataInputStream;28import java.io.IOException;29import java.io.InputStream;30import java.text.CollationKey;31import java.util.regex.Matcher;32import java.util.regex.Pattern;3334import javax.tools.FileObject;3536import com.sun.javadoc.*;37import com.sun.source.util.TreePath;38import com.sun.tools.doclets.internal.toolkit.util.FatalError;39import com.sun.tools.javac.tree.JCTree;40import com.sun.tools.javac.tree.JCTree.JCCompilationUnit;41import com.sun.tools.javac.util.Position;4243/**44* abstract base class of all Doc classes. Doc item's are representations45* of java language constructs (class, package, method,...) which have46* comments and have been processed by this run of javadoc. All Doc items47* are unique, that is, they are == comparable.48*49* <p><b>This is NOT part of any supported API.50* If you write code that depends on this, you do so at your own risk.51* This code and its internal interfaces are subject to change or52* deletion without notice.</b>53*54* @since 1.255* @author Robert Field56* @author Atul M Dambalkar57* @author Neal Gafter (rewrite)58*/59public abstract class DocImpl implements Doc, Comparable<Object> {6061/**62* Doc environment63*/64protected final DocEnv env; //### Rename this everywhere to 'docenv' ?6566/**67* Back pointer to the tree node for this doc item.68* May be null if there is no associated tree.69*/70protected TreePath treePath;7172/**73* The complex comment object, lazily initialized.74*/75private Comment comment;7677/**78* The cached sort key, to take care of Natural Language Text sorting.79*/80private CollationKey collationkey = null;8182/**83* Raw documentation string.84*/85protected String documentation; // Accessed in PackageDocImpl, RootDocImpl8687/**88* Cached first sentence.89*/90private Tag[] firstSentence;9192/**93* Cached inline tags.94*/95private Tag[] inlineTags;9697/**98* Constructor.99*/100DocImpl(DocEnv env, TreePath treePath) {101this.treePath = treePath;102this.documentation = getCommentText(treePath);103this.env = env;104}105106private static String getCommentText(TreePath p) {107if (p == null)108return null;109110JCCompilationUnit topLevel = (JCCompilationUnit) p.getCompilationUnit();111JCTree tree = (JCTree) p.getLeaf();112return topLevel.docComments.getCommentText(tree);113}114115/**116* So subclasses have the option to do lazy initialization of117* "documentation" string.118*/119protected String documentation() {120if (documentation == null) documentation = "";121return documentation;122}123124/**125* For lazy initialization of comment.126*/127Comment comment() {128if (comment == null) {129String d = documentation();130if (env.javaScriptScanner != null) {131env.javaScriptScanner.parse(d, new JavaScriptScanner.Reporter() {132@Override133public void report() {134env.error(DocImpl.this, "javadoc.JavaScript_in_comment");135throw new FatalError();136}137});138}139if (env.doclint != null140&& treePath != null141&& d.equals(getCommentText(treePath))) {142env.doclint.scan(treePath);143}144comment = new Comment(this, d);145}146return comment;147}148149/**150* Return the text of the comment for this doc item.151* TagImpls have been removed.152*/153public String commentText() {154return comment().commentText();155}156157/**158* Return all tags in this Doc item.159*160* @return an array of TagImpl containing all tags on this Doc item.161*/162public Tag[] tags() {163return comment().tags();164}165166/**167* Return tags of the specified kind in this Doc item.168*169* @param tagname name of the tag kind to search for.170* @return an array of TagImpl containing all tags whose 'kind()'171* matches 'tagname'.172*/173public Tag[] tags(String tagname) {174return comment().tags(tagname);175}176177/**178* Return the see also tags in this Doc item.179*180* @return an array of SeeTag containing all @see tags.181*/182public SeeTag[] seeTags() {183return comment().seeTags();184}185186public Tag[] inlineTags() {187if (inlineTags == null) {188inlineTags = Comment.getInlineTags(this, commentText());189}190return inlineTags;191}192193public Tag[] firstSentenceTags() {194if (firstSentence == null) {195//Parse all sentences first to avoid duplicate warnings.196inlineTags();197try {198env.setSilent(true);199firstSentence = Comment.firstSentenceTags(this, commentText());200} finally {201env.setSilent(false);202}203}204return firstSentence;205}206207/**208* Utility for subclasses which read HTML documentation files.209*/210String readHTMLDocumentation(InputStream input, FileObject filename) throws IOException {211byte[] filecontents = new byte[input.available()];212try {213DataInputStream dataIn = new DataInputStream(input);214dataIn.readFully(filecontents);215} finally {216input.close();217}218String encoding = env.getEncoding();219String rawDoc = (encoding!=null)220? new String(filecontents, encoding)221: new String(filecontents);222Pattern bodyPat = Pattern.compile("(?is).*<body\\b[^>]*>(.*)</body\\b.*");223Matcher m = bodyPat.matcher(rawDoc);224if (m.matches()) {225return m.group(1);226} else {227String key = rawDoc.matches("(?is).*<body\\b.*")228? "javadoc.End_body_missing_from_html_file"229: "javadoc.Body_missing_from_html_file";230env.error(SourcePositionImpl.make(filename, Position.NOPOS, null), key);231return "";232}233}234235/**236* Return the full unprocessed text of the comment. Tags237* are included as text. Used mainly for store and retrieve238* operations like internalization.239*/240public String getRawCommentText() {241return documentation();242}243244/**245* Set the full unprocessed text of the comment. Tags246* are included as text. Used mainly for store and retrieve247* operations like internalization.248*/249public void setRawCommentText(String rawDocumentation) {250treePath = null;251documentation = rawDocumentation;252comment = null;253}254255/**256* Set the full unprocessed text of the comment and tree path.257*/258void setTreePath(TreePath treePath) {259this.treePath = treePath;260documentation = getCommentText(treePath);261comment = null;262}263264/**265* return a key for sorting.266*/267CollationKey key() {268if (collationkey == null) {269collationkey = generateKey();270}271return collationkey;272}273274/**275* Generate a key for sorting.276* <p>277* Default is name().278*/279CollationKey generateKey() {280String k = name();281// System.out.println("COLLATION KEY FOR " + this + " is \"" + k + "\"");282return env.doclocale.collator.getCollationKey(k);283}284285/**286* Returns a string representation of this Doc item.287*/288@Override289public String toString() {290return qualifiedName();291}292293/**294* Returns the name of this Doc item.295*296* @return the name297*/298public abstract String name();299300/**301* Returns the qualified name of this Doc item.302*303* @return the name304*/305public abstract String qualifiedName();306307/**308* Compares this Object with the specified Object for order. Returns a309* negative integer, zero, or a positive integer as this Object is less310* than, equal to, or greater than the given Object.311* <p>312* Included so that Doc item are java.lang.Comparable.313*314* @param obj the {@code Object} to be compared.315* @return a negative integer, zero, or a positive integer as this Object316* is less than, equal to, or greater than the given Object.317* @exception ClassCastException the specified Object's type prevents it318* from being compared to this Object.319*/320public int compareTo(Object obj) {321// System.out.println("COMPARE \"" + this + "\" to \"" + obj + "\" = " + key().compareTo(((DocImpl)obj).key()));322return key().compareTo(((DocImpl)obj).key());323}324325/**326* Is this Doc item a field? False until overridden.327*328* @return true if it represents a field329*/330public boolean isField() {331return false;332}333334/**335* Is this Doc item an enum constant? False until overridden.336*337* @return true if it represents an enum constant338*/339public boolean isEnumConstant() {340return false;341}342343/**344* Is this Doc item a constructor? False until overridden.345*346* @return true if it represents a constructor347*/348public boolean isConstructor() {349return false;350}351352/**353* Is this Doc item a method (but not a constructor or annotation354* type element)?355* False until overridden.356*357* @return true if it represents a method358*/359public boolean isMethod() {360return false;361}362363/**364* Is this Doc item an annotation type element?365* False until overridden.366*367* @return true if it represents an annotation type element368*/369public boolean isAnnotationTypeElement() {370return false;371}372373/**374* Is this Doc item a interface (but not an annotation type)?375* False until overridden.376*377* @return true if it represents a interface378*/379public boolean isInterface() {380return false;381}382383/**384* Is this Doc item a exception class? False until overridden.385*386* @return true if it represents a exception387*/388public boolean isException() {389return false;390}391392/**393* Is this Doc item a error class? False until overridden.394*395* @return true if it represents a error396*/397public boolean isError() {398return false;399}400401/**402* Is this Doc item an enum type? False until overridden.403*404* @return true if it represents an enum type405*/406public boolean isEnum() {407return false;408}409410/**411* Is this Doc item an annotation type? False until overridden.412*413* @return true if it represents an annotation type414*/415public boolean isAnnotationType() {416return false;417}418419/**420* Is this Doc item an ordinary class (i.e. not an interface,421* annotation type, enumeration, exception, or error)?422* False until overridden.423*424* @return true if it represents an ordinary class425*/426public boolean isOrdinaryClass() {427return false;428}429430/**431* Is this Doc item a class432* (and not an interface or annotation type)?433* This includes ordinary classes, enums, errors and exceptions.434* False until overridden.435*436* @return true if it represents a class437*/438public boolean isClass() {439return false;440}441442/**443* return true if this Doc is include in the active set.444*/445public abstract boolean isIncluded();446447/**448* Return the source position of the entity, or null if449* no position is available.450*/451public SourcePosition position() { return null; }452}453454455