Path: blob/master/src/jdk.javadoc/share/classes/jdk/javadoc/internal/doclint/Env.java
41161 views
/*1* Copyright (c) 2012, 2020, 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 jdk.javadoc.internal.doclint;262728import java.util.Arrays;29import java.util.HashSet;30import java.util.LinkedHashSet;31import java.util.List;32import java.util.Set;33import java.util.regex.Pattern;3435import javax.lang.model.element.Element;36import javax.lang.model.element.ElementKind;37import javax.lang.model.element.ExecutableElement;38import javax.lang.model.element.Modifier;39import javax.lang.model.type.TypeMirror;40import javax.lang.model.util.Elements;41import javax.lang.model.util.Types;42import javax.tools.Diagnostic.Kind;4344import com.sun.source.doctree.DocCommentTree;45import com.sun.source.tree.CompilationUnitTree;46import com.sun.source.util.DocTrees;47import com.sun.source.util.JavacTask;48import com.sun.source.util.SourcePositions;49import com.sun.source.util.TreePath;50import com.sun.tools.javac.model.JavacTypes;51import com.sun.tools.javac.tree.JCTree;52import com.sun.tools.javac.util.MatchingUtils;53import com.sun.tools.javac.util.StringUtils;5455/**56* Utility container for current execution environment,57* providing the current declaration and its doc comment.58*59* <p><b>This is NOT part of any supported API.60* If you write code that depends on this, you do so at your own61* risk. This code and its internal interfaces are subject to change62* or deletion without notice.</b></p>63*/64public class Env {65/**66* Access kinds for declarations.67*/68public enum AccessKind {69PRIVATE,70PACKAGE,71PROTECTED,72PUBLIC;7374static boolean accepts(String opt) {75for (AccessKind g: values())76if (opt.equals(StringUtils.toLowerCase(g.name()))) return true;77return false;78}7980static AccessKind of(Set<Modifier> mods) {81if (mods.contains(Modifier.PUBLIC))82return AccessKind.PUBLIC;83else if (mods.contains(Modifier.PROTECTED))84return AccessKind.PROTECTED;85else if (mods.contains(Modifier.PRIVATE))86return AccessKind.PRIVATE;87else88return AccessKind.PACKAGE;89}90}9192/** Message handler. */93final Messages messages;9495Set<String> customTags;9697Set<Pattern> includePackages;98Set<Pattern> excludePackages;99100// Utility classes101DocTrees trees;102Elements elements;103Types types;104105// Types used when analysing doc comments.106TypeMirror java_lang_Error;107TypeMirror java_lang_RuntimeException;108TypeMirror java_lang_Throwable;109TypeMirror java_lang_Void;110111/** The path for the declaration containing the comment currently being analyzed. */112TreePath currPath;113/** The element for the declaration containing the comment currently being analyzed. */114Element currElement;115/** The comment current being analyzed. */116DocCommentTree currDocComment;117/**118* The access kind of the declaration containing the comment currently being analyzed.119* This is the minimum (most restrictive) access kind of the declaration itself120* and that of its containers. For example, a public method in a private class is121* noted as private.122*/123AccessKind currAccess;124/** The set of methods, if any, that the current declaration overrides. */125Set<? extends ExecutableElement> currOverriddenMethods;126127Env() {128messages = new Messages(this);129}130131void init(JavacTask task) {132init(DocTrees.instance(task), task.getElements(), task.getTypes());133}134135void init(DocTrees trees, Elements elements, Types types) {136this.trees = trees;137this.elements = elements;138this.types = types;139}140141void initTypes() {142if (java_lang_Error != null)143return ;144145java_lang_Error = elements.getTypeElement("java.lang.Error").asType();146java_lang_RuntimeException = elements.getTypeElement("java.lang.RuntimeException").asType();147java_lang_Throwable = elements.getTypeElement("java.lang.Throwable").asType();148java_lang_Void = elements.getTypeElement("java.lang.Void").asType();149}150151void setCustomTags(String cTags) {152customTags = new LinkedHashSet<>();153for (String s : cTags.split(DocLint.SEPARATOR)) {154if (!s.isEmpty())155customTags.add(s);156}157}158159void setCheckPackages(String packages) {160includePackages = new HashSet<>();161excludePackages = new HashSet<>();162for (String pack : packages.split(DocLint.SEPARATOR)) {163boolean excluded = false;164if (pack.startsWith("-")) {165pack = pack.substring(1);166excluded = true;167}168if (pack.isEmpty())169continue;170Pattern pattern = MatchingUtils.validImportStringToPattern(pack);171if (excluded) {172excludePackages.add(pattern);173} else {174includePackages.add(pattern);175}176}177}178179static boolean validatePackages(String packages) {180for (String pack : packages.split(DocLint.SEPARATOR)) {181if (pack.startsWith("-")) {182pack = pack.substring(1);183}184if (!pack.isEmpty() && !MatchingUtils.isValidImportString(pack))185return false;186}187return true;188}189190/** Set the current declaration and its doc comment. */191void setCurrent(TreePath path, DocCommentTree comment) {192currPath = path;193currDocComment = comment;194currElement = trees.getElement(currPath);195currOverriddenMethods = ((JavacTypes) types).getOverriddenMethods(currElement);196197AccessKind ak = AccessKind.PUBLIC;198for (TreePath p = path; p != null; p = p.getParentPath()) {199Element e = trees.getElement(p);200if (e != null && e.getKind() != ElementKind.PACKAGE && e.getKind() != ElementKind.MODULE) {201ak = min(ak, AccessKind.of(e.getModifiers()));202}203}204currAccess = ak;205}206207AccessKind getAccessKind() {208return currAccess;209}210211long getPos(TreePath p) {212return ((JCTree) p.getLeaf()).pos;213}214215long getStartPos(TreePath p) {216SourcePositions sp = trees.getSourcePositions();217return sp.getStartPosition(p.getCompilationUnit(), p.getLeaf());218}219220boolean shouldCheck(CompilationUnitTree unit) {221if (includePackages == null)222return true;223224String packageName = unit.getPackageName() != null225? unit.getPackageName().toString()226: "";227228if (!includePackages.isEmpty()) {229boolean included = false;230for (Pattern pack : includePackages) {231if (pack.matcher(packageName).matches()) {232included = true;233break;234}235}236if (!included)237return false;238}239240for (Pattern pack : excludePackages) {241if (pack.matcher(packageName).matches()) {242return false;243}244}245246return true;247}248249private <T extends Comparable<T>> T min(T item1, T item2) {250return (item1 == null) ? item2251: (item2 == null) ? item1252: item1.compareTo(item2) <= 0 ? item1 : item2;253}254}255256257