Added a description to the patch
Emmanuel Bourg
8 years ago
0 | Index: jtype/src/main/java/com/googlecode/jtype/DefaultTypeVariable.java | |
1 | =================================================================== | |
2 | --- jtype.orig/src/main/java/com/googlecode/jtype/DefaultTypeVariable.java | |
3 | +++ jtype/src/main/java/com/googlecode/jtype/DefaultTypeVariable.java | |
4 | @@ -34,7 +34,7 @@ import java.util.Arrays; | |
0 | Description: Use guava's dynamic proxy solution to allow compilation on Java 8 | |
1 | Author: Chris West <git@goeswhere.com> | |
2 | Bug: https://github.com/markhobson/jtype/pull/3 | |
3 | Bug-Debian: https://bugs.debian.org/751526 | |
4 | --- a/src/main/java/com/googlecode/jtype/DefaultTypeVariable.java | |
5 | +++ b/src/main/java/com/googlecode/jtype/DefaultTypeVariable.java | |
6 | @@ -34,7 +34,7 @@ | |
5 | 7 | * the type of generic declaration that declared the type variable |
6 | 8 | * @see TypeVariable |
7 | 9 | */ |
10 | 12 | { |
11 | 13 | // constants -------------------------------------------------------------- |
12 | 14 | |
13 | @@ -154,16 +154,7 @@ class DefaultTypeVariable<D extends Gene | |
15 | @@ -154,16 +154,7 @@ | |
14 | 16 | && name.equals(typeVariable.getName()) |
15 | 17 | && Arrays.equals(bounds, typeVariable.getBounds()); |
16 | 18 | } |
28 | 30 | // private methods -------------------------------------------------------- |
29 | 31 | |
30 | 32 | private static boolean isValidFirstBound(Type bound) |
31 | Index: jtype/src/main/java/com/googlecode/jtype/Types.java | |
32 | =================================================================== | |
33 | --- jtype.orig/src/main/java/com/googlecode/jtype/Types.java | |
34 | +++ jtype/src/main/java/com/googlecode/jtype/Types.java | |
35 | @@ -17,10 +17,15 @@ package com.googlecode.jtype; | |
33 | --- a/src/main/java/com/googlecode/jtype/Types.java | |
34 | +++ b/src/main/java/com/googlecode/jtype/Types.java | |
35 | @@ -17,10 +17,15 @@ | |
36 | 36 | |
37 | 37 | import static com.googlecode.jtype.Utils.checkNotNull; |
38 | 38 | |
48 | 48 | import java.lang.reflect.Type; |
49 | 49 | import java.lang.reflect.TypeVariable; |
50 | 50 | import java.lang.reflect.WildcardType; |
51 | @@ -76,7 +81,10 @@ public final class Types | |
51 | @@ -76,7 +81,10 @@ | |
52 | 52 | public static <D extends GenericDeclaration> TypeVariable<D> typeVariable(D declaration, String name, |
53 | 53 | Type... bounds) |
54 | 54 | { |
60 | 60 | } |
61 | 61 | |
62 | 62 | /** |
63 | @@ -347,4 +355,40 @@ public final class Types | |
63 | @@ -347,4 +355,40 @@ | |
64 | 64 | |
65 | 65 | return wildcardType(upperBounds, lowerBounds); |
66 | 66 | } |
101 | 101 | + } |
102 | 102 | + } |
103 | 103 | } |
104 | Index: jtype/src/test/java/com/googlecode/jtype/DefaultTypeVariableTest.java | |
105 | =================================================================== | |
106 | --- jtype.orig/src/test/java/com/googlecode/jtype/DefaultTypeVariableTest.java | |
107 | +++ jtype/src/test/java/com/googlecode/jtype/DefaultTypeVariableTest.java | |
108 | @@ -61,7 +61,7 @@ public class DefaultTypeVariableTest | |
104 | --- a/src/test/java/com/googlecode/jtype/DefaultTypeVariableTest.java | |
105 | +++ b/src/test/java/com/googlecode/jtype/DefaultTypeVariableTest.java | |
106 | @@ -61,7 +61,7 @@ | |
109 | 107 | { |
110 | 108 | try |
111 | 109 | { |
114 | 112 | } |
115 | 113 | catch (NullPointerException exception) |
116 | 114 | { |
117 | @@ -76,7 +76,7 @@ public class DefaultTypeVariableTest | |
115 | @@ -76,7 +76,7 @@ | |
118 | 116 | { |
119 | 117 | try |
120 | 118 | { |
123 | 121 | } |
124 | 122 | catch (NullPointerException exception) |
125 | 123 | { |
126 | @@ -173,7 +173,7 @@ public class DefaultTypeVariableTest | |
124 | @@ -173,7 +173,7 @@ | |
127 | 125 | @Test |
128 | 126 | public void constructorWithNullBounds() |
129 | 127 | { |
132 | 130 | |
133 | 131 | assertEquals(constructor, typeVariable.getGenericDeclaration()); |
134 | 132 | assertEquals("T", typeVariable.getName()); |
135 | @@ -183,7 +183,7 @@ public class DefaultTypeVariableTest | |
133 | @@ -183,7 +183,7 @@ | |
136 | 134 | @Test |
137 | 135 | public void constructorWithEmptyBounds() |
138 | 136 | { |
141 | 139 | new Type[0]); |
142 | 140 | |
143 | 141 | assertEquals(constructor, typeVariable.getGenericDeclaration()); |
144 | @@ -194,10 +194,10 @@ public class DefaultTypeVariableTest | |
142 | @@ -194,10 +194,10 @@ | |
145 | 143 | @Test |
146 | 144 | public void hashCodeTest() |
147 | 145 | { |
154 | 152 | new Type[] {Number.class, Comparable.class}); |
155 | 153 | |
156 | 154 | assertEquals(typeVariable1.hashCode(), typeVariable2.hashCode()); |
157 | @@ -206,10 +206,10 @@ public class DefaultTypeVariableTest | |
155 | @@ -206,10 +206,10 @@ | |
158 | 156 | @Test |
159 | 157 | public void equalsWhenEqual() |
160 | 158 | { |
167 | 165 | new Type[] {Number.class, Comparable.class}); |
168 | 166 | |
169 | 167 | assertEquals(typeVariable1, typeVariable2); |
170 | @@ -218,7 +218,7 @@ public class DefaultTypeVariableTest | |
168 | @@ -218,7 +218,7 @@ | |
171 | 169 | @Test |
172 | 170 | public void equalsWithDifferentClass() |
173 | 171 | { |
176 | 174 | |
177 | 175 | assertFalse(typeVariable.equals(new Object())); |
178 | 176 | } |
179 | @@ -226,12 +226,12 @@ public class DefaultTypeVariableTest | |
177 | @@ -226,12 +226,12 @@ | |
180 | 178 | @Test |
181 | 179 | public void equalsWithDifferentDeclarations() throws NoSuchMethodException |
182 | 180 | { |
191 | 189 | |
192 | 190 | assertFalse(typeVariable1.equals(typeVariable2)); |
193 | 191 | } |
194 | @@ -239,10 +239,10 @@ public class DefaultTypeVariableTest | |
192 | @@ -239,10 +239,10 @@ | |
195 | 193 | @Test |
196 | 194 | public void equalsWithDifferentNames() |
197 | 195 | { |
204 | 202 | new Type[] {Number.class}); |
205 | 203 | |
206 | 204 | assertFalse(typeVariable1.equals(typeVariable2)); |
207 | @@ -251,10 +251,10 @@ public class DefaultTypeVariableTest | |
205 | @@ -251,10 +251,10 @@ | |
208 | 206 | @Test |
209 | 207 | public void equalsWithDifferentBounds() |
210 | 208 | { |
217 | 215 | new Type[] {Integer.class}); |
218 | 216 | |
219 | 217 | assertFalse(typeVariable1.equals(typeVariable2)); |
220 | @@ -263,7 +263,7 @@ public class DefaultTypeVariableTest | |
218 | @@ -263,7 +263,7 @@ | |
221 | 219 | @Test |
222 | 220 | public void toStringWithNoBounds() |
223 | 221 | { |
226 | 224 | |
227 | 225 | assertEquals("T", typeVariable.toString()); |
228 | 226 | } |
229 | @@ -271,7 +271,7 @@ public class DefaultTypeVariableTest | |
227 | @@ -271,7 +271,7 @@ | |
230 | 228 | @Test |
231 | 229 | public void toStringWithSingleBound() |
232 | 230 | { |
235 | 233 | new Type[] {Number.class}); |
236 | 234 | |
237 | 235 | assertEquals("T extends java.lang.Number", typeVariable.toString()); |
238 | @@ -280,7 +280,7 @@ public class DefaultTypeVariableTest | |
236 | @@ -280,7 +280,7 @@ | |
239 | 237 | @Test |
240 | 238 | public void toStringWithMultipleBounds() |
241 | 239 | { |
244 | 242 | new Type[] {Number.class, Comparable.class}); |
245 | 243 | |
246 | 244 | assertEquals("T extends java.lang.Number & java.lang.Comparable", typeVariable.toString()); |
247 | @@ -289,7 +289,7 @@ public class DefaultTypeVariableTest | |
245 | @@ -289,7 +289,7 @@ | |
248 | 246 | @Test |
249 | 247 | public void serializable() throws IOException, ClassNotFoundException |
250 | 248 | { |
253 | 251 | new Type[] {Number.class, Comparable.class}); |
254 | 252 | |
255 | 253 | assertSerializable(type); |
256 | @@ -299,7 +299,7 @@ public class DefaultTypeVariableTest | |
254 | @@ -299,7 +299,7 @@ | |
257 | 255 | |
258 | 256 | private static <D extends GenericDeclaration> void assertConstructor(D declaration, String name, Type... bounds) |
259 | 257 | { |
33 | 33 | * the type of generic declaration that declared the type variable |
34 | 34 | * @see TypeVariable |
35 | 35 | */ |
36 | class DefaultTypeVariable<D extends GenericDeclaration> implements TypeVariable<D>, Serializable | |
36 | class DefaultTypeVariable<D extends GenericDeclaration> implements Type, Serializable | |
37 | 37 | { |
38 | 38 | // constants -------------------------------------------------------------- |
39 | 39 | |
153 | 153 | && name.equals(typeVariable.getName()) |
154 | 154 | && Arrays.equals(bounds, typeVariable.getBounds()); |
155 | 155 | } |
156 | ||
157 | /** | |
158 | * {@inheritDoc} | |
159 | */ | |
160 | @Override | |
161 | public String toString() | |
162 | { | |
163 | return TypeUtils.toString(this); | |
164 | } | |
165 | ||
156 | ||
166 | 157 | // private methods -------------------------------------------------------- |
167 | 158 | |
168 | 159 | private static boolean isValidFirstBound(Type bound) |
16 | 16 | |
17 | 17 | import static com.googlecode.jtype.Utils.checkNotNull; |
18 | 18 | |
19 | import java.io.Serializable; | |
19 | 20 | import java.lang.reflect.GenericArrayType; |
20 | 21 | import java.lang.reflect.GenericDeclaration; |
21 | 22 | import java.lang.reflect.MalformedParameterizedTypeException; |
23 | import java.lang.reflect.InvocationHandler; | |
24 | import java.lang.reflect.InvocationTargetException; | |
22 | 25 | import java.lang.reflect.ParameterizedType; |
26 | import java.lang.reflect.Method; | |
27 | import java.lang.reflect.Proxy; | |
23 | 28 | import java.lang.reflect.Type; |
24 | 29 | import java.lang.reflect.TypeVariable; |
25 | 30 | import java.lang.reflect.WildcardType; |
75 | 80 | public static <D extends GenericDeclaration> TypeVariable<D> typeVariable(D declaration, String name, |
76 | 81 | Type... bounds) |
77 | 82 | { |
78 | return new DefaultTypeVariable<D>(declaration, name, bounds); | |
83 | return (TypeVariable<D>) Proxy.newProxyInstance( | |
84 | Types.class.getClassLoader(), | |
85 | new Class[]{TypeVariable.class}, | |
86 | new TypeVariableInvocationHandler(new DefaultTypeVariable<D>(declaration, name, bounds))); | |
79 | 87 | } |
80 | 88 | |
81 | 89 | /** |
346 | 354 | |
347 | 355 | return wildcardType(upperBounds, lowerBounds); |
348 | 356 | } |
357 | ||
358 | private static class TypeVariableInvocationHandler implements InvocationHandler, Serializable { | |
359 | private static final Map<String, Method> typeVariableMethods = new HashMap<String, Method>(); | |
360 | static { | |
361 | for (Method method : DefaultTypeVariable.class.getMethods()) { | |
362 | if (method.getDeclaringClass().equals(DefaultTypeVariable.class)) { | |
363 | typeVariableMethods.put(method.getName(), method); | |
364 | } | |
365 | } | |
366 | } | |
367 | ||
368 | private final DefaultTypeVariable<?> typeVariable; | |
369 | ||
370 | public TypeVariableInvocationHandler(DefaultTypeVariable<?> typeVariable) { | |
371 | this.typeVariable = typeVariable; | |
372 | } | |
373 | ||
374 | public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { | |
375 | String methodName = method.getName(); | |
376 | ||
377 | if ("toString".equals(methodName)) { | |
378 | return TypeUtils.toString((Type) proxy); | |
379 | } | |
380 | ||
381 | Method typeVariableMethod = typeVariableMethods.get(methodName); | |
382 | if (typeVariableMethod == null) { | |
383 | throw new UnsupportedOperationException(methodName); | |
384 | } else { | |
385 | try { | |
386 | return typeVariableMethod.invoke(typeVariable, args); | |
387 | } catch (InvocationTargetException e) { | |
388 | throw e.getCause(); | |
389 | } | |
390 | } | |
391 | } | |
392 | } | |
349 | 393 | } |
60 | 60 | { |
61 | 61 | try |
62 | 62 | { |
63 | new DefaultTypeVariable<Constructor<?>>(null, "T", new Type[] {Number.class}); | |
63 | Types.<Constructor<?>>typeVariable(null, "T", new Type[] {Number.class}); | |
64 | 64 | } |
65 | 65 | catch (NullPointerException exception) |
66 | 66 | { |
75 | 75 | { |
76 | 76 | try |
77 | 77 | { |
78 | new DefaultTypeVariable<Constructor<?>>(constructor, null, new Type[] {Number.class}); | |
78 | Types.<Constructor<?>>typeVariable(constructor, null, new Type[] {Number.class}); | |
79 | 79 | } |
80 | 80 | catch (NullPointerException exception) |
81 | 81 | { |
172 | 172 | @Test |
173 | 173 | public void constructorWithNullBounds() |
174 | 174 | { |
175 | TypeVariable<Constructor<?>> typeVariable = new DefaultTypeVariable<Constructor<?>>(constructor, "T", null); | |
175 | TypeVariable<Constructor<?>> typeVariable = Types.<Constructor<?>>typeVariable(constructor, "T", null); | |
176 | 176 | |
177 | 177 | assertEquals(constructor, typeVariable.getGenericDeclaration()); |
178 | 178 | assertEquals("T", typeVariable.getName()); |
182 | 182 | @Test |
183 | 183 | public void constructorWithEmptyBounds() |
184 | 184 | { |
185 | TypeVariable<Constructor<?>> typeVariable = new DefaultTypeVariable<Constructor<?>>(constructor, "T", | |
185 | TypeVariable<Constructor<?>> typeVariable = Types.<Constructor<?>>typeVariable(constructor, "T", | |
186 | 186 | new Type[0]); |
187 | 187 | |
188 | 188 | assertEquals(constructor, typeVariable.getGenericDeclaration()); |
193 | 193 | @Test |
194 | 194 | public void hashCodeTest() |
195 | 195 | { |
196 | TypeVariable<Constructor<?>> typeVariable1 = new DefaultTypeVariable<Constructor<?>>(constructor, "T", | |
197 | new Type[] {Number.class, Comparable.class}); | |
198 | ||
199 | TypeVariable<Constructor<?>> typeVariable2 = new DefaultTypeVariable<Constructor<?>>(constructor, "T", | |
196 | TypeVariable<Constructor<?>> typeVariable1 = Types.<Constructor<?>>typeVariable(constructor, "T", | |
197 | new Type[] {Number.class, Comparable.class}); | |
198 | ||
199 | TypeVariable<Constructor<?>> typeVariable2 = Types.<Constructor<?>>typeVariable(constructor, "T", | |
200 | 200 | new Type[] {Number.class, Comparable.class}); |
201 | 201 | |
202 | 202 | assertEquals(typeVariable1.hashCode(), typeVariable2.hashCode()); |
205 | 205 | @Test |
206 | 206 | public void equalsWhenEqual() |
207 | 207 | { |
208 | TypeVariable<Constructor<?>> typeVariable1 = new DefaultTypeVariable<Constructor<?>>(constructor, "T", | |
209 | new Type[] {Number.class, Comparable.class}); | |
210 | ||
211 | TypeVariable<Constructor<?>> typeVariable2 = new DefaultTypeVariable<Constructor<?>>(constructor, "T", | |
208 | TypeVariable<Constructor<?>> typeVariable1 = Types.<Constructor<?>>typeVariable(constructor, "T", | |
209 | new Type[] {Number.class, Comparable.class}); | |
210 | ||
211 | TypeVariable<Constructor<?>> typeVariable2 = Types.<Constructor<?>>typeVariable(constructor, "T", | |
212 | 212 | new Type[] {Number.class, Comparable.class}); |
213 | 213 | |
214 | 214 | assertEquals(typeVariable1, typeVariable2); |
217 | 217 | @Test |
218 | 218 | public void equalsWithDifferentClass() |
219 | 219 | { |
220 | TypeVariable<Constructor<?>> typeVariable = new DefaultTypeVariable<Constructor<?>>(constructor, "T", null); | |
220 | TypeVariable<Constructor<?>> typeVariable = Types.<Constructor<?>>typeVariable(constructor, "T", null); | |
221 | 221 | |
222 | 222 | assertFalse(typeVariable.equals(new Object())); |
223 | 223 | } |
225 | 225 | @Test |
226 | 226 | public void equalsWithDifferentDeclarations() throws NoSuchMethodException |
227 | 227 | { |
228 | TypeVariable<Constructor<?>> typeVariable1 = new DefaultTypeVariable<Constructor<?>>(constructor, "T", | |
228 | TypeVariable<Constructor<?>> typeVariable1 = Types.<Constructor<?>>typeVariable(constructor, "T", | |
229 | 229 | new Type[] {Number.class}); |
230 | 230 | |
231 | 231 | Method method = getClass().getDeclaredMethod("equalsWithDifferentDeclarations"); |
232 | 232 | |
233 | TypeVariable<Method> typeVariable2 = new DefaultTypeVariable<Method>(method, "T", new Type[] {Number.class}); | |
233 | TypeVariable<Method> typeVariable2 = Types.<Method>typeVariable(method, "T", new Type[] {Number.class}); | |
234 | 234 | |
235 | 235 | assertFalse(typeVariable1.equals(typeVariable2)); |
236 | 236 | } |
238 | 238 | @Test |
239 | 239 | public void equalsWithDifferentNames() |
240 | 240 | { |
241 | TypeVariable<Constructor<?>> typeVariable1 = new DefaultTypeVariable<Constructor<?>>(constructor, "T", | |
242 | new Type[] {Number.class}); | |
243 | ||
244 | TypeVariable<Constructor<?>> typeVariable2 = new DefaultTypeVariable<Constructor<?>>(constructor, "U", | |
241 | TypeVariable<Constructor<?>> typeVariable1 = Types.<Constructor<?>>typeVariable(constructor, "T", | |
242 | new Type[] {Number.class}); | |
243 | ||
244 | TypeVariable<Constructor<?>> typeVariable2 = Types.<Constructor<?>>typeVariable(constructor, "U", | |
245 | 245 | new Type[] {Number.class}); |
246 | 246 | |
247 | 247 | assertFalse(typeVariable1.equals(typeVariable2)); |
250 | 250 | @Test |
251 | 251 | public void equalsWithDifferentBounds() |
252 | 252 | { |
253 | TypeVariable<Constructor<?>> typeVariable1 = new DefaultTypeVariable<Constructor<?>>(constructor, "T", | |
254 | new Type[] {Number.class}); | |
255 | ||
256 | TypeVariable<Constructor<?>> typeVariable2 = new DefaultTypeVariable<Constructor<?>>(constructor, "T", | |
253 | TypeVariable<Constructor<?>> typeVariable1 = Types.<Constructor<?>>typeVariable(constructor, "T", | |
254 | new Type[] {Number.class}); | |
255 | ||
256 | TypeVariable<Constructor<?>> typeVariable2 = Types.<Constructor<?>>typeVariable(constructor, "T", | |
257 | 257 | new Type[] {Integer.class}); |
258 | 258 | |
259 | 259 | assertFalse(typeVariable1.equals(typeVariable2)); |
262 | 262 | @Test |
263 | 263 | public void toStringWithNoBounds() |
264 | 264 | { |
265 | TypeVariable<Constructor<?>> typeVariable = new DefaultTypeVariable<Constructor<?>>(constructor, "T", null); | |
265 | TypeVariable<Constructor<?>> typeVariable = Types.<Constructor<?>>typeVariable(constructor, "T", null); | |
266 | 266 | |
267 | 267 | assertEquals("T", typeVariable.toString()); |
268 | 268 | } |
270 | 270 | @Test |
271 | 271 | public void toStringWithSingleBound() |
272 | 272 | { |
273 | TypeVariable<Constructor<?>> typeVariable = new DefaultTypeVariable<Constructor<?>>(constructor, "T", | |
273 | TypeVariable<Constructor<?>> typeVariable = Types.<Constructor<?>>typeVariable(constructor, "T", | |
274 | 274 | new Type[] {Number.class}); |
275 | 275 | |
276 | 276 | assertEquals("T extends java.lang.Number", typeVariable.toString()); |
279 | 279 | @Test |
280 | 280 | public void toStringWithMultipleBounds() |
281 | 281 | { |
282 | TypeVariable<Constructor<?>> typeVariable = new DefaultTypeVariable<Constructor<?>>(constructor, "T", | |
282 | TypeVariable<Constructor<?>> typeVariable = Types.<Constructor<?>>typeVariable(constructor, "T", | |
283 | 283 | new Type[] {Number.class, Comparable.class}); |
284 | 284 | |
285 | 285 | assertEquals("T extends java.lang.Number & java.lang.Comparable", typeVariable.toString()); |
288 | 288 | @Test |
289 | 289 | public void serializable() throws IOException, ClassNotFoundException |
290 | 290 | { |
291 | TypeVariable<Class<?>> type = new DefaultTypeVariable<Class<?>>(getClass(), "T", | |
291 | TypeVariable<Class<?>> type = Types.<Class<?>>typeVariable(getClass(), "T", | |
292 | 292 | new Type[] {Number.class, Comparable.class}); |
293 | 293 | |
294 | 294 | assertSerializable(type); |
298 | 298 | |
299 | 299 | private static <D extends GenericDeclaration> void assertConstructor(D declaration, String name, Type... bounds) |
300 | 300 | { |
301 | TypeVariable<D> typeVariable = new DefaultTypeVariable<D>(declaration, name, bounds); | |
301 | TypeVariable<D> typeVariable = Types.<D>typeVariable(declaration, name, bounds); | |
302 | 302 | |
303 | 303 | assertEquals("Generic declaration", declaration, typeVariable.getGenericDeclaration()); |
304 | 304 | assertEquals("Name", name, typeVariable.getName()); |