Env.java revision 2571:10fc81ac75b4
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 */
25
26package com.sun.tools.doclint;
27
28
29import java.util.Set;
30import java.util.LinkedHashSet;
31
32import javax.lang.model.element.Element;
33import javax.lang.model.element.ElementKind;
34import javax.lang.model.element.ExecutableElement;
35import javax.lang.model.element.Modifier;
36import javax.lang.model.type.TypeMirror;
37import javax.lang.model.util.Elements;
38import javax.lang.model.util.Types;
39
40import com.sun.source.doctree.DocCommentTree;
41import com.sun.source.util.DocTrees;
42import com.sun.source.util.JavacTask;
43import com.sun.source.util.SourcePositions;
44import com.sun.source.util.TreePath;
45import com.sun.tools.javac.model.JavacTypes;
46import com.sun.tools.javac.tree.JCTree;
47import com.sun.tools.javac.util.StringUtils;
48
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 */
58public class Env {
59    /**
60     * Access kinds for declarations.
61     */
62    public enum AccessKind {
63        PRIVATE,
64        PACKAGE,
65        PROTECTED,
66        PUBLIC;
67
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        }
73
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    }
85
86    /** Message handler. */
87    final Messages messages;
88
89    int implicitHeaderLevel = 0;
90
91    Set<String> customTags;
92
93    // Utility classes
94    DocTrees trees;
95    Elements elements;
96    Types types;
97
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;
103
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;
119
120    Env() {
121        messages = new Messages(this);
122    }
123
124    void init(JavacTask task) {
125        init(DocTrees.instance(task), task.getElements(), task.getTypes());
126    }
127
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    }
137
138    void setImplicitHeaders(int n) {
139        implicitHeaderLevel = n;
140    }
141
142    void setCustomTags(String cTags) {
143        customTags = new LinkedHashSet<>();
144        for (String s : cTags.split(DocLint.TAGS_SEPARATOR)) {
145            if (!s.isEmpty())
146                customTags.add(s);
147        }
148    }
149
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);
156
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    }
166
167    AccessKind getAccessKind() {
168        return currAccess;
169    }
170
171    long getPos(TreePath p) {
172        return ((JCTree) p.getLeaf()).pos;
173    }
174
175    long getStartPos(TreePath p) {
176        SourcePositions sp = trees.getSourcePositions();
177        return sp.getStartPosition(p.getCompilationUnit(), p.getLeaf());
178    }
179
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}
186