Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/lang/reflect/Generics/TestPlainArrayNotGeneric.java
38828 views
/*1* Copyright (c) 2008, 2010, 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 504178426* @summary Check that plain arrays like String[] are never represented as27* GenericArrayType.28* @author Eamonn McManus29*/3031import java.lang.reflect.Constructor;32import java.lang.reflect.GenericArrayType;33import java.lang.reflect.GenericDeclaration;34import java.lang.reflect.Method;35import java.lang.reflect.ParameterizedType;36import java.lang.reflect.Type;37import java.lang.reflect.TypeVariable;38import java.lang.reflect.WildcardType;39import java.util.HashSet;40import java.util.List;41import java.util.Map;42import java.util.Set;4344public class TestPlainArrayNotGeneric {45public String[] m1(List<String> p1) {return null;}46public List<String> m2(String[] p1) {return null;}47public void m3(List<String> p1, String[] p2) {}48public void m4(List<String[]> p1) {}49public TestPlainArrayNotGeneric(List<String[]> p1) {}50public TestPlainArrayNotGeneric(List<String> p1, String[] p2) {}5152public <T extends List<String[]>> T m5(T p1) {return null;}53public <T extends Object> T[] m6(T[] p1, List<T[]> p2) {return null;}5455public List<? extends Object[]> m6(List<? extends Object[]> p1) {return null;}56public <T extends List<? extends Object[]>> T m7(T[] p1) {return null;}57public List<? super Object[]> m8(List<? super Object[]> p1) {return null;}58public <T extends List<? super Object[]>> T[] m9(T[] p1) {return null;}5960public static interface XMap extends Map<List<String[]>, String[]> {}61public static interface YMap<K extends List<String[]>, V>62extends Map<K[], V[]> {}636465private static String lastFailure;66private static int failureCount;6768public static void main(String[] args) throws Exception {69checkClass(TestPlainArrayNotGeneric.class);7071if (failureCount == 0)72System.out.println("TEST PASSED");73else74throw new Exception("TEST FAILED: Last failure: " + lastFailure);75}7677private static void checkClass(Class<?> c) throws Exception {78Method[] methods = c.getMethods();79for (Method m : methods) {80check(m.getGenericReturnType(), "return type of method " + m);81check(m.getGenericParameterTypes(), "parameter", "method " + m);82check(m.getTypeParameters(), "type parameter", "method " + m);83}8485Constructor[] constructors = c.getConstructors();86for (Constructor constr : constructors) {87check(constr.getGenericParameterTypes(), "parameter",88"constructor " + constr);89check(constr.getTypeParameters(), "type parameter",90"constructor " + constr);91}9293Class<?>[] inners = c.getDeclaredClasses();94for (Class inner : inners)95checkClass(inner);96}9798private static void check(Type[] types, String elementKind, String what) {99for (int i = 0; i < types.length; i++) {100Type t = types[i];101check(t, elementKind + " " + (i+1) + " of " + what);102}103}104105private static final Set<Type> checking = new HashSet<>();106107private static void check(Type t, String what) {108if (t == null || !checking.add(t))109return;110// Avoid infinite recursion. t can be null e.g. for superclass of Object.111try {112check2(t, what);113} finally {114checking.remove(t);115}116}117118private static void check2(Type t, String what) {119if (t instanceof ParameterizedType) {120ParameterizedType pt = (ParameterizedType) t;121check(pt.getActualTypeArguments(), "type argument", what);122} else if (t instanceof TypeVariable) {123TypeVariable<?> tv = (TypeVariable<?>) t;124check(tv.getBounds(), "bound", what);125GenericDeclaration gd = tv.getGenericDeclaration();126if (gd instanceof Type)127check((Type) gd, "declaration containing " + what);128} else if (t instanceof WildcardType) {129WildcardType wt = (WildcardType) t;130check(wt.getLowerBounds(), "lower bound", "wildcard type in " + what);131check(wt.getUpperBounds(), "upper bound", "wildcard type in " + what);132} else if (t instanceof Class<?>) {133Class<?> c = (Class<?>) t;134check(c.getGenericInterfaces(), "superinterface", c.toString());135check(c.getGenericSuperclass(), "superclass of " + c);136check(c.getTypeParameters(), "type parameter", c.toString());137} else if (t instanceof GenericArrayType) {138GenericArrayType gat = (GenericArrayType) t;139Type comp = gat.getGenericComponentType();140if (comp instanceof Class) {141fail("Type " + t + " uses GenericArrayType when plain " +142"array would do, in " + what);143} else144check(comp, "component type of " + what);145} else {146fail("TEST BUG: mutant Type " + t + " (a " + t.getClass().getName() + ")");147}148}149150private static void fail(String why) {151System.out.println("FAIL: " + why);152lastFailure = why;153failureCount++;154}155}156157158