src/share/classes/com/sun/tools/doclint/Env.java

Thu, 24 May 2018 18:02:46 +0800

author
aoqi
date
Thu, 24 May 2018 18:02:46 +0800
changeset 3446
e468915bad3a
parent 2525
2eb010b6cb22
permissions
-rw-r--r--

Merge

aoqi@0 1 /*
aoqi@0 2 * Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved.
aoqi@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aoqi@0 4 *
aoqi@0 5 * This code is free software; you can redistribute it and/or modify it
aoqi@0 6 * under the terms of the GNU General Public License version 2 only, as
aoqi@0 7 * published by the Free Software Foundation. Oracle designates this
aoqi@0 8 * particular file as subject to the "Classpath" exception as provided
aoqi@0 9 * by Oracle in the LICENSE file that accompanied this code.
aoqi@0 10 *
aoqi@0 11 * This code is distributed in the hope that it will be useful, but WITHOUT
aoqi@0 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aoqi@0 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aoqi@0 14 * version 2 for more details (a copy is included in the LICENSE file that
aoqi@0 15 * accompanied this code).
aoqi@0 16 *
aoqi@0 17 * You should have received a copy of the GNU General Public License version
aoqi@0 18 * 2 along with this work; if not, write to the Free Software Foundation,
aoqi@0 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aoqi@0 20 *
aoqi@0 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aoqi@0 22 * or visit www.oracle.com if you need additional information or have any
aoqi@0 23 * questions.
aoqi@0 24 */
aoqi@0 25
aoqi@0 26 package com.sun.tools.doclint;
aoqi@0 27
aoqi@0 28
aoqi@0 29 import java.util.Set;
aoqi@0 30 import java.util.LinkedHashSet;
aoqi@0 31
aoqi@0 32 import javax.lang.model.element.Element;
aoqi@0 33 import javax.lang.model.element.ElementKind;
aoqi@0 34 import javax.lang.model.element.ExecutableElement;
aoqi@0 35 import javax.lang.model.element.Modifier;
aoqi@0 36 import javax.lang.model.type.TypeMirror;
aoqi@0 37 import javax.lang.model.util.Elements;
aoqi@0 38 import javax.lang.model.util.Types;
aoqi@0 39
aoqi@0 40 import com.sun.source.doctree.DocCommentTree;
aoqi@0 41 import com.sun.source.util.DocTrees;
aoqi@0 42 import com.sun.source.util.JavacTask;
aoqi@0 43 import com.sun.source.util.SourcePositions;
aoqi@0 44 import com.sun.source.util.TreePath;
aoqi@0 45 import com.sun.tools.javac.model.JavacTypes;
aoqi@0 46 import com.sun.tools.javac.tree.JCTree;
aoqi@0 47 import com.sun.tools.javac.util.StringUtils;
aoqi@0 48
aoqi@0 49 /**
aoqi@0 50 * Utility container for current execution environment,
aoqi@0 51 * providing the current declaration and its doc comment.
aoqi@0 52 *
aoqi@0 53 * <p><b>This is NOT part of any supported API.
aoqi@0 54 * If you write code that depends on this, you do so at your own
aoqi@0 55 * risk. This code and its internal interfaces are subject to change
aoqi@0 56 * or deletion without notice.</b></p>
aoqi@0 57 */
aoqi@0 58 public class Env {
aoqi@0 59 /**
aoqi@0 60 * Access kinds for declarations.
aoqi@0 61 */
aoqi@0 62 public enum AccessKind {
aoqi@0 63 PRIVATE,
aoqi@0 64 PACKAGE,
aoqi@0 65 PROTECTED,
aoqi@0 66 PUBLIC;
aoqi@0 67
aoqi@0 68 static boolean accepts(String opt) {
aoqi@0 69 for (AccessKind g: values())
aoqi@0 70 if (opt.equals(StringUtils.toLowerCase(g.name()))) return true;
aoqi@0 71 return false;
aoqi@0 72 }
aoqi@0 73
aoqi@0 74 static AccessKind of(Set<Modifier> mods) {
aoqi@0 75 if (mods.contains(Modifier.PUBLIC))
aoqi@0 76 return AccessKind.PUBLIC;
aoqi@0 77 else if (mods.contains(Modifier.PROTECTED))
aoqi@0 78 return AccessKind.PROTECTED;
aoqi@0 79 else if (mods.contains(Modifier.PRIVATE))
aoqi@0 80 return AccessKind.PRIVATE;
aoqi@0 81 else
aoqi@0 82 return AccessKind.PACKAGE;
aoqi@0 83 }
aoqi@0 84 };
aoqi@0 85
aoqi@0 86 /** Message handler. */
aoqi@0 87 final Messages messages;
aoqi@0 88
aoqi@0 89 int implicitHeaderLevel = 0;
aoqi@0 90
aoqi@0 91 Set<String> customTags;
aoqi@0 92
aoqi@0 93 // Utility classes
aoqi@0 94 DocTrees trees;
aoqi@0 95 Elements elements;
aoqi@0 96 Types types;
aoqi@0 97
aoqi@0 98 // Types used when analysing doc comments.
aoqi@0 99 TypeMirror java_lang_Error;
aoqi@0 100 TypeMirror java_lang_RuntimeException;
aoqi@0 101 TypeMirror java_lang_Throwable;
aoqi@0 102 TypeMirror java_lang_Void;
aoqi@0 103
aoqi@0 104 /** The path for the declaration containing the comment currently being analyzed. */
aoqi@0 105 TreePath currPath;
aoqi@0 106 /** The element for the declaration containing the comment currently being analyzed. */
aoqi@0 107 Element currElement;
aoqi@0 108 /** The comment current being analyzed. */
aoqi@0 109 DocCommentTree currDocComment;
aoqi@0 110 /**
aoqi@0 111 * The access kind of the declaration containing the comment currently being analyzed.
aoqi@0 112 * This is the minimum (most restrictive) access kind of the declaration itself
aoqi@0 113 * and that of its containers. For example, a public method in a private class is
aoqi@0 114 * noted as private.
aoqi@0 115 */
aoqi@0 116 AccessKind currAccess;
aoqi@0 117 /** The set of methods, if any, that the current declaration overrides. */
aoqi@0 118 Set<? extends ExecutableElement> currOverriddenMethods;
aoqi@0 119
aoqi@0 120 Env() {
aoqi@0 121 messages = new Messages(this);
aoqi@0 122 }
aoqi@0 123
aoqi@0 124 void init(JavacTask task) {
aoqi@0 125 init(DocTrees.instance(task), task.getElements(), task.getTypes());
aoqi@0 126 }
aoqi@0 127
aoqi@0 128 void init(DocTrees trees, Elements elements, Types types) {
aoqi@0 129 this.trees = trees;
aoqi@0 130 this.elements = elements;
aoqi@0 131 this.types = types;
aoqi@0 132 java_lang_Error = elements.getTypeElement("java.lang.Error").asType();
aoqi@0 133 java_lang_RuntimeException = elements.getTypeElement("java.lang.RuntimeException").asType();
aoqi@0 134 java_lang_Throwable = elements.getTypeElement("java.lang.Throwable").asType();
aoqi@0 135 java_lang_Void = elements.getTypeElement("java.lang.Void").asType();
aoqi@0 136 }
aoqi@0 137
aoqi@0 138 void setImplicitHeaders(int n) {
aoqi@0 139 implicitHeaderLevel = n;
aoqi@0 140 }
aoqi@0 141
aoqi@0 142 void setCustomTags(String cTags) {
aoqi@0 143 customTags = new LinkedHashSet<String>();
aoqi@0 144 for (String s : cTags.split(DocLint.TAGS_SEPARATOR)) {
aoqi@0 145 if (!s.isEmpty())
aoqi@0 146 customTags.add(s);
aoqi@0 147 }
aoqi@0 148 }
aoqi@0 149
aoqi@0 150 /** Set the current declaration and its doc comment. */
aoqi@0 151 void setCurrent(TreePath path, DocCommentTree comment) {
aoqi@0 152 currPath = path;
aoqi@0 153 currDocComment = comment;
aoqi@0 154 currElement = trees.getElement(currPath);
aoqi@0 155 currOverriddenMethods = ((JavacTypes) types).getOverriddenMethods(currElement);
aoqi@0 156
aoqi@0 157 AccessKind ak = AccessKind.PUBLIC;
aoqi@0 158 for (TreePath p = path; p != null; p = p.getParentPath()) {
aoqi@0 159 Element e = trees.getElement(p);
aoqi@0 160 if (e != null && e.getKind() != ElementKind.PACKAGE) {
aoqi@0 161 ak = min(ak, AccessKind.of(e.getModifiers()));
aoqi@0 162 }
aoqi@0 163 }
aoqi@0 164 currAccess = ak;
aoqi@0 165 }
aoqi@0 166
aoqi@0 167 AccessKind getAccessKind() {
aoqi@0 168 return currAccess;
aoqi@0 169 }
aoqi@0 170
aoqi@0 171 long getPos(TreePath p) {
aoqi@0 172 return ((JCTree) p.getLeaf()).pos;
aoqi@0 173 }
aoqi@0 174
aoqi@0 175 long getStartPos(TreePath p) {
aoqi@0 176 SourcePositions sp = trees.getSourcePositions();
aoqi@0 177 return sp.getStartPosition(p.getCompilationUnit(), p.getLeaf());
aoqi@0 178 }
aoqi@0 179
aoqi@0 180 private <T extends Comparable<T>> T min(T item1, T item2) {
aoqi@0 181 return (item1 == null) ? item2
aoqi@0 182 : (item2 == null) ? item1
aoqi@0 183 : item1.compareTo(item2) <= 0 ? item1 : item2;
aoqi@0 184 }
aoqi@0 185 }

mercurial