author | briangoetz |
Wed, 18 Dec 2013 16:05:18 -0500 | |
changeset 22163 | 3651128c74eb |
parent 22153 | f9f06fcca59d |
permissions | -rw-r--r-- |
14952 | 1 |
/* |
16799
fbe4d27b8791
8006346: doclint should make allowance for headers generated by standard doclet
jjg
parents:
14952
diff
changeset
|
2 |
* Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved. |
14952 | 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; |
|
21500
475e59d3b40c
8006248: Since addition of -Xdoclint, javadoc ignores unknown tags
bpatel
parents:
18908
diff
changeset
|
30 |
import java.util.LinkedHashSet; |
14952 | 31 |
|
32 |
import javax.lang.model.element.Element; |
|
17557
9c6ace1881fe
8012929: Trees.getElement should work not only for declaration trees, but also for use-trees
jlahoda
parents:
16799
diff
changeset
|
33 |
import javax.lang.model.element.ElementKind; |
14952 | 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; |
|
39 |
||
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; |
|
22153
f9f06fcca59d
8029800: Flags.java uses String.toLowerCase without specifying Locale
jlahoda
parents:
21500
diff
changeset
|
47 |
import com.sun.tools.javac.util.StringUtils; |
14952 | 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 |
*/ |
|
58 |
public 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()) |
|
22153
f9f06fcca59d
8029800: Flags.java uses String.toLowerCase without specifying Locale
jlahoda
parents:
21500
diff
changeset
|
70 |
if (opt.equals(StringUtils.toLowerCase(g.name()))) return true; |
14952 | 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 |
} |
|
22163 | 84 |
} |
14952 | 85 |
|
86 |
/** Message handler. */ |
|
87 |
final Messages messages; |
|
88 |
||
16799
fbe4d27b8791
8006346: doclint should make allowance for headers generated by standard doclet
jjg
parents:
14952
diff
changeset
|
89 |
int implicitHeaderLevel = 0; |
fbe4d27b8791
8006346: doclint should make allowance for headers generated by standard doclet
jjg
parents:
14952
diff
changeset
|
90 |
|
21500
475e59d3b40c
8006248: Since addition of -Xdoclint, javadoc ignores unknown tags
bpatel
parents:
18908
diff
changeset
|
91 |
Set<String> customTags; |
475e59d3b40c
8006248: Since addition of -Xdoclint, javadoc ignores unknown tags
bpatel
parents:
18908
diff
changeset
|
92 |
|
14952 | 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. |
|
16799
fbe4d27b8791
8006346: doclint should make allowance for headers generated by standard doclet
jjg
parents:
14952
diff
changeset
|
112 |
* This is the minimum (most restrictive) access kind of the declaration itself |
14952 | 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 |
||
16799
fbe4d27b8791
8006346: doclint should make allowance for headers generated by standard doclet
jjg
parents:
14952
diff
changeset
|
138 |
void setImplicitHeaders(int n) { |
fbe4d27b8791
8006346: doclint should make allowance for headers generated by standard doclet
jjg
parents:
14952
diff
changeset
|
139 |
implicitHeaderLevel = n; |
fbe4d27b8791
8006346: doclint should make allowance for headers generated by standard doclet
jjg
parents:
14952
diff
changeset
|
140 |
} |
fbe4d27b8791
8006346: doclint should make allowance for headers generated by standard doclet
jjg
parents:
14952
diff
changeset
|
141 |
|
21500
475e59d3b40c
8006248: Since addition of -Xdoclint, javadoc ignores unknown tags
bpatel
parents:
18908
diff
changeset
|
142 |
void setCustomTags(String cTags) { |
22163 | 143 |
customTags = new LinkedHashSet<>(); |
21500
475e59d3b40c
8006248: Since addition of -Xdoclint, javadoc ignores unknown tags
bpatel
parents:
18908
diff
changeset
|
144 |
for (String s : cTags.split(DocLint.TAGS_SEPARATOR)) { |
475e59d3b40c
8006248: Since addition of -Xdoclint, javadoc ignores unknown tags
bpatel
parents:
18908
diff
changeset
|
145 |
if (!s.isEmpty()) |
475e59d3b40c
8006248: Since addition of -Xdoclint, javadoc ignores unknown tags
bpatel
parents:
18908
diff
changeset
|
146 |
customTags.add(s); |
475e59d3b40c
8006248: Since addition of -Xdoclint, javadoc ignores unknown tags
bpatel
parents:
18908
diff
changeset
|
147 |
} |
475e59d3b40c
8006248: Since addition of -Xdoclint, javadoc ignores unknown tags
bpatel
parents:
18908
diff
changeset
|
148 |
} |
475e59d3b40c
8006248: Since addition of -Xdoclint, javadoc ignores unknown tags
bpatel
parents:
18908
diff
changeset
|
149 |
|
14952 | 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 |
||
18908 | 157 |
AccessKind ak = AccessKind.PUBLIC; |
14952 | 158 |
for (TreePath p = path; p != null; p = p.getParentPath()) { |
159 |
Element e = trees.getElement(p); |
|
17557
9c6ace1881fe
8012929: Trees.getElement should work not only for declaration trees, but also for use-trees
jlahoda
parents:
16799
diff
changeset
|
160 |
if (e != null && e.getKind() != ElementKind.PACKAGE) { |
14952 | 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 |
} |