8285935: Spurious lint warning for static method accessed through instance qualifier

Reviewed-by: jlahoda
This commit is contained in:
Vicente Romero 2022-08-04 17:57:28 +00:00
parent 92d2982f3f
commit 3ba317167d
6 changed files with 33 additions and 4 deletions
src/jdk.compiler/share/classes/com/sun/tools/javac
test/langtools/tools/javac

@ -4473,7 +4473,11 @@ public class Attr extends JCTree.Visitor {
sym.name != names._class) {
// If the qualified item is not a type and the selected item is static, report
// a warning. Make allowance for the class of an array type e.g. Object[].class)
chk.warnStatic(tree, Warnings.StaticNotQualifiedByType(sym.kind.kindName(), sym.owner));
if (!sym.owner.isAnonymous()) {
chk.warnStatic(tree, Warnings.StaticNotQualifiedByType(sym.kind.kindName(), sym.owner));
} else {
chk.warnStatic(tree, Warnings.StaticNotQualifiedByType2(sym.kind.kindName()));
}
}
// If we are selecting an instance member via a `super', ...

@ -2010,6 +2010,10 @@ compiler.warn.big.major.version=\
compiler.warn.static.not.qualified.by.type=\
static {0} should be qualified by type name, {1}, instead of by an expression
# 0: kind name
compiler.warn.static.not.qualified.by.type2=\
static {0} should not be used as a member of an anonymous class
# 0: string
compiler.warn.source.no.bootclasspath=\
bootstrap class path not set in conjunction with -source {0}

@ -4,6 +4,8 @@ T4880220.java:22:27: compiler.warn.static.not.qualified.by.type: kindname.variab
T4880220.java:24:29: compiler.warn.static.not.qualified.by.type: kindname.method, T4880220.C
T4880220.java:25:29: compiler.warn.static.not.qualified.by.type: kindname.variable, T4880220.C
T4880220.java:26:29: compiler.warn.static.not.qualified.by.type: kindname.variable, T4880220.C
T4880220.java:39:12: compiler.warn.static.not.qualified.by.type2: kindname.method
T4880220.java:40:20: compiler.warn.static.not.qualified.by.type2: kindname.variable
- compiler.err.warnings.and.werror
1 error
6 warnings
8 warnings

@ -1,6 +1,6 @@
/*
* @test /nodynamiccopyright/
* @bug 4880220
* @bug 4880220 8285935
* @summary Add a warning when accessing a static method via an reference
*
* @compile/ref=T4880220.empty.out T4880220.java
@ -31,6 +31,15 @@ public class T4880220 {
Class<?> good_2 = C[].class;
}
void m3() {
var obj = new Object() {
static void foo() {}
static int i = 0;
};
obj.foo();
int j = obj.i;
}
C c() {
return new C();
}

@ -4,4 +4,6 @@ T4880220.java:22:27: compiler.warn.static.not.qualified.by.type: kindname.variab
T4880220.java:24:29: compiler.warn.static.not.qualified.by.type: kindname.method, T4880220.C
T4880220.java:25:29: compiler.warn.static.not.qualified.by.type: kindname.variable, T4880220.C
T4880220.java:26:29: compiler.warn.static.not.qualified.by.type: kindname.variable, T4880220.C
6 warnings
T4880220.java:39:12: compiler.warn.static.not.qualified.by.type2: kindname.method
T4880220.java:40:20: compiler.warn.static.not.qualified.by.type2: kindname.variable
8 warnings

@ -22,12 +22,20 @@
*/
// key: compiler.warn.static.not.qualified.by.type
// key: compiler.warn.static.not.qualified.by.type2
// options: -Xlint:static
class StaticNotQualifiedByType {
int m(Other other) {
return other.i;
}
void m2() {
var obj = new Object() {
static void foo() {}
};
obj.foo();
}
}
class Other {