7152104: javac should not warn about missing serialVersionUID for anonymous inner classes
authoralundblad
Mon, 18 Apr 2016 22:25:50 +0200
changeset 37634 a5f740ef93f2
parent 37633 d16d6d59446d
child 37635 9834489579e5
7152104: javac should not warn about missing serialVersionUID for anonymous inner classes Summary: Javac no longer issues warnings for missing serialVersionUID in anonymous classes. Reviewed-by: jlahoda
langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Attr.java
langtools/test/tools/javac/SerialWarn.java
langtools/test/tools/javac/SerialWarn.out
langtools/test/tools/javac/T6554097.java
langtools/test/tools/javac/T6554097.out
langtools/test/tools/javac/diags/examples/AnonymousClass.java
langtools/test/tools/javac/positions/T6253161.java
langtools/test/tools/javac/positions/T6253161.out
langtools/test/tools/javac/positions/T6253161a.java
langtools/test/tools/javac/positions/T6253161a.out
langtools/test/tools/javac/serial/SerialWarn.java
langtools/test/tools/javac/serial/SerialWarn.out
langtools/test/tools/javac/serial/SerialWarnAnon.java
--- a/langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Attr.java	Mon Apr 18 21:07:50 2016 +0200
+++ b/langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Attr.java	Mon Apr 18 22:25:50 2016 +0200
@@ -4491,10 +4491,11 @@
         chk.checkNonCyclicElements(tree);
 
         // Check for proper use of serialVersionUID
-        if (env.info.lint.isEnabled(LintCategory.SERIAL) &&
-            isSerializable(c.type) &&
-            (c.flags() & Flags.ENUM) == 0 &&
-            checkForSerial(c)) {
+        if (env.info.lint.isEnabled(LintCategory.SERIAL)
+                && isSerializable(c.type)
+                && (c.flags() & Flags.ENUM) == 0
+                && !c.isAnonymous()
+                && checkForSerial(c)) {
             checkSerialVersionUID(tree, c);
         }
         if (allowTypeAnnos) {
--- a/langtools/test/tools/javac/SerialWarn.java	Mon Apr 18 21:07:50 2016 +0200
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,11 +0,0 @@
-/*
- * @test /nodynamiccopyright/
- * @bug 4854628
- * @summary include Throwable subclasses in missing serialVersionUID warning
- * @author gafter
- *
- * @compile                    -Werror SerialWarn.java
- * @compile/fail/ref=SerialWarn.out -XDrawDiagnostics -Xlint:serial -Werror SerialWarn.java
- */
-
-class SerialWarn extends Throwable {}
--- a/langtools/test/tools/javac/SerialWarn.out	Mon Apr 18 21:07:50 2016 +0200
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,4 +0,0 @@
-SerialWarn.java:11:1: compiler.warn.missing.SVUID: SerialWarn
-- compiler.err.warnings.and.werror
-1 error
-1 warning
--- a/langtools/test/tools/javac/T6554097.java	Mon Apr 18 21:07:50 2016 +0200
+++ b/langtools/test/tools/javac/T6554097.java	Mon Apr 18 22:25:50 2016 +0200
@@ -3,24 +3,27 @@
  * @bug     6554097
  * @summary "final" confuses at-SuppressWarnings
  * @compile T6554097.java
- * @compile/fail/ref=T6554097.out -XDrawDiagnostics -Werror -Xlint:serial T6554097.java
+ * @compile/fail/ref=T6554097.out -XDrawDiagnostics -Werror -Xlint:rawtypes T6554097.java
  */
 
+import java.util.ArrayList;
+
 class T6554097 {
-    @SuppressWarnings("serial") final Throwable[] v1 = { new Throwable() {} };
-    @SuppressWarnings("serial")       Throwable[] v2 = { new Throwable() {} };
+
+    @SuppressWarnings("unchecked") final ArrayList[] v1 = { new ArrayList() {} };
+    @SuppressWarnings("unchecked")       ArrayList[] v2 = { new ArrayList() {} };
 
     public static void m1() throws Throwable {
-            @SuppressWarnings("serial") final Throwable[] v3 = { new Throwable() {} };
-            @SuppressWarnings("serial")       Throwable[] v4 = { new Throwable() {} };
+            @SuppressWarnings("unchecked") final ArrayList[] v3 = { new ArrayList() {} };
+            @SuppressWarnings("unchecked")       ArrayList[] v4 = { new ArrayList() {} };
     }
 
-    final Throwable[] v5 = { new Throwable() {} };
-          Throwable[] v6 = { new Throwable() {} };
+    final ArrayList[] v5 = { new ArrayList() {} };
+          ArrayList[] v6 = { new ArrayList() {} };
 
     public static void m2() throws Throwable {
-        final Throwable[] v7 = { new Throwable() {} };
-                  Throwable[] v8 = { new Throwable() {} };
+        final ArrayList[] v7 = { new ArrayList() {} };
+              ArrayList[] v8 = { new ArrayList() {} };
     }
 }
 
--- a/langtools/test/tools/javac/T6554097.out	Mon Apr 18 21:07:50 2016 +0200
+++ b/langtools/test/tools/javac/T6554097.out	Mon Apr 18 22:25:50 2016 +0200
@@ -1,7 +1,19 @@
-T6554097.java:18:46: compiler.warn.missing.SVUID: compiler.misc.anonymous.class: T6554097$5
-T6554097.java:19:46: compiler.warn.missing.SVUID: compiler.misc.anonymous.class: T6554097$6
-T6554097.java:22:50: compiler.warn.missing.SVUID: compiler.misc.anonymous.class: T6554097$7
-T6554097.java:23:54: compiler.warn.missing.SVUID: compiler.misc.anonymous.class: T6554097$8
+T6554097.java:13:42: compiler.warn.raw.class.use: java.util.ArrayList, java.util.ArrayList<E>
+T6554097.java:13:65: compiler.warn.raw.class.use: java.util.ArrayList, java.util.ArrayList<E>
+T6554097.java:14:42: compiler.warn.raw.class.use: java.util.ArrayList, java.util.ArrayList<E>
+T6554097.java:14:65: compiler.warn.raw.class.use: java.util.ArrayList, java.util.ArrayList<E>
+T6554097.java:17:50: compiler.warn.raw.class.use: java.util.ArrayList, java.util.ArrayList<E>
+T6554097.java:17:73: compiler.warn.raw.class.use: java.util.ArrayList, java.util.ArrayList<E>
+T6554097.java:18:50: compiler.warn.raw.class.use: java.util.ArrayList, java.util.ArrayList<E>
+T6554097.java:18:73: compiler.warn.raw.class.use: java.util.ArrayList, java.util.ArrayList<E>
+T6554097.java:21:11: compiler.warn.raw.class.use: java.util.ArrayList, java.util.ArrayList<E>
+T6554097.java:21:34: compiler.warn.raw.class.use: java.util.ArrayList, java.util.ArrayList<E>
+T6554097.java:22:11: compiler.warn.raw.class.use: java.util.ArrayList, java.util.ArrayList<E>
+T6554097.java:22:34: compiler.warn.raw.class.use: java.util.ArrayList, java.util.ArrayList<E>
+T6554097.java:25:15: compiler.warn.raw.class.use: java.util.ArrayList, java.util.ArrayList<E>
+T6554097.java:25:38: compiler.warn.raw.class.use: java.util.ArrayList, java.util.ArrayList<E>
+T6554097.java:26:15: compiler.warn.raw.class.use: java.util.ArrayList, java.util.ArrayList<E>
+T6554097.java:26:38: compiler.warn.raw.class.use: java.util.ArrayList, java.util.ArrayList<E>
 - compiler.err.warnings.and.werror
 1 error
-4 warnings
+16 warnings
--- a/langtools/test/tools/javac/diags/examples/AnonymousClass.java	Mon Apr 18 21:07:50 2016 +0200
+++ b/langtools/test/tools/javac/diags/examples/AnonymousClass.java	Mon Apr 18 22:25:50 2016 +0200
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2010, 2016, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -22,12 +22,15 @@
  */
 
 // key: compiler.misc.anonymous.class
-// key: compiler.warn.missing.SVUID
-// options: -Xlint:serial
+// key: compiler.err.prob.found.req
+// key: compiler.misc.inconvertible.types
+// options: -Xlint:rawtypes
 // run: simple
 
+import java.util.ArrayList;
+
 class AnonymousClass {
-    Exception m() {
-        return new Exception() { };
+     Object m() {
+         return (ArrayList<String>) new ArrayList<Object>() { };
     }
 }
--- a/langtools/test/tools/javac/positions/T6253161.java	Mon Apr 18 21:07:50 2016 +0200
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,36 +0,0 @@
-/*
- * @test  /nodynamiccopyright/
- * @bug     6253161
- * @summary Compiler will fail to find the correct location of serial warnings for anonymous inner classes
- * @author  Seetharama Avadhanam
- * @compile -Xlint:serial -XDdev T6253161.java
- * @compile/ref=T6253161.out -Xlint:serial -XDdev -XDrawDiagnostics T6253161.java
- */
-import java.util.List;
-import java.util.ArrayList;
-
-public class T6253161 {
-    @SuppressWarnings("unchecked")
-    public void anonymousMethod(){
-           List list = new ArrayList<String>(){
-           static final long serialVersionUID = 1;
-           List list = new ArrayList<Integer>();
-           public List<Integer> getMyList(){
-                final List floatList = new ArrayList<Float>(){
-                    List integerList = new ArrayList<Float>();
-                    public List<Float> getMyList(){
-                        for(int i=0;i<10;i++)
-                            integerList.add((int)((Float.parseFloat(i+""))+(1.11F)));
-                        return (List)(Object)integerList;
-                    }
-                    public void testMethods(){
-                        //...
-                    }
-                }.getMyList();
-                for(int i=0;i<10;i++)
-                    list.add((Float)(floatList.get(i)) * 11.232F * i);
-                return list;
-            }
-         }.getMyList();
-    }
-}
--- a/langtools/test/tools/javac/positions/T6253161.out	Mon Apr 18 21:07:50 2016 +0200
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,2 +0,0 @@
-T6253161.java:19:62: compiler.warn.missing.SVUID: compiler.misc.anonymous.class: T6253161$1$1
-1 warning
--- a/langtools/test/tools/javac/positions/T6253161a.java	Mon Apr 18 21:07:50 2016 +0200
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,28 +0,0 @@
-/*
- * @test  /nodynamiccopyright/
- * @bug     6253161
- * @summary Compiler will fail to find the correct location of serial warnings for anonymous inner classes
- * @author  Seetharama Avadhanam
- * @compile -Xlint:serial -XDdev T6253161a.java
- * @compile/ref=T6253161a.out -Xlint:serial -XDdev -XDrawDiagnostics T6253161a.java
- */
-import java.util.List;
-import java.util.ArrayList;
-
-public class T6253161a {
-    @SuppressWarnings("unchecked")
-    public void anonymousMethod(){
-           List list = new ArrayList<String>(){
-           static final long serialVersionUID = 1;
-           List list = new ArrayList<Integer>();
-           public List<Integer> getMyList(){
-                final List floatList = new ArrayList<Float>(){
-                    // Blank ....
-                };
-                for(int i=0;i<10;i++)
-                    list.add((Float)(floatList.get(i)) * 11.232F * i);
-                return list;
-            }
-         }.getMyList();
-    }
-}
--- a/langtools/test/tools/javac/positions/T6253161a.out	Mon Apr 18 21:07:50 2016 +0200
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,2 +0,0 @@
-T6253161a.java:19:62: compiler.warn.missing.SVUID: compiler.misc.anonymous.class: T6253161a$1$1
-1 warning
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/langtools/test/tools/javac/serial/SerialWarn.java	Mon Apr 18 22:25:50 2016 +0200
@@ -0,0 +1,11 @@
+/*
+ * @test /nodynamiccopyright/
+ * @bug 4854628
+ * @summary include Throwable subclasses in missing serialVersionUID warning
+ * @author gafter
+ *
+ * @compile                    -Werror SerialWarn.java
+ * @compile/fail/ref=SerialWarn.out -XDrawDiagnostics -Xlint:serial -Werror SerialWarn.java
+ */
+
+class SerialWarn extends Throwable {}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/langtools/test/tools/javac/serial/SerialWarn.out	Mon Apr 18 22:25:50 2016 +0200
@@ -0,0 +1,4 @@
+SerialWarn.java:11:1: compiler.warn.missing.SVUID: SerialWarn
+- compiler.err.warnings.and.werror
+1 error
+1 warning
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/langtools/test/tools/javac/serial/SerialWarnAnon.java	Mon Apr 18 22:25:50 2016 +0200
@@ -0,0 +1,15 @@
+/*
+ * @test /nodynamiccopyright/
+ * @bug 7152104
+ * @summary Make sure no warning is emitted for anonymous classes
+ *          without serialVersionUID
+ * @compile SerialWarn.java
+ * @compile -Werror -XDrawDiagnostics -Xlint:serial SerialWarnAnon.java
+ */
+
+class SerialWarnAnon {
+    interface SerialWarnAnonInterface extends java.io.Serializable { }
+    Object m() {
+        return new SerialWarnAnonInterface() { };
+    }
+}