Swing generification changes in JDK 9 b24

Andrej Golovnin andrej.golovnin at gmail.com
Mon Jul 28 12:04:25 UTC 2014


Hi Joe,

I'm not sure if it is what you are looking for. But I tried to compile my
project with the new build. And I got a compile error in one of my classes.
I have a class which implements the TreeNode interface and looks like this:

class MyTreeNode implements TreeNode {
....
    @Override
    public Enumeration<MyTreeNode> children() {
        return ....;
    }
...
}

The error message is: "return type Enumeration<MyTreeNode> is not
compatible with Enumeration<TreeNode>".

If I change (see attached patch) the definition of the children()-method in
the TreeNode-interface to:

public interface TreeNode {
...
    Enumeration<? extends TreeNode> children();
...
}

then my code compiles.

BTW, a good test for your changes would be to try to compile NetBeans
and/or IntelliJ IDEA using the new JDK 9 build. They both are really big
Swing applications which make use maybe of all Swing APIs.

Best regards,
Andrej Golovnin
-------------- next part --------------
diff --git a/src/share/classes/javax/swing/JTree.java b/src/share/classes/javax/swing/JTree.java
--- a/src/share/classes/javax/swing/JTree.java
+++ b/src/share/classes/javax/swing/JTree.java
@@ -4032,7 +4032,7 @@
         /**
          * Subclassed to load the children, if necessary.
          */
-        public Enumeration<TreeNode> children() {
+        public Enumeration<? extends TreeNode> children() {
             if(!loadedChildren)
                 loadChildren();
             return super.children();
diff --git a/src/share/classes/javax/swing/tree/DefaultMutableTreeNode.java b/src/share/classes/javax/swing/tree/DefaultMutableTreeNode.java
--- a/src/share/classes/javax/swing/tree/DefaultMutableTreeNode.java
+++ b/src/share/classes/javax/swing/tree/DefaultMutableTreeNode.java
@@ -95,7 +95,7 @@
      * An enumeration that is always empty. This is used when an enumeration
      * of a leaf node's children is requested.
      */
-    static public final Enumeration<TreeNode> EMPTY_ENUMERATION
+    static public final Enumeration<? extends TreeNode> EMPTY_ENUMERATION
         = Collections.emptyEnumeration();
 
     /** this node's parent, or null if this node has no parent */
@@ -290,7 +290,7 @@
      *
      * @return  an Enumeration of this node's children
      */
-    public Enumeration<TreeNode> children() {
+    public Enumeration<? extends TreeNode> children() {
         if (children == null) {
             return EMPTY_ENUMERATION;
         } else {
@@ -1308,7 +1308,7 @@
     }
 
     private final class PreorderEnumeration implements Enumeration<TreeNode> {
-        private final Stack<Enumeration<TreeNode>> stack = new Stack<>();
+        private final Stack<Enumeration<? extends TreeNode>> stack = new Stack<>();
 
         public PreorderEnumeration(TreeNode rootNode) {
             super();
@@ -1322,10 +1322,10 @@
         }
 
         public TreeNode nextElement() {
-            Enumeration<TreeNode> enumer = stack.peek();
+            Enumeration<? extends TreeNode> enumer = stack.peek();
             TreeNode    node = enumer.nextElement();
             @SuppressWarnings("unchecked")
-            Enumeration<TreeNode> children = node.children();
+            Enumeration<? extends TreeNode> children = node.children();
 
             if (!enumer.hasMoreElements()) {
                 stack.pop();
@@ -1342,8 +1342,8 @@
 
     final class PostorderEnumeration implements Enumeration<TreeNode> {
         protected TreeNode root;
-        protected Enumeration<TreeNode> children;
-        protected Enumeration<TreeNode> subtree;
+        protected Enumeration<? extends TreeNode> children;
+        protected Enumeration<? extends TreeNode> subtree;
 
         public PostorderEnumeration(TreeNode rootNode) {
             super();
diff --git a/src/share/classes/javax/swing/tree/TreeNode.java b/src/share/classes/javax/swing/tree/TreeNode.java
--- a/src/share/classes/javax/swing/tree/TreeNode.java
+++ b/src/share/classes/javax/swing/tree/TreeNode.java
@@ -99,5 +99,5 @@
      *
      * @return              the children of the receiver as an {@code Enumeration}
      */
-    Enumeration<TreeNode> children();
+    Enumeration<? extends TreeNode> children();
 }
diff --git a/src/share/classes/javax/swing/tree/VariableHeightLayoutCache.java b/src/share/classes/javax/swing/tree/VariableHeightLayoutCache.java
--- a/src/share/classes/javax/swing/tree/VariableHeightLayoutCache.java
+++ b/src/share/classes/javax/swing/tree/VariableHeightLayoutCache.java
@@ -1100,7 +1100,7 @@
          * empty enumeration.
          */
         @Override
-        public Enumeration<TreeNode> children() {
+        public Enumeration<? extends TreeNode> children() {
             if (!this.isExpanded()) {
                 return DefaultMutableTreeNode.EMPTY_ENUMERATION;
             } else {
@@ -1406,7 +1406,7 @@
          * <code>createIfNeeded</code> is true, the children are first
          * loaded.
          */
-        protected Enumeration<TreeNode> getLoadedChildren(boolean createIfNeeded) {
+        protected Enumeration<? extends TreeNode> getLoadedChildren(boolean createIfNeeded) {
             if(!createIfNeeded || hasBeenExpanded)
                 return super.children();
 


More information about the jdk9-dev mailing list