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