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

Generic types have to be casted to proper type

    XMLWordPrintable

Details

    • x86_64
    • generic

    Description

      FULL PRODUCT VERSION :
      java version "1.8.0_152"
      Java(TM) SE Runtime Environment (build 1.8.0_152-b16)
      Java HotSpot(TM) 64-Bit Server VM (build 25.152-b16, mixed mode)

      ADDITIONAL OS VERSION INFORMATION :
      Darwin iMac-Marcin 17.3.0 Darwin Kernel Version 17.3.0: Thu Nov 9 18:09:22 PST 2017; root:xnu-4570.31.3~1/RELEASE_X86_64 x86_64

      A DESCRIPTION OF THE PROBLEM :
      Returned generic type which is declared from inner public classes loses original type.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      I provided code below


      ERROR MESSAGES/STACK TRACES THAT OCCUR :
      Error:(7, 55) java: cannot find symbol
        symbol: method getActionButtonEvent()
        location: class com.marurban.BaseViewModel<com.marurban.FavoriteLeaguesViewModel.Accessor,com.marurban.FavoriteLeaguesViewModel.ViewEvents,com.marurban.FavoriteLeaguesViewModel.FlowEvents>.FlowEvents

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      public class Main {

          public static void main(String[] args) {

              new FavoriteLeaguesViewModel().getFlowEvents().getActionButtonEvent();
          }
      }

      public abstract class BaseViewModel<FlowEvents extends BaseViewModel.FlowEvents> {

          private FlowEvents flowEvents;

          public BaseViewModel() {
              flowEvents = createFlow();
          }

          protected abstract FlowEvents createFlow();

          public FlowEvents getFlowEvents() {
              return flowEvents;
          }

          public class FlowEvents {
          }
      }

      public abstract class FavoritesViewModel<FlowEvents extends FavoritesViewModel.FlowEvents>
              extends BaseViewModel<FlowEvents> {

          List list;

          public FavoritesViewModel() {
              super();
          }

          public abstract class FlowEvents extends BaseViewModel.FlowEvents {

              public List getActionButtonEvent() {
                  return list;
              }
          }
      }


      public class FavoriteLeaguesViewModel extends FavoritesViewModel<FavoriteLeaguesViewModel.FlowEvents> {

          public FavoriteLeaguesViewModel() {
              super();
          }

          @Override
          protected FlowEvents createFlow() {
              return new FlowEvents();
          }

          public class FlowEvents extends FavoritesViewModel.FlowEvents {

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

      CUSTOMER SUBMITTED WORKAROUND :
      You need to explicitly cast to proper class.

      public class Main {

          public static void main(String[] args) {
              FavoriteLeaguesViewModel viewModel = new FavoriteLeaguesViewModel();
              FavoriteLeaguesViewModel.FlowEvents flowEvents = (FavoriteLeaguesViewModel.FlowEvents) viewModel.getFlowEvents();
              flowEvents.getActionButtonEvent();
          }
      }


      Attachments

        1. BaseViewModel.java
          0.4 kB
        2. FavoriteLeaguesViewModel.java
          0.3 kB
        3. FavoritesViewModel.java
          0.4 kB
        4. Main.java
          0.4 kB

        Issue Links

          Activity

            People

              vromero Vicente Arturo Romero Zaldivar
              webbuggrp Webbug Group
              Votes:
              0 Vote for this issue
              Watchers:
              4 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: