--- old/src/java.base/share/classes/sun/reflect/annotation/AnnotatedTypeFactory.java 2018-08-16 12:24:18.934000000 -0700 +++ new/src/java.base/share/classes/sun/reflect/annotation/AnnotatedTypeFactory.java 2018-08-16 12:24:18.790000000 -0700 @@ -31,6 +31,8 @@ import java.util.Arrays; import java.util.List; import java.util.Map; +import java.util.Objects; +import java.util.StringJoiner; import static sun.reflect.annotation.TypeAnnotation.*; @@ -202,6 +204,42 @@ } + @Override + public String toString() { + Annotation[] annotations = getAnnotations(); + StringJoiner sj = new StringJoiner("\n"); + if (annotations != null) { + for (Annotation annotation : annotations) { + sj.add(annotation.toString()); + } + sj.add(""); // Add a newline + } + return sj.toString() + type.toString(); + } + + /* + * Testing notes: see if type.toString() is a postfix of AnnotatedType.toString() + + * For the annotations, for multiple annotations, compare with + * a runtime construction since the order of annotations from + * getAnnotations is not specified. + */ + + boolean equalsTypeAndAnnotations(AnnotatedType that) { + return getType().equals(that.getType()) && + // Treat ordering of annotations as significant + Arrays.equals(getAnnotations(), that.getAnnotations()) && + Objects.equals(getAnnotatedOwnerType(), that.getAnnotatedOwnerType()); + } + + int baseHashCode() { + return type.hashCode() ^ + // Acceptable to use Objects.hash rather than + // Arrays.deepHashCode since the elements of the array + // are not themselves arrays. + Objects.hash((Object[])getAnnotations()); + } + // Implementation details final LocationInfo getLocation() { return location; @@ -244,6 +282,23 @@ } return ((GenericArrayType)t).getGenericComponentType(); } + + @Override + public boolean equals(Object o) { + if (o instanceof AnnotatedArrayType) { + AnnotatedArrayType that = (AnnotatedArrayType) o; + return equalsTypeAndAnnotations(that) && + Objects.equals(getAnnotatedGenericComponentType(), + that.getAnnotatedGenericComponentType()); + } else { + return false; + } + } + + @Override + public int hashCode() { + return baseHashCode() ^ getAnnotatedGenericComponentType().hashCode(); + } } private static final class AnnotatedTypeVariableImpl extends AnnotatedTypeBaseImpl implements AnnotatedTypeVariable { @@ -266,6 +321,23 @@ private TypeVariable getTypeVariable() { return (TypeVariable)getType(); } + + @Override + public boolean equals(Object o) { + if (o instanceof AnnotatedTypeVariable) { + AnnotatedTypeVariable that = (AnnotatedTypeVariable) o; + return equalsTypeAndAnnotations(that) && + Arrays.equals(getAnnotatedBounds(), that.getAnnotatedBounds()); + } else { + return false; + } + } + + @Override + public int hashCode() { + return baseHashCode() ^ + Objects.hash((Object[])getAnnotatedBounds()); + } } private static final class AnnotatedParameterizedTypeImpl extends AnnotatedTypeBaseImpl @@ -316,6 +388,23 @@ private ParameterizedType getParameterizedType() { return (ParameterizedType)getType(); } + + @Override + public boolean equals(Object o) { + if (o instanceof AnnotatedParameterizedType) { + AnnotatedParameterizedType that = (AnnotatedParameterizedType) o; + return equalsTypeAndAnnotations(that) && + Arrays.equals(getAnnotatedActualTypeArguments(), that.getAnnotatedActualTypeArguments()); + } else { + return false; + } + } + + @Override + public int hashCode() { + return baseHashCode() ^ + Objects.hash((Object[])getAnnotatedActualTypeArguments()); + } } private static final class AnnotatedWildcardTypeImpl extends AnnotatedTypeBaseImpl implements AnnotatedWildcardType { @@ -378,5 +467,26 @@ private boolean hasUpperBounds() { return hasUpperBounds; } + + @Override + public boolean equals(Object o) { + if (o instanceof AnnotatedWildcardType) { + AnnotatedWildcardType that = (AnnotatedWildcardType) o; + return equalsTypeAndAnnotations(that) && + // Treats ordering as significant + Arrays.equals(getAnnotatedLowerBounds(), that.getAnnotatedLowerBounds()) && + // Treats ordering as significant + Arrays.equals(getAnnotatedUpperBounds(), that.getAnnotatedUpperBounds()); + } else { + return false; + } + } + + @Override + public int hashCode() { + return baseHashCode() ^ + Objects.hash((Object[])getAnnotatedLowerBounds()) ^ + Objects.hash((Object[])getAnnotatedUpperBounds()); + } } } --- /dev/null 2018-07-23 19:23:52.252000000 -0700 +++ new/test/jdk/java/lang/annotation/typeAnnotations/TestObjectMethods.java 2018-08-16 12:24:19.158000000 -0700 @@ -0,0 +1,92 @@ +/* + * Copyright (c) 2018, 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 + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +/* + * @test + * @bug 8058202 + * @summary Test java.lang.Object methods on AnnotatedType's. + */ + +import java.lang.reflect.*; +import java.util.*; + +/* + * Testing notes: see if type.toString() is a postfix of AnnotatedType.toString() + + * For the annotations, for multiple annotations, compare with + * a runtime construction since the order of annotations from + * getAnnotations is not specified. + */ + + +/** + * Test toString, equals, and hashCode on various AnnotatedType objects. + */ +public class TestObjectMethods { + + public static void main(String... args) { + testToString(); + } + + static void testToString() { + Method[] methods = TypeAnnotationHost.class.getDeclaredMethods(); + for (Method m : methods) { + AnnotatedType annotType = m.getAnnotatedReturnType(); + String annotTypeString = annotType.toString(); + + Type type = m.getGenericReturnType(); + String typeString = type.toString(); + + System.out.println(typeString + "\t\t" + annotTypeString + + "\t" + annotTypeString.endsWith(typeString)); + } + } + + /* + * There are various subtypes of AnnotatedType implementations: + * + * AnnotatedType + * AnnotatedArrayType + * AnnotatedParameterizedType + * AnnotatedTypeVariable + * AnnotatedWildcardType + * + * The implementations of each these implementions should be examined. + */ + + static class TypeAnnotationHost { + public int foo() {return 0;} + public String fooString() {return null;} + + public int[] fooIntArray() {return null;} + public String[] fooStringArray() {return null;} + + public Set fooSetString() {return null;} + public E fooE() {return null;} + public F fooF() {return null;} + public G fooG() {return null;} + + public Set fooNumberSet() {return null;} + public Set fooNumberSet2() {return null;} + } +}