private static boolean isEqualsDefined(Class<?> cls) { try { return !cls.getDeclaredMethod("equals", Object.class).isSynthetic(); } catch (NoSuchMethodException e) { return false; } }
static WildcardType getWildcardType(String methodName) throws Exception { ParameterizedType parameterType = (ParameterizedType) WithWildcardType.class.getDeclaredMethod(methodName, List.class) .getGenericParameterTypes()[0]; return (WildcardType) parameterType.getActualTypeArguments()[0]; } }
static Throwable[] getSuppressed(Throwable throwable) { try { Method getSuppressed = Throwable.class.getDeclaredMethod("getSuppressed"); return (Throwable[]) getSuppressed.invoke(throwable); } catch (Exception e) { throw new AssertionError(e); // only called if running on JDK7 } }
private Method getTestSubscriberMethod(String name) { try { return getClass().getDeclaredMethod(name, Object.class); } catch (NoSuchMethodException e) { throw new AssertionError(); } }
static TypeVariable<?> getTypeVariable(String methodName) throws Exception { ParameterizedType parameterType = (ParameterizedType) WithTypeVariable.class.getDeclaredMethod(methodName, List.class) .getGenericParameterTypes()[0]; return (TypeVariable<?>) parameterType.getActualTypeArguments()[0]; } }
private Throwable tryInternalFastPathGetFailure(Future<?> future) throws Exception { Method tryInternalFastPathGetFailureMethod = abstractFutureClass.getDeclaredMethod("tryInternalFastPathGetFailure"); tryInternalFastPathGetFailureMethod.setAccessible(true); return (Throwable) tryInternalFastPathGetFailureMethod.invoke(future); } }
static Invokable<Prepender, Object> method(String name, Class<?>... parameterTypes) { try { Method method = Prepender.class.getDeclaredMethod(name, parameterTypes); @SuppressWarnings("unchecked") // The method is from Prepender. Invokable<Prepender, Object> invokable = (Invokable<Prepender, Object>) Invokable.from(method); return invokable; } catch (NoSuchMethodException e) { throw new IllegalArgumentException(e); } }
Type getTargetType(String methodName) throws Exception { ParameterizedType parameterType = (ParameterizedType) WithGenericBound.class.getDeclaredMethod(methodName, List.class) .getGenericParameterTypes()[0]; parameterType = (ParameterizedType) TypeToken.of(this.getClass()).resolveType(parameterType).getType(); return parameterType.getActualTypeArguments()[0]; } }
public void testToGenericType_staticMemberClass() throws Exception { Method getStaticAnonymousClassMethod = TypeTokenTest.class.getDeclaredMethod("getStaticAnonymousClass", Object.class); ParameterizedType javacReturnType = (ParameterizedType) getStaticAnonymousClassMethod.getGenericReturnType(); ParameterizedType parameterizedType = (ParameterizedType) TypeToken.toGenericType(GenericClass.class).getType(); assertThat(parameterizedType.getOwnerType()).isEqualTo(javacReturnType.getOwnerType()); }
public <T> void testCaptureTypeParameter() throws Exception { TypeVariable<?> variable = new TypeParameter<T>() {}.typeVariable; TypeVariable<?> expected = TypeParameterTest.class.getDeclaredMethod("testCaptureTypeParameter") .getTypeParameters()[0]; assertEquals(expected, variable); }
public void testVisibility_protected() throws Exception { assertFalse( Visibility.PROTECTED.isVisible(VisibilityMethods.class.getDeclaredMethod("privateMethod"))); assertFalse( Visibility.PROTECTED.isVisible( VisibilityMethods.class.getDeclaredMethod("packagePrivateMethod"))); assertTrue( Visibility.PROTECTED.isVisible( VisibilityMethods.class.getDeclaredMethod("protectedMethod"))); assertTrue( Visibility.PROTECTED.isVisible(VisibilityMethods.class.getDeclaredMethod("publicMethod"))); }
static Element method(String name, Class<?>... parameterTypes) throws Exception { Element element = new Element(A.class.getDeclaredMethod(name, parameterTypes)); assertEquals(name, element.getName()); assertEquals(A.class, element.getDeclaringClass()); return element; }
public void testVisibility_public() throws Exception { assertFalse( Visibility.PUBLIC.isVisible(VisibilityMethods.class.getDeclaredMethod("privateMethod"))); assertFalse( Visibility.PUBLIC.isVisible( VisibilityMethods.class.getDeclaredMethod("packagePrivateMethod"))); assertFalse( Visibility.PUBLIC.isVisible(VisibilityMethods.class.getDeclaredMethod("protectedMethod"))); assertTrue( Visibility.PUBLIC.isVisible(VisibilityMethods.class.getDeclaredMethod("publicMethod"))); }
public void testVisibility_package() throws Exception { assertFalse( Visibility.PACKAGE.isVisible(VisibilityMethods.class.getDeclaredMethod("privateMethod"))); assertTrue( Visibility.PACKAGE.isVisible( VisibilityMethods.class.getDeclaredMethod("packagePrivateMethod"))); assertTrue( Visibility.PACKAGE.isVisible(VisibilityMethods.class.getDeclaredMethod("protectedMethod"))); assertTrue( Visibility.PACKAGE.isVisible(VisibilityMethods.class.getDeclaredMethod("publicMethod"))); }
public void testNulls() throws Exception { new ClassSanityTester() .setDefault(Method.class, FreshValueGeneratorTest.class.getDeclaredMethod("testNulls")) .testNulls(FreshValueGenerator.class); }
public void testWildcardCaptured_wildcardWithImplicitBound() throws Exception { TypeToken<Holder<?>> type = new TypeToken<Holder<?>>() {}; ImmutableList<Parameter> parameters = type.method(Holder.class.getDeclaredMethod("setList", List.class)).getParameters(); assertThat(parameters).hasSize(1); TypeToken<?> parameterType = parameters.get(0).getType(); Type[] typeArgs = ((ParameterizedType) parameterType.getType()).getActualTypeArguments(); assertThat(typeArgs).asList().hasSize(1); TypeVariable<?> captured = (TypeVariable<?>) typeArgs[0]; assertThat(captured.getBounds()).asList().containsExactly(Object.class); assertThat(new TypeToken<List<?>>() {}.isSupertypeOf(parameterType)).isTrue(); }
public void testWildcardCaptured_wildcardWithExplicitBound() throws Exception { TypeToken<Holder<? extends Number>> type = new TypeToken<Holder<? extends Number>>() {}; ImmutableList<Parameter> parameters = type.method(Holder.class.getDeclaredMethod("setList", List.class)).getParameters(); assertThat(parameters).hasSize(1); TypeToken<?> parameterType = parameters.get(0).getType(); Type[] typeArgs = ((ParameterizedType) parameterType.getType()).getActualTypeArguments(); assertThat(typeArgs).asList().hasSize(1); TypeVariable<?> captured = (TypeVariable<?>) typeArgs[0]; assertThat(captured.getBounds()).asList().containsExactly(Number.class); assertThat(new TypeToken<List<? extends Number>>() {}.isSupertypeOf(parameterType)).isTrue(); }
public void testWildcardCaptured_methodParameter_upperBound() throws Exception { TypeToken<Holder<?>> type = new TypeToken<Holder<?>>() {}; ImmutableList<Parameter> parameters = type.method(Holder.class.getDeclaredMethod("setList", List.class)).getParameters(); assertThat(parameters).hasSize(1); TypeToken<?> parameterType = parameters.get(0).getType(); assertEquals(List.class, parameterType.getRawType()); assertFalse( parameterType.getType().toString(), parameterType.isSupertypeOf(new TypeToken<List<Integer>>() {})); }
public <A, B> void testEquals() throws Exception { Method method = TypeParameterTest.class.getDeclaredMethod("testEquals"); new EqualsTester() .addEqualityGroup(new TypeParameter<A>() {}, new TypeParameter<A>() {}) .addEqualityGroup(new TypeParameter<B>() {}) .testEquals(); }
public void testNonFinalMethodInFinalClass_isOverridable() throws Exception { Invokable<?, ?> delegate = Invokable.from(FinalClass.class.getDeclaredMethod("notFinalMethod")); assertFalse(delegate.isOverridable()); assertFalse(delegate.isVarArgs()); }