8224012: AnnotatedType implementations of hashCode() lead to StackOverflowError
authordarcy
Wed, 29 May 2019 09:53:28 -0700
changeset 55090 c1ad2862d0dd
parent 55089 934d68e9c45d
child 55091 c58772f2dbce
8224012: AnnotatedType implementations of hashCode() lead to StackOverflowError Reviewed-by: jfranck
src/java.base/share/classes/sun/reflect/annotation/AnnotatedTypeFactory.java
test/jdk/java/lang/annotation/typeAnnotations/TestObjectMethods.java
--- a/src/java.base/share/classes/sun/reflect/annotation/AnnotatedTypeFactory.java	Wed May 29 10:57:19 2019 -0400
+++ b/src/java.base/share/classes/sun/reflect/annotation/AnnotatedTypeFactory.java	Wed May 29 09:53:28 2019 -0700
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2013, 2018, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2013, 2019, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -393,29 +393,22 @@
             return (TypeVariable)getType();
         }
 
-        // For toString, the declaration of a type variable should
-        // including information about its bounds, etc. However, the
+        // The declaration of a type variable should
+        // include information about its bounds, etc. However, the
         // use of a type variable should not. For that reason, it is
-        // acceptable for the toString implementation of
+        // acceptable for the toString and hashCode implementations of
         // AnnotatedTypeVariableImpl to use the inherited
-        // implementation from AnnotatedTypeBaseImpl.
+        // implementations from AnnotatedTypeBaseImpl.
 
         @Override
         public boolean equals(Object o) {
             if (o instanceof AnnotatedTypeVariable) {
                 AnnotatedTypeVariable that = (AnnotatedTypeVariable) o;
-                return equalsTypeAndAnnotations(that) &&
-                    Arrays.equals(getAnnotatedBounds(), that.getAnnotatedBounds());
+                return equalsTypeAndAnnotations(that);
             } else {
                 return false;
             }
         }
-
-        @Override
-        public int hashCode() {
-            return baseHashCode() ^
-                Objects.hash((Object[])getAnnotatedBounds());
-        }
     }
 
     private static final class AnnotatedParameterizedTypeImpl extends AnnotatedTypeBaseImpl
--- a/test/jdk/java/lang/annotation/typeAnnotations/TestObjectMethods.java	Wed May 29 10:57:19 2019 -0400
+++ b/test/jdk/java/lang/annotation/typeAnnotations/TestObjectMethods.java	Wed May 29 09:53:28 2019 -0700
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2018, 2019, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -23,7 +23,7 @@
 
 /*
  * @test
- * @bug 8058202 8212081
+ * @bug 8058202 8212081 8224012
  * @summary Test java.lang.Object methods on AnnotatedType objects.
  */
 
@@ -73,6 +73,8 @@
 
         testWildcards();
 
+        testFbounds();
+
         if (errors > 0) {
             throw new RuntimeException(errors + " errors");
         }
@@ -314,6 +316,23 @@
         }
     }
 
+    static void testFbounds() {
+        // Make sure equals and hashCode work fine for a type
+        // involving an F-bound, in particular Comparable<E> in
+        // java.lang.Enum:
+        //
+        // class Enum<E extends Enum<E>>
+        // implements Constable, Comparable<E>, Serializable
+
+        AnnotatedType[] types = Enum.class.getAnnotatedInterfaces();
+
+        for (int i = 0; i < types.length; i ++) {
+            for (int j = 0; j < types.length; j ++) {
+                checkTypesForEquality(types[i], types[j], i == j);
+            }
+        }
+    }
+
     // The TypeHost and AnnotatedTypeHost classes declare methods with
     // the same name and signatures but with the AnnotatedTypeHost
     // methods having annotations on their return type, where