1 /* 2 * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 /* 25 * @test 26 * @bug 5041784 27 * @summary Check that plain arrays like String[] are never represented as 28 * GenericArrayType. 29 * @author Eamonn McManus 30 */ 31 32 import java.lang.reflect.Constructor; 33 import java.lang.reflect.GenericArrayType; 34 import java.lang.reflect.GenericDeclaration; 35 import java.lang.reflect.Method; 36 import java.lang.reflect.ParameterizedType; 37 import java.lang.reflect.Type; 38 import java.lang.reflect.TypeVariable; 39 import java.lang.reflect.WildcardType; 40 import java.util.HashSet; 41 import java.util.List; 42 import java.util.Map; 43 import java.util.Set; 44 45 public class TestPlainArrayNotGeneric { m1(List<String> p1)46 public String[] m1(List<String> p1) {return null;} m2(String[] p1)47 public List<String> m2(String[] p1) {return null;} m3(List<String> p1, String[] p2)48 public void m3(List<String> p1, String[] p2) {} m4(List<String[]> p1)49 public void m4(List<String[]> p1) {} TestPlainArrayNotGeneric(List<String[]> p1)50 public TestPlainArrayNotGeneric(List<String[]> p1) {} TestPlainArrayNotGeneric(List<String> p1, String[] p2)51 public TestPlainArrayNotGeneric(List<String> p1, String[] p2) {} 52 m5(T p1)53 public <T extends List<String[]>> T m5(T p1) {return null;} m6(T[] p1, List<T[]> p2)54 public <T extends Object> T[] m6(T[] p1, List<T[]> p2) {return null;} 55 m6(List<? extends Object[]> p1)56 public List<? extends Object[]> m6(List<? extends Object[]> p1) {return null;} m7(T[] p1)57 public <T extends List<? extends Object[]>> T m7(T[] p1) {return null;} m8(List<? super Object[]> p1)58 public List<? super Object[]> m8(List<? super Object[]> p1) {return null;} m9(T[] p1)59 public <T extends List<? super Object[]>> T[] m9(T[] p1) {return null;} 60 61 public static interface XMap extends Map<List<String[]>, String[]> {} 62 public static interface YMap<K extends List<String[]>, V> 63 extends Map<K[], V[]> {} 64 65 66 private static String lastFailure; 67 private static int failureCount; 68 main(String[] args)69 public static void main(String[] args) throws Exception { 70 checkClass(TestPlainArrayNotGeneric.class); 71 72 if (failureCount == 0) 73 System.out.println("TEST PASSED"); 74 else 75 throw new Exception("TEST FAILED: Last failure: " + lastFailure); 76 } 77 checkClass(Class<?> c)78 private static void checkClass(Class<?> c) throws Exception { 79 Method[] methods = c.getMethods(); 80 for (Method m : methods) { 81 check(m.getGenericReturnType(), "return type of method " + m); 82 check(m.getGenericParameterTypes(), "parameter", "method " + m); 83 check(m.getTypeParameters(), "type parameter", "method " + m); 84 } 85 86 Constructor[] constructors = c.getConstructors(); 87 for (Constructor constr : constructors) { 88 check(constr.getGenericParameterTypes(), "parameter", 89 "constructor " + constr); 90 check(constr.getTypeParameters(), "type parameter", 91 "constructor " + constr); 92 } 93 94 Class<?>[] inners = c.getDeclaredClasses(); 95 for (Class inner : inners) 96 checkClass(inner); 97 } 98 check(Type[] types, String elementKind, String what)99 private static void check(Type[] types, String elementKind, String what) { 100 for (int i = 0; i < types.length; i++) { 101 Type t = types[i]; 102 check(t, elementKind + " " + (i+1) + " of " + what); 103 } 104 } 105 106 private static final Set<Type> checking = new HashSet<>(); 107 check(Type t, String what)108 private static void check(Type t, String what) { 109 if (t == null || !checking.add(t)) 110 return; 111 // Avoid infinite recursion. t can be null e.g. for superclass of Object. 112 try { 113 check2(t, what); 114 } finally { 115 checking.remove(t); 116 } 117 } 118 check2(Type t, String what)119 private static void check2(Type t, String what) { 120 if (t instanceof ParameterizedType) { 121 ParameterizedType pt = (ParameterizedType) t; 122 check(pt.getActualTypeArguments(), "type argument", what); 123 } else if (t instanceof TypeVariable) { 124 TypeVariable<?> tv = (TypeVariable<?>) t; 125 check(tv.getBounds(), "bound", what); 126 GenericDeclaration gd = tv.getGenericDeclaration(); 127 if (gd instanceof Type) 128 check((Type) gd, "declaration containing " + what); 129 } else if (t instanceof WildcardType) { 130 WildcardType wt = (WildcardType) t; 131 check(wt.getLowerBounds(), "lower bound", "wildcard type in " + what); 132 check(wt.getUpperBounds(), "upper bound", "wildcard type in " + what); 133 } else if (t instanceof Class<?>) { 134 Class<?> c = (Class<?>) t; 135 check(c.getGenericInterfaces(), "superinterface", c.toString()); 136 check(c.getGenericSuperclass(), "superclass of " + c); 137 check(c.getTypeParameters(), "type parameter", c.toString()); 138 } else if (t instanceof GenericArrayType) { 139 GenericArrayType gat = (GenericArrayType) t; 140 Type comp = gat.getGenericComponentType(); 141 if (comp instanceof Class) { 142 fail("Type " + t + " uses GenericArrayType when plain " + 143 "array would do, in " + what); 144 } else 145 check(comp, "component type of " + what); 146 } else { 147 fail("TEST BUG: mutant Type " + t + " (a " + t.getClass().getName() + ")"); 148 } 149 } 150 fail(String why)151 private static void fail(String why) { 152 System.out.println("FAIL: " + why); 153 lastFailure = why; 154 failureCount++; 155 } 156 } 157