author | jlahoda |
Wed, 11 Mar 2015 22:24:05 +0100 | |
changeset 29427 | 44f4e6905b67 |
parent 25874 | 83c19f00452c |
child 29957 | 7740f9657f56 |
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 |
||
29427
44f4e6905b67
8071851: Provide filtering of doclint checking based on packages
jlahoda
parents:
25874
diff
changeset
|
29 |
import java.util.Arrays; |
44f4e6905b67
8071851: Provide filtering of doclint checking based on packages
jlahoda
parents:
25874
diff
changeset
|
30 |
import java.util.HashSet; |
44f4e6905b67
8071851: Provide filtering of doclint checking based on packages
jlahoda
parents:
25874
diff
changeset
|
31 |
import java.util.LinkedHashSet; |
44f4e6905b67
8071851: Provide filtering of doclint checking based on packages
jlahoda
parents:
25874
diff
changeset
|
32 |
import java.util.List; |
14952 | 33 |
import java.util.Set; |
29427
44f4e6905b67
8071851: Provide filtering of doclint checking based on packages
jlahoda
parents:
25874
diff
changeset
|
34 |
import java.util.regex.Pattern; |
14952 | 35 |
|
36 |
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
|
37 |
import javax.lang.model.element.ElementKind; |
14952 | 38 |
import javax.lang.model.element.ExecutableElement; |
39 |
import javax.lang.model.element.Modifier; |
|
40 |
import javax.lang.model.type.TypeMirror; |
|
41 |
import javax.lang.model.util.Elements; |
|
42 |
import javax.lang.model.util.Types; |
|
29427
44f4e6905b67
8071851: Provide filtering of doclint checking based on packages
jlahoda
parents:
25874
diff
changeset
|
43 |
import javax.tools.Diagnostic.Kind; |
14952 | 44 |
|
45 |
import com.sun.source.doctree.DocCommentTree; |
|
46 |
import com.sun.source.util.DocTrees; |
|
47 |
import com.sun.source.util.JavacTask; |
|
48 |
import com.sun.source.util.SourcePositions; |
|
49 |
import com.sun.source.util.TreePath; |
|
50 |
import com.sun.tools.javac.model.JavacTypes; |
|
51 |
import com.sun.tools.javac.tree.JCTree; |
|
29427
44f4e6905b67
8071851: Provide filtering of doclint checking based on packages
jlahoda
parents:
25874
diff
changeset
|
52 |
import com.sun.tools.javac.util.MatchingUtils; |
22153
f9f06fcca59d
8029800: Flags.java uses String.toLowerCase without specifying Locale
jlahoda
parents:
21500
diff
changeset
|
53 |
import com.sun.tools.javac.util.StringUtils; |
14952 | 54 |
|
55 |
/** |
|
56 |
* Utility container for current execution environment, |
|
57 |
* providing the current declaration and its doc comment. |
|
58 |
* |
|
59 |
* <p><b>This is NOT part of any supported API. |
|
60 |
* If you write code that depends on this, you do so at your own |
|
61 |
* risk. This code and its internal interfaces are subject to change |
|
62 |
* or deletion without notice.</b></p> |
|
63 |
*/ |
|
64 |
public class Env { |
|
65 |
/** |
|
66 |
* Access kinds for declarations. |
|
67 |
*/ |
|
68 |
public enum AccessKind { |
|
69 |
PRIVATE, |
|
70 |
PACKAGE, |
|
71 |
PROTECTED, |
|
72 |
PUBLIC; |
|
73 |
||
74 |
static boolean accepts(String opt) { |
|
75 |
for (AccessKind g: values()) |
|
22153
f9f06fcca59d
8029800: Flags.java uses String.toLowerCase without specifying Locale
jlahoda
parents:
21500
diff
changeset
|
76 |
if (opt.equals(StringUtils.toLowerCase(g.name()))) return true; |
14952 | 77 |
return false; |
78 |
} |
|
79 |
||
80 |
static AccessKind of(Set<Modifier> mods) { |
|
81 |
if (mods.contains(Modifier.PUBLIC)) |
|
82 |
return AccessKind.PUBLIC; |
|
83 |
else if (mods.contains(Modifier.PROTECTED)) |
|
84 |
return AccessKind.PROTECTED; |
|
85 |
else if (mods.contains(Modifier.PRIVATE)) |
|
86 |
return AccessKind.PRIVATE; |
|
87 |
else |
|
88 |
return AccessKind.PACKAGE; |
|
89 |
} |
|
22163 | 90 |
} |
14952 | 91 |
|
92 |
/** Message handler. */ |
|
93 |
final Messages messages; |
|
94 |
||
16799
fbe4d27b8791
8006346: doclint should make allowance for headers generated by standard doclet
jjg
parents:
14952
diff
changeset
|
95 |
int implicitHeaderLevel = 0; |
fbe4d27b8791
8006346: doclint should make allowance for headers generated by standard doclet
jjg
parents:
14952
diff
changeset
|
96 |
|
21500
475e59d3b40c
8006248: Since addition of -Xdoclint, javadoc ignores unknown tags
bpatel
parents:
18908
diff
changeset
|
97 |
Set<String> customTags; |
475e59d3b40c
8006248: Since addition of -Xdoclint, javadoc ignores unknown tags
bpatel
parents:
18908
diff
changeset
|
98 |
|
29427
44f4e6905b67
8071851: Provide filtering of doclint checking based on packages
jlahoda
parents:
25874
diff
changeset
|
99 |
Set<Pattern> includePackages; |
44f4e6905b67
8071851: Provide filtering of doclint checking based on packages
jlahoda
parents:
25874
diff
changeset
|
100 |
Set<Pattern> excludePackages; |
44f4e6905b67
8071851: Provide filtering of doclint checking based on packages
jlahoda
parents:
25874
diff
changeset
|
101 |
|
14952 | 102 |
// Utility classes |
103 |
DocTrees trees; |
|
104 |
Elements elements; |
|
105 |
Types types; |
|
106 |
||
107 |
// Types used when analysing doc comments. |
|
108 |
TypeMirror java_lang_Error; |
|
109 |
TypeMirror java_lang_RuntimeException; |
|
110 |
TypeMirror java_lang_Throwable; |
|
111 |
TypeMirror java_lang_Void; |
|
112 |
||
113 |
/** The path for the declaration containing the comment currently being analyzed. */ |
|
114 |
TreePath currPath; |
|
115 |
/** The element for the declaration containing the comment currently being analyzed. */ |
|
116 |
Element currElement; |
|
117 |
/** The comment current being analyzed. */ |
|
118 |
DocCommentTree currDocComment; |
|
119 |
/** |
|
120 |
* 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
|
121 |
* This is the minimum (most restrictive) access kind of the declaration itself |
14952 | 122 |
* and that of its containers. For example, a public method in a private class is |
123 |
* noted as private. |
|
124 |
*/ |
|
125 |
AccessKind currAccess; |
|
126 |
/** The set of methods, if any, that the current declaration overrides. */ |
|
127 |
Set<? extends ExecutableElement> currOverriddenMethods; |
|
128 |
||
129 |
Env() { |
|
130 |
messages = new Messages(this); |
|
131 |
} |
|
132 |
||
133 |
void init(JavacTask task) { |
|
134 |
init(DocTrees.instance(task), task.getElements(), task.getTypes()); |
|
135 |
} |
|
136 |
||
137 |
void init(DocTrees trees, Elements elements, Types types) { |
|
138 |
this.trees = trees; |
|
139 |
this.elements = elements; |
|
140 |
this.types = types; |
|
29427
44f4e6905b67
8071851: Provide filtering of doclint checking based on packages
jlahoda
parents:
25874
diff
changeset
|
141 |
} |
44f4e6905b67
8071851: Provide filtering of doclint checking based on packages
jlahoda
parents:
25874
diff
changeset
|
142 |
|
44f4e6905b67
8071851: Provide filtering of doclint checking based on packages
jlahoda
parents:
25874
diff
changeset
|
143 |
void initTypes() { |
44f4e6905b67
8071851: Provide filtering of doclint checking based on packages
jlahoda
parents:
25874
diff
changeset
|
144 |
if (java_lang_Error != null) |
44f4e6905b67
8071851: Provide filtering of doclint checking based on packages
jlahoda
parents:
25874
diff
changeset
|
145 |
return ; |
44f4e6905b67
8071851: Provide filtering of doclint checking based on packages
jlahoda
parents:
25874
diff
changeset
|
146 |
|
14952 | 147 |
java_lang_Error = elements.getTypeElement("java.lang.Error").asType(); |
148 |
java_lang_RuntimeException = elements.getTypeElement("java.lang.RuntimeException").asType(); |
|
149 |
java_lang_Throwable = elements.getTypeElement("java.lang.Throwable").asType(); |
|
150 |
java_lang_Void = elements.getTypeElement("java.lang.Void").asType(); |
|
151 |
} |
|
152 |
||
16799
fbe4d27b8791
8006346: doclint should make allowance for headers generated by standard doclet
jjg
parents:
14952
diff
changeset
|
153 |
void setImplicitHeaders(int n) { |
fbe4d27b8791
8006346: doclint should make allowance for headers generated by standard doclet
jjg
parents:
14952
diff
changeset
|
154 |
implicitHeaderLevel = n; |
fbe4d27b8791
8006346: doclint should make allowance for headers generated by standard doclet
jjg
parents:
14952
diff
changeset
|
155 |
} |
fbe4d27b8791
8006346: doclint should make allowance for headers generated by standard doclet
jjg
parents:
14952
diff
changeset
|
156 |
|
21500
475e59d3b40c
8006248: Since addition of -Xdoclint, javadoc ignores unknown tags
bpatel
parents:
18908
diff
changeset
|
157 |
void setCustomTags(String cTags) { |
22163 | 158 |
customTags = new LinkedHashSet<>(); |
29427
44f4e6905b67
8071851: Provide filtering of doclint checking based on packages
jlahoda
parents:
25874
diff
changeset
|
159 |
for (String s : cTags.split(DocLint.SEPARATOR)) { |
21500
475e59d3b40c
8006248: Since addition of -Xdoclint, javadoc ignores unknown tags
bpatel
parents:
18908
diff
changeset
|
160 |
if (!s.isEmpty()) |
475e59d3b40c
8006248: Since addition of -Xdoclint, javadoc ignores unknown tags
bpatel
parents:
18908
diff
changeset
|
161 |
customTags.add(s); |
475e59d3b40c
8006248: Since addition of -Xdoclint, javadoc ignores unknown tags
bpatel
parents:
18908
diff
changeset
|
162 |
} |
475e59d3b40c
8006248: Since addition of -Xdoclint, javadoc ignores unknown tags
bpatel
parents:
18908
diff
changeset
|
163 |
} |
475e59d3b40c
8006248: Since addition of -Xdoclint, javadoc ignores unknown tags
bpatel
parents:
18908
diff
changeset
|
164 |
|
29427
44f4e6905b67
8071851: Provide filtering of doclint checking based on packages
jlahoda
parents:
25874
diff
changeset
|
165 |
void setCheckPackages(String packages) { |
44f4e6905b67
8071851: Provide filtering of doclint checking based on packages
jlahoda
parents:
25874
diff
changeset
|
166 |
includePackages = new HashSet<>(); |
44f4e6905b67
8071851: Provide filtering of doclint checking based on packages
jlahoda
parents:
25874
diff
changeset
|
167 |
excludePackages = new HashSet<>(); |
44f4e6905b67
8071851: Provide filtering of doclint checking based on packages
jlahoda
parents:
25874
diff
changeset
|
168 |
for (String pack : packages.split(DocLint.SEPARATOR)) { |
44f4e6905b67
8071851: Provide filtering of doclint checking based on packages
jlahoda
parents:
25874
diff
changeset
|
169 |
boolean excluded = false; |
44f4e6905b67
8071851: Provide filtering of doclint checking based on packages
jlahoda
parents:
25874
diff
changeset
|
170 |
if (pack.startsWith("-")) { |
44f4e6905b67
8071851: Provide filtering of doclint checking based on packages
jlahoda
parents:
25874
diff
changeset
|
171 |
pack = pack.substring(1); |
44f4e6905b67
8071851: Provide filtering of doclint checking based on packages
jlahoda
parents:
25874
diff
changeset
|
172 |
excluded = true; |
44f4e6905b67
8071851: Provide filtering of doclint checking based on packages
jlahoda
parents:
25874
diff
changeset
|
173 |
} |
44f4e6905b67
8071851: Provide filtering of doclint checking based on packages
jlahoda
parents:
25874
diff
changeset
|
174 |
if (pack.isEmpty()) |
44f4e6905b67
8071851: Provide filtering of doclint checking based on packages
jlahoda
parents:
25874
diff
changeset
|
175 |
continue; |
44f4e6905b67
8071851: Provide filtering of doclint checking based on packages
jlahoda
parents:
25874
diff
changeset
|
176 |
Pattern pattern = MatchingUtils.validImportStringToPattern(pack); |
44f4e6905b67
8071851: Provide filtering of doclint checking based on packages
jlahoda
parents:
25874
diff
changeset
|
177 |
if (excluded) { |
44f4e6905b67
8071851: Provide filtering of doclint checking based on packages
jlahoda
parents:
25874
diff
changeset
|
178 |
excludePackages.add(pattern); |
44f4e6905b67
8071851: Provide filtering of doclint checking based on packages
jlahoda
parents:
25874
diff
changeset
|
179 |
} else { |
44f4e6905b67
8071851: Provide filtering of doclint checking based on packages
jlahoda
parents:
25874
diff
changeset
|
180 |
includePackages.add(pattern); |
44f4e6905b67
8071851: Provide filtering of doclint checking based on packages
jlahoda
parents:
25874
diff
changeset
|
181 |
} |
44f4e6905b67
8071851: Provide filtering of doclint checking based on packages
jlahoda
parents:
25874
diff
changeset
|
182 |
} |
44f4e6905b67
8071851: Provide filtering of doclint checking based on packages
jlahoda
parents:
25874
diff
changeset
|
183 |
} |
44f4e6905b67
8071851: Provide filtering of doclint checking based on packages
jlahoda
parents:
25874
diff
changeset
|
184 |
|
44f4e6905b67
8071851: Provide filtering of doclint checking based on packages
jlahoda
parents:
25874
diff
changeset
|
185 |
static boolean validatePackages(String packages) { |
44f4e6905b67
8071851: Provide filtering of doclint checking based on packages
jlahoda
parents:
25874
diff
changeset
|
186 |
for (String pack : packages.split(DocLint.SEPARATOR)) { |
44f4e6905b67
8071851: Provide filtering of doclint checking based on packages
jlahoda
parents:
25874
diff
changeset
|
187 |
if (pack.startsWith("-")) { |
44f4e6905b67
8071851: Provide filtering of doclint checking based on packages
jlahoda
parents:
25874
diff
changeset
|
188 |
pack = pack.substring(1); |
44f4e6905b67
8071851: Provide filtering of doclint checking based on packages
jlahoda
parents:
25874
diff
changeset
|
189 |
} |
44f4e6905b67
8071851: Provide filtering of doclint checking based on packages
jlahoda
parents:
25874
diff
changeset
|
190 |
if (!pack.isEmpty() && !MatchingUtils.isValidImportString(pack)) |
44f4e6905b67
8071851: Provide filtering of doclint checking based on packages
jlahoda
parents:
25874
diff
changeset
|
191 |
return false; |
44f4e6905b67
8071851: Provide filtering of doclint checking based on packages
jlahoda
parents:
25874
diff
changeset
|
192 |
} |
44f4e6905b67
8071851: Provide filtering of doclint checking based on packages
jlahoda
parents:
25874
diff
changeset
|
193 |
return true; |
44f4e6905b67
8071851: Provide filtering of doclint checking based on packages
jlahoda
parents:
25874
diff
changeset
|
194 |
} |
44f4e6905b67
8071851: Provide filtering of doclint checking based on packages
jlahoda
parents:
25874
diff
changeset
|
195 |
|
14952 | 196 |
/** Set the current declaration and its doc comment. */ |
197 |
void setCurrent(TreePath path, DocCommentTree comment) { |
|
198 |
currPath = path; |
|
199 |
currDocComment = comment; |
|
200 |
currElement = trees.getElement(currPath); |
|
201 |
currOverriddenMethods = ((JavacTypes) types).getOverriddenMethods(currElement); |
|
202 |
||
18908 | 203 |
AccessKind ak = AccessKind.PUBLIC; |
14952 | 204 |
for (TreePath p = path; p != null; p = p.getParentPath()) { |
205 |
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
|
206 |
if (e != null && e.getKind() != ElementKind.PACKAGE) { |
14952 | 207 |
ak = min(ak, AccessKind.of(e.getModifiers())); |
208 |
} |
|
209 |
} |
|
210 |
currAccess = ak; |
|
211 |
} |
|
212 |
||
213 |
AccessKind getAccessKind() { |
|
214 |
return currAccess; |
|
215 |
} |
|
216 |
||
217 |
long getPos(TreePath p) { |
|
218 |
return ((JCTree) p.getLeaf()).pos; |
|
219 |
} |
|
220 |
||
221 |
long getStartPos(TreePath p) { |
|
222 |
SourcePositions sp = trees.getSourcePositions(); |
|
223 |
return sp.getStartPosition(p.getCompilationUnit(), p.getLeaf()); |
|
224 |
} |
|
225 |
||
226 |
private <T extends Comparable<T>> T min(T item1, T item2) { |
|
227 |
return (item1 == null) ? item2 |
|
228 |
: (item2 == null) ? item1 |
|
229 |
: item1.compareTo(item2) <= 0 ? item1 : item2; |
|
230 |
} |
|
231 |
} |