changeset 504:87eb6edd4f21

4880220: Add a warning when accessing a static method via an reference Reviewed-by: darcy
author jjg
date Thu, 25 Feb 2010 09:42:35 -0800
parents 6eca0895a644
children 85242c273d31
files make/build.properties src/share/classes/com/sun/tools/javac/code/Lint.java src/share/classes/com/sun/tools/javac/comp/Attr.java src/share/classes/com/sun/tools/javac/comp/Check.java src/share/classes/com/sun/tools/javac/resources/compiler.properties test/tools/javac/4880220/T4880220.empty.out test/tools/javac/4880220/T4880220.error.out test/tools/javac/4880220/T4880220.java test/tools/javac/4880220/T4880220.warn.out
diffstat 8 files changed, 78 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/make/build.properties	Tue Feb 23 18:43:02 2010 -0800
+++ b/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/src/share/classes/com/sun/tools/javac/code/Lint.java	Tue Feb 23 18:43:02 2010 -0800
+++ b/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/src/share/classes/com/sun/tools/javac/comp/Attr.java	Tue Feb 23 18:43:02 2010 -0800
+++ b/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/src/share/classes/com/sun/tools/javac/comp/Check.java	Tue Feb 23 18:43:02 2010 -0800
+++ b/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/src/share/classes/com/sun/tools/javac/resources/compiler.properties	Tue Feb 23 18:43:02 2010 -0800
+++ b/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/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/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/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