@BeforeClass public void backupMethodsForTypeToken() { originalMethodsForTypeToken = getStaticField(Reflection2.class, "methodsForTypeToken"); }
@AfterClass(alwaysRun = true) public void restoreMethodsForTypeToken() { setStaticField(Reflection2.class, "methodsForTypeToken", originalMethodsForTypeToken); } }
public void testOverriddenMethodWithNarrowedReturnType() throws NoSuchMethodException { // expecting two methods: the declared "method" and the bridge version with return type Set final Method[] methods = ChildOverridesAndNarrowsMethod.class.getDeclaredMethods(); /* * Force Reflection2.methodsForTypeToken to reflect the fact that the declared methods * of a class are not returned in any particular order. */ setStaticField(Reflection2.class, "methodsForTypeToken", keyOverridingCache( TypeToken.of(ChildOverridesAndNarrowsMethod.class), ImmutableSet.<Invokable<?, ?>>of(Invokable.from(methods[0]), Invokable.from(methods[1])))); // getMethod returns the method with the *narrowest* return type if one exists Invokable<?, Object> mostSpecificMethod = Invokable.from(ChildOverridesAndNarrowsMethod.class.getMethod("method")); assertEquals(Reflection2.method(ChildOverridesAndNarrowsMethod.class, "method"), mostSpecificMethod); // now the opposite order Reflection2OverriddenMethodTest.<LoadingCache<?, ?>> getStaticField(Reflection2.class, "methodForParams").invalidateAll(); setStaticField(Reflection2.class, "methodsForTypeToken", keyOverridingCache( TypeToken.of(ChildOverridesAndNarrowsMethod.class), ImmutableSet.<Invokable<?, ?>>of(Invokable.from(methods[1]), Invokable.from(methods[0])))); assertEquals(Reflection2.method(ChildOverridesAndNarrowsMethod.class, "method"), mostSpecificMethod); }
public void testOverriddenMethodWithNarrowedReturnType() throws NoSuchMethodException { // expecting two methods: the declared "method" and the bridge version with return type Set final Method[] methods = ChildOverridesAndNarrowsMethod.class.getDeclaredMethods(); /* * Force Reflection2.methodsForTypeToken to reflect the fact that the declared methods * of a class are not returned in any particular order. */ setStaticField(Reflection2.class, "methodsForTypeToken", keyOverridingCache( TypeToken.of(ChildOverridesAndNarrowsMethod.class), ImmutableSet.<Invokable<?, ?>>of(Invokable.from(methods[0]), Invokable.from(methods[1])))); // getMethod returns the method with the *narrowest* return type if one exists Invokable<?, Object> mostSpecificMethod = Invokable.from(ChildOverridesAndNarrowsMethod.class.getMethod("method")); assertEquals(Reflection2.method(ChildOverridesAndNarrowsMethod.class, "method"), mostSpecificMethod); // now the opposite order Reflection2OverriddenMethodTest.<LoadingCache<?, ?>> getStaticField(Reflection2.class, "methodForParams").invalidateAll(); setStaticField(Reflection2.class, "methodsForTypeToken", keyOverridingCache( TypeToken.of(ChildOverridesAndNarrowsMethod.class), ImmutableSet.<Invokable<?, ?>>of(Invokable.from(methods[1]), Invokable.from(methods[0])))); assertEquals(Reflection2.method(ChildOverridesAndNarrowsMethod.class, "method"), mostSpecificMethod); }
@AfterClass(alwaysRun = true) public void restoreMethodsForTypeToken() { setStaticField(Reflection2.class, "methodsForTypeToken", originalMethodsForTypeToken); } }
@BeforeClass public void backupMethodsForTypeToken() { originalMethodsForTypeToken = getStaticField(Reflection2.class, "methodsForTypeToken"); }