Fri, 10 May 2013 15:15:50 +0200
8012929: Trees.getElement should work not only for declaration trees, but also for use-trees
Reviewed-by: jjg
Contributed-by: Dusan Balek <dbalek@netbeans.org>, Jan Lahoda <jlahoda@netbeans.org>
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;
31 import javax.lang.model.element.Element;
32 import javax.lang.model.element.ElementKind;
33 import javax.lang.model.element.ExecutableElement;
34 import javax.lang.model.element.Modifier;
35 import javax.lang.model.type.TypeMirror;
36 import javax.lang.model.util.Elements;
37 import javax.lang.model.util.Types;
39 import com.sun.source.doctree.DocCommentTree;
40 import com.sun.source.util.DocTrees;
41 import com.sun.source.util.JavacTask;
42 import com.sun.source.util.SourcePositions;
43 import com.sun.source.util.TreePath;
44 import com.sun.tools.javac.model.JavacTypes;
45 import com.sun.tools.javac.tree.JCTree;
47 /**
48 * Utility container for current execution environment,
49 * providing the current declaration and its doc comment.
50 *
51 * <p><b>This is NOT part of any supported API.
52 * If you write code that depends on this, you do so at your own
53 * risk. This code and its internal interfaces are subject to change
54 * or deletion without notice.</b></p>
55 */
56 public class Env {
57 /**
58 * Access kinds for declarations.
59 */
60 public enum AccessKind {
61 PRIVATE,
62 PACKAGE,
63 PROTECTED,
64 PUBLIC;
66 static boolean accepts(String opt) {
67 for (AccessKind g: values())
68 if (opt.equals(g.name().toLowerCase())) return true;
69 return false;
70 }
72 static AccessKind of(Set<Modifier> mods) {
73 if (mods.contains(Modifier.PUBLIC))
74 return AccessKind.PUBLIC;
75 else if (mods.contains(Modifier.PROTECTED))
76 return AccessKind.PROTECTED;
77 else if (mods.contains(Modifier.PRIVATE))
78 return AccessKind.PRIVATE;
79 else
80 return AccessKind.PACKAGE;
81 }
82 };
84 /** Message handler. */
85 final Messages messages;
87 int implicitHeaderLevel = 0;
89 // Utility classes
90 DocTrees trees;
91 Elements elements;
92 Types types;
94 // Types used when analysing doc comments.
95 TypeMirror java_lang_Error;
96 TypeMirror java_lang_RuntimeException;
97 TypeMirror java_lang_Throwable;
98 TypeMirror java_lang_Void;
100 /** The path for the declaration containing the comment currently being analyzed. */
101 TreePath currPath;
102 /** The element for the declaration containing the comment currently being analyzed. */
103 Element currElement;
104 /** The comment current being analyzed. */
105 DocCommentTree currDocComment;
106 /**
107 * The access kind of the declaration containing the comment currently being analyzed.
108 * This is the minimum (most restrictive) access kind of the declaration itself
109 * and that of its containers. For example, a public method in a private class is
110 * noted as private.
111 */
112 AccessKind currAccess;
113 /** The set of methods, if any, that the current declaration overrides. */
114 Set<? extends ExecutableElement> currOverriddenMethods;
116 Env() {
117 messages = new Messages(this);
118 }
120 void init(JavacTask task) {
121 init(DocTrees.instance(task), task.getElements(), task.getTypes());
122 }
124 void init(DocTrees trees, Elements elements, Types types) {
125 this.trees = trees;
126 this.elements = elements;
127 this.types = types;
128 java_lang_Error = elements.getTypeElement("java.lang.Error").asType();
129 java_lang_RuntimeException = elements.getTypeElement("java.lang.RuntimeException").asType();
130 java_lang_Throwable = elements.getTypeElement("java.lang.Throwable").asType();
131 java_lang_Void = elements.getTypeElement("java.lang.Void").asType();
132 }
134 void setImplicitHeaders(int n) {
135 implicitHeaderLevel = n;
136 }
138 /** Set the current declaration and its doc comment. */
139 void setCurrent(TreePath path, DocCommentTree comment) {
140 currPath = path;
141 currDocComment = comment;
142 currElement = trees.getElement(currPath);
143 currOverriddenMethods = ((JavacTypes) types).getOverriddenMethods(currElement);
145 AccessKind ak = null;
146 for (TreePath p = path; p != null; p = p.getParentPath()) {
147 Element e = trees.getElement(p);
148 if (e != null && e.getKind() != ElementKind.PACKAGE) {
149 ak = min(ak, AccessKind.of(e.getModifiers()));
150 }
151 }
152 currAccess = ak;
153 }
155 AccessKind getAccessKind() {
156 return currAccess;
157 }
159 long getPos(TreePath p) {
160 return ((JCTree) p.getLeaf()).pos;
161 }
163 long getStartPos(TreePath p) {
164 SourcePositions sp = trees.getSourcePositions();
165 return sp.getStartPosition(p.getCompilationUnit(), p.getLeaf());
166 }
168 private <T extends Comparable<T>> T min(T item1, T item2) {
169 return (item1 == null) ? item2
170 : (item2 == null) ? item1
171 : item1.compareTo(item2) <= 0 ? item1 : item2;
172 }
173 }