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

Wed, 10 Jun 2015 14:22:04 -0700

author
mfang
date
Wed, 10 Jun 2015 14:22:04 -0700
changeset 2815
d4051d4f5daf
parent 2413
fe033d997ddf
child 2525
2eb010b6cb22
permissions
-rw-r--r--

8083601: jdk8u60 l10n resource file translation update 2
Reviewed-by: ksrini, yhuang

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

mercurial