Skip to content

Commit dbffe33

Browse files
committed
8345263: Make sure that lint categories are used correctly when logging lint warnings
Reviewed-by: vromero, jlahoda
1 parent 32c8195 commit dbffe33

24 files changed

+530
-392
lines changed

make/langtools/tools/propertiesparser/gen/ClassGenerator.java

+55-16
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
import java.util.Collections;
4949
import java.util.List;
5050
import java.util.Map;
51+
import java.util.Map.Entry;
5152
import java.util.Properties;
5253
import java.util.Set;
5354
import java.util.TreeMap;
@@ -87,9 +88,12 @@ enum StubKind {
8788
FACTORY_METHOD_DECL("factory.decl.method"),
8889
FACTORY_METHOD_ARG("factory.decl.method.arg"),
8990
FACTORY_METHOD_BODY("factory.decl.method.body"),
91+
FACTORY_METHOD_BODY_LINT("factory.decl.method.body.lint"),
9092
FACTORY_FIELD("factory.decl.field"),
93+
FACTORY_FIELD_LINT("factory.decl.field.lint"),
9194
WILDCARDS_EXTENDS("wildcards.extends"),
92-
SUPPRESS_WARNINGS("suppress.warnings");
95+
SUPPRESS_WARNINGS("suppress.warnings"),
96+
LINT_CATEGORY("lint.category");
9397

9498
/** stub key (as it appears in the property file) */
9599
String key;
@@ -114,6 +118,7 @@ String format(Object... args) {
114118
enum FactoryKind {
115119
ERR("err", "Error", "Errors"),
116120
WARN("warn", "Warning", "Warnings"),
121+
LINT_WARN("warn", "LintWarning", "LintWarnings"),
117122
NOTE("note", "Note", "Notes"),
118123
MISC("misc", "Fragment", "Fragments"),
119124
OTHER(null, null, null);
@@ -136,13 +141,24 @@ enum FactoryKind {
136141
/**
137142
* Utility method for parsing a factory kind from a resource key prefix.
138143
*/
139-
static FactoryKind parseFrom(String prefix) {
144+
static FactoryKind of(Entry<String, Message> messageEntry) {
145+
String prefix = messageEntry.getKey().split("\\.")[1];
146+
FactoryKind selected = null;
140147
for (FactoryKind k : FactoryKind.values()) {
141148
if (k.prefix == null || k.prefix.equals(prefix)) {
142-
return k;
149+
selected = k;
150+
break;
143151
}
144152
}
145-
return null;
153+
if (selected == WARN) {
154+
for (MessageLine line : messageEntry.getValue().getLines(false)) {
155+
if (line.isLint()) {
156+
selected = LINT_WARN;
157+
break;
158+
}
159+
}
160+
}
161+
return selected;
146162
}
147163
}
148164

@@ -155,7 +171,7 @@ public void generateFactory(MessageFile messageFile, File outDir) {
155171
messageFile.messages.entrySet().stream()
156172
.collect(
157173
Collectors.groupingBy(
158-
e -> FactoryKind.parseFrom(e.getKey().split("\\.")[1]),
174+
FactoryKind::of,
159175
TreeMap::new,
160176
toList()));
161177
//generate nested classes
@@ -165,7 +181,7 @@ public void generateFactory(MessageFile messageFile, File outDir) {
165181
if (entry.getKey() == FactoryKind.OTHER) continue;
166182
//emit members
167183
String members = entry.getValue().stream()
168-
.flatMap(e -> generateFactoryMethodsAndFields(e.getKey(), e.getValue()).stream())
184+
.flatMap(e -> generateFactoryMethodsAndFields(entry.getKey(), e.getKey(), e.getValue()).stream())
169185
.collect(Collectors.joining("\n\n"));
170186
//emit nested class
171187
String factoryDecl =
@@ -230,22 +246,35 @@ List<String> generateImports(Set<String> importedTypes) {
230246
/**
231247
* Generate a list of factory methods/fields to be added to a given factory nested class.
232248
*/
233-
List<String> generateFactoryMethodsAndFields(String key, Message msg) {
249+
List<String> generateFactoryMethodsAndFields(FactoryKind k, String key, Message msg) {
234250
MessageInfo msgInfo = msg.getMessageInfo();
235251
List<MessageLine> lines = msg.getLines(false);
236252
String javadoc = lines.stream()
237253
.filter(ml -> !ml.isInfo() && !ml.isEmptyOrComment())
238254
.map(ml -> ml.text)
239255
.collect(Collectors.joining("\n *"));
240256
String[] keyParts = key.split("\\.");
241-
FactoryKind k = FactoryKind.parseFrom(keyParts[1]);
257+
String lintCategory = lines.stream()
258+
.filter(MessageLine::isLint)
259+
.map(MessageLine::lintCategory)
260+
.findFirst().orElse(null);
261+
//System.out.println("category for " + key + " = " + lintCategory);
242262
String factoryName = factoryName(key);
243263
if (msgInfo.getTypes().isEmpty()) {
244264
//generate field
245-
String factoryField = StubKind.FACTORY_FIELD.format(k.keyClazz, factoryName,
246-
"\"" + keyParts[0] + "\"",
247-
"\"" + Stream.of(keyParts).skip(2).collect(Collectors.joining(".")) + "\"",
248-
javadoc);
265+
String factoryField;
266+
if (lintCategory == null) {
267+
factoryField = StubKind.FACTORY_FIELD.format(k.keyClazz, factoryName,
268+
"\"" + keyParts[0] + "\"",
269+
"\"" + Stream.of(keyParts).skip(2).collect(Collectors.joining(".")) + "\"",
270+
javadoc);
271+
} else {
272+
factoryField = StubKind.FACTORY_FIELD_LINT.format(k.keyClazz, factoryName,
273+
StubKind.LINT_CATEGORY.format("\"" + lintCategory + "\""),
274+
"\"" + keyParts[0] + "\"",
275+
"\"" + Stream.of(keyParts).skip(2).collect(Collectors.joining(".")) + "\"",
276+
javadoc);
277+
}
249278
return Collections.singletonList(factoryField);
250279
} else {
251280
//generate method
@@ -255,12 +284,22 @@ List<String> generateFactoryMethodsAndFields(String key, Message msg) {
255284
List<String> argNames = argNames(types.size());
256285
String suppressionString = needsSuppressWarnings(msgTypes) ?
257286
StubKind.SUPPRESS_WARNINGS.format() : "";
287+
String methodBody;
288+
if (lintCategory == null) {
289+
methodBody = StubKind.FACTORY_METHOD_BODY.format(k.keyClazz,
290+
"\"" + keyParts[0] + "\"",
291+
"\"" + Stream.of(keyParts).skip(2).collect(Collectors.joining(".")) + "\"",
292+
argNames.stream().collect(Collectors.joining(", ")));
293+
} else {
294+
methodBody = StubKind.FACTORY_METHOD_BODY_LINT.format(k.keyClazz,
295+
StubKind.LINT_CATEGORY.format("\"" + lintCategory + "\""),
296+
"\"" + keyParts[0] + "\"",
297+
"\"" + Stream.of(keyParts).skip(2).collect(Collectors.joining(".")) + "\"",
298+
argNames.stream().collect(Collectors.joining(", ")));
299+
}
258300
String factoryMethod = StubKind.FACTORY_METHOD_DECL.format(suppressionString, k.keyClazz,
259301
factoryName, argDecls(types, argNames).stream().collect(Collectors.joining(", ")),
260-
indent(StubKind.FACTORY_METHOD_BODY.format(k.keyClazz,
261-
"\"" + keyParts[0] + "\"",
262-
"\"" + Stream.of(keyParts).skip(2).collect(Collectors.joining(".")) + "\"",
263-
argNames.stream().collect(Collectors.joining(", "))), 1),
302+
indent(methodBody, 1),
264303
javadoc);
265304
factoryMethods.add(factoryMethod);
266305
}

make/langtools/tools/propertiesparser/parser/Message.java

+4-1
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@ public final class Message {
4949
public MessageInfo getMessageInfo() {
5050
if (messageInfo == null) {
5151
MessageLine l = firstLine.prev;
52+
if (l != null && l.isLint()) {
53+
l = l.prev;
54+
}
5255
if (l != null && l.isInfo())
5356
messageInfo = new MessageInfo(l.text);
5457
else
@@ -71,7 +74,7 @@ public List<MessageLine> getLines(boolean includeAllPrecedingComments) {
7174
while (l.text.isEmpty())
7275
l = l.next;
7376
} else {
74-
if (l.prev != null && l.prev.isInfo())
77+
if (l.prev != null && (l.prev.isInfo() || l.prev.isLint()))
7578
l = l.prev;
7679
}
7780

make/langtools/tools/propertiesparser/parser/MessageLine.java

+15
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
package propertiesparser.parser;
2727

28+
import java.util.regex.Matcher;
2829
import java.util.regex.Pattern;
2930

3031
/**
@@ -37,6 +38,7 @@ public class MessageLine {
3738
static final Pattern typePattern = Pattern.compile("[-\\\\'A-Z\\.a-z ]+( \\([-A-Za-z 0-9]+\\))?");
3839
static final Pattern infoPattern = Pattern.compile(String.format("# ([0-9]+: %s, )*[0-9]+: %s",
3940
typePattern.pattern(), typePattern.pattern()));
41+
static final Pattern lintPattern = Pattern.compile("# lint: ([a-z\\-]+)");
4042

4143
public String text;
4244
MessageLine prev;
@@ -54,6 +56,19 @@ public boolean isInfo() {
5456
return infoPattern.matcher(text).matches();
5557
}
5658

59+
public boolean isLint() {
60+
return lintPattern.matcher(text).matches();
61+
}
62+
63+
public String lintCategory() {
64+
Matcher matcher = lintPattern.matcher(text);
65+
if (matcher.matches()) {
66+
return matcher.group(1);
67+
} else {
68+
return null;
69+
}
70+
}
71+
5772
boolean hasContinuation() {
5873
return (next != null) && text.endsWith("\\");
5974
}

make/langtools/tools/propertiesparser/resources/templates.properties

+13
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,10 @@ toplevel.decl=\
2929
{1}\n\
3030
import com.sun.tools.javac.util.JCDiagnostic.Error;\n\
3131
import com.sun.tools.javac.util.JCDiagnostic.Warning;\n\
32+
import com.sun.tools.javac.util.JCDiagnostic.LintWarning;\n\
3233
import com.sun.tools.javac.util.JCDiagnostic.Note;\n\
3334
import com.sun.tools.javac.util.JCDiagnostic.Fragment;\n\
35+
import com.sun.tools.javac.code.Lint.LintCategory;\n\
3436
\n\
3537
public class {2} '{'\n\
3638
{3}\n\
@@ -58,16 +60,27 @@ factory.decl.method.arg=\
5860
factory.decl.method.body=\
5961
return new {0}({1}, {2}, {3});
6062

63+
factory.decl.method.body.lint=\
64+
return new {0}({1}, {2}, {3}, {4});
65+
6166
factory.decl.field=\
6267
/**\n\
6368
' '* {4}\n\
6469
' '*/\n\
6570
public static final {0} {1} = new {0}({2}, {3});
6671

72+
factory.decl.field.lint=\
73+
/**\n\
74+
' '* {5}\n\
75+
' '*/\n\
76+
public static final {0} {1} = new {0}({2}, {3}, {4});
77+
6778
wildcards.extends=\
6879
{0}<? extends {1}>
6980

7081
suppress.warnings=\
7182
@SuppressWarnings("rawtypes")\n
7283

84+
lint.category=\
85+
LintCategory.get({0})
7386

src/jdk.compiler/share/classes/com/sun/tools/javac/code/Lint.java

+13-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,10 @@
3333
import com.sun.tools.javac.code.Symbol.*;
3434
import com.sun.tools.javac.main.Option;
3535
import com.sun.tools.javac.util.Context;
36+
import com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition;
37+
import com.sun.tools.javac.util.JCDiagnostic.LintWarning;
3638
import com.sun.tools.javac.util.List;
39+
import com.sun.tools.javac.util.Log;
3740
import com.sun.tools.javac.util.Options;
3841
import com.sun.tools.javac.util.Pair;
3942

@@ -359,7 +362,7 @@ public enum LintCategory {
359362
map.put(option, this);
360363
}
361364

362-
static LintCategory get(String option) {
365+
public static LintCategory get(String option) {
363366
return map.get(option);
364367
}
365368

@@ -385,6 +388,15 @@ public boolean isSuppressed(LintCategory lc) {
385388
return suppressedValues.contains(lc);
386389
}
387390

391+
/**
392+
* Helper method. Log a lint warning if its lint category is enabled.
393+
*/
394+
public void logIfEnabled(Log log, DiagnosticPosition pos, LintWarning warning) {
395+
if (isEnabled(warning.getLintCategory())) {
396+
log.warning(pos, warning);
397+
}
398+
}
399+
388400
protected static class AugmentVisitor implements Attribute.Visitor {
389401
private final Context context;
390402
private Symtab syms;

src/jdk.compiler/share/classes/com/sun/tools/javac/code/Preview.java

+7-5
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,13 @@
3030
import com.sun.tools.javac.code.Symbol.ModuleSymbol;
3131
import com.sun.tools.javac.jvm.Target;
3232
import com.sun.tools.javac.resources.CompilerProperties.Errors;
33+
import com.sun.tools.javac.resources.CompilerProperties.LintWarnings;
3334
import com.sun.tools.javac.resources.CompilerProperties.Warnings;
3435
import com.sun.tools.javac.util.Assert;
3536
import com.sun.tools.javac.util.Context;
3637
import com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition;
3738
import com.sun.tools.javac.util.JCDiagnostic.Error;
39+
import com.sun.tools.javac.util.JCDiagnostic.LintWarning;
3840
import com.sun.tools.javac.util.JCDiagnostic.SimpleDiagnosticPosition;
3941
import com.sun.tools.javac.util.JCDiagnostic.Warning;
4042
import com.sun.tools.javac.util.Log;
@@ -175,8 +177,8 @@ public void warnPreview(DiagnosticPosition pos, Feature feature) {
175177
if (!lint.isSuppressed(LintCategory.PREVIEW)) {
176178
sourcesWithPreviewFeatures.add(log.currentSourceFile());
177179
previewHandler.report(pos, feature.isPlural() ?
178-
Warnings.PreviewFeatureUsePlural(feature.nameFragment()) :
179-
Warnings.PreviewFeatureUse(feature.nameFragment()));
180+
LintWarnings.PreviewFeatureUsePlural(feature.nameFragment()) :
181+
LintWarnings.PreviewFeatureUse(feature.nameFragment()));
180182
}
181183
}
182184

@@ -188,16 +190,16 @@ public void warnPreview(DiagnosticPosition pos, Feature feature) {
188190
public void warnPreview(JavaFileObject classfile, int majorVersion) {
189191
Assert.check(isEnabled());
190192
if (lint.isEnabled(LintCategory.PREVIEW)) {
191-
log.mandatoryWarning(LintCategory.PREVIEW, null,
192-
Warnings.PreviewFeatureUseClassfile(classfile, majorVersionToSource.get(majorVersion).name));
193+
log.mandatoryWarning(null,
194+
LintWarnings.PreviewFeatureUseClassfile(classfile, majorVersionToSource.get(majorVersion).name));
193195
}
194196
}
195197

196198
public void markUsesPreview(DiagnosticPosition pos) {
197199
sourcesWithPreviewFeatures.add(log.currentSourceFile());
198200
}
199201

200-
public void reportPreviewWarning(DiagnosticPosition pos, Warning warnKey) {
202+
public void reportPreviewWarning(DiagnosticPosition pos, LintWarning warnKey) {
201203
previewHandler.report(pos, warnKey);
202204
}
203205

0 commit comments

Comments
 (0)