|
47 | 47 | import java.io.OutputStream; |
48 | 48 | import java.io.Serializable; |
49 | 49 | import java.lang.reflect.Field; |
| 50 | +import java.lang.reflect.Method; |
50 | 51 | import java.lang.reflect.Type; |
51 | 52 | import java.lang.reflect.TypeVariable; |
52 | 53 | import java.math.BigDecimal; |
@@ -373,6 +374,28 @@ public void testField() { |
373 | 374 | assertEquals("T", ((TypeVariable<?>) field.getGenericType()).getName()); |
374 | 375 | } |
375 | 376 |
|
| 377 | + /** Tests {@link Types#method}. */ |
| 378 | + @Test |
| 379 | + public void testMethod() { |
| 380 | + final Method objectMethod = Types.method(Thing.class, "toString"); |
| 381 | + assertSame(Object.class, objectMethod.getDeclaringClass()); |
| 382 | + assertEquals("toString", objectMethod.getName()); |
| 383 | + assertSame(String.class, objectMethod.getReturnType()); |
| 384 | + assertEquals(0, objectMethod.getParameterTypes().length); |
| 385 | + |
| 386 | + final Method wordsMethod = // |
| 387 | + Types.method(Words.class, "valueOf", String.class); |
| 388 | + // NB: What is going on under the hood to make the Enum |
| 389 | + // subtype Words be the declaring class for the 'valueOf' |
| 390 | + // method? The compiler must internally override the valueOf |
| 391 | + // method for each enum type, to narrow the return type... |
| 392 | + assertSame(Words.class, wordsMethod.getDeclaringClass()); |
| 393 | + assertEquals("valueOf", wordsMethod.getName()); |
| 394 | + assertSame(Words.class, wordsMethod.getReturnType()); |
| 395 | + assertEquals(1, wordsMethod.getParameterTypes().length); |
| 396 | + assertSame(String.class, wordsMethod.getParameterTypes()[0]); |
| 397 | + } |
| 398 | + |
376 | 399 | /** Tests {@link Types#array}. */ |
377 | 400 | @Test |
378 | 401 | public void testArray() { |
|
0 commit comments