4880220: Add a warning when accessing a static method via an reference
Reviewed-by: darcy
This commit is contained in:
parent
f37b9d8245
commit
0cc023734d
@ -68,7 +68,7 @@ javac.no.jdk.warnings = -XDignore.symbol.file=true
|
|||||||
# set the following to -version to verify the versions of javac being used
|
# set the following to -version to verify the versions of javac being used
|
||||||
javac.version.opt =
|
javac.version.opt =
|
||||||
# in time, there should be no exceptions to -Xlint:all
|
# in time, there should be no exceptions to -Xlint:all
|
||||||
javac.lint.opts = -Xlint:all,-deprecation -Werror
|
javac.lint.opts = -Xlint:all,-deprecation,-static -Werror
|
||||||
|
|
||||||
# options for the <javadoc> task for javac
|
# options for the <javadoc> task for javac
|
||||||
javadoc.jls3.url=http://java.sun.com/docs/books/jls/
|
javadoc.jls3.url=http://java.sun.com/docs/books/jls/
|
||||||
|
@ -198,7 +198,12 @@ public class Lint
|
|||||||
/**
|
/**
|
||||||
* Warn about Sun proprietary API that may be removed in a future release.
|
* Warn about Sun proprietary API that may be removed in a future release.
|
||||||
*/
|
*/
|
||||||
SUNAPI("sunapi", true);
|
SUNAPI("sunapi", true),
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Warn about issues relating to use of statics
|
||||||
|
*/
|
||||||
|
STATIC("static");
|
||||||
|
|
||||||
LintCategory(String option) {
|
LintCategory(String option) {
|
||||||
this(option, false);
|
this(option, false);
|
||||||
|
@ -2020,6 +2020,10 @@ public class Attr extends JCTree.Visitor {
|
|||||||
tree.pos(), site, sym.name, true);
|
tree.pos(), site, sym.name, true);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
} else if (sym.kind != ERR && (sym.flags() & STATIC) != 0 && 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, "static.not.qualified.by.type", Kinds.kindName(sym.kind), sym.owner);
|
||||||
}
|
}
|
||||||
|
|
||||||
// If we are selecting an instance member via a `super', ...
|
// If we are selecting an instance member via a `super', ...
|
||||||
|
@ -189,6 +189,11 @@ public class Check {
|
|||||||
sunApiHandler.report(pos, msg, args);
|
sunApiHandler.report(pos, msg, args);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void warnStatic(DiagnosticPosition pos, String msg, Object... args) {
|
||||||
|
if (lint.isEnabled(LintCategory.STATIC))
|
||||||
|
log.warning(pos, msg, args);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Report any deferred diagnostics.
|
* Report any deferred diagnostics.
|
||||||
*/
|
*/
|
||||||
|
@ -720,6 +720,9 @@ compiler.warn.big.major.version=\
|
|||||||
{0}: major version {1} is newer than {2}, the highest major version supported by this compiler.\n\
|
{0}: major version {1} is newer than {2}, the highest major version supported by this compiler.\n\
|
||||||
It is recommended that the compiler be upgraded.
|
It is recommended that the compiler be upgraded.
|
||||||
|
|
||||||
|
compiler.warn.static.not.qualified.by.type=\
|
||||||
|
[static] static {0} should be qualified by type name, {1}, instead of by an expression
|
||||||
|
|
||||||
# Warnings related to annotation processing
|
# Warnings related to annotation processing
|
||||||
compiler.warn.proc.package.does.not.exist=\
|
compiler.warn.proc.package.does.not.exist=\
|
||||||
package {0} does not exist
|
package {0} does not exist
|
||||||
|
9
langtools/test/tools/javac/4880220/T4880220.error.out
Normal file
9
langtools/test/tools/javac/4880220/T4880220.error.out
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
T4880220.java:20:27: compiler.warn.static.not.qualified.by.type: kindname.method, T4880220.C
|
||||||
|
T4880220.java:21:27: compiler.warn.static.not.qualified.by.type: kindname.variable, T4880220.C
|
||||||
|
T4880220.java:22:27: compiler.warn.static.not.qualified.by.type: kindname.variable, T4880220.C
|
||||||
|
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
|
||||||
|
- compiler.err.warnings.and.werror
|
||||||
|
1 error
|
||||||
|
6 warnings
|
43
langtools/test/tools/javac/4880220/T4880220.java
Normal file
43
langtools/test/tools/javac/4880220/T4880220.java
Normal file
@ -0,0 +1,43 @@
|
|||||||
|
/*
|
||||||
|
* @test /nodynamiccopyright/
|
||||||
|
* @bug 4880220
|
||||||
|
* @summary Add a warning when accessing a static method via an reference
|
||||||
|
*
|
||||||
|
* @compile/ref=T4880220.empty.out T4880220.java
|
||||||
|
* @compile/ref=T4880220.warn.out -XDrawDiagnostics -Xlint:static T4880220.java
|
||||||
|
* @compile/ref=T4880220.warn.out -XDrawDiagnostics -Xlint:all T4880220.java
|
||||||
|
* @compile/ref=T4880220.empty.out -XDrawDiagnostics -Xlint:all,-static T4880220.java
|
||||||
|
* @compile/ref=T4880220.error.out/fail -XDrawDiagnostics -Werror -Xlint:all T4880220.java
|
||||||
|
*/
|
||||||
|
|
||||||
|
public class T4880220 {
|
||||||
|
void m1() {
|
||||||
|
int good_1 = C.m();
|
||||||
|
int good_2 = C.f;
|
||||||
|
int good_3 = C.x;
|
||||||
|
|
||||||
|
C c = new C();
|
||||||
|
int bad_inst_1 = c.m();
|
||||||
|
int bad_inst_2 = c.f;
|
||||||
|
int bad_inst_3 = c.x;
|
||||||
|
|
||||||
|
int bad_expr_1 = c().m();
|
||||||
|
int bad_expr_2 = c().f;
|
||||||
|
int bad_expr_3 = c().x;
|
||||||
|
}
|
||||||
|
|
||||||
|
void m2() {
|
||||||
|
Class<?> good_1 = C.class;
|
||||||
|
Class<?> good_2 = C[].class;
|
||||||
|
}
|
||||||
|
|
||||||
|
C c() {
|
||||||
|
return new C();
|
||||||
|
}
|
||||||
|
|
||||||
|
static class C {
|
||||||
|
static int m() { return 0; }
|
||||||
|
static int f;
|
||||||
|
static final int x = 3;
|
||||||
|
}
|
||||||
|
}
|
7
langtools/test/tools/javac/4880220/T4880220.warn.out
Normal file
7
langtools/test/tools/javac/4880220/T4880220.warn.out
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
T4880220.java:20:27: compiler.warn.static.not.qualified.by.type: kindname.method, T4880220.C
|
||||||
|
T4880220.java:21:27: compiler.warn.static.not.qualified.by.type: kindname.variable, T4880220.C
|
||||||
|
T4880220.java:22:27: compiler.warn.static.not.qualified.by.type: kindname.variable, T4880220.C
|
||||||
|
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
|
Loading…
x
Reference in New Issue
Block a user