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

Problem with casting inside lambdas for java.util.stream.Stream.map method

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Not an Issue
    • Icon: P4 P4
    • None
    • 8, 11, 12, 13
    • tools
    • generic
    • generic

      ADDITIONAL SYSTEM INFORMATION :
      uname -a:
      Linux 3.10.0-862.14.4.el7.x86_64 #1 SMP Wed Sep 26 15:12:11 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux
      javac -version:
      javac 1.8.0_212
      Although I've reproduced this on multiple OSes with different JDKs(openjdk and older versions)

      A DESCRIPTION OF THE PROBLEM :
      The problem seems to be that casting the parameter in the map() method of java.util.stream.Stream is not honored by javac. So the compiler cannot correctly determine the lambda type(thinks the argument of the lambda is of type java.lang.Object). Intellij does not consider this a compile error.

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

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      no compile error
      OR
      better error description: the compiler is considering variable providerLoginPageConfiguration of type Object but in the previous map function the variable is cast to ProviderLoginPageConfiguration.
      ACTUAL -
      WebflowConfiguration.java:58: error: cannot find symbol
                                      providerLoginPageConfiguration.getName(),
                                                                    ^
        symbol: method getName()
        location: variable providerLoginPageConfiguration of type Object
      WebflowConfiguration.java:59: error: cannot find symbol
                                      providerLoginPageConfiguration.getRedirectUrl(),
                                                                    ^
        symbol: method getRedirectUrl()
        location: variable providerLoginPageConfiguration of type Object
      WebflowConfiguration.java:60: error: cannot find symbol
                                      providerLoginPageConfiguration.getType(),
                                                                    ^
        symbol: method getType()
        location: variable providerLoginPageConfiguration of type Object
      WebflowConfiguration.java:61: error: cannot find symbol
                                      providerLoginPageConfiguration.getCssClass(),
                                                                    ^
        symbol: method getCssClass()
        location: variable providerLoginPageConfiguration of type Object
      WebflowConfiguration.java:62: error: cannot find symbol
                                      providerLoginPageConfiguration.isAutoRedirect()
                                                                    ^
        symbol: method isAutoRedirect()
        location: variable providerLoginPageConfiguration of type Object
      Note: WebflowConfiguration.java uses unchecked or unsafe operations.
      Note: Recompile with -Xlint:unchecked for details.
      5 errors

      ---------- BEGIN SOURCE ----------
      package ir.rns.cas.conf.cas;

      import java.io.Serializable;
      import java.util.ArrayList;
      import java.util.Collection;
      import java.util.stream.Stream;

      import static java.util.Optional.ofNullable;
      import static java.util.stream.Collectors.toSet;

      public class WebflowConfiguration {
          public static class ProviderLoginPageConfiguration implements Serializable {
              private static final long serialVersionUID = 6216882278086699364L;
              private final String name;
              private final String redirectUrl;
              private final String type;
              private final String cssClass;
              private final boolean autoRedirect;

              public ProviderLoginPageConfiguration(String name, String redirectUrl, String type, String cssClass,
                      boolean autoRedirect) {
                  this.name = name;
                  this.redirectUrl = redirectUrl;
                  this.type = type;
                  this.cssClass = cssClass;
                  this.autoRedirect = autoRedirect;
              }

              public String getName() {
                  return name;
              }

              public String getRedirectUrl() {
                  return redirectUrl;
              }

              public String getType() {
                  return type;
              }

              public String getCssClass() {
                  return cssClass;
              }

              public boolean isAutoRedirect() {
                  return autoRedirect;
              }
          }

          public static void main(String[] args) {
              final ArrayList value = new ArrayList<>();
              ofNullable(value)
                      .map(Collection::stream)
                      .orElseGet(Stream::empty)
                      .map(it -> (ProviderLoginPageConfiguration) it)
                      .map(providerLoginPageConfiguration ->
                              new ProviderLoginPageConfiguration(
                                      providerLoginPageConfiguration.getName(),
                                      providerLoginPageConfiguration.getRedirectUrl(),
                                      providerLoginPageConfiguration.getType(),
                                      providerLoginPageConfiguration.getCssClass(),
                                      providerLoginPageConfiguration.isAutoRedirect()
                              )
                      ).collect(toSet());
          }
      }

      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      replace this line:
      final ArrayList value = new ArrayList<>();
      with this:
      final ArrayList<Object> value = new ArrayList<>();

      or cast 'providerLoginPageConfiguration' again inside the next lamda.

      FREQUENCY : always


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

              Created:
              Updated:
              Resolved: