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

Parent does not support child generics

XMLWordPrintable

    • Icon: Enhancement Enhancement
    • Resolution: Unresolved
    • Icon: P4 P4
    • tbd
    • fx2.0
    • javafx

      We have created a number of custom visual components. Some of these components are general parent components in the sense that they work with any child components. Others are not, in the sense that they work only with a certain class of child components. For instance we have a Palette which is a parent that only works with ColorControl children.

      Since this class of parent components only works with a restricted set of child components, it would be natural to declare them somewhat like this

      public class ParentX<C extends SomeChildType> extends Parent<C> { ... }

      however, the class Parent as well as the actual Pane classes I really want to extend from do not declare the child type generically.

      The downside to this design is, I cannot keep my API users from adding improperly typed components at compile time. Obviously, another problem is that the lack of generics enforces a lot of casting both in my code and in my API user's code.

      You could call this a feature, if you will, but I think it is a bug. Generics are an intrinsic concept of Java, and I believe the fact that JavaFX does not support generics in the component hierarchy itself is a serious design bug.

      Generics support should of course distinguish between regular children and managed children because in many cases what you really want to restrict is your managed children. In other words, my Palette class might be concerned that all the managed children are ColorControls, but it might not care if someone added an unmanaged child called ToolTip.

      Please note that this can easily be fixed, since Parent currently supports children of type Node, all you have to do is to refactor this so that parents have children of type <N extends Node>. Of course this would mean that you would have to update several of the descendants of Parent in the JavaFX API (layout panes etc.), but - more importantly - this does *not* mean that you break backwards compatibility. As you already know, if you change the classes like this

      public class Parent<M extends Node, C extends Node> { ... }
      public class Pane<M extends Node, C extends Node> extends Parent<M, C> { ... }
      (where M is the managed children class and C is the children class)

      then anyone can still run code containing classes that do not use generics like

      public class MyClass extends Pane { ... }

      but visual component developers would then be able to develop proper generics based parent classes like

      public class Palette extends Pane<ColorControl, Node> { ... }
      or
      public class KitchenDesigner<KitchenElement, Node> { ... }

            Unassigned Unassigned
            risaksen Randahl Isaksen
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Imported: