Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/lang/reflect/Generics/HashCodeTest.java
38828 views
/*1* Copyright (c) 2004, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 509785626* @summary Computing hashCode of objects modeling generics shouldn't blow stack27* @author Joseph D. Darcy28*/2930import java.util.*;31import java.lang.reflect.*;3233public class HashCodeTest {3435// Mutually recursive interface types36interface Edge<N extends Node<? extends Edge<N>>> {37void setEndNode(N n);38}39interface Node<E extends Edge<? extends Node<E>>> {40E getOutEdge();41}4243public static void main(String argv[]) {44List<Class<?>> classes = new ArrayList<Class<?>>();45Set<TypeVariable> typeVariables = new HashSet<TypeVariable>();4647classes.add(java.lang.Class.class);// Simple case48classes.add(java.util.Map.class);49classes.add(java.lang.Enum.class); // Contains f-bound50classes.add(Edge.class);51classes.add(Node.class);5253for(Class<?> clazz: classes) {54System.out.println(clazz);5556for (TypeVariable<?> tv : clazz.getTypeParameters()) {57int hc = tv.hashCode();58typeVariables.add(tv);59System.out.printf("\t%s 0x%x (%d)%n", tv.getName(), hc, hc);60}61}6263// Loop over classes again, making sure all type variables are64// already present65int count = 0;66for(Class<?> clazz: classes) {67for (TypeVariable<?> tv : clazz.getTypeParameters()) {68if (!typeVariables.remove(tv))69throw new RuntimeException("Type variable " + tv + " not found.");70}71}7273if (typeVariables.size() != 0 )74throw new RuntimeException("Unexpected number of type variables.");7576}77}787980