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

The Type conversion threw an exception at the wrong place

    XMLWordPrintable

Details

    Description

      ADDITIONAL SYSTEM INFORMATION :
      java;
      version: java 17 / 20 (jdk 17.0.1, openjdk 17.0.2, jdk 20.0.1, openjdk 20.0.1)
      openjdk version "17.0.2" 2022-01-18
      OpenJDK Runtime Environment (build 17.0.2+8-86)
      OpenJDK 64-Bit Server VM (build 17.0.2+8-86, mixed mode, sharing)

      os:
      mac

      maven 3:
      Apache Maven 3.6.3 (cecedd343002696d0abb50b32b541b8a6ba2883f)
      Maven home: /Users/liuxun/Documents/apache-maven-3.6.3
      Java version: 17.0.2, vendor: Oracle Corporation, runtime: /Library/Java/JavaVirtualMachines/jdk-17.0.2.jdk/Contents/Home
      Default locale: zh_CN_#Hans, platform encoding: UTF-8
      OS name: "mac os x", version: "12.6.3", arch: "x86_64", family: "mac"

      A DESCRIPTION OF THE PROBLEM :
      1: follow the code[see belowed block: Source code for an executable test case] or refer the test project issue : https://github.com/chwod/bugs/issues/1
      2: run the main method, then throw the exception:
      Exception in thread "main" java.lang.ClassCastException: class java.util.Arrays$ArrayList cannot be cast to class java.lang.String (java.util.Arrays$ArrayList and java.lang.String are in module java.base of loader 'bootstrap')
      at java.base/java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:197)
      at java.base/java.util.ArrayList$ArrayListSpliterator.forEachRemaining(ArrayList.java:1625)
      at java.base/java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:509)
      at java.base/java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:499)
      at java.base/java.util.stream.ReduceOps$ReduceTask.doLeaf(ReduceOps.java:960)
      at java.base/java.util.stream.ReduceOps$ReduceTask.doLeaf(ReduceOps.java:934)
      at java.base/java.util.stream.AbstractTask.compute(AbstractTask.java:327)
      at java.base/java.util.concurrent.CountedCompleter.exec(CountedCompleter.java:754)
      at java.base/java.util.concurrent.ForkJoinTask.doExec(ForkJoinTask.java:387)
      at java.base/java.util.concurrent.ForkJoinTask.invoke(ForkJoinTask.java:667)
      at java.base/java.util.stream.ReduceOps$ReduceOp.evaluateParallel(ReduceOps.java:927)
      at java.base/java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:233)
      at java.base/java.util.stream.ReferencePipeline.collect(ReferencePipeline.java:682)
      at Test.main(Test.java:22)

      the exception threw at the line 22, but the line 22 is the vaue used after the value cast(line 11):we expect that the exception threw at line 11, when the type cast unsupport.
      3; if the exception throw at the line 22, I think we do not know how to avoid it. if throw at the line 11, we can surround with try/catch block to avoid it.
      4: if the exception not throw at the line 11, would cause the return parameter type to not work as expected´╝î see the follow inspect picture: https://github.com/chwod/bugs/blob/main/inspect.png?raw=true

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      the exception threw at the line 11
      ACTUAL -
      the exception throw at the line 22

      ---------- BEGIN SOURCE ----------
      import java.util.ArrayList;
      import java.util.Arrays;
      import java.util.List;
      import java.util.stream.Collectors;

      public class Test {

      private static <T> List<T> get(Object o) {
      var list = new ArrayList<T>();
      list.add(
      (T) o
      );
      return list;
      }

      public static void main(String... strings) {
      var a = Arrays.asList("v");
      List<String> b = Test.<String>get(a);
      System.out.println(b);
      var c = b.parallelStream()
      .map(d -> d)
      .collect(
      Collectors.toList());
      System.out.println(c);
      }

      }
      ---------- END SOURCE ----------

      FREQUENCY : always


      Attachments

        Activity

          People

            vklang Viktor Klang
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            5 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: