Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
8253736: Cleanup some of WorkArounds and usage thereof
Reviewed-by: vromero, ksrini
  • Loading branch information
jonathan-gibbons committed Oct 2, 2020
1 parent 87d77eb commit 7778047
Show file tree
Hide file tree
Showing 9 changed files with 268 additions and 233 deletions.
Expand Up @@ -30,6 +30,14 @@
import com.sun.source.util.JavacTask;
import com.sun.source.util.Plugin;

/**
* The base class for the DocLint service used by javac.
*
* <p><b>This is NOT part of any supported API.
* If you write code that depends on this, you do so at your own risk.
* This code and its internal interfaces are subject to change or
* deletion without notice.</b>
*/
public abstract class DocLint implements Plugin {
public static final String XMSGS_OPTION = "-Xmsgs";
public static final String XMSGS_CUSTOM_PREFIX = "-Xmsgs:";
Expand Down
Expand Up @@ -25,9 +25,13 @@

package jdk.javadoc.internal.doclets.formats.html;

import java.util.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.stream.Collectors;

import javax.lang.model.element.Element;
import javax.lang.model.element.PackageElement;
import javax.lang.model.element.TypeElement;
Expand All @@ -36,7 +40,6 @@
import javax.tools.StandardJavaFileManager;

import com.sun.source.util.DocTreePath;

import jdk.javadoc.doclet.Doclet;
import jdk.javadoc.doclet.DocletEnvironment;
import jdk.javadoc.doclet.Reporter;
Expand Down Expand Up @@ -201,7 +204,7 @@ public boolean finishOptionSettings() {
docPaths = new DocPaths(utils);
setCreateOverview();
setTopFile(docEnv);
workArounds.initDocLint(options.doclintOpts(), tagletManager.getAllTagletNames());
initDocLint(options.doclintOpts(), tagletManager.getAllTagletNames());
return true;
}

Expand Down Expand Up @@ -321,12 +324,12 @@ public JavaFileManager getFileManager() {

@Override
public boolean showMessage(DocTreePath path, String key) {
return (path == null || workArounds.haveDocLint());
return (path == null || !haveDocLint());
}

@Override
public boolean showMessage(Element e, String key) {
return (e == null || workArounds.haveDocLint());
return (e == null || !haveDocLint());
}

@Override
Expand Down
Expand Up @@ -25,26 +25,43 @@

package jdk.javadoc.internal.doclets.toolkit;

import java.io.*;
import java.util.*;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import java.util.SortedMap;
import java.util.SortedSet;
import java.util.TreeMap;
import java.util.TreeSet;

import javax.lang.model.SourceVersion;
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.Elements;
import javax.lang.model.util.SimpleElementVisitor14;
import javax.tools.JavaFileManager;
import javax.tools.JavaFileObject;

import com.sun.source.tree.CompilationUnitTree;
import com.sun.source.util.DocTreePath;
import com.sun.source.util.TreePath;
import com.sun.tools.javac.util.DefinedBy;
import com.sun.tools.javac.util.DefinedBy.Api;
import jdk.javadoc.doclet.Doclet;
import jdk.javadoc.doclet.DocletEnvironment;
import jdk.javadoc.doclet.Reporter;
import jdk.javadoc.doclet.StandardDoclet;
import jdk.javadoc.doclet.Taglet;
import jdk.javadoc.internal.doclets.formats.html.HtmlDoclet;
import jdk.javadoc.internal.doclets.toolkit.builders.BuilderFactory;
import jdk.javadoc.internal.doclets.toolkit.taglets.TagletManager;
import jdk.javadoc.internal.doclets.toolkit.util.Comparators;
Expand All @@ -60,6 +77,7 @@
import jdk.javadoc.internal.doclets.toolkit.util.Utils.Pair;
import jdk.javadoc.internal.doclets.toolkit.util.VisibleMemberCache;
import jdk.javadoc.internal.doclets.toolkit.util.VisibleMemberTable;
import jdk.javadoc.internal.doclint.DocLint;

/**
* Configure the output based on the options. Doclets should sub-class
Expand Down Expand Up @@ -201,7 +219,7 @@ public abstract class BaseConfiguration {
* @apiNote The {@code doclet} parameter is used when
* {@link Taglet#init(DocletEnvironment, Doclet) initializing tags}.
* Some doclets (such as the {@link StandardDoclet}), may delegate to another
* (such as the {@link HtmlDoclet}). In such cases, the primary doclet (i.e
* (such as the {@code HtmlDoclet}). In such cases, the primary doclet (i.e
* {@code StandardDoclet}) should be provided here, and not any internal
* class like {@code HtmlDoclet}.
*
Expand Down Expand Up @@ -367,7 +385,16 @@ protected boolean finishOptionSettings0() throws DocletException {
group.checkPackageGroups(grp.first, grp.second);
}
});
overviewElement = new OverviewElement(workArounds.getUnnamedPackage(), getOverviewPath());

PackageElement unnamedPackage;
Elements elementUtils = utils.elementUtils;
if (docEnv.getSourceVersion().compareTo(SourceVersion.RELEASE_9) >= 0) {
ModuleElement unnamedModule = elementUtils.getModuleElement("");
unnamedPackage = elementUtils.getPackageElement(unnamedModule, "");
} else {
unnamedPackage = elementUtils.getPackageElement("");
}
overviewElement = new OverviewElement(unnamedPackage, getOverviewPath());
return true;
}

Expand Down Expand Up @@ -690,4 +717,91 @@ public boolean isJavaFXMode() {
|| javafxModule.isUnnamed()
|| javafxModule.getQualifiedName().contentEquals("javafx.base");
}


//<editor-fold desc="DocLint support">

private DocLint doclint;

Map<CompilationUnitTree, Boolean> shouldCheck = new HashMap<>();

public void runDocLint(TreePath path) {
CompilationUnitTree unit = path.getCompilationUnit();
if (doclint != null && shouldCheck.computeIfAbsent(unit, doclint::shouldCheck)) {
doclint.scan(path);
}
}

/**
* Initializes DocLint, if appropriate, depending on options derived
* from the doclet command-line options, and the set of custom tags
* that should be ignored by DocLint.
*
* DocLint is not enabled if the option {@code -Xmsgs:none} is given,
* and it is not followed by any options to enable any groups.
* Note that arguments for {@code -Xmsgs:} can be given individually
* in separate {@code -Xmsgs:} options, or in a comma-separated list
* for a single option. For example, the following are equivalent:
* <ul>
* <li>{@code -Xmsgs:all} {@code -Xmsgs:-html}
* <li>{@code -Xmsgs:all,-html}
* </ul>
*
* @param opts options for DocLint, derived from the corresponding doclet
* command-line options
* @param customTagNames the names of custom tags, to be ignored by doclint
*/
public void initDocLint(List<String> opts, Set<String> customTagNames) {
List<String> doclintOpts = new ArrayList<>();

// basic analysis of -Xmsgs and -Xmsgs: options to see if doclint is enabled
Set<String> groups = new HashSet<>();
boolean seenXmsgs = false;
for (String opt : opts) {
if (opt.equals(DocLint.XMSGS_OPTION)) {
groups.add("all");
seenXmsgs = true;
} else if (opt.startsWith(DocLint.XMSGS_CUSTOM_PREFIX)) {
String[] args = opt.substring(DocLint.XMSGS_CUSTOM_PREFIX.length())
.split(DocLint.SEPARATOR);
for (String a : args) {
if (a.equals("none")) {
groups.clear();
} else if (a.startsWith("-")) {
groups.remove(a.substring(1));
} else {
groups.add(a);
}
}
seenXmsgs = true;
}
doclintOpts.add(opt);
}

if (seenXmsgs) {
if (groups.isEmpty()) {
// no groups enabled; do not init doclint
return;
}
} else {
// no -Xmsgs options of any kind, use default
doclintOpts.add(DocLint.XMSGS_OPTION);
}

if (!customTagNames.isEmpty()) {
String customTags = String.join(DocLint.SEPARATOR, customTagNames);
doclintOpts.add(DocLint.XCUSTOM_TAGS_PREFIX + customTags);
}

doclintOpts.add(DocLint.XHTML_VERSION_PREFIX + "html5");

doclint = new DocLint();
doclint.init(docEnv.getDocTrees(), docEnv.getElementUtils(), docEnv.getTypeUtils(),
doclintOpts.toArray(new String[0]));
}

public boolean haveDocLint() {
return (doclint != null);
}
//</editor-fold>
}
Expand Up @@ -427,8 +427,8 @@ public DocCommentInfo getHtmlCommentInfo(Element e) {
}
break;
case PACKAGE:
fo = configuration.workArounds.getJavaFileObject((PackageElement)e);
pe = (PackageElement)e;
pe = (PackageElement) e;
fo = configuration.workArounds.getJavaFileObject(pe);
break;
default:
return null;
Expand Down
Expand Up @@ -116,8 +116,9 @@ public void warning(String key, Object... args) {
* @param args optional arguments to be replaced in the message.
*/
public void warning(DocTreePath path, String key, Object... args) {
if (configuration.showMessage(path, key))
if (configuration.showMessage(path, key)) {
report(WARNING, path, resources.getText(key, args));
}
}

/**
Expand Down

1 comment on commit 7778047

@bridgekeeper
Copy link

@bridgekeeper bridgekeeper bot commented on 7778047 Oct 2, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.