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

Filtering streams using an unchecked method invocation won't compile

XMLWordPrintable

      ADDITIONAL SYSTEM INFORMATION :
      docker image of openjdk:8

      A DESCRIPTION OF THE PROBLEM :
      In case a filter method which is invoked in an unchecked manner, the collect(Collector col) fails to compile. For instance, given the attached code, the compiler will fail with this error: error: incompatible types: Object cannot be converted to Collection<Target>
                      .collect( Collectors.toList());

      even if the compiler is instructed to ignore the unchecked code warnings using the -Xlint:unchecked flag.

      The code compiles fine (with the right warning) in the openjdk 10 branch.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      javac StreamBugCase.java

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      compiles with warnings
      ACTUAL -
      doesn't compile

      ---------- BEGIN SOURCE ----------
      import java.util.Collection;
      import java.util.stream.Collectors;

      public class StreamBugCase
      {
          Class clazz;
          Collection<Target> targets;

          public Collection<Target> getTargets()
          {
              Collection<Target> myTargets = targets.stream()
                      .filter( t -> t.isApplicable( clazz ) )
                      .collect( Collectors.toList() );
              return myTargets;
          }

          public interface Target
          {
              boolean isApplicable(Class<? extends MainType> clazz);
          }

          public interface MainType
          {
          }
      }
      ---------- END SOURCE ----------

      FREQUENCY : always


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

              Created:
              Updated:
              Resolved: