J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2004 Sun Microsystems, Inc. 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
| 20 | * CA 95054 USA or visit www.sun.com if you need additional information or |
| 21 | * have any questions. |
| 22 | */ |
| 23 | |
| 24 | /* |
| 25 | * @test |
| 26 | * @bug 5061485 |
| 27 | * @summary Test sematics of ParameterizedType.equals |
| 28 | * @author Joseph D. Darcy |
| 29 | */ |
| 30 | |
| 31 | import java.util.*; |
| 32 | import java.lang.reflect.*; |
| 33 | |
| 34 | public class TestParameterizedType { |
| 35 | public <T> T genericMethod0() { |
| 36 | return null; |
| 37 | } |
| 38 | |
| 39 | public <T> Set<T> genericMethod1() { |
| 40 | return null; |
| 41 | } |
| 42 | |
| 43 | public <T> Set<T> genericMethod2() { |
| 44 | return null; |
| 45 | } |
| 46 | |
| 47 | public <S> List<S> genericMethod3() { |
| 48 | return null; |
| 49 | } |
| 50 | |
| 51 | public <X, Y> Map<X, Y> genericMethod4() { |
| 52 | return null; |
| 53 | } |
| 54 | |
| 55 | public <T> T[] genericMethod5() { |
| 56 | return null; |
| 57 | } |
| 58 | |
| 59 | public <T> T[] genericMethod6() { |
| 60 | return null; |
| 61 | } |
| 62 | |
| 63 | public Set<? extends Cloneable> genericMethod7() { |
| 64 | return null; |
| 65 | } |
| 66 | |
| 67 | public Set<? super Number> genericMethod8() { |
| 68 | return null; |
| 69 | } |
| 70 | |
| 71 | public Set<?> genericMethod9() { |
| 72 | return null; |
| 73 | } |
| 74 | |
| 75 | |
| 76 | static List<Type> createTypes() throws Exception { |
| 77 | List<Type> typeList = new ArrayList<Type>(3); |
| 78 | String[] methodNames = {"genericMethod0", |
| 79 | "genericMethod1", |
| 80 | "genericMethod2", |
| 81 | "genericMethod3", |
| 82 | "genericMethod4", |
| 83 | "genericMethod5", |
| 84 | "genericMethod6", |
| 85 | "genericMethod7", |
| 86 | "genericMethod8", |
| 87 | "genericMethod9", |
| 88 | }; |
| 89 | |
| 90 | for(String s : methodNames) { |
| 91 | Type t = TestParameterizedType.class.getDeclaredMethod(s).getGenericReturnType(); |
| 92 | // if (! (t instanceof ParameterizedType)) |
| 93 | // throw new RuntimeException("Unexpected kind of return type"); |
| 94 | typeList.add(t); |
| 95 | } |
| 96 | |
| 97 | return typeList; |
| 98 | } |
| 99 | |
| 100 | static boolean testReflexes(List<Type> typeList) { |
| 101 | for(Type t : typeList) { |
| 102 | if (! t.equals(t) ) { |
| 103 | System.err.printf("Bad reflexes for%s %s%n", t, t.getClass()); |
| 104 | return true; |
| 105 | } |
| 106 | } |
| 107 | return false; |
| 108 | } |
| 109 | |
| 110 | public static void main(String[] argv) throws Exception { |
| 111 | boolean failed = false; |
| 112 | |
| 113 | List<Type> take1 = createTypes(); |
| 114 | List<Type> take2 = createTypes(); |
| 115 | |
| 116 | // Test reflexivity |
| 117 | failed = failed | testReflexes(take1); |
| 118 | failed = failed | testReflexes(take2); |
| 119 | |
| 120 | for(int i = 0; i < take1.size(); i++) { |
| 121 | Type type1 = take1.get(i); |
| 122 | |
| 123 | for(int j = 0; j < take2.size(); j++) { |
| 124 | Type type2 = take2.get(j); |
| 125 | |
| 126 | if (i == j) { |
| 127 | // corresponding types should be .equals |
| 128 | if (!type1.equals(type2) ) { |
| 129 | failed = true; |
| 130 | System.err.printf("Unexpected inequality: [%d, %d] %n\t%s%n\t%s%n", |
| 131 | i, j, type1, type2); |
| 132 | } |
| 133 | } else { |
| 134 | // non-corresponding types should *not* be .equals |
| 135 | if (type1.equals(type2) ) { |
| 136 | failed = true; |
| 137 | System.err.printf("Unexpected equality: [%d, %d] %n\t%s%n\t%s%n", |
| 138 | i, j, type1, type2); |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | if (failed) |
| 146 | throw new RuntimeException("Bad equality on ParameterizedTypes"); |
| 147 | } |
| 148 | } |