package jdk.javadoc.internal.doclets.formats.html;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.SortedSet;
import java.util.TreeMap;
import java.util.TreeSet;
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.util.ElementFilter;
import com.sun.source.doctree.DeprecatedTree;
import com.sun.source.doctree.DocTree;
import jdk.javadoc.doclet.DocletEnvironment.ModuleMode;
import jdk.javadoc.internal.doclets.formats.html.markup.BodyContents;
import jdk.javadoc.internal.doclets.formats.html.markup.ContentBuilder;
import jdk.javadoc.internal.doclets.formats.html.markup.Entity;
import jdk.javadoc.internal.doclets.formats.html.markup.HtmlStyle;
import jdk.javadoc.internal.doclets.formats.html.markup.TagName;
import jdk.javadoc.internal.doclets.formats.html.markup.HtmlTree;
import jdk.javadoc.internal.doclets.formats.html.Navigation.PageMode;
import jdk.javadoc.internal.doclets.formats.html.markup.Text;
import jdk.javadoc.internal.doclets.toolkit.Content;
import jdk.javadoc.internal.doclets.toolkit.ModuleSummaryWriter;
import jdk.javadoc.internal.doclets.toolkit.util.CommentHelper;
import jdk.javadoc.internal.doclets.toolkit.util.DocFileIOException;
public class ModuleWriterImpl extends HtmlDocletWriter implements ModuleSummaryWriter {
protected ModuleElement mdle;
private final ModuleMode moduleMode;
private final Map<ModuleElement, Content> requires
= new TreeMap<>(comparators.makeModuleComparator());
private final Map<ModuleElement, Content> indirectModules
= new TreeMap<>(comparators.makeModuleComparator());
class PackageEntry {
Set<ModuleElement> exportedTo;
Set<ModuleElement> openedTo;
}
private final Map<PackageElement, PackageEntry> packages = new TreeMap<>(utils.comparators.makePackageComparator());
private final Map<ModuleElement, SortedSet<PackageElement>> indirectPackages
= new TreeMap<>(comparators.makeModuleComparator());
private final Map<ModuleElement, SortedSet<PackageElement>> indirectOpenPackages
= new TreeMap<>(comparators.makeModuleComparator());
private final SortedSet<TypeElement> uses
= new TreeSet<>(comparators.makeAllClassesComparator());
private final Map<TypeElement, Content> usesTrees
= new TreeMap<>(comparators.makeAllClassesComparator());
private final Map<TypeElement, SortedSet<TypeElement>> provides
= new TreeMap<>(comparators.makeAllClassesComparator());
private final Map<TypeElement, Content> providesTrees
= new TreeMap<>(comparators.makeAllClassesComparator());
private final BodyContents bodyContents = new BodyContents();
public ModuleWriterImpl(HtmlConfiguration configuration, ModuleElement mdle) {
super(configuration, configuration.docPaths.moduleSummary(mdle));
this.mdle = mdle;
this.moduleMode = configuration.docEnv.getModuleMode();
computeModulesData();
}
@Override
public Content (String heading) {
HtmlTree bodyTree = getBody(getWindowTitle(mdle.getQualifiedName().toString()));
HtmlTree div = new HtmlTree(TagName.DIV);
div.setStyle(HtmlStyle.header);
Content moduleHead = new ContentBuilder();
moduleHead.add(mdle.isOpen() && (configuration.docEnv.getModuleMode() == ModuleMode.ALL)
? contents.openModuleLabel : contents.moduleLabel);
moduleHead.add(" ").add(heading);
Content tHeading = HtmlTree.HEADING_TITLE(Headings.PAGE_TITLE_HEADING,
HtmlStyle.title, moduleHead);
div.add(tHeading);
bodyContents.setHeader(getHeader(PageMode.MODULE, mdle))
.addMainContent(div);
return bodyTree;
}
@Override
protected Navigation getNavBar(PageMode pageMode, Element element) {
return super.getNavBar(pageMode, element)
.setDisplaySummaryModuleDescLink(!utils.getFullBody(mdle).isEmpty() && !options.noComment())
.setDisplaySummaryModulesLink(display(requires) || display(indirectModules))
.setDisplaySummaryPackagesLink(display(packages) || display(indirectPackages)
|| display(indirectOpenPackages))
.setDisplaySummaryServicesLink(displayServices(uses, usesTrees) || displayServices(provides.keySet(), providesTrees));
}
@Override
public Content () {
return new ContentBuilder();
}
@Override
public Content getSummariesList() {
return new HtmlTree(TagName.UL).setStyle(HtmlStyle.summaryList);
}
@Override
public Content getSummaryTree(Content summaryContentTree) {
return HtmlTree.SECTION(HtmlStyle.summary, summaryContentTree);
}
public void computeModulesData() {
CommentHelper ch = utils.getCommentHelper(mdle);
Map<ModuleElement, String> dependentModules = utils.getDependentModules(mdle);
dependentModules.forEach((module, mod) -> {
if (shouldDocument(module)) {
indirectModules.put(module, Text.of(mod));
}
});
ElementFilter.requiresIn(mdle.getDirectives()).forEach(directive -> {
ModuleElement m = directive.getDependency();
if (shouldDocument(m)) {
if (moduleMode == ModuleMode.ALL || directive.isTransitive()) {
requires.put(m, Text.of(utils.getModifiers(directive)));
} else {
dependentModules.remove(m);
}
indirectModules.remove(m);
}
});
for (PackageElement pkg : utils.getModulePackageMap().getOrDefault(mdle, Collections.emptySet())) {
if (shouldDocument(pkg) && (mdle.isOpen() || moduleMode == ModuleMode.ALL)) {
PackageEntry e = new PackageEntry();
if (mdle.isOpen()) {
e.openedTo = Collections.emptySet();
}
packages.put(pkg, e);
}
}
for (ModuleElement.ExportsDirective directive : ElementFilter.exportsIn(mdle.getDirectives())) {
PackageElement p = directive.getPackage();
if (shouldDocument(p)) {
List<? extends ModuleElement> targetMdles = directive.getTargetModules();
if (moduleMode == ModuleMode.ALL || targetMdles == null) {
PackageEntry packageEntry = packages.computeIfAbsent(p, pkg -> new PackageEntry());
SortedSet<ModuleElement> mdleList = new TreeSet<>(utils.comparators.makeModuleComparator());
if (targetMdles != null) {
mdleList.addAll(targetMdles);
}
packageEntry.exportedTo = mdleList;
}
}
}
for (ModuleElement.OpensDirective directive : ElementFilter.opensIn(mdle.getDirectives())) {
PackageElement p = directive.getPackage();
if (shouldDocument(p)) {
List<? extends ModuleElement> targetMdles = directive.getTargetModules();
if (moduleMode == ModuleMode.ALL || targetMdles == null) {
PackageEntry packageEntry = packages.computeIfAbsent(p, pkg -> new PackageEntry());
SortedSet<ModuleElement> mdleList = new TreeSet<>(utils.comparators.makeModuleComparator());
if (targetMdles != null) {
mdleList.addAll(targetMdles);
}
packageEntry.openedTo = mdleList;
}
}
}
dependentModules.forEach((module, mod) -> {
SortedSet<PackageElement> exportedPackages = new TreeSet<>(utils.comparators.makePackageComparator());
ElementFilter.exportsIn(module.getDirectives()).forEach(directive -> {
PackageElement pkg = directive.getPackage();
if (shouldDocument(pkg)) {
if (moduleMode == ModuleMode.ALL || directive.getTargetModules() == null) {
exportedPackages.add(pkg);
}
}
});
if (!exportedPackages.isEmpty()) {
indirectPackages.put(module, exportedPackages);
}
SortedSet<PackageElement> openPackages = new TreeSet<>(utils.comparators.makePackageComparator());
if (module.isOpen()) {
openPackages.addAll(utils.getModulePackageMap().getOrDefault(module, Collections.emptySet()));
} else {
ElementFilter.opensIn(module.getDirectives()).forEach(directive -> {
PackageElement pkg = directive.getPackage();
if (shouldDocument(pkg)) {
if (moduleMode == ModuleMode.ALL || directive.getTargetModules() == null) {
openPackages.add(pkg);
}
}
});
}
if (!openPackages.isEmpty()) {
indirectOpenPackages.put(module, openPackages);
}
});
ElementFilter.usesIn(mdle.getDirectives()).forEach(directive -> {
TypeElement u = directive.getService();
if (shouldDocument(u)) {
uses.add(u);
}
});
ElementFilter.providesIn(mdle.getDirectives()).forEach(directive -> {
TypeElement u = directive.getService();
if (shouldDocument(u)) {
List<? extends TypeElement> implList = directive.getImplementations();
SortedSet<TypeElement> implSet = new TreeSet<>(utils.comparators.makeAllClassesComparator());
implSet.addAll(implList);
provides.put(u, implSet);
}
});
utils.getProvidesTrees(mdle).forEach(tree -> {
TypeElement t = ch.getServiceType(tree);
if (t != null) {
providesTrees.put(t, commentTagsToContent(tree, mdle, ch.getDescription(tree), false, true));
}
});
utils.getUsesTrees(mdle).forEach(tree -> {
TypeElement t = ch.getServiceType(tree);
if (t != null) {
usesTrees.put(t, commentTagsToContent(tree, mdle, ch.getDescription(tree), false, true));
}
});
}
public boolean shouldDocument(Element element) {
return (moduleMode == ModuleMode.ALL || utils.isIncluded(element));
}
public boolean display(Set<? extends Element> section) {
return section != null && !section.isEmpty();
}
public boolean display(Map<? extends Element, ?> section) {
return section != null && !section.isEmpty();
}
private boolean displayServices(Set<TypeElement> typeElements,
Map<TypeElement, Content> tagsMap) {
return typeElements != null &&
typeElements.stream().anyMatch(v -> displayServiceDirective(v, tagsMap));
}
private boolean displayServiceDirective(TypeElement typeElement,
Map<TypeElement, Content> tagsMap) {
return moduleMode == ModuleMode.ALL || tagsMap.containsKey(typeElement);
}
public void (Content startMarker, Content heading,
Content htmltree) {
htmltree.add(startMarker);
htmltree.add(HtmlTree.HEADING(Headings.ModuleDeclaration.SUMMARY_HEADING, heading));
}
private Table (Content caption, TableHeader tableHeader) {
return new Table(HtmlStyle.detailsTable)
.setCaption(caption)
.setHeader(tableHeader)
.setColumnStyles(HtmlStyle.colFirst, HtmlStyle.colLast);
}
private Table (Content caption, TableHeader tableHeader) {
return new Table(HtmlStyle.detailsTable)
.setCaption(caption)
.setHeader(tableHeader)
.setColumnStyles(HtmlStyle.colFirst, HtmlStyle.colSecond, HtmlStyle.colLast);
}
@Override
public void addModulesSummary(Content summariesList) {
if (display(requires) || display(indirectModules)) {
TableHeader requiresTableHeader =
new TableHeader(contents.modifierLabel, contents.moduleLabel,
contents.descriptionLabel);
HtmlTree section = HtmlTree.SECTION(HtmlStyle.modulesSummary)
.setId(HtmlIds.MODULES);
addSummaryHeader(MarkerComments.START_OF_MODULES_SUMMARY, contents.navModules, section);
if (display(requires)) {
String text = resources.getText("doclet.Requires_Summary");
Content caption = Text.of(text);
Table table = getTable3(caption, requiresTableHeader);
addModulesList(requires, table);
section.add(table);
}
if (display(indirectModules)) {
String amrText = resources.getText("doclet.Indirect_Requires_Summary");
Content amrCaption = Text.of(amrText);
Table amrTable = getTable3(amrCaption, requiresTableHeader);
addModulesList(indirectModules, amrTable);
section.add(amrTable);
}
summariesList.add(HtmlTree.LI(section));
}
}
private void addModulesList(Map<ModuleElement, Content> mdleMap, Table table) {
for (ModuleElement m : mdleMap.keySet()) {
Content modifiers = mdleMap.get(m);
Content moduleLink = getModuleLink(m, Text.of(m.getQualifiedName()));
Content moduleSummary = new ContentBuilder();
addSummaryComment(m, moduleSummary);
table.addRow(modifiers, moduleLink, moduleSummary);
}
}
@Override
public void addPackagesSummary(Content summariesList) {
if (display(packages)
|| display(indirectPackages) || display(indirectOpenPackages)) {
HtmlTree section = HtmlTree.SECTION(HtmlStyle.packagesSummary)
.setId(HtmlIds.PACKAGES);
addSummaryHeader(MarkerComments.START_OF_PACKAGES_SUMMARY, contents.navPackages, section);
if (display(packages)) {
addPackageSummary(section);
}
TableHeader indirectPackagesHeader =
new TableHeader(contents.fromLabel, contents.packagesLabel);
if (display(indirectPackages)) {
String aepText = resources.getText("doclet.Indirect_Exports_Summary");
Table aepTable = getTable2(Text.of(aepText), indirectPackagesHeader);
addIndirectPackages(aepTable, indirectPackages);
section.add(aepTable);
}
if (display(indirectOpenPackages)) {
String aopText = resources.getText("doclet.Indirect_Opens_Summary");
Table aopTable = getTable2(Text.of(aopText), indirectPackagesHeader);
addIndirectPackages(aopTable, indirectOpenPackages);
section.add(aopTable);
}
summariesList.add(HtmlTree.LI(section));
}
}
public void addPackageSummary(HtmlTree li) {
Table table = new Table(HtmlStyle.summaryTable)
.setId(HtmlIds.PACKAGE_SUMMARY_TABLE)
.setDefaultTab(resources.getText("doclet.All_Packages"))
.addTab(resources.getText("doclet.Exported_Packages_Summary"), this::isExported)
.addTab(resources.getText("doclet.Opened_Packages_Summary"), this::isOpened)
.addTab(resources.getText("doclet.Concealed_Packages_Summary"), this::isConcealed);
int numExports = 0;
int numUnqualifiedExports = 0;
int numOpens = 0;
int numUnqualifiedOpens = 0;
for (PackageEntry e : packages.values()) {
if (e.exportedTo != null) {
numExports++;
if (e.exportedTo.isEmpty()) {
numUnqualifiedExports++;
}
}
if (e.openedTo != null) {
numOpens++;
if (e.openedTo.isEmpty()) {
numUnqualifiedOpens++;
}
}
}
boolean showExportedTo = numExports > 0 && (numOpens > 0 || numUnqualifiedExports < packages.size());
boolean showOpenedTo = numOpens > 0 && (numExports > 0 || numUnqualifiedOpens < packages.size());
List<Content> colHeaders = new ArrayList<>();
List<HtmlStyle> colStyles = new ArrayList<>();
colHeaders.add(contents.packageLabel);
colStyles.add(HtmlStyle.colFirst);
if (showExportedTo) {
colHeaders.add(contents.exportedTo);
colStyles.add(HtmlStyle.colSecond);
}
if (showOpenedTo) {
colHeaders.add(contents.openedTo);
colStyles.add(HtmlStyle.colSecond);
}
colHeaders.add(contents.descriptionLabel);
colStyles.add(HtmlStyle.colLast);
table.setHeader(new TableHeader(colHeaders).styles(colStyles))
.setColumnStyles(colStyles);
for (Map.Entry<PackageElement, PackageEntry> e : packages.entrySet()) {
PackageElement pkg = e.getKey();
PackageEntry entry = e.getValue();
List<Content> row = new ArrayList<>();
Content pkgLinkContent = getPackageLink(pkg, getLocalizedPackageName(pkg));
row.add(pkgLinkContent);
if (showExportedTo) {
row.add(getPackageExportOpensTo(entry.exportedTo));
}
if (showOpenedTo) {
row.add(getPackageExportOpensTo(entry.openedTo));
}
Content summary = new ContentBuilder();
addPreviewSummary(pkg, summary);
addSummaryComment(pkg, summary);
row.add(summary);
table.addRow(pkg, row);
}
li.add(table);
if (table.needsScript()) {
mainBodyScript.append(table.getScript());
}
}
private boolean isExported(Element e) {
PackageEntry entry = packages.get((PackageElement) e);
return (entry != null) && (entry.exportedTo != null);
}
private boolean isOpened(Element e) {
PackageEntry entry = packages.get((PackageElement) e);
return (entry != null) && (entry.openedTo != null);
}
private boolean isConcealed(Element e) {
PackageEntry entry = packages.get((PackageElement) e);
return (entry != null) && (entry.exportedTo == null) && (entry.openedTo == null);
}
private Content getPackageExportOpensTo(Set<ModuleElement> modules) {
if (modules == null) {
return Text.of(resources.getText("doclet.None"));
} else if (modules.isEmpty()) {
return Text.of(resources.getText("doclet.All_Modules"));
} else {
Content list = new ContentBuilder();
for (ModuleElement m : modules) {
if (!list.isEmpty()) {
list.add(Text.of(", "));
}
list.add(getModuleLink(m, Text.of(m.getQualifiedName())));
}
return list;
}
}
public void addIndirectPackages(Table table, Map<ModuleElement, SortedSet<PackageElement>> ip) {
for (Map.Entry<ModuleElement, SortedSet<PackageElement>> entry : ip.entrySet()) {
ModuleElement m = entry.getKey();
SortedSet<PackageElement> pkgList = entry.getValue();
Content moduleLinkContent = getModuleLink(m, Text.of(m.getQualifiedName()));
Content list = new ContentBuilder();
String sep = "";
for (PackageElement pkg : pkgList) {
list.add(sep);
list.add(getPackageLink(pkg, getLocalizedPackageName(pkg)));
sep = " ";
}
table.addRow(moduleLinkContent, list);
}
}
@Override
public void addServicesSummary(Content summariesList) {
boolean haveUses = displayServices(uses, usesTrees);
boolean haveProvides = displayServices(provides.keySet(), providesTrees);
if (haveProvides || haveUses) {
HtmlTree section = HtmlTree.SECTION(HtmlStyle.servicesSummary)
.setId(HtmlIds.SERVICES);
addSummaryHeader(MarkerComments.START_OF_SERVICES_SUMMARY, contents.navServices, section);
TableHeader usesProvidesTableHeader =
new TableHeader(contents.typeLabel, contents.descriptionLabel);
if (haveProvides) {
String label = resources.getText("doclet.Provides_Summary");
Table table = getTable2(Text.of(label), usesProvidesTableHeader);
addProvidesList(table);
if (!table.isEmpty()) {
section.add(table);
}
}
if (haveUses){
String label = resources.getText("doclet.Uses_Summary");
Table table = getTable2(Text.of(label), usesProvidesTableHeader);
addUsesList(table);
if (!table.isEmpty()) {
section.add(table);
}
}
summariesList.add(HtmlTree.LI(section));
}
}
public void addUsesList(Table table) {
Content typeLinkContent;
Content description;
for (TypeElement t : uses) {
if (!displayServiceDirective(t, usesTrees)) {
continue;
}
typeLinkContent = getLink(new HtmlLinkInfo(configuration, HtmlLinkInfo.Kind.PACKAGE, t));
Content summary = new ContentBuilder();
if (display(usesTrees)) {
description = usesTrees.get(t);
if (description != null && !description.isEmpty()) {
summary.add(HtmlTree.DIV(HtmlStyle.block, description));
} else {
addSummaryComment(t, summary);
}
} else {
summary.add(Entity.NO_BREAK_SPACE);
}
table.addRow(typeLinkContent, summary);
}
}
public void addProvidesList(Table table) {
SortedSet<TypeElement> implSet;
Content description;
for (Map.Entry<TypeElement, SortedSet<TypeElement>> entry : provides.entrySet()) {
TypeElement srv = entry.getKey();
if (!displayServiceDirective(srv, providesTrees)) {
continue;
}
implSet = entry.getValue();
Content srvLinkContent = getLink(new HtmlLinkInfo(configuration, HtmlLinkInfo.Kind.PACKAGE, srv));
Content desc = new ContentBuilder();
if (display(providesTrees)) {
description = providesTrees.get(srv);
desc.add((description != null && !description.isEmpty())
? HtmlTree.DIV(HtmlStyle.block, description)
: Entity.NO_BREAK_SPACE);
} else {
desc.add(Entity.NO_BREAK_SPACE);
}
if (moduleMode == ModuleMode.ALL && !implSet.isEmpty()) {
desc.add(new HtmlTree(TagName.BR));
desc.add("(");
HtmlTree implSpan = HtmlTree.SPAN(HtmlStyle.implementationLabel, contents.implementation);
desc.add(implSpan);
desc.add(Entity.NO_BREAK_SPACE);
String sep = "";
for (TypeElement impl : implSet) {
desc.add(sep);
desc.add(getLink(new HtmlLinkInfo(configuration, HtmlLinkInfo.Kind.PACKAGE, impl)));
sep = ", ";
}
desc.add(")");
}
table.addRow(srvLinkContent, desc);
}
}
public void addDeprecationInfo(Content div) {
List<? extends DeprecatedTree> deprs = utils.getDeprecatedTrees(mdle);
if (utils.isDeprecated(mdle)) {
CommentHelper ch = utils.getCommentHelper(mdle);
HtmlTree deprDiv = new HtmlTree(TagName.DIV);
deprDiv.setStyle(HtmlStyle.deprecationBlock);
Content deprPhrase = HtmlTree.SPAN(HtmlStyle.deprecatedLabel, getDeprecatedPhrase(mdle));
deprDiv.add(deprPhrase);
if (!deprs.isEmpty()) {
List<? extends DocTree> commentTags = ch.getDescription(deprs.get(0));
if (!commentTags.isEmpty()) {
addInlineDeprecatedComment(mdle, deprs.get(0), deprDiv);
}
}
div.add(deprDiv);
}
}
@Override
public void addModuleDescription(Content moduleContentTree) {
addPreviewInfo(mdle, moduleContentTree);
if (!utils.getFullBody(mdle).isEmpty()) {
HtmlTree tree = HtmlTree.SECTION(HtmlStyle.moduleDescription)
.setId(HtmlIds.MODULE_DESCRIPTION);
addDeprecationInfo(tree);
tree.add(MarkerComments.START_OF_MODULE_DESCRIPTION);
addInlineComment(mdle, tree);
addTagsInfo(mdle, tree);
moduleContentTree.add(tree);
}
}
@Override
public void addModuleSignature(Content moduleContentTree) {
moduleContentTree.add(new HtmlTree(TagName.HR));
moduleContentTree.add(Signatures.getModuleSignature(mdle, this));
}
@Override
public void addModuleContent(Content moduleContentTree) {
bodyContents.addMainContent(moduleContentTree);
}
@Override
public void () {
bodyContents.setFooter(getFooter());
}
@Override
public void printDocument(Content contentTree) throws DocFileIOException {
contentTree.add(bodyContents);
printHtmlDocument(configuration.metakeywords.getMetaKeywordsForModule(mdle),
getDescription("declaration", mdle), getLocalStylesheets(mdle), contentTree);
}
public void addPackageDeprecationInfo(Content li, PackageElement pkg) {
if (utils.isDeprecated(pkg)) {
List<? extends DeprecatedTree> deprs = utils.getDeprecatedTrees(pkg);
HtmlTree deprDiv = new HtmlTree(TagName.DIV);
deprDiv.setStyle(HtmlStyle.deprecationBlock);
Content deprPhrase = HtmlTree.SPAN(HtmlStyle.deprecatedLabel, getDeprecatedPhrase(pkg));
deprDiv.add(deprPhrase);
if (!deprs.isEmpty()) {
CommentHelper ch = utils.getCommentHelper(pkg);
List<? extends DocTree> commentTags = ch.getDescription(deprs.get(0));
if (!commentTags.isEmpty()) {
addInlineDeprecatedComment(pkg, deprs.get(0), deprDiv);
}
}
li.add(deprDiv);
}
}
}