package jdk.javadoc.internal.doclets.toolkit.util;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
import javax.lang.model.element.Element;
import javax.lang.model.element.ModuleElement;
import javax.lang.model.element.PackageElement;
import javax.lang.model.element.TypeElement;
import javax.lang.model.type.TypeMirror;
import com.sun.source.doctree.AttributeTree;
import com.sun.source.doctree.AttributeTree.ValueKind;
import com.sun.source.doctree.AuthorTree;
import com.sun.source.doctree.BlockTagTree;
import com.sun.source.doctree.CommentTree;
import com.sun.source.doctree.DeprecatedTree;
import com.sun.source.doctree.DocCommentTree;
import com.sun.source.doctree.DocTree;
import com.sun.source.doctree.EndElementTree;
import com.sun.source.doctree.EntityTree;
import com.sun.source.doctree.IdentifierTree;
import com.sun.source.doctree.InlineTagTree;
import com.sun.source.doctree.LinkTree;
import com.sun.source.doctree.LiteralTree;
import com.sun.source.doctree.ParamTree;
import com.sun.source.doctree.ProvidesTree;
import com.sun.source.doctree.ReferenceTree;
import com.sun.source.doctree.ReturnTree;
import com.sun.source.doctree.SeeTree;
import com.sun.source.doctree.SerialDataTree;
import com.sun.source.doctree.SerialFieldTree;
import com.sun.source.doctree.SerialTree;
import com.sun.source.doctree.SinceTree;
import com.sun.source.doctree.StartElementTree;
import com.sun.source.doctree.TextTree;
import com.sun.source.doctree.ThrowsTree;
import com.sun.source.doctree.UnknownBlockTagTree;
import com.sun.source.doctree.UsesTree;
import com.sun.source.doctree.ValueTree;
import com.sun.source.doctree.VersionTree;
import com.sun.source.util.DocTreePath;
import com.sun.source.util.DocTrees;
import com.sun.source.util.SimpleDocTreeVisitor;
import com.sun.source.util.TreePath;
import jdk.javadoc.internal.doclets.toolkit.BaseConfiguration;
import static com.sun.source.doctree.DocTree.Kind.*;
public class {
private final BaseConfiguration ;
public final TreePath ;
public final DocCommentTree ;
public final Element ;
private Element ;
public static final String = " ";
public (BaseConfiguration configuration, Element element, TreePath path, DocCommentTree dcTree) {
this.configuration = configuration;
this.element = element;
this.path = path;
this.dcTree = dcTree;
}
public void (Element ove) {
if (this.element == ove) {
throw new AssertionError("cannot set given element as overridden element");
}
overriddenElement = ove;
}
public String (DocTree dtree) {
switch (dtree.getKind()) {
case AUTHOR:
case DEPRECATED:
case PARAM:
case PROVIDES:
case RETURN:
case SEE:
case SERIAL_DATA:
case SERIAL_FIELD:
case THROWS:
case UNKNOWN_BLOCK_TAG:
case USES:
case VERSION:
return ((BlockTagTree) dtree).getTagName();
case UNKNOWN_INLINE_TAG:
return ((InlineTagTree) dtree).getTagName();
case ERRONEOUS:
return "erroneous";
default:
return dtree.getKind().tagName;
}
}
public boolean (DocTree dtree) {
if (dtree.getKind() == PARAM) {
return ((ParamTree)dtree).isTypeParameter();
}
return false;
}
public String (DocTree dtree) {
if (dtree.getKind() == PARAM) {
return ((ParamTree) dtree).getName().toString();
} else {
return null;
}
}
Element (ReferenceTree rtree) {
Utils utils = configuration.utils;
if (path == null) {
TypeMirror symbol = utils.getSymbol(rtree.getSignature());
if (symbol == null) {
return null;
}
return configuration.docEnv.getTypeUtils().asElement(symbol);
}
if (dcTree == null && overriddenElement != null) {
CommentHelper ovch = utils.getCommentHelper(overriddenElement);
return ovch.getElement(rtree);
}
if (dcTree == null) {
return null;
}
DocTreePath docTreePath = DocTreePath.getPath(path, dcTree, rtree);
if (docTreePath == null) {
if (overriddenElement != null) {
CommentHelper ovch = utils.getCommentHelper(overriddenElement);
return ovch.getElement(rtree);
}
return null;
}
DocTrees doctrees = configuration.docEnv.getDocTrees();
return doctrees.getElement(docTreePath);
}
public TypeMirror (ReferenceTree rtree) {
DocTreePath docTreePath = DocTreePath.getPath(path, dcTree, rtree);
if (docTreePath != null) {
DocTrees doctrees = configuration.docEnv.getDocTrees();
return doctrees.getType(docTreePath);
}
return null;
}
public Element (DocTree dtree) {
if (dtree.getKind() == THROWS || dtree.getKind() == EXCEPTION) {
ThrowsTree tt = (ThrowsTree)dtree;
ReferenceTree exceptionName = tt.getExceptionName();
return getElement(exceptionName);
}
return null;
}
public List<? extends DocTree> (DocTree dtree) {
return getTags(dtree);
}
public String (List<? extends DocTree> list) {
StringBuilder sb = new StringBuilder();
for (DocTree dt : list) {
sb.append(getText0(dt));
}
return sb.toString();
}
public String (DocTree dt) {
return getText0(dt).toString();
}
private StringBuilder (DocTree dt) {
final StringBuilder sb = new StringBuilder();
new SimpleDocTreeVisitor<Void, Void>() {
@Override
public Void (AttributeTree node, Void p) {
sb.append(SPACER).append(node.getName());
if (node.getValueKind() == ValueKind.EMPTY) {
return null;
}
sb.append("=");
String quote;
switch (node.getValueKind()) {
case DOUBLE:
quote = "\"";
break;
case SINGLE:
quote = "'";
break;
default:
quote = "";
break;
}
sb.append(quote);
node.getValue().forEach(dt -> dt.accept(this, null));
sb.append(quote);
return null;
}
@Override
public Void (EndElementTree node, Void p) {
sb.append("</")
.append(node.getName())
.append(">");
return null;
}
@Override
public Void (EntityTree node, Void p) {
sb.append(node.toString());
return null;
}
@Override
public Void (LinkTree node, Void p) {
if (node.getReference() == null) {
return null;
}
node.getReference().accept(this, null);
node.getLabel().forEach(dt -> dt.accept(this, null));
return null;
}
@Override
public Void (LiteralTree node, Void p) {
if (node.getKind() == CODE) {
sb.append("<").append(node.getKind().tagName).append(">");
}
sb.append(node.getBody().toString());
if (node.getKind() == CODE) {
sb.append("</").append(node.getKind().tagName).append(">");
}
return null;
}
@Override
public Void (ReferenceTree node, Void p) {
sb.append(node.getSignature());
return null;
}
@Override
public Void (SeeTree node, Void p) {
node.getReference().forEach(dt -> dt.accept(this, null));
return null;
}
@Override
public Void (SerialTree node, Void p) {
node.getDescription().forEach(dt -> dt.accept(this, null));
return null;
}
@Override
public Void (StartElementTree node, Void p) {
sb.append("<");
sb.append(node.getName());
node.getAttributes().forEach(dt -> dt.accept(this, null));
sb.append(node.isSelfClosing() ? "/>" : ">");
return null;
}
@Override
public Void (TextTree node, Void p) {
sb.append(node.getBody());
return null;
}
@Override
public Void (UnknownBlockTagTree node, Void p) {
node.getContent().forEach(dt -> dt.accept(this, null));
return null;
}
@Override
public Void (ValueTree node, Void p) {
return node.getReference().accept(this, null);
}
@Override
protected Void (DocTree node, Void p) {
sb.append(node.toString());
return null;
}
}.visit(dt, null);
return sb;
}
public String (DocTree dtree) {
return new SimpleDocTreeVisitor<String, Void>() {
@Override
public String (LinkTree node, Void p) {
return node.getLabel().stream()
.map(dt -> getText(dt))
.collect(Collectors.joining());
}
@Override
public String (SeeTree node, Void p) {
Utils utils = configuration.utils;
return node.getReference().stream()
.filter(utils::isText)
.map(dt -> ((TextTree) dt).getBody())
.collect(Collectors.joining());
}
@Override
protected String (DocTree node, Void p) {
return "";
}
}.visit(dtree, null);
}
public TypeElement (DocTree dtree) {
Utils utils = configuration.utils;
Element e = getReferencedElement(dtree);
if (e == null) {
return null;
} else if (utils.isTypeElement(e)) {
return (TypeElement) e;
} else if (!utils.isPackage(e) && !utils.isModule(e)) {
return utils.getEnclosingTypeElement(e);
}
return null;
}
public String (DocTree dtree) {
String s = getReferencedSignature(dtree);
if (s == null || s.contains("#") || s.contains("(")) {
return null;
}
int n = s.indexOf("/");
return (n == -1) ? s : s.substring(0, n);
}
public Element (DocTree dtree) {
Utils utils = configuration.utils;
Element e = getReferencedElement(dtree);
if (e == null) {
return null;
}
return (utils.isExecutableElement(e) || utils.isVariableElement(e)) ? e : null;
}
public String (DocTree dtree) {
String s = getReferencedSignature(dtree);
if (s == null) {
return null;
}
int n = s.indexOf("#");
return (n == -1) ? null : s.substring(n + 1);
}
public PackageElement (DocTree dtree) {
Element e = getReferencedElement(dtree);
if (e != null) {
Utils utils = configuration.utils;
return utils.containingPackage(e);
}
return null;
}
public ModuleElement (DocTree dtree) {
Element e = getReferencedElement(dtree);
if (e != null && configuration.utils.isModule(e)) {
return (ModuleElement) e;
}
return null;
}
public List<? extends DocTree> (List<? extends DocTree> body) {
return configuration.docEnv.getDocTrees().getFirstSentence(body);
}
public List<? extends DocTree> (DocTree dtree) {
return getFirstSentenceTrees(getBody(dtree));
}
private Element (DocTree dtree) {
return new ReferenceDocTreeVisitor<Element>() {
@Override
public Element (ReferenceTree node, Void p) {
return getElement(node);
}
}.visit(dtree, null);
}
public TypeMirror (DocTree dtree) {
return new ReferenceDocTreeVisitor<TypeMirror>() {
@Override
public TypeMirror (ReferenceTree node, Void p) {
return getType(node);
}
}.visit(dtree, null);
}
public TypeElement (DocTree dtree) {
Element e = getReferencedElement(dtree);
if (e != null) {
Utils utils = configuration.utils;
return utils.isTypeElement(e) ? (TypeElement) e : null;
}
return null;
}
public String (DocTree dtree) {
return new ReferenceDocTreeVisitor<String>() {
@Override
public String (ReferenceTree node, Void p) {
return node.getSignature();
}
}.visit(dtree, null);
}
private static class <R> extends SimpleDocTreeVisitor<R, Void> {
@Override
public R (SeeTree node, Void p) {
for (DocTree dt : node.getReference()) {
return visit(dt, null);
}
return null;
}
@Override
public R (LinkTree node, Void p) {
return visit(node.getReference(), null);
}
@Override
public R (ProvidesTree node, Void p) {
return visit(node.getServiceType(), null);
}
@Override
public R (ValueTree node, Void p) {
return visit(node.getReference(), null);
}
@Override
public R (SerialFieldTree node, Void p) {
return visit(node.getType(), null);
}
@Override
public R (UsesTree node, Void p) {
return visit(node.getServiceType(), null);
}
@Override
protected R (DocTree node, Void p) {
return null;
}
}
public List<? extends DocTree> (DocTree dtree) {
return dtree.getKind() == SEE ? ((SeeTree)dtree).getReference() : null;
}
public ReferenceTree (DocTree dtree) {
return (dtree.getKind() == THROWS || dtree.getKind() == EXCEPTION)
? ((ThrowsTree)dtree).getExceptionName()
: null;
}
public IdentifierTree (DocTree dtree) {
switch (dtree.getKind()) {
case PARAM:
return ((ParamTree)dtree).getName();
case SERIAL_FIELD:
return ((SerialFieldTree)dtree).getName();
default:
return null;
}
}
public List<? extends DocTree> (DocTree dtree) {
return new SimpleDocTreeVisitor<List<? extends DocTree>, Void>() {
List<? extends DocTree> (String content) {
List<DocTree> out = new ArrayList<>();
out.add(configuration.cmtUtils.makeTextTree(content));
return out;
}
@Override
public List<? extends DocTree> (AuthorTree node, Void p) {
return node.getName();
}
@Override
public List<? extends DocTree> (CommentTree node, Void p) {
return asList(node.getBody());
}
@Override
public List<? extends DocTree> (DeprecatedTree node, Void p) {
return node.getBody();
}
@Override
public List<? extends DocTree> (DocCommentTree node, Void p) {
return node.getBody();
}
@Override
public List<? extends DocTree> (LiteralTree node, Void p) {
return asList(node.getBody().getBody());
}
@Override
public List<? extends DocTree> (ProvidesTree node, Void p) {
return node.getDescription();
}
@Override
public List<? extends DocTree> (SinceTree node, Void p) {
return node.getBody();
}
@Override
public List<? extends DocTree> (TextTree node, Void p) {
return asList(node.getBody());
}
@Override
public List<? extends DocTree> (VersionTree node, Void p) {
return node.getBody();
}
@Override
public List<? extends DocTree> (ParamTree node, Void p) {
return node.getDescription();
}
@Override
public List<? extends DocTree> (ReturnTree node, Void p) {
return node.getDescription();
}
@Override
public List<? extends DocTree> (SeeTree node, Void p) {
return node.getReference();
}
@Override
public List<? extends DocTree> (SerialTree node, Void p) {
return node.getDescription();
}
@Override
public List<? extends DocTree> (SerialDataTree node, Void p) {
return node.getDescription();
}
@Override
public List<? extends DocTree> (SerialFieldTree node, Void p) {
return node.getDescription();
}
@Override
public List<? extends DocTree> (ThrowsTree node, Void p) {
return node.getDescription();
}
@Override
public List<? extends DocTree> (UnknownBlockTagTree node, Void p) {
return node.getContent();
}
@Override
public List<? extends DocTree> (UsesTree node, Void p) {
return node.getDescription();
}
@Override
protected List<? extends DocTree> (DocTree node, Void p) {
return Collections.emptyList();
}
}.visit(dtree, null);
}
public List<? extends DocTree> getBody(DocTree dtree) {
return getTags(dtree);
}
public ReferenceTree (DocTree dtree) {
if (dtree.getKind() == SERIAL_FIELD) {
return ((SerialFieldTree) dtree).getType();
} else {
return null;
}
}
public DocTreePath (DocTree dtree) {
if (path == null || dcTree == null || dtree == null)
return null;
return DocTreePath.getPath(path, dcTree, dtree);
}
public Element () {
return overriddenElement;
}
@Override
public String () {
StringBuilder sb = new StringBuilder("CommentHelper{" + "path=" + path + ", dcTree=" + dcTree);
sb.append(", element=");
sb.append(element.getEnclosingElement());
sb.append("::");
sb.append(element);
sb.append(", overriddenElement=");
if (overriddenElement != null) {
sb.append(overriddenElement.getEnclosingElement());
sb.append("::");
sb.append(overriddenElement);
} else {
sb.append("<none>");
}
sb.append('}');
return sb.toString();
}
}