Skip to content

Commit 2ac97b2

Browse files
author
Srikanth Adayapalam
committed
8254274: Development to add 'lint' warning for @valuebased classes
Reviewed-by: dlsmith, jlaskey
1 parent f2f03c8 commit 2ac97b2

File tree

12 files changed

+173
-2
lines changed

12 files changed

+173
-2
lines changed

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ protected Lint(Context context) {
125125
if (!options.isSet(Option.PREVIEW)) {
126126
values.add(LintCategory.PREVIEW);
127127
}
128+
values.add(LintCategory.SYNCHRONIZATION);
128129
}
129130

130131
// Look for specific overrides
@@ -282,6 +283,11 @@ public enum LintCategory {
282283
*/
283284
STATIC("static"),
284285

286+
/**
287+
* Warn about synchronization attempts on instances of @ValueBased classes.
288+
*/
289+
SYNCHRONIZATION("synchronization"),
290+
285291
/**
286292
* Warn about issues relating to use of text blocks
287293
*/

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,7 @@ public static Symtab instance(Context context) {
220220
public final Type previewFeatureInternalType;
221221
public final Type typeDescriptorType;
222222
public final Type recordType;
223+
public final Type valueBasedType;
223224

224225
/** The symbol representing the length field of an array.
225226
*/
@@ -584,6 +585,7 @@ public <R, P> R accept(ElementVisitor<R, P> v, P p) {
584585
previewFeatureInternalType = enterSyntheticAnnotation("jdk.internal.PreviewFeature+Annotation");
585586
typeDescriptorType = enterClass("java.lang.invoke.TypeDescriptor");
586587
recordType = enterClass("java.lang.Record");
588+
valueBasedType = enterClass("jdk.internal.ValueBased");
587589

588590
synthesizeEmptyInterfaceIfMissing(autoCloseableType);
589591
synthesizeEmptyInterfaceIfMissing(cloneableType);

src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Attr.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1737,9 +1737,24 @@ private Symbol enumConstant(JCTree tree, Type enumType) {
17371737

17381738
public void visitSynchronized(JCSynchronized tree) {
17391739
chk.checkRefType(tree.pos(), attribExpr(tree.lock, env));
1740+
if (env.info.lint.isEnabled(LintCategory.SYNCHRONIZATION) && isValueBased(tree.lock.type)) {
1741+
log.warning(LintCategory.SYNCHRONIZATION, tree.pos(), Warnings.AttemptToSynchronizeOnInstanceOfValueBasedClass);
1742+
}
17401743
attribStat(tree.body, env);
17411744
result = null;
17421745
}
1746+
// where
1747+
private boolean isValueBased(Type t) {
1748+
if (t != null && t.tsym != null) {
1749+
for (Attribute.Compound a: t.tsym.getDeclarationAttributes()) {
1750+
if (a.type.tsym == syms.valueBasedType.tsym) {
1751+
return true;
1752+
}
1753+
}
1754+
}
1755+
return false;
1756+
}
1757+
17431758

17441759
public void visitTry(JCTry tree) {
17451760
// Create a new local environment with a local

src/jdk.compiler/share/classes/com/sun/tools/javac/resources/compiler.properties

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3742,3 +3742,7 @@ compiler.err.preview.not.latest=\
37423742

37433743
compiler.err.preview.without.source.or.release=\
37443744
--enable-preview must be used with either -source or --release
3745+
3746+
compiler.warn.attempt.to.synchronize.on.instance.of.value.based.class=\
3747+
attempt to synchronize on an instance of a value-based class
3748+

src/jdk.compiler/share/classes/com/sun/tools/javac/resources/javac.properties

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -256,10 +256,13 @@ javac.opt.Xlint.desc.unchecked=\
256256
Warn about unchecked operations.
257257

258258
javac.opt.Xlint.desc.varargs=\
259-
Warn about potentially unsafe vararg methods
259+
Warn about potentially unsafe vararg methods.
260260

261261
javac.opt.Xlint.desc.preview=\
262-
Warn about use of preview language features
262+
Warn about use of preview language features.
263+
264+
javac.opt.Xlint.desc.synchronization=\
265+
Warn about synchronization attempts on instances of value-based classes.
263266

264267
javac.opt.Xdoclint=\
265268
Enable recommended checks for problems in javadoc comments
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
* Copyright (c) 2020, 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.
8+
*
9+
* This code is distributed in the hope that it will be useful, but WITHOUT
10+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+
* version 2 for more details (a copy is included in the LICENSE file that
13+
* accompanied this code).
14+
*
15+
* You should have received a copy of the GNU General Public License version
16+
* 2 along with this work; if not, write to the Free Software Foundation,
17+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18+
*
19+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20+
* or visit www.oracle.com if you need additional information or have any
21+
* questions.
22+
*/
23+
24+
// key: compiler.warn.attempt.to.synchronize.on.instance.of.value.based.class
25+
// options: -Xlint:synchronization
26+
27+
class AttemptToSynchronizeOnInstanceOfVbc {
28+
void foo(Integer i) {
29+
synchronized(i) {
30+
}
31+
}
32+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*
2+
* @test /nodynamiccopyright/
3+
* @bug 8254274
4+
* @summary lint should warn when an instance of a value based class is synchronized upon
5+
* @compile/fail/ref=ExternalAbuseOfVbc.out -XDrawDiagnostics -Werror -Xlint ExternalAbuseOfVbc.java
6+
* @compile/fail/ref=ExternalAbuseOfVbc.out -XDrawDiagnostics -Werror -Xlint:all ExternalAbuseOfVbc.java
7+
* @compile/fail/ref=ExternalAbuseOfVbc.out -XDrawDiagnostics -Werror -Xlint:synchronization ExternalAbuseOfVbc.java
8+
* @compile/ref=LintModeOffAbuseOfVbc.out -XDrawDiagnostics -Werror -Xlint:-synchronization ExternalAbuseOfVbc.java
9+
*/
10+
11+
public final class ExternalAbuseOfVbc {
12+
13+
final Integer val = Integer.valueOf(42);
14+
final String ref = "String";
15+
16+
void abuseVbc() {
17+
synchronized(ref) { // OK
18+
synchronized (val) { // WARN
19+
}
20+
}
21+
}
22+
}
23+
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
ExternalAbuseOfVbc.java:18:13: compiler.warn.attempt.to.synchronize.on.instance.of.value.based.class
2+
- compiler.err.warnings.and.werror
3+
1 error
4+
1 warning
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/*
2+
* @test /nodynamiccopyright/
3+
* @bug 8254274
4+
* @summary lint should warn when an instance of a value based class is synchronized upon
5+
* @compile/fail/ref=JdkInternalAbuseOfVbc.out --patch-module java.base=${test.src} -XDrawDiagnostics -Werror -Xlint SomeVbc.java JdkInternalAbuseOfVbc.java
6+
*/
7+
8+
package java.lang;
9+
10+
public final class JdkInternalAbuseOfVbc {
11+
12+
public JdkInternalAbuseOfVbc() {}
13+
14+
void abuseVbc(SomeVbc vbc) {
15+
16+
synchronized(this) { // OK
17+
synchronized (vbc) { // WARN
18+
}
19+
}
20+
}
21+
}
22+
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
SomeVbc.java:38:13: compiler.warn.attempt.to.synchronize.on.instance.of.value.based.class
2+
- compiler.err.warnings.and.werror
3+
SomeVbc.java:49:13: compiler.warn.attempt.to.synchronize.on.instance.of.value.based.class
4+
JdkInternalAbuseOfVbc.java:17:13: compiler.warn.attempt.to.synchronize.on.instance.of.value.based.class
5+
1 error
6+
3 warnings

0 commit comments

Comments
 (0)