Codebase list libjtype-java / 45771b7
Added a description to the patch Emmanuel Bourg 8 years ago
4 changed file(s) with 105 addition(s) and 72 deletion(s). Raw diff Collapse all Expand all
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 @@
57 * the type of generic declaration that declared the type variable
68 * @see TypeVariable
79 */
1012 {
1113 // constants --------------------------------------------------------------
1214
13 @@ -154,16 +154,7 @@ class DefaultTypeVariable<D extends Gene
15 @@ -154,16 +154,7 @@
1416 && name.equals(typeVariable.getName())
1517 && Arrays.equals(bounds, typeVariable.getBounds());
1618 }
2830 // private methods --------------------------------------------------------
2931
3032 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 @@
3636
3737 import static com.googlecode.jtype.Utils.checkNotNull;
3838
4848 import java.lang.reflect.Type;
4949 import java.lang.reflect.TypeVariable;
5050 import java.lang.reflect.WildcardType;
51 @@ -76,7 +81,10 @@ public final class Types
51 @@ -76,7 +81,10 @@
5252 public static <D extends GenericDeclaration> TypeVariable<D> typeVariable(D declaration, String name,
5353 Type... bounds)
5454 {
6060 }
6161
6262 /**
63 @@ -347,4 +355,40 @@ public final class Types
63 @@ -347,4 +355,40 @@
6464
6565 return wildcardType(upperBounds, lowerBounds);
6666 }
101101 + }
102102 + }
103103 }
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 @@
109107 {
110108 try
111109 {
114112 }
115113 catch (NullPointerException exception)
116114 {
117 @@ -76,7 +76,7 @@ public class DefaultTypeVariableTest
115 @@ -76,7 +76,7 @@
118116 {
119117 try
120118 {
123121 }
124122 catch (NullPointerException exception)
125123 {
126 @@ -173,7 +173,7 @@ public class DefaultTypeVariableTest
124 @@ -173,7 +173,7 @@
127125 @Test
128126 public void constructorWithNullBounds()
129127 {
132130
133131 assertEquals(constructor, typeVariable.getGenericDeclaration());
134132 assertEquals("T", typeVariable.getName());
135 @@ -183,7 +183,7 @@ public class DefaultTypeVariableTest
133 @@ -183,7 +183,7 @@
136134 @Test
137135 public void constructorWithEmptyBounds()
138136 {
141139 new Type[0]);
142140
143141 assertEquals(constructor, typeVariable.getGenericDeclaration());
144 @@ -194,10 +194,10 @@ public class DefaultTypeVariableTest
142 @@ -194,10 +194,10 @@
145143 @Test
146144 public void hashCodeTest()
147145 {
154152 new Type[] {Number.class, Comparable.class});
155153
156154 assertEquals(typeVariable1.hashCode(), typeVariable2.hashCode());
157 @@ -206,10 +206,10 @@ public class DefaultTypeVariableTest
155 @@ -206,10 +206,10 @@
158156 @Test
159157 public void equalsWhenEqual()
160158 {
167165 new Type[] {Number.class, Comparable.class});
168166
169167 assertEquals(typeVariable1, typeVariable2);
170 @@ -218,7 +218,7 @@ public class DefaultTypeVariableTest
168 @@ -218,7 +218,7 @@
171169 @Test
172170 public void equalsWithDifferentClass()
173171 {
176174
177175 assertFalse(typeVariable.equals(new Object()));
178176 }
179 @@ -226,12 +226,12 @@ public class DefaultTypeVariableTest
177 @@ -226,12 +226,12 @@
180178 @Test
181179 public void equalsWithDifferentDeclarations() throws NoSuchMethodException
182180 {
191189
192190 assertFalse(typeVariable1.equals(typeVariable2));
193191 }
194 @@ -239,10 +239,10 @@ public class DefaultTypeVariableTest
192 @@ -239,10 +239,10 @@
195193 @Test
196194 public void equalsWithDifferentNames()
197195 {
204202 new Type[] {Number.class});
205203
206204 assertFalse(typeVariable1.equals(typeVariable2));
207 @@ -251,10 +251,10 @@ public class DefaultTypeVariableTest
205 @@ -251,10 +251,10 @@
208206 @Test
209207 public void equalsWithDifferentBounds()
210208 {
217215 new Type[] {Integer.class});
218216
219217 assertFalse(typeVariable1.equals(typeVariable2));
220 @@ -263,7 +263,7 @@ public class DefaultTypeVariableTest
218 @@ -263,7 +263,7 @@
221219 @Test
222220 public void toStringWithNoBounds()
223221 {
226224
227225 assertEquals("T", typeVariable.toString());
228226 }
229 @@ -271,7 +271,7 @@ public class DefaultTypeVariableTest
227 @@ -271,7 +271,7 @@
230228 @Test
231229 public void toStringWithSingleBound()
232230 {
235233 new Type[] {Number.class});
236234
237235 assertEquals("T extends java.lang.Number", typeVariable.toString());
238 @@ -280,7 +280,7 @@ public class DefaultTypeVariableTest
236 @@ -280,7 +280,7 @@
239237 @Test
240238 public void toStringWithMultipleBounds()
241239 {
244242 new Type[] {Number.class, Comparable.class});
245243
246244 assertEquals("T extends java.lang.Number & java.lang.Comparable", typeVariable.toString());
247 @@ -289,7 +289,7 @@ public class DefaultTypeVariableTest
245 @@ -289,7 +289,7 @@
248246 @Test
249247 public void serializable() throws IOException, ClassNotFoundException
250248 {
253251 new Type[] {Number.class, Comparable.class});
254252
255253 assertSerializable(type);
256 @@ -299,7 +299,7 @@ public class DefaultTypeVariableTest
254 @@ -299,7 +299,7 @@
257255
258256 private static <D extends GenericDeclaration> void assertConstructor(D declaration, String name, Type... bounds)
259257 {
3333 * the type of generic declaration that declared the type variable
3434 * @see TypeVariable
3535 */
36 class DefaultTypeVariable<D extends GenericDeclaration> implements TypeVariable<D>, Serializable
36 class DefaultTypeVariable<D extends GenericDeclaration> implements Type, Serializable
3737 {
3838 // constants --------------------------------------------------------------
3939
153153 && name.equals(typeVariable.getName())
154154 && Arrays.equals(bounds, typeVariable.getBounds());
155155 }
156
157 /**
158 * {@inheritDoc}
159 */
160 @Override
161 public String toString()
162 {
163 return TypeUtils.toString(this);
164 }
165
156
166157 // private methods --------------------------------------------------------
167158
168159 private static boolean isValidFirstBound(Type bound)
1616
1717 import static com.googlecode.jtype.Utils.checkNotNull;
1818
19 import java.io.Serializable;
1920 import java.lang.reflect.GenericArrayType;
2021 import java.lang.reflect.GenericDeclaration;
2122 import java.lang.reflect.MalformedParameterizedTypeException;
23 import java.lang.reflect.InvocationHandler;
24 import java.lang.reflect.InvocationTargetException;
2225 import java.lang.reflect.ParameterizedType;
26 import java.lang.reflect.Method;
27 import java.lang.reflect.Proxy;
2328 import java.lang.reflect.Type;
2429 import java.lang.reflect.TypeVariable;
2530 import java.lang.reflect.WildcardType;
7580 public static <D extends GenericDeclaration> TypeVariable<D> typeVariable(D declaration, String name,
7681 Type... bounds)
7782 {
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)));
7987 }
8088
8189 /**
346354
347355 return wildcardType(upperBounds, lowerBounds);
348356 }
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 }
349393 }
6060 {
6161 try
6262 {
63 new DefaultTypeVariable<Constructor<?>>(null, "T", new Type[] {Number.class});
63 Types.<Constructor<?>>typeVariable(null, "T", new Type[] {Number.class});
6464 }
6565 catch (NullPointerException exception)
6666 {
7575 {
7676 try
7777 {
78 new DefaultTypeVariable<Constructor<?>>(constructor, null, new Type[] {Number.class});
78 Types.<Constructor<?>>typeVariable(constructor, null, new Type[] {Number.class});
7979 }
8080 catch (NullPointerException exception)
8181 {
172172 @Test
173173 public void constructorWithNullBounds()
174174 {
175 TypeVariable<Constructor<?>> typeVariable = new DefaultTypeVariable<Constructor<?>>(constructor, "T", null);
175 TypeVariable<Constructor<?>> typeVariable = Types.<Constructor<?>>typeVariable(constructor, "T", null);
176176
177177 assertEquals(constructor, typeVariable.getGenericDeclaration());
178178 assertEquals("T", typeVariable.getName());
182182 @Test
183183 public void constructorWithEmptyBounds()
184184 {
185 TypeVariable<Constructor<?>> typeVariable = new DefaultTypeVariable<Constructor<?>>(constructor, "T",
185 TypeVariable<Constructor<?>> typeVariable = Types.<Constructor<?>>typeVariable(constructor, "T",
186186 new Type[0]);
187187
188188 assertEquals(constructor, typeVariable.getGenericDeclaration());
193193 @Test
194194 public void hashCodeTest()
195195 {
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",
200200 new Type[] {Number.class, Comparable.class});
201201
202202 assertEquals(typeVariable1.hashCode(), typeVariable2.hashCode());
205205 @Test
206206 public void equalsWhenEqual()
207207 {
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",
212212 new Type[] {Number.class, Comparable.class});
213213
214214 assertEquals(typeVariable1, typeVariable2);
217217 @Test
218218 public void equalsWithDifferentClass()
219219 {
220 TypeVariable<Constructor<?>> typeVariable = new DefaultTypeVariable<Constructor<?>>(constructor, "T", null);
220 TypeVariable<Constructor<?>> typeVariable = Types.<Constructor<?>>typeVariable(constructor, "T", null);
221221
222222 assertFalse(typeVariable.equals(new Object()));
223223 }
225225 @Test
226226 public void equalsWithDifferentDeclarations() throws NoSuchMethodException
227227 {
228 TypeVariable<Constructor<?>> typeVariable1 = new DefaultTypeVariable<Constructor<?>>(constructor, "T",
228 TypeVariable<Constructor<?>> typeVariable1 = Types.<Constructor<?>>typeVariable(constructor, "T",
229229 new Type[] {Number.class});
230230
231231 Method method = getClass().getDeclaredMethod("equalsWithDifferentDeclarations");
232232
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});
234234
235235 assertFalse(typeVariable1.equals(typeVariable2));
236236 }
238238 @Test
239239 public void equalsWithDifferentNames()
240240 {
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",
245245 new Type[] {Number.class});
246246
247247 assertFalse(typeVariable1.equals(typeVariable2));
250250 @Test
251251 public void equalsWithDifferentBounds()
252252 {
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",
257257 new Type[] {Integer.class});
258258
259259 assertFalse(typeVariable1.equals(typeVariable2));
262262 @Test
263263 public void toStringWithNoBounds()
264264 {
265 TypeVariable<Constructor<?>> typeVariable = new DefaultTypeVariable<Constructor<?>>(constructor, "T", null);
265 TypeVariable<Constructor<?>> typeVariable = Types.<Constructor<?>>typeVariable(constructor, "T", null);
266266
267267 assertEquals("T", typeVariable.toString());
268268 }
270270 @Test
271271 public void toStringWithSingleBound()
272272 {
273 TypeVariable<Constructor<?>> typeVariable = new DefaultTypeVariable<Constructor<?>>(constructor, "T",
273 TypeVariable<Constructor<?>> typeVariable = Types.<Constructor<?>>typeVariable(constructor, "T",
274274 new Type[] {Number.class});
275275
276276 assertEquals("T extends java.lang.Number", typeVariable.toString());
279279 @Test
280280 public void toStringWithMultipleBounds()
281281 {
282 TypeVariable<Constructor<?>> typeVariable = new DefaultTypeVariable<Constructor<?>>(constructor, "T",
282 TypeVariable<Constructor<?>> typeVariable = Types.<Constructor<?>>typeVariable(constructor, "T",
283283 new Type[] {Number.class, Comparable.class});
284284
285285 assertEquals("T extends java.lang.Number & java.lang.Comparable", typeVariable.toString());
288288 @Test
289289 public void serializable() throws IOException, ClassNotFoundException
290290 {
291 TypeVariable<Class<?>> type = new DefaultTypeVariable<Class<?>>(getClass(), "T",
291 TypeVariable<Class<?>> type = Types.<Class<?>>typeVariable(getClass(), "T",
292292 new Type[] {Number.class, Comparable.class});
293293
294294 assertSerializable(type);
298298
299299 private static <D extends GenericDeclaration> void assertConstructor(D declaration, String name, Type... bounds)
300300 {
301 TypeVariable<D> typeVariable = new DefaultTypeVariable<D>(declaration, name, bounds);
301 TypeVariable<D> typeVariable = Types.<D>typeVariable(declaration, name, bounds);
302302
303303 assertEquals("Generic declaration", declaration, typeVariable.getGenericDeclaration());
304304 assertEquals("Name", name, typeVariable.getName());