|
1 | 1 | /*- |
2 | 2 | * #%L |
3 | | - * SciJava library for generic type reasoning. |
| 3 | + * Common functionality widely used across SciJava modules. |
4 | 4 | * %% |
5 | | - * Copyright (C) 2016 - 2024 SciJava developers. |
| 5 | + * Copyright (C) 2021 - 2024 SciJava developers. |
6 | 6 | * %% |
7 | 7 | * Redistribution and use in source and binary forms, with or without |
8 | 8 | * modification, are permitted provided that the following conditions are met: |
|
27 | 27 | * #L% |
28 | 28 | */ |
29 | 29 |
|
30 | | -package org.scijava.types; |
| 30 | +package org.scijava.common3; |
31 | 31 |
|
32 | 32 | import static org.junit.jupiter.api.Assertions.assertFalse; |
33 | 33 | import static org.junit.jupiter.api.Assertions.assertTrue; |
@@ -64,158 +64,142 @@ static class WeirdThing extends RecursiveThing<WeirdThing> { } |
64 | 64 |
|
65 | 65 | @Test |
66 | 66 | public void testDouble() { |
67 | | - Type t1 = new Nil<Double>() {}.type(); |
68 | | - Type t2 = new Nil<Double>() {}.type(); |
| 67 | + Type t1 = Double.class; |
| 68 | + Type t2 = Double.class; |
69 | 69 | Type superType = Types.superTypeOf(new Type[] { t1, t2 }, |
70 | 70 | false); |
71 | 71 | assertTrue(superType.equals(Double.class)); |
72 | 72 | } |
73 | 73 |
|
74 | 74 | @Test |
75 | 75 | public void testNumber() { |
76 | | - Type t1 = new Nil<Double>() {}.type(); |
77 | | - Type t2 = new Nil<Long>() {}.type(); |
| 76 | + Type t1 = Double.class; |
| 77 | + Type t2 = Long.class; |
78 | 78 | Type superType = Types.superTypeOf(new Type[] { t1, t2 }, |
79 | 79 | false); |
80 | 80 | assertTrue(superType.equals(Number.class)); |
81 | 81 | } |
82 | 82 |
|
83 | 83 | @Test |
84 | 84 | public void testObject() { |
85 | | - Type t1 = new Nil<Double>() {}.type(); |
86 | | - Type t2 = new Nil<String>() {}.type(); |
| 85 | + Type t1 = Double.class; |
| 86 | + Type t2 = String.class; |
87 | 87 | Type superType = Types.superTypeOf(new Type[] { t1, t2 }, |
88 | 88 | false); |
89 | | - Type expected = new Nil<Comparable<?>>() {}.type(); |
| 89 | + Type expected = Types.parameterize(Comparable.class, new Type[] { Types.wildcard() }); |
90 | 90 | assertTrue(superType.equals(expected)); |
91 | 91 | } |
92 | 92 |
|
93 | 93 | @SuppressWarnings("rawtypes") |
94 | 94 | @Test |
95 | 95 | public void testListOfSame() { |
96 | | - Type t1 = new Nil<List<Double>>() {}.type(); |
97 | | - Type t2 = new Nil<List<Double>>() {}.type(); |
98 | | - Type superType = Types.superTypeOf(new Type[] { t1, t2 }, |
| 96 | + Type listOfDouble = Types.parameterize(List.class, new Type[] { Double.class }); |
| 97 | + Type superType = Types.superTypeOf(new Type[] { listOfDouble, listOfDouble }, |
99 | 98 | false); |
100 | | - assertTrue(superType.equals(new Nil<List<Double>>() {}.type())); |
101 | | - assertFalse(superType.equals(new Nil<List<Object>>() {}.type()), |
| 99 | + assertTrue(superType.equals(listOfDouble)); |
| 100 | + Type listOfObject = Types.parameterize(List.class, new Type[] { Object.class }); |
| 101 | + assertFalse(superType.equals(listOfObject), |
102 | 102 | "Class Double should take precedence over Object"); |
103 | | - assertFalse(superType.equals(new Nil<List<?>>() {}.type()), |
| 103 | + Type listOfQ = Types.parameterize(List.class, new Type[] { Types.wildcard() }); |
| 104 | + assertFalse(superType.equals(listOfQ), |
104 | 105 | "Class Double should be discernable over wildcard"); |
105 | | - assertFalse(superType.equals(new Nil<List>() {}.type()), |
| 106 | + assertFalse(superType.equals(List.class), |
106 | 107 | "Class Double should be discernable, rawtype should not be returned"); |
107 | 108 | } |
108 | 109 |
|
109 | 110 | @Test |
110 | 111 | public void testListOfDifferent() { |
111 | | - Type t1 = new Nil<List<Double>>() {}.type(); |
112 | | - Type t2 = new Nil<List<String>>() {}.type(); |
| 112 | + Type t1 = Types.parameterize(List.class, new Type[] { Double.class }); |
| 113 | + Type t2 = Types.parameterize(List.class, new Type[] { String.class }); |
113 | 114 | Type superType = Types.superTypeOf(new Type[] { t1, t2 }, |
114 | 115 | false); |
115 | | - Type expectedListType = Types.wildcard(new Type[] { |
116 | | - new Nil<Comparable<?>>() |
117 | | - {}.type() }, new Type[] {}); |
118 | | - Type expected = Types.parameterize(List.class, new Type[] { |
119 | | - expectedListType }); |
| 116 | + Type comparableQ = Types.parameterize(Comparable.class, new Type[] { Types.wildcard() }); |
| 117 | + Type expectedListType = Types.wildcard(new Type[] { comparableQ }, new Type[] {}); |
| 118 | + Type expected = Types.parameterize(List.class, new Type[] { expectedListType }); |
120 | 119 | assertTrue(superType.equals(expected)); |
121 | 120 | } |
122 | 121 |
|
123 | 122 | @Test |
124 | 123 | public void testListOfListOfDifferent() { |
125 | | - Type t1 = new Nil<List<List<Double>>>() {}.type(); |
126 | | - Type t2 = new Nil<List<List<String>>>() {}.type(); |
127 | | - Type superType = Types.superTypeOf(new Type[] { t1, t2 }, |
128 | | - false); |
129 | | - Type expectedType = Types.wildcard(new Type[] { new Nil<Comparable<?>>() {} |
130 | | - .type() }, new Type[] {}); |
131 | | - Type expectedList = Types.parameterize(List.class, new Type[] { |
132 | | - expectedType }); |
133 | | - Type expectedListType = Types.wildcard(new Type[] { expectedList }, |
134 | | - new Type[] {}); |
135 | | - Type expected = Types.parameterize(List.class, new Type[] { |
136 | | - expectedListType }); |
| 124 | + Type listDouble = Types.parameterize(List.class, new Type[] { Double.class }); |
| 125 | + Type listString = Types.parameterize(List.class, new Type[] { String.class }); |
| 126 | + Type t1 = Types.parameterize(List.class, new Type[] { listDouble }); |
| 127 | + Type t2 = Types.parameterize(List.class, new Type[] { listString }); |
| 128 | + Type superType = Types.superTypeOf(new Type[] { t1, t2 }, false); |
| 129 | + Type comparableQ = Types.parameterize(Comparable.class, new Type[] { Types.wildcard() }); |
| 130 | + Type expectedType = Types.wildcard(new Type[] { comparableQ }, new Type[] {}); |
| 131 | + Type expectedList = Types.parameterize(List.class, new Type[] { expectedType }); |
| 132 | + Type expectedListType = Types.wildcard(new Type[] { expectedList }, new Type[] {}); |
| 133 | + Type expected = Types.parameterize(List.class, new Type[] { expectedListType }); |
137 | 134 | assertTrue(superType.equals(expected)); |
138 | 135 | } |
139 | 136 |
|
140 | 137 | @Test |
141 | 138 | public void testArrayListAndList() { |
142 | | - Type t1 = new Nil<List<Double>>() {}.type(); |
143 | | - Type t2 = new Nil<ArrayList<Double>>() {}.type(); |
144 | | - Type superType = Types.superTypeOf(new Type[] { t1, t2 }, |
145 | | - false); |
146 | | - assertTrue(superType.equals(new Nil<List<Double>>() {}.type())); |
147 | | - Type t3 = new Nil<ArrayList<Double>>() {}.type(); |
148 | | - Type t4 = new Nil<List<Double>>() {}.type(); |
149 | | - Type superType2 = Types.superTypeOf(new Type[] { t3, t4 }, |
150 | | - false); |
151 | | - assertTrue(superType2.equals(new Nil<List<Double>>() {}.type())); |
| 139 | + Type t1 = Types.parameterize(List.class, new Type[] { Double.class }); |
| 140 | + Type t2 = Types.parameterize(ArrayList.class, new Type[] { Double.class }); |
| 141 | + Type superType = Types.superTypeOf(new Type[] { t1, t2 }, false); |
| 142 | + assertTrue(superType.equals(t1)); |
| 143 | + Type superType2 = Types.superTypeOf(new Type[] { t2, t1 }, false); |
| 144 | + assertTrue(superType2.equals(t1)); |
152 | 145 | } |
153 | 146 |
|
154 | 147 | @Test |
155 | 148 | public void testNThingQThing() { |
156 | | - Type t3 = new Nil<NThing>() {}.type(); |
157 | | - Type t4 = new Nil<QThing>() {}.type(); |
158 | | - Type superType = Types.superTypeOf(new Type[] { t3, t4 }, |
159 | | - false); |
160 | | - Type expected = Types.wildcard(new Type[] { new Nil<Thing>() {}.type(), |
161 | | - new Nil<Stuff>() |
162 | | - {}.type() }, new Type[] {}); |
| 149 | + Type t3 = NThing.class; |
| 150 | + Type t4 = QThing.class; |
| 151 | + Type superType = Types.superTypeOf(new Type[] { t3, t4 }, false); |
| 152 | + Type expected = Types.wildcard(new Type[] { Thing.class, Stuff.class }, new Type[] {}); |
163 | 153 | assertTrue(superType.equals(expected)); |
164 | 154 | } |
165 | 155 |
|
166 | 156 | @Test |
167 | 157 | public void testNThingYThing() { |
168 | | - Type t3 = new Nil<NThing>() {}.type(); |
169 | | - Type t4 = new Nil<YThing>() {}.type(); |
170 | | - Type superType = Types.superTypeOf(new Type[] { t3, t4 }, |
171 | | - false); |
172 | | - Type expected = Types.wildcard(new Type[] { new Nil<Thing>() {}.type() }, |
173 | | - new Type[] {}); |
| 158 | + Type t3 = NThing.class; |
| 159 | + Type t4 = YThing.class; |
| 160 | + Type superType = Types.superTypeOf(new Type[] { t3, t4 }, false); |
| 161 | + Type expected = Types.wildcard(new Type[] { Thing.class }, new Type[] {}); |
174 | 162 | assertFalse(superType.equals(expected), |
175 | 163 | "Greatest common type should not be a wildcard"); |
176 | | - assertTrue(superType.equals(new Nil<Thing>() {}.type())); |
| 164 | + assertTrue(superType.equals(Thing.class)); |
177 | 165 | } |
178 | 166 |
|
179 | 167 | @Test |
180 | 168 | public void testNThingXThing() { |
181 | | - Type t3 = new Nil<NThing>() {}.type(); |
182 | | - Type t4 = new Nil<XThing>() {}.type(); |
183 | | - Type superType = Types.superTypeOf(new Type[] { t3, t4 }, |
184 | | - false); |
185 | | - assertTrue(superType.equals(new Nil<Base>() {}.type())); |
186 | | - assertFalse(superType.equals(new Nil<Thing>() {}.type()), |
| 169 | + Type t3 = NThing.class; |
| 170 | + Type t4 = XThing.class; |
| 171 | + Type superType = Types.superTypeOf(new Type[] { t3, t4 }, false); |
| 172 | + assertTrue(superType.equals(Base.class)); |
| 173 | + assertFalse(superType.equals(Thing.class), |
187 | 174 | "Non-Object classes should take precedence over interfaces"); |
188 | 175 | } |
189 | 176 |
|
190 | 177 | @Test |
191 | 178 | public void testRecursiveClass() { |
192 | | - Type t1 = new Nil<StrangeThing>() {}.type(); |
193 | | - Type t2 = new Nil<WeirdThing>() {}.type(); |
| 179 | + Type t1 = StrangeThing.class; |
| 180 | + Type t2 = WeirdThing.class; |
194 | 181 | Type superType = Types.superTypeOf(new Type[] { t1, t2 }, |
195 | 182 | false); |
196 | | - Nil<RecursiveThing<?>> expected = new Nil<>() {}; |
197 | | - assertTrue(superType.equals(expected.type())); |
| 183 | + Type expected = Types.parameterize(RecursiveThing.class, new Type[] { Types.wildcard() }); |
| 184 | + assertTrue(superType.equals(expected)); |
198 | 185 | } |
199 | 186 |
|
200 | 187 | @Test |
201 | | - public <T extends Base> void testTypeVar() { |
202 | | - Type t1 = new Nil<T>() {}.type(); |
203 | | - Type t2 = new Nil<NThing>() {}.type(); |
204 | | - Type superType = Types.superTypeOf(new Type[] { t1, t2 }, |
205 | | - false); |
206 | | - Nil<Base> expected = new Nil<>() {}; |
207 | | - assertTrue(superType.equals(expected.type())); |
| 188 | + public void testTypeVar() { |
| 189 | + class C<T extends Base> {} |
| 190 | + Type t1 = C.class.getTypeParameters()[0]; |
| 191 | + Type t2 = NThing.class; |
| 192 | + Type superType = Types.superTypeOf(new Type[] { t1, t2 }, false); |
| 193 | + assertTrue(superType.equals(Base.class)); |
208 | 194 | } |
209 | 195 |
|
210 | 196 | @Test |
211 | 197 | public void testWildcardType() { |
212 | | - Type typeWithWildcard = new Nil<List<? extends NThing>>() {}.type(); |
213 | | - Type t1 = ((ParameterizedType) typeWithWildcard) |
214 | | - .getActualTypeArguments()[0]; |
215 | | - Type t2 = new Nil<XThing>() {}.type(); |
216 | | - Type superType = Types.superTypeOf(new Type[] { t1, t2 }, |
217 | | - false); |
218 | | - Nil<Base> expected = new Nil<>() {}; |
219 | | - assertTrue(superType.equals(expected.type())); |
| 198 | + Type qNThing = Types.wildcard(NThing.class, null); |
| 199 | + Type typeWithWildcard = Types.parameterize(List.class, new Type[] { qNThing }); |
| 200 | + Type t1 = ((ParameterizedType) typeWithWildcard).getActualTypeArguments()[0]; |
| 201 | + Type t2 = XThing.class; |
| 202 | + Type superType = Types.superTypeOf(new Type[] { t1, t2 }, false); |
| 203 | + assertTrue(superType.equals(Base.class)); |
220 | 204 | } |
221 | 205 | } |
0 commit comments