Skip to content

Commit 3aefd1c

Browse files
committed
8320234: Merge doclint.Env.AccessKind with tool.AccessKind
Reviewed-by: jjg
1 parent d6d7bdc commit 3aefd1c

File tree

7 files changed

+132
-189
lines changed

7 files changed

+132
-189
lines changed

src/jdk.javadoc/share/classes/jdk/javadoc/internal/doclets/toolkit/BaseConfiguration.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@
7373
import jdk.javadoc.internal.doclets.toolkit.util.VisibleMemberCache;
7474
import jdk.javadoc.internal.doclets.toolkit.util.VisibleMemberTable;
7575
import jdk.javadoc.internal.doclint.DocLint;
76-
import jdk.javadoc.internal.doclint.Env;
76+
import jdk.javadoc.internal.tool.AccessLevel;
7777

7878
/**
7979
* Configure the output based on the options. Doclets should subclass
@@ -654,10 +654,10 @@ public boolean isDocLintSyntaxGroupEnabled() {
654654

655655
private boolean isDocLintGroupEnabled(jdk.javadoc.internal.doclint.Messages.Group group) {
656656
// Use AccessKind.PUBLIC as a stand-in, since it is not common to
657-
// set DocLint options per access kind (as is common with javac.)
658-
// A more sophisticated solution might be to derive the access kind from the
657+
// set DocLint options per access level (as is common with javac.)
658+
// A more sophisticated solution might be to derive the access level from the
659659
// element owning the comment, and its enclosing elements.
660-
return doclint != null && doclint.isGroupEnabled(group, Env.AccessKind.PUBLIC);
660+
return doclint != null && doclint.isGroupEnabled(group, AccessLevel.PUBLIC);
661661
}
662662
//</editor-fold>
663663
}

src/jdk.javadoc/share/classes/jdk/javadoc/internal/doclint/DocLint.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@
6464
import com.sun.tools.javac.util.DefinedBy;
6565
import com.sun.tools.javac.util.DefinedBy.Api;
6666
import com.sun.tools.javac.util.StringUtils.DamerauLevenshteinDistance;
67+
import jdk.javadoc.internal.tool.AccessLevel;
6768

6869
/**
6970
* Multi-function entry point for the doc check utility.
@@ -410,8 +411,8 @@ public boolean isValidOption(String opt) {
410411
return false;
411412
}
412413

413-
public boolean isGroupEnabled(Messages.Group group, Env.AccessKind accessKind) {
414-
return env.messages.isEnabled(group, accessKind);
414+
public boolean isGroupEnabled(Messages.Group group, AccessLevel accessLevel) {
415+
return env.messages.isEnabled(group, accessLevel);
415416
}
416417

417418
private String localize(String code, Object... args) {

src/jdk.javadoc/share/classes/jdk/javadoc/internal/doclint/Env.java

Lines changed: 12 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2012, 2022, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2012, 2023, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -62,38 +62,13 @@
6262
import com.sun.tools.javac.tree.JCTree;
6363
import com.sun.tools.javac.util.MatchingUtils;
6464
import com.sun.tools.javac.util.StringUtils;
65+
import jdk.javadoc.internal.tool.AccessLevel;
6566

6667
/**
6768
* Utility container for current execution environment,
6869
* providing the current declaration and its doc comment.
6970
*/
7071
public class Env {
71-
/**
72-
* Access kinds for declarations.
73-
*/
74-
public enum AccessKind {
75-
PRIVATE,
76-
PACKAGE,
77-
PROTECTED,
78-
PUBLIC;
79-
80-
static boolean accepts(String opt) {
81-
for (AccessKind g: values())
82-
if (opt.equals(StringUtils.toLowerCase(g.name()))) return true;
83-
return false;
84-
}
85-
86-
static AccessKind of(Set<Modifier> mods) {
87-
if (mods.contains(Modifier.PUBLIC))
88-
return AccessKind.PUBLIC;
89-
else if (mods.contains(Modifier.PROTECTED))
90-
return AccessKind.PROTECTED;
91-
else if (mods.contains(Modifier.PRIVATE))
92-
return AccessKind.PRIVATE;
93-
else
94-
return AccessKind.PACKAGE;
95-
}
96-
}
9772

9873
/** Message handler. */
9974
final Messages messages;
@@ -136,12 +111,12 @@ else if (mods.contains(Modifier.PRIVATE))
136111
/** The comment current being analyzed. */
137112
DocCommentTree currDocComment;
138113
/**
139-
* The access kind of the declaration containing the comment currently being analyzed.
140-
* This is the minimum (most restrictive) access kind of the declaration itself
114+
* The access level of the declaration containing the comment currently being analyzed.
115+
* This is the most limiting access level of the declaration itself
141116
* and that of its containers. For example, a public method in a private class is
142117
* noted as private.
143118
*/
144-
AccessKind currAccess;
119+
AccessLevel currAccess;
145120
/** The set of methods, if any, that the current declaration overrides. */
146121
Set<? extends ExecutableElement> currOverriddenMethods;
147122

@@ -219,18 +194,18 @@ void setCurrent(TreePath path, DocCommentTree comment) {
219194
currElement = trees.getElement(currPath);
220195
currOverriddenMethods = ((JavacTypes) types).getOverriddenMethods(currElement);
221196

222-
AccessKind ak = AccessKind.PUBLIC;
197+
// It's convenient to use AccessLevel to model effects that nesting has
198+
// on access. While very similar, those are not the same concept.
199+
var mostLimitingSoFar = AccessLevel.PUBLIC;
223200
for (TreePath p = path; p != null; p = p.getParentPath()) {
224201
Element e = trees.getElement(p);
225202
if (e != null && e.getKind() != ElementKind.PACKAGE && e.getKind() != ElementKind.MODULE) {
226-
ak = min(ak, AccessKind.of(e.getModifiers()));
203+
var level = AccessLevel.of(e.getModifiers());
204+
mostLimitingSoFar = mostLimitingSoFar.compareTo(level) <= 0
205+
? mostLimitingSoFar : level;
227206
}
228207
}
229-
currAccess = ak;
230-
}
231-
232-
AccessKind getAccessKind() {
233-
return currAccess;
208+
currAccess = mostLimitingSoFar;
234209
}
235210

236211
long getPos(TreePath p) {
@@ -359,11 +334,4 @@ private List<String> getSuppressWarningsValue(Element e) {
359334
}
360335
return List.of();
361336
}
362-
363-
364-
private <T extends Comparable<T>> T min(T item1, T item2) {
365-
return (item1 == null) ? item2
366-
: (item2 == null) ? item1
367-
: item1.compareTo(item2) <= 0 ? item1 : item2;
368-
}
369337
}

src/jdk.javadoc/share/classes/jdk/javadoc/internal/doclint/Messages.java

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
import com.sun.source.tree.Tree;
4444
import com.sun.tools.javac.api.JavacTrees;
4545
import com.sun.tools.javac.util.StringUtils;
46-
import jdk.javadoc.internal.doclint.Env.AccessKind;
46+
import jdk.javadoc.internal.tool.AccessLevel;
4747

4848
/**
4949
* Message reporting for DocLint.
@@ -109,8 +109,8 @@ void setStatsEnabled(boolean b) {
109109
stats.setEnabled(b);
110110
}
111111

112-
boolean isEnabled(Group group, Env.AccessKind ak) {
113-
return options.isEnabled(group, ak);
112+
boolean isEnabled(Group group, AccessLevel al) {
113+
return options.isEnabled(group, al);
114114
}
115115

116116
void reportStats(PrintWriter out) {
@@ -176,7 +176,7 @@ String localize(String code, Object... args) {
176176
* Handler for (sub)options specific to message handling.
177177
*/
178178
static class Options {
179-
Map<String, Env.AccessKind> map = new HashMap<>();
179+
private final Map<String, AccessLevel> map = new HashMap<>();
180180
private final Stats stats;
181181

182182
static boolean isValidOptions(String opts) {
@@ -195,35 +195,40 @@ private static boolean isValidOption(String opt) {
195195
int sep = opt.indexOf("/");
196196
String grp = opt.substring(begin, (sep != -1) ? sep : opt.length());
197197
return ((begin == 0 && grp.equals("all")) || Group.accepts(grp))
198-
&& ((sep == -1) || AccessKind.accepts(opt.substring(sep + 1)));
198+
&& ((sep == -1) || accepts(opt.substring(sep + 1)));
199+
}
200+
201+
static boolean accepts(String opt) {
202+
for (var level: AccessLevel.values())
203+
if (opt.equals(StringUtils.toLowerCase(level.name()))) return true;
204+
return false;
199205
}
200206

201207
Options(Stats stats) {
202208
this.stats = stats;
203209
}
204210

205211
/** Determine if a message group is enabled for a particular access level. */
206-
boolean isEnabled(Group g, Env.AccessKind access) {
212+
boolean isEnabled(Group g, AccessLevel access) {
207213
if (map.isEmpty())
208-
map.put("all", Env.AccessKind.PROTECTED);
214+
map.put(ALL, AccessLevel.PROTECTED);
209215

210-
Env.AccessKind ak = map.get(g.optName());
211-
if (ak != null && access.compareTo(ak) >= 0)
216+
AccessLevel al = map.get(g.optName());
217+
if (al != null && access.compareTo(al) >= 0)
212218
return true;
213219

214-
ak = map.get(ALL);
215-
if (ak != null && access.compareTo(ak) >= 0) {
216-
ak = map.get(g.notOptName());
217-
if (ak == null || access.compareTo(ak) > 0) // note >, not >=
218-
return true;
220+
al = map.get(ALL);
221+
if (al != null && access.compareTo(al) >= 0) {
222+
al = map.get(g.notOptName());
223+
return al == null || access.compareTo(al) > 0; // note >, not >=
219224
}
220225

221226
return false;
222227
}
223228

224229
void setOptions(String opts) {
225230
if (opts == null)
226-
setOption(ALL, Env.AccessKind.PRIVATE);
231+
setOption(ALL, AccessLevel.PRIVATE);
227232
else {
228233
for (String opt: opts.split(","))
229234
setOption(StringUtils.toLowerCase(opt.trim()));
@@ -238,16 +243,16 @@ private void setOption(String arg) throws IllegalArgumentException {
238243

239244
int sep = arg.indexOf("/");
240245
if (sep > 0) {
241-
Env.AccessKind ak = Env.AccessKind.valueOf(StringUtils.toUpperCase(arg.substring(sep + 1)));
242-
setOption(arg.substring(0, sep), ak);
246+
var al = AccessLevel.valueOf(StringUtils.toUpperCase(arg.substring(sep + 1)));
247+
setOption(arg.substring(0, sep), al);
243248
} else {
244249
setOption(arg, null);
245250
}
246251
}
247252

248-
private void setOption(String opt, Env.AccessKind ak) {
249-
map.put(opt, (ak != null) ? ak
250-
: opt.startsWith("-") ? Env.AccessKind.PUBLIC : Env.AccessKind.PRIVATE);
253+
private void setOption(String opt, AccessLevel al) {
254+
map.put(opt, (al != null) ? al
255+
: opt.startsWith("-") ? AccessLevel.PUBLIC : AccessLevel.PRIVATE);
251256
}
252257

253258
private static final String ALL = "all";

src/jdk.javadoc/share/classes/jdk/javadoc/internal/tool/AccessKind.java renamed to src/jdk.javadoc/share/classes/jdk/javadoc/internal/tool/AccessLevel.java

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2016, 2022, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2016, 2023, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -25,16 +25,40 @@
2525

2626
package jdk.javadoc.internal.tool;
2727

28+
import java.util.Set;
29+
import javax.lang.model.element.Modifier;
30+
2831
/**
29-
* The access value kinds.
32+
* The access levels.
33+
*
34+
* These constants are ordered by their access limiting power. The bigger the
35+
* {@link #ordinal() ordinal} of a constant, the more limiting power that
36+
* constant has.
37+
*
38+
* That has a few useful implications. For example, the levels can be compared
39+
* by {@link #compareTo}. It also means that {@code AccessLevel.values()[0]} and
40+
* {@code AccessLevel.values()[values.length() - 1] and the constants with the
41+
* smallest and the biggest limiting powers respectively.
3042
*/
31-
public enum AccessKind {
32-
/** Limits access to public entities */
33-
PUBLIC,
43+
public enum AccessLevel implements Comparable<AccessLevel> {
44+
45+
/** Does not limit access */
46+
PRIVATE,
47+
/** Limits access to entities that are public, protected, or declared with package access */
48+
PACKAGE,
3449
/** Limits access to public and protected entities */
3550
PROTECTED,
36-
/** Limits access to public, protected and package private entities */
37-
PACKAGE,
38-
/** No limits */
39-
PRIVATE;
51+
/** Limits access to public entities */
52+
PUBLIC;
53+
54+
public static AccessLevel of(Set<Modifier> mods) {
55+
if (mods.contains(Modifier.PUBLIC))
56+
return AccessLevel.PUBLIC;
57+
else if (mods.contains(Modifier.PROTECTED))
58+
return AccessLevel.PROTECTED;
59+
else if (mods.contains(Modifier.PRIVATE))
60+
return AccessLevel.PRIVATE;
61+
else
62+
return AccessLevel.PACKAGE;
63+
}
4064
}

0 commit comments

Comments
 (0)