4880220: Add a warning when accessing a static method via an reference
authorjjg
Thu, 25 Feb 2010 09:42:35 -0800
changeset 5002 12a9e8562200
parent 5001 71ed0cc22974
child 5003 fd0b30cdbe5c
4880220: Add a warning when accessing a static method via an reference Reviewed-by: darcy
langtools/make/build.properties
langtools/src/share/classes/com/sun/tools/javac/code/Lint.java
langtools/src/share/classes/com/sun/tools/javac/comp/Attr.java
langtools/src/share/classes/com/sun/tools/javac/comp/Check.java
langtools/src/share/classes/com/sun/tools/javac/resources/compiler.properties
langtools/test/tools/javac/4880220/T4880220.empty.out
langtools/test/tools/javac/4880220/T4880220.error.out
langtools/test/tools/javac/4880220/T4880220.java
langtools/test/tools/javac/4880220/T4880220.warn.out
--- a/langtools/make/build.properties	Tue Feb 23 18:43:02 2010 -0800
+++ b/langtools/make/build.properties	Thu Feb 25 09:42:35 2010 -0800
@@ -68,7 +68,7 @@
 # set the following to -version to verify the versions of javac being used
 javac.version.opt =
 # 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
 javadoc.jls3.url=http://java.sun.com/docs/books/jls/
--- a/langtools/src/share/classes/com/sun/tools/javac/code/Lint.java	Tue Feb 23 18:43:02 2010 -0800
+++ b/langtools/src/share/classes/com/sun/tools/javac/code/Lint.java	Thu Feb 25 09:42:35 2010 -0800
@@ -198,7 +198,12 @@
         /**
          * 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) {
             this(option, false);
--- a/langtools/src/share/classes/com/sun/tools/javac/comp/Attr.java	Tue Feb 23 18:43:02 2010 -0800
+++ b/langtools/src/share/classes/com/sun/tools/javac/comp/Attr.java	Thu Feb 25 09:42:35 2010 -0800
@@ -2020,6 +2020,10 @@
                               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', ...
--- a/langtools/src/share/classes/com/sun/tools/javac/comp/Check.java	Tue Feb 23 18:43:02 2010 -0800
+++ b/langtools/src/share/classes/com/sun/tools/javac/comp/Check.java	Thu Feb 25 09:42:35 2010 -0800
@@ -189,6 +189,11 @@
             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.
      */
--- a/langtools/src/share/classes/com/sun/tools/javac/resources/compiler.properties	Tue Feb 23 18:43:02 2010 -0800
+++ b/langtools/src/share/classes/com/sun/tools/javac/resources/compiler.properties	Thu Feb 25 09:42:35 2010 -0800
@@ -720,6 +720,9 @@
     {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.
 
+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
 compiler.warn.proc.package.does.not.exist=\
     package {0} does not exist
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/langtools/test/tools/javac/4880220/T4880220.error.out	Thu Feb 25 09:42:35 2010 -0800
@@ -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
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/langtools/test/tools/javac/4880220/T4880220.java	Thu Feb 25 09:42:35 2010 -0800
@@ -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;
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/langtools/test/tools/javac/4880220/T4880220.warn.out	Thu Feb 25 09:42:35 2010 -0800
@@ -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