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

Unexpected compiler behaviors on generic class's non-static method with Map para

XMLWordPrintable

    • generic
    • generic

      ADDITIONAL SYSTEM INFORMATION :
      OS: CentOS Linux release 7.2.1511 (Core)
      # uname -a
      Linux SZE-L0163338 3.10.0-862.9.1.el7.x86_64 #1 SMP Mon Jul 16 16:29:36 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux

      Java: have tried 12.0.1, and 14 early access:
      # ./java -version
      java version "12.0.1" 2019-04-16
      Java(TM) SE Runtime Environment (build 12.0.1+12)
      Java HotSpot(TM) 64-Bit Server VM (build 12.0.1+12, mixed mode, sharing)

      # java -version
      openjdk version "14-ea" 2020-03-17
      OpenJDK Runtime Environment (build 14-ea+2-29)
      OpenJDK 64-Bit Server VM (build 14-ea+2-29, mixed mode, sharing)


      A DESCRIPTION OF THE PROBLEM :
      The following compiler error is expected on line 9. In fact, compilation is succeed after line 14-16 has been commented, and then it failed in execution because of ClassCastException as expected.

      Expected compiler error examples:
      CallGGNSGM.java:14: error: incompatible types: Map<String,String> cannot be converted to Map<String,Object>
              GGSGM.call(map);
                         ^
      CallGGNSGM.java:15: error: incompatible types: Map<String,String> cannot be converted to Map<String,Object>
              (new NSGM()).call(map);
                                ^
      CallGGNSGM.java:16: error: incompatible types: Map<String,String> cannot be converted to Map<String,Object>
              SGM.call(map);
                       ^

      Main class (CallGGNSGM) implementation:

      import java.util.HashMap;
      import java.util.Map;

      public class CallGGNSGM {
          public static void main(String[] args) {
              Map<String, String> map = new HashMap();
              map.put("k", "v");
              System.out.println("CallGGSGM try gg_instantiated_gm");
              (new GGNSGM()).call(map); //Compiler error(as lines 14-16) expected here
              System.out.println(map);
              for (Map.Entry<String, String> entry : map.entrySet()) {
                  System.out.println(entry.getKey() + " : " + entry.getValue());
              }
      // GGSGM.call(map);
      // (new NSGM()).call(map);
      // SGM.call(map);
          }
      }

      GGNSGM:
      import java.util.Map;

      public class GGNSGM<T> {
          public void call(Map<String, Object> map) {
              System.out.println("call gg_instantiated_gm_map<String, Object> " + map);
              map.put("k2", new Integer(1));
          }
      }

      GGSGN:
      import java.util.Map;

      public class GGSGM<T> {
          public static void call(Map<String, Object> map) {
              System.out.println("call gg_static_gm_map<String, Object> " + map);
          }
      }

      NSGM:
      import java.util.Map;

      public class NSGM {
          public void call(Map<String, Object> map) {
              System.out.println("call instantiated_gm_map<String, Object> " + map);
          }
      }

      SGM:
      import java.util.Map;

      public class SGM {
          public static void call(Map<String, Object> map) {
              System.out.println("call static_gm_map<String, Object> " + map);
          }
      }


      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Try compile the attached source files: CallGGNSGM.java, GGNSGM.java, GGSGM.java, NSGM.java, SGM.java, and then launch CallGGNSGM.class

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      Compiler error expected while compiling.
      ACTUAL -
      Compilation succeeded, and then failed in execution because of ClassCastException.

      ---------- BEGIN SOURCE ----------
      Main class (CallGGNSGM) implementation:

      import java.util.HashMap;
      import java.util.Map;

      public class CallGGNSGM {
          public static void main(String[] args) {
              Map<String, String> map = new HashMap();
              map.put("k", "v");
              System.out.println("CallGGSGM try gg_instantiated_gm");
              (new GGNSGM()).call(map); //Compiler error(as lines 14-16) expected here
              System.out.println(map);
              for (Map.Entry<String, String> entry : map.entrySet()) {
                  System.out.println(entry.getKey() + " : " + entry.getValue());
              }
      // GGSGM.call(map);
      // (new NSGM()).call(map);
      // SGM.call(map);
          }
      }

      GGNSGM:
      import java.util.Map;

      public class GGNSGM<T> {
          public void call(Map<String, Object> map) {
              System.out.println("call gg_instantiated_gm_map<String, Object> " + map);
              map.put("k2", new Integer(1));
          }
      }

      GGSGN:
      import java.util.Map;

      public class GGSGM<T> {
          public static void call(Map<String, Object> map) {
              System.out.println("call gg_static_gm_map<String, Object> " + map);
          }
      }

      NSGM:
      import java.util.Map;

      public class NSGM {
          public void call(Map<String, Object> map) {
              System.out.println("call instantiated_gm_map<String, Object> " + map);
          }
      }

      SGM:
      import java.util.Map;

      public class SGM {
          public static void call(Map<String, Object> map) {
              System.out.println("call static_gm_map<String, Object> " + map);
          }
      }
      ---------- END SOURCE ----------

      FREQUENCY : always


        1. CallGGNSGM.java
          0.6 kB
        2. GGNSGM.java
          0.3 kB
        3. GGSGN.java
          0.2 kB
        4. NSGM.java
          0.2 kB
        5. SGM.java
          0.2 kB

            Unassigned Unassigned
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated: