package jdk.javadoc.internal.doclets.toolkit;
import java.net.URI;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.lang.model.element.Element;
import javax.lang.model.element.ExecutableElement;
import javax.lang.model.element.Name;
import javax.lang.model.element.PackageElement;
import javax.lang.model.element.RecordComponentElement;
import javax.lang.model.element.TypeElement;
import javax.lang.model.element.VariableElement;
import javax.lang.model.util.Elements;
import javax.tools.FileObject;
import javax.tools.JavaFileObject;
import javax.tools.SimpleJavaFileObject;
import com.sun.source.doctree.AttributeTree;
import com.sun.source.doctree.DocCommentTree;
import com.sun.source.doctree.DocTree;
import com.sun.source.doctree.IdentifierTree;
import com.sun.source.doctree.ParamTree;
import com.sun.source.doctree.ReferenceTree;
import com.sun.source.doctree.TextTree;
import com.sun.source.util.DocTreeFactory;
import com.sun.source.util.DocTreePath;
import com.sun.source.util.DocTrees;
import com.sun.source.util.TreePath;
import com.sun.tools.javac.util.DefinedBy;
import com.sun.tools.javac.util.DefinedBy.Api;
import jdk.javadoc.internal.doclets.toolkit.util.Utils;
public class {
final BaseConfiguration ;
final Utils ;
final Resources ;
final DocTreeFactory ;
final DocTrees ;
final Elements ;
final HashMap<Element, DocCommentInfo> = new HashMap<>();
protected (BaseConfiguration configuration) {
this.configuration = configuration;
utils = configuration.utils;
resources = configuration.getDocResources();
trees = configuration.docEnv.getDocTrees();
treeFactory = trees.getDocTreeFactory();
elementUtils = configuration.docEnv.getElementUtils();
}
public List<? extends DocTree> (List<? extends DocTree> content) {
List<DocTree> out = new ArrayList<>();
Name name = elementUtils.getName("propertyDescription");
out.add(treeFactory.newUnknownBlockTagTree(name, content));
return out;
}
public List<? extends DocTree> (String content) {
List<DocTree> inlist = new ArrayList<>();
inlist.add(treeFactory.newCommentTree(content));
List<DocTree> out = new ArrayList<>();
Name name = elementUtils.getName("propertyDescription");
out.add(treeFactory.newUnknownBlockTagTree(name, inlist));
return out;
}
public List<? extends DocTree> (String content) {
List<DocTree> out = new ArrayList<>();
out.add(treeFactory.newTextTree(content));
return out;
}
public DocTree (String sig, Element e) {
List<DocTree> list = new ArrayList<>();
list.add(treeFactory.newReferenceTree(sig));
return treeFactory.newSeeTree(list);
}
public TextTree (String content) {
return treeFactory.newTextTree(content);
}
public TextTree (String key) {
return treeFactory.newTextTree(resources.getText(key));
}
public void (ExecutableElement ee) {
List<DocTree> fullBody = new ArrayList<>();
fullBody.add(treeFactory.newTextTree(resources.getText("doclet.enum_values_doc.fullbody")));
List<DocTree> descriptions = new ArrayList<>();
descriptions.add(treeFactory.newTextTree(resources.getText("doclet.enum_values_doc.return")));
List<DocTree> tags = new ArrayList<>();
tags.add(treeFactory.newReturnTree(descriptions));
DocCommentTree docTree = treeFactory.newDocCommentTree(fullBody, tags);
dcInfoMap.put(ee, new DocCommentInfo(null, docTree));
}
public void (ExecutableElement ee) {
List<DocTree> fullBody = new ArrayList<>();
fullBody.add(treeFactory.newTextTree(resources.getText("doclet.enum_valueof_doc.fullbody")));
List<DocTree> tags = new ArrayList<>();
List<DocTree> paramDescs = new ArrayList<>();
paramDescs.add(treeFactory.newTextTree(resources.getText("doclet.enum_valueof_doc.param_name")));
java.util.List<? extends VariableElement> parameters = ee.getParameters();
VariableElement param = parameters.get(0);
IdentifierTree id = treeFactory.newIdentifierTree(elementUtils.getName(param.getSimpleName().toString()));
tags.add(treeFactory.newParamTree(false, id, paramDescs));
List<DocTree> returnDescs = new ArrayList<>();
returnDescs.add(treeFactory.newTextTree(resources.getText("doclet.enum_valueof_doc.return")));
tags.add(treeFactory.newReturnTree(returnDescs));
List<DocTree> throwsDescs = new ArrayList<>();
throwsDescs.add(treeFactory.newTextTree(resources.getText("doclet.enum_valueof_doc.throws_ila")));
ReferenceTree ref = treeFactory.newReferenceTree("java.lang.IllegalArgumentException");
tags.add(treeFactory.newThrowsTree(ref, throwsDescs));
throwsDescs = new ArrayList<>();
throwsDescs.add(treeFactory.newTextTree(resources.getText("doclet.enum_valueof_doc.throws_npe")));
ref = treeFactory.newReferenceTree("java.lang.NullPointerException");
tags.add(treeFactory.newThrowsTree(ref, throwsDescs));
DocCommentTree docTree = treeFactory.newDocCommentTree(fullBody, tags);
dcInfoMap.put(ee, new DocCommentInfo(null, docTree));
}
public void (ExecutableElement ee) {
TypeElement te = utils.getEnclosingTypeElement(ee);
List<DocTree> fullBody =
makeDescriptionWithName("doclet.record_constructor_doc.fullbody", te.getSimpleName());
List<DocTree> tags = new ArrayList<>();
java.util.List<? extends VariableElement> parameters = ee.getParameters();
for (VariableElement param : ee.getParameters()) {
Name name = param.getSimpleName();
IdentifierTree id = treeFactory.newIdentifierTree(name);
tags.add(treeFactory.newParamTree(false, id,
makeDescriptionWithComponent("doclet.record_constructor_doc.param_name", te, name)));
}
DocCommentTree docTree = treeFactory.newDocCommentTree(fullBody, tags);
dcInfoMap.put(ee, new DocCommentInfo(null, docTree));
}
@SuppressWarnings("preview")
public void (ExecutableElement ee) {
List<DocTree> fullBody = new ArrayList<>();
add(fullBody, "doclet.record_equals_doc.fullbody.head");
fullBody.add(treeFactory.newTextTree(" "));
List<? extends RecordComponentElement> comps = ((TypeElement) ee.getEnclosingElement()).getRecordComponents();
boolean hasPrimitiveComponents =
comps.stream().anyMatch(e -> e.asType().getKind().isPrimitive());
boolean hasReferenceComponents =
comps.stream().anyMatch(e -> !e.asType().getKind().isPrimitive());
if (hasPrimitiveComponents && hasReferenceComponents) {
add(fullBody, "doclet.record_equals_doc.fullbody.tail.both");
} else if (hasPrimitiveComponents) {
add(fullBody, "doclet.record_equals_doc.fullbody.tail.primitive");
} else if (hasReferenceComponents) {
add(fullBody, "doclet.record_equals_doc.fullbody.tail.reference");
}
Name paramName = ee.getParameters().get(0).getSimpleName();
IdentifierTree id = treeFactory.newIdentifierTree(paramName);
List<DocTree> paramDesc =
makeDescriptionWithName("doclet.record_equals_doc.param_name", paramName);
DocTree paramTree = treeFactory.newParamTree(false, id, paramDesc);
DocTree returnTree = treeFactory.newReturnTree(
makeDescriptionWithName("doclet.record_equals_doc.return", paramName));
TreePath treePath = utils.getTreePath(ee.getEnclosingElement());
DocCommentTree docTree = treeFactory.newDocCommentTree(fullBody, List.of(paramTree, returnTree));
dcInfoMap.put(ee, new DocCommentInfo(treePath, docTree));
}
private void (List<DocTree> contents, String resourceKey) {
String body = resources.getText(resourceKey);
Pattern p = Pattern.compile("\\{@link (\\S*)(.*)}");
Matcher m = p.matcher(body);
int start = 0;
while (m.find(start)) {
if (m.start() > start) {
contents.add(treeFactory.newTextTree(body.substring(start, m.start())));
}
ReferenceTree refTree = treeFactory.newReferenceTree(m.group(1));
List<DocTree> descr = List.of(treeFactory.newTextTree(m.group(2).trim())) ;
contents.add(treeFactory.newLinkTree(refTree, descr));
start = m.end();
}
if (start < body.length()) {
contents.add(treeFactory.newTextTree(body.substring(start)));
}
}
public void (ExecutableElement ee) {
List<DocTree> fullBody = List.of(makeTextTreeForResource("doclet.record_hashCode_doc.fullbody"));
DocTree returnTree = treeFactory.newReturnTree(
List.of(makeTextTreeForResource("doclet.record_hashCode_doc.return")));
DocCommentTree docTree = treeFactory.newDocCommentTree(fullBody, List.of(returnTree));
dcInfoMap.put(ee, new DocCommentInfo(null, docTree));
}
public void (ExecutableElement ee) {
List<DocTree> fullBody = List.of(
treeFactory.newTextTree(resources.getText("doclet.record_toString_doc.fullbody")));
DocTree returnTree = treeFactory.newReturnTree(List.of(
treeFactory.newTextTree(resources.getText("doclet.record_toString_doc.return"))));
DocCommentTree docTree = treeFactory.newDocCommentTree(fullBody, List.of(returnTree));
dcInfoMap.put(ee, new DocCommentInfo(null, docTree));
}
public void (ExecutableElement ee) {
TypeElement te = utils.getEnclosingTypeElement(ee);
List<DocTree> fullBody =
makeDescriptionWithComponent("doclet.record_accessor_doc.fullbody", te, ee.getSimpleName());
DocTree returnTree = treeFactory.newReturnTree(
makeDescriptionWithComponent("doclet.record_accessor_doc.return", te, ee.getSimpleName()));
DocCommentTree docTree = treeFactory.newDocCommentTree(fullBody, List.of(returnTree));
dcInfoMap.put(ee, new DocCommentInfo(null, docTree));
}
public void (VariableElement ve) {
TypeElement te = utils.getEnclosingTypeElement(ve);
List<DocTree> fullBody =
makeDescriptionWithComponent("doclet.record_field_doc.fullbody", te, ve.getSimpleName());
DocCommentTree docTree = treeFactory.newDocCommentTree(fullBody, List.of());
dcInfoMap.put(ve, new DocCommentInfo(null, docTree));
}
private List<DocTree> (String key, TypeElement elem, Name component) {
List<DocTree> result = new ArrayList<>();
String text = resources.getText(key);
int index = text.indexOf("{0}");
result.add(treeFactory.newTextTree(text.substring(0, index)));
Name A = elementUtils.getName("a");
Name CODE = elementUtils.getName("code");
Name HREF = elementUtils.getName("href");
List<DocTree> code = List.of(
treeFactory.newStartElementTree(CODE, List.of(), false),
treeFactory.newTextTree(component.toString()),
treeFactory.newEndElementTree(CODE));
if (hasParamForComponent(elem, component)) {
DocTree href = treeFactory.newAttributeTree(HREF,
AttributeTree.ValueKind.DOUBLE,
List.of(treeFactory.newTextTree("#param-" + component)));
result.add(treeFactory.newStartElementTree(A, List.of(href), false));
result.addAll(code);
result.add(treeFactory.newEndElementTree(A));
} else {
result.addAll(code);
}
result.add(treeFactory.newTextTree(text.substring(index + 3)));
return result;
}
private boolean (TypeElement elem, Name component) {
DocCommentTree elemComment = utils.getDocCommentTree(elem);
if (elemComment == null) {
return false;
}
for (DocTree t : elemComment.getBlockTags()) {
if (t instanceof ParamTree && ((ParamTree) t).getName().getName() == component) {
return true;
}
}
return false;
}
private List<DocTree> (String key, Name name) {
String text = resources.getText(key);
int index = text.indexOf("{0}");
if (index == -1) {
return List.of(treeFactory.newTextTree(text));
} else {
Name CODE = elementUtils.getName("code");
return List.of(
treeFactory.newTextTree(text.substring(0, index)),
treeFactory.newStartElementTree(CODE, List.of(), false),
treeFactory.newTextTree(name.toString()),
treeFactory.newEndElementTree(CODE),
treeFactory.newTextTree(text.substring(index + 3))
);
}
}
public DocCommentInfo (Element e) {
return dcInfoMap.get(e);
}
public DocCommentInfo (Element e) {
FileObject fo = null;
PackageElement pe = null;
switch (e.getKind()) {
case OTHER:
if (e instanceof DocletElement) {
DocletElement de = (DocletElement)e;
fo = de.getFileObject();
pe = de.getPackageElement();
}
break;
case PACKAGE:
pe = (PackageElement) e;
fo = configuration.workArounds.getJavaFileObject(pe);
break;
default:
return null;
}
if (fo == null) {
return null;
}
DocCommentTree dcTree = trees.getDocCommentTree(fo);
if (dcTree == null) {
return null;
}
DocTreePath treePath = trees.getDocTreePath(fo, pe);
return new DocCommentInfo(treePath.getTreePath(), dcTree);
}
public DocCommentTree (URI uri, String text) {
return trees.getDocCommentTree(new SimpleJavaFileObject(
uri, JavaFileObject.Kind.SOURCE) {
@Override @DefinedBy(Api.COMPILER)
public CharSequence (boolean ignoreEncoding) {
return text;
}
});
}
public void (Element element, List<? extends DocTree> fullBody,
List<? extends DocTree> blockTags) {
DocCommentTree docTree = treeFactory.newDocCommentTree(fullBody, blockTags);
dcInfoMap.put(element, new DocCommentInfo(null, docTree));
utils.removeCommentHelper(element);
}
public static class {
public final TreePath ;
public final DocCommentTree ;
public (TreePath treePath, DocCommentTree dcTree) {
this.treePath = treePath;
this.dcTree = dcTree;
}
}
}