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
32import java.lang.reflect.Constructor;
33import java.lang.reflect.GenericArrayType;
34import java.lang.reflect.GenericDeclaration;
35import java.lang.reflect.Method;
36import java.lang.reflect.ParameterizedType;
37import java.lang.reflect.Type;
38import java.lang.reflect.TypeVariable;
39import java.lang.reflect.WildcardType;
40import java.util.HashSet;
41import java.util.List;
42import java.util.Map;
43import java.util.Set;
44
45public class TestPlainArrayNotGeneric {
46    public String[] m1(List<String> p1) {return null;}
47    public List<String> m2(String[] p1) {return null;}
48    public void m3(List<String> p1, String[] p2) {}
49    public void m4(List<String[]> p1) {}
50    public TestPlainArrayNotGeneric(List<String[]> p1) {}
51    public TestPlainArrayNotGeneric(List<String> p1, String[] p2) {}
52
53    public <T extends List<String[]>> T m5(T p1) {return null;}
54    public <T extends Object> T[] m6(T[] p1, List<T[]> p2) {return null;}
55
56    public List<? extends Object[]> m6(List<? extends Object[]> p1) {return null;}
57    public <T extends List<? extends Object[]>> T m7(T[] p1) {return null;}
58    public List<? super Object[]> m8(List<? super Object[]> p1) {return null;}
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
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
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
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
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
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
151    private static void fail(String why) {
152        System.out.println("FAIL: " + why);
153        lastFailure = why;
154        failureCount++;
155    }
156}
157