Path: blob/aarch64-shenandoah-jdk8u272-b10/langtools/src/share/classes/com/sun/tools/doclint/Env.java
38899 views
/*1* Copyright (c) 2012, 2013, 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 com.sun.tools.doclint;262728import java.util.Set;29import java.util.LinkedHashSet;3031import javax.lang.model.element.Element;32import javax.lang.model.element.ElementKind;33import javax.lang.model.element.ExecutableElement;34import javax.lang.model.element.Modifier;35import javax.lang.model.type.TypeMirror;36import javax.lang.model.util.Elements;37import javax.lang.model.util.Types;3839import com.sun.source.doctree.DocCommentTree;40import com.sun.source.util.DocTrees;41import com.sun.source.util.JavacTask;42import com.sun.source.util.SourcePositions;43import com.sun.source.util.TreePath;44import com.sun.tools.javac.model.JavacTypes;45import com.sun.tools.javac.tree.JCTree;46import com.sun.tools.javac.util.StringUtils;4748/**49* Utility container for current execution environment,50* providing the current declaration and its doc comment.51*52* <p><b>This is NOT part of any supported API.53* If you write code that depends on this, you do so at your own54* risk. This code and its internal interfaces are subject to change55* or deletion without notice.</b></p>56*/57public class Env {58/**59* Access kinds for declarations.60*/61public enum AccessKind {62PRIVATE,63PACKAGE,64PROTECTED,65PUBLIC;6667static boolean accepts(String opt) {68for (AccessKind g: values())69if (opt.equals(StringUtils.toLowerCase(g.name()))) return true;70return false;71}7273static AccessKind of(Set<Modifier> mods) {74if (mods.contains(Modifier.PUBLIC))75return AccessKind.PUBLIC;76else if (mods.contains(Modifier.PROTECTED))77return AccessKind.PROTECTED;78else if (mods.contains(Modifier.PRIVATE))79return AccessKind.PRIVATE;80else81return AccessKind.PACKAGE;82}83};8485/** Message handler. */86final Messages messages;8788int implicitHeaderLevel = 0;8990Set<String> customTags;9192// Utility classes93DocTrees trees;94Elements elements;95Types types;9697// Types used when analysing doc comments.98TypeMirror java_lang_Error;99TypeMirror java_lang_RuntimeException;100TypeMirror java_lang_Throwable;101TypeMirror java_lang_Void;102103/** The path for the declaration containing the comment currently being analyzed. */104TreePath currPath;105/** The element for the declaration containing the comment currently being analyzed. */106Element currElement;107/** The comment current being analyzed. */108DocCommentTree currDocComment;109/**110* The access kind of the declaration containing the comment currently being analyzed.111* This is the minimum (most restrictive) access kind of the declaration itself112* and that of its containers. For example, a public method in a private class is113* noted as private.114*/115AccessKind currAccess;116/** The set of methods, if any, that the current declaration overrides. */117Set<? extends ExecutableElement> currOverriddenMethods;118119Env() {120messages = new Messages(this);121}122123void init(JavacTask task) {124init(DocTrees.instance(task), task.getElements(), task.getTypes());125}126127void init(DocTrees trees, Elements elements, Types types) {128this.trees = trees;129this.elements = elements;130this.types = types;131java_lang_Error = elements.getTypeElement("java.lang.Error").asType();132java_lang_RuntimeException = elements.getTypeElement("java.lang.RuntimeException").asType();133java_lang_Throwable = elements.getTypeElement("java.lang.Throwable").asType();134java_lang_Void = elements.getTypeElement("java.lang.Void").asType();135}136137void setImplicitHeaders(int n) {138implicitHeaderLevel = n;139}140141void setCustomTags(String cTags) {142customTags = new LinkedHashSet<String>();143for (String s : cTags.split(DocLint.TAGS_SEPARATOR)) {144if (!s.isEmpty())145customTags.add(s);146}147}148149/** Set the current declaration and its doc comment. */150void setCurrent(TreePath path, DocCommentTree comment) {151currPath = path;152currDocComment = comment;153currElement = trees.getElement(currPath);154currOverriddenMethods = ((JavacTypes) types).getOverriddenMethods(currElement);155156AccessKind ak = AccessKind.PUBLIC;157for (TreePath p = path; p != null; p = p.getParentPath()) {158Element e = trees.getElement(p);159if (e != null && e.getKind() != ElementKind.PACKAGE) {160ak = min(ak, AccessKind.of(e.getModifiers()));161}162}163currAccess = ak;164}165166AccessKind getAccessKind() {167return currAccess;168}169170long getPos(TreePath p) {171return ((JCTree) p.getLeaf()).pos;172}173174long getStartPos(TreePath p) {175SourcePositions sp = trees.getSourcePositions();176return sp.getStartPosition(p.getCompilationUnit(), p.getLeaf());177}178179private <T extends Comparable<T>> T min(T item1, T item2) {180return (item1 == null) ? item2181: (item2 == null) ? item1182: item1.compareTo(item2) <= 0 ? item1 : item2;183}184}185186187