Skip to content

Commit 3ba3171

Browse files
author
Vicente Romero
committed
8285935: Spurious lint warning for static method accessed through instance qualifier
Reviewed-by: jlahoda
1 parent 92d2982 commit 3ba3171

File tree

6 files changed

+33
-4
lines changed

6 files changed

+33
-4
lines changed

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4473,7 +4473,11 @@ public void visitSelect(JCFieldAccess tree) {
44734473
sym.name != names._class) {
44744474
// If the qualified item is not a type and the selected item is static, report
44754475
// a warning. Make allowance for the class of an array type e.g. Object[].class)
4476-
chk.warnStatic(tree, Warnings.StaticNotQualifiedByType(sym.kind.kindName(), sym.owner));
4476+
if (!sym.owner.isAnonymous()) {
4477+
chk.warnStatic(tree, Warnings.StaticNotQualifiedByType(sym.kind.kindName(), sym.owner));
4478+
} else {
4479+
chk.warnStatic(tree, Warnings.StaticNotQualifiedByType2(sym.kind.kindName()));
4480+
}
44774481
}
44784482

44794483
// If we are selecting an instance member via a `super', ...

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
@@ -2010,6 +2010,10 @@ compiler.warn.big.major.version=\
20102010
compiler.warn.static.not.qualified.by.type=\
20112011
static {0} should be qualified by type name, {1}, instead of by an expression
20122012

2013+
# 0: kind name
2014+
compiler.warn.static.not.qualified.by.type2=\
2015+
static {0} should not be used as a member of an anonymous class
2016+
20132017
# 0: string
20142018
compiler.warn.source.no.bootclasspath=\
20152019
bootstrap class path not set in conjunction with -source {0}

test/langtools/tools/javac/4880220/T4880220.error.out

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ T4880220.java:22:27: compiler.warn.static.not.qualified.by.type: kindname.variab
44
T4880220.java:24:29: compiler.warn.static.not.qualified.by.type: kindname.method, T4880220.C
55
T4880220.java:25:29: compiler.warn.static.not.qualified.by.type: kindname.variable, T4880220.C
66
T4880220.java:26:29: compiler.warn.static.not.qualified.by.type: kindname.variable, T4880220.C
7+
T4880220.java:39:12: compiler.warn.static.not.qualified.by.type2: kindname.method
8+
T4880220.java:40:20: compiler.warn.static.not.qualified.by.type2: kindname.variable
79
- compiler.err.warnings.and.werror
810
1 error
9-
6 warnings
11+
8 warnings

test/langtools/tools/javac/4880220/T4880220.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
22
* @test /nodynamiccopyright/
3-
* @bug 4880220
3+
* @bug 4880220 8285935
44
* @summary Add a warning when accessing a static method via an reference
55
*
66
* @compile/ref=T4880220.empty.out T4880220.java
@@ -31,6 +31,15 @@ void m2() {
3131
Class<?> good_2 = C[].class;
3232
}
3333

34+
void m3() {
35+
var obj = new Object() {
36+
static void foo() {}
37+
static int i = 0;
38+
};
39+
obj.foo();
40+
int j = obj.i;
41+
}
42+
3443
C c() {
3544
return new C();
3645
}

test/langtools/tools/javac/4880220/T4880220.warn.out

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,6 @@ T4880220.java:22:27: compiler.warn.static.not.qualified.by.type: kindname.variab
44
T4880220.java:24:29: compiler.warn.static.not.qualified.by.type: kindname.method, T4880220.C
55
T4880220.java:25:29: compiler.warn.static.not.qualified.by.type: kindname.variable, T4880220.C
66
T4880220.java:26:29: compiler.warn.static.not.qualified.by.type: kindname.variable, T4880220.C
7-
6 warnings
7+
T4880220.java:39:12: compiler.warn.static.not.qualified.by.type2: kindname.method
8+
T4880220.java:40:20: compiler.warn.static.not.qualified.by.type2: kindname.variable
9+
8 warnings

test/langtools/tools/javac/diags/examples/StaticNotQualifiedByType.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,20 @@
2222
*/
2323

2424
// key: compiler.warn.static.not.qualified.by.type
25+
// key: compiler.warn.static.not.qualified.by.type2
2526
// options: -Xlint:static
2627

2728
class StaticNotQualifiedByType {
2829
int m(Other other) {
2930
return other.i;
3031
}
32+
33+
void m2() {
34+
var obj = new Object() {
35+
static void foo() {}
36+
};
37+
obj.foo();
38+
}
3139
}
3240

3341
class Other {

0 commit comments

Comments
 (0)