Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-6253161

Incorrect position of serial warning in anonymous class

XMLWordPrintable

    • b65
    • generic
    • generic
    • Verified

      Tried on Linux JDK build 1.6.0-auto-094

      Compiler will fail to find the location of [serial] warnings for anonymous inner classes. It will always gives the last method or the last variable of the class as the location of warning, if the class is blank i.e. no methods or variables then compiler will not give any line number.

      Please see the following code:
      <code>
      import java.util.List;
      import java.util.ArrayList;

      public class Test13{
          @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();
          }
      }
      </code>
      ---
      The output of the code when compiled :
      bash-2.05b$ $b/javac -Xlint Test13.java
      Test13.java:18: warning: [serial] serializable class <anonymous Test13$1$1> has no definition of serialVersionUID
                          public void testMethods(){
                                      ^
      1 warning
      ---
      As testMethods is the last method of anonymous inner classes, compiler treats this as location of the [serial] warning.

      If the all the methods and veriable are removed from the class then compiler will not give any line number.
      Please see the following code:

      <code>
      import java.util.List;
      import java.util.ArrayList;

      public class Test13{
          @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();
          }
      }
      </code>

      Output of the code when compiled:
      bash-2.05b$ $b/javac -Xlint Test13.java
      warning: [serial] serializable class <anonymous Test13$1$1> has no definition of serialVersionUID
      1 warning

      No line number for the warning..
      --
      ###@###.### 2005-04-11 10:55:04 GMT

            ahe Peter Ahe
            savadhansunw Seetharama Avadhanam (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: