private static <T> @Nullable T invoke(Invokable<?, ? extends T> factory, List<?> args) throws InvocationTargetException, IllegalAccessException { T returnValue = factory.invoke(null, args.toArray()); if (returnValue == null) { Assert.assertTrue( factory + " returns null but it's not annotated with @Nullable", isNullable(factory)); } return returnValue; }
public void testStaticMethod_call() throws Exception { Invokable<?, ?> delegate = Prepender.method("prepend", String.class, Iterable.class); @SuppressWarnings("unchecked") // prepend() returns Iterable<String> Iterable<String> result = (Iterable<String>) delegate.invoke(null, "a", ImmutableList.of("b", "c")); assertEquals(ImmutableList.of("a", "b", "c"), ImmutableList.copyOf(result)); }
@SuppressWarnings("unchecked") // We'll get a runtime exception if the type is wrong. Invokable<Object, ?> unsafe = (Invokable<Object, ?>) invokable; unsafe.invoke(instance, params); Assert.fail( "No exception thrown for parameter at index "
public void testMethod_declaredBySuperclass() throws Exception { Method toStringMethod = Object.class.getMethod("toString"); ImmutableList<String> list = ImmutableList.of("foo"); assertEquals(list.toString(), TypeToken.of(List.class).method(toStringMethod).invoke(list)); }
public void testInstanceMethod_call() throws Exception { Invokable<Prepender, ?> delegate = Prepender.method("prepend", Iterable.class); @SuppressWarnings("unchecked") // prepend() returns Iterable<String> Iterable<String> result = (Iterable<String>) delegate.invoke(new Prepender("a", 2), ImmutableList.of("b", "c")); assertEquals(ImmutableList.of("a", "a", "b", "c"), ImmutableList.copyOf(result)); }
public void testStaticMethod_returning() throws Exception { Invokable<?, Iterable<String>> delegate = Prepender.method("prepend", String.class, Iterable.class) .returning(new TypeToken<Iterable<String>>() {}); assertEquals(new TypeToken<Iterable<String>>() {}, delegate.getReturnType()); Iterable<String> result = delegate.invoke(null, "a", ImmutableList.of("b", "c")); assertEquals(ImmutableList.of("a", "b", "c"), ImmutableList.copyOf(result)); }
public void testStaticMethod_returningRawType() throws Exception { @SuppressWarnings("rawtypes") // the purpose is to test raw type Invokable<?, Iterable> delegate = Prepender.method("prepend", String.class, Iterable.class).returning(Iterable.class); assertEquals(new TypeToken<Iterable<String>>() {}, delegate.getReturnType()); @SuppressWarnings("unchecked") // prepend() returns Iterable<String> Iterable<String> result = delegate.invoke(null, "a", ImmutableList.of("b", "c")); assertEquals(ImmutableList.of("a", "b", "c"), ImmutableList.copyOf(result)); }
public void testInstanceMethod_returning() throws Exception { Invokable<Prepender, Iterable<String>> delegate = Prepender.method("prepend", Iterable.class).returning(new TypeToken<Iterable<String>>() {}); assertEquals(new TypeToken<Iterable<String>>() {}, delegate.getReturnType()); Iterable<String> result = delegate.invoke(new Prepender("a", 2), ImmutableList.of("b", "c")); assertEquals(ImmutableList.of("a", "a", "b", "c"), ImmutableList.copyOf(result)); }
public void testConstructor_call() throws Exception { Invokable<?, Prepender> delegate = Prepender.constructor(String.class, int.class); Prepender prepender = delegate.invoke(null, "a", 1); assertEquals("a", prepender.prefix); assertEquals(1, prepender.times); }
public void testInstanceMethod_returningRawType() throws Exception { @SuppressWarnings("rawtypes") // the purpose is to test raw type Invokable<Prepender, Iterable> delegate = Prepender.method("prepend", Iterable.class).returning(Iterable.class); assertEquals(new TypeToken<Iterable<String>>() {}, delegate.getReturnType()); @SuppressWarnings("unchecked") // prepend() returns Iterable<String> Iterable<String> result = delegate.invoke(new Prepender("a", 2), ImmutableList.of("b", "c")); assertEquals(ImmutableList.of("a", "a", "b", "c"), ImmutableList.copyOf(result)); }
public void testConstructor_returning() throws Exception { Invokable<?, Prepender> delegate = Prepender.constructor(String.class, int.class).returning(Prepender.class); Prepender prepender = delegate.invoke(null, "a", 1); assertEquals("a", prepender.prefix); assertEquals(1, prepender.times); }
private static <I> void invokeOnInjectee(Invokable<? super I, Object> method, I injectee) { try { method.invoke(injectee); } catch (InvocationTargetException ie) { throw propagate(ie.getTargetException()); } catch (IllegalAccessException e) { throw propagate(e); } } }
private static <I> void invokeOnInjectee(Invokable<? super I, Object> method, I injectee) { try { method.invoke(injectee); } catch (InvocationTargetException ie) { throw propagate(ie.getTargetException()); } catch (IllegalAccessException e) { throw propagate(e); } } }
private static <I> void invokeOnInjectee(Invokable<? super I, Object> method, I injectee) { try { method.invoke(injectee); } catch (InvocationTargetException ie) { throw propagate(ie.getTargetException()); } catch (IllegalAccessException e) { throw propagate(e); } } }
private T newInstance(Object[] ctorParams) throws AssertionError { try { return parameterizedCtor.invoke(null, ctorParams); } catch (IllegalAccessException e) { throw new AssertionError(e); } catch (InvocationTargetException e) { if (e.getCause() instanceof RuntimeException) throw RuntimeException.class.cast(e.getCause()); throw new AssertionError(e); } }
private T newInstance(Object[] ctorParams) throws AssertionError { try { return parameterizedCtor.invoke(null, ctorParams); } catch (IllegalAccessException e) { throw new AssertionError(e); } catch (InvocationTargetException e) { if (e.getCause() instanceof RuntimeException) throw RuntimeException.class.cast(e.getCause()); throw new AssertionError(e); } }
private T newInstance(Object[] ctorParams) throws AssertionError { try { return parameterizedCtor.invoke(null, ctorParams); } catch (IllegalAccessException e) { throw new AssertionError(e); } catch (InvocationTargetException e) { if (e.getCause() instanceof RuntimeException) throw RuntimeException.class.cast(e.getCause()); throw new AssertionError(e); } }
private static <T> @Nullable T invoke(Invokable<?, ? extends T> factory, List<?> args) throws InvocationTargetException, IllegalAccessException { T returnValue = factory.invoke(null, args.toArray()); if (returnValue == null) { Assert.assertTrue( factory + " returns null but it's not annotated with @Nullable", isNullable(factory)); } return returnValue; }
public void testStaticMethod_call() throws Exception { Invokable<?, ?> delegate = Prepender.method("prepend", String.class, Iterable.class); @SuppressWarnings("unchecked") // prepend() returns Iterable<String> Iterable<String> result = (Iterable<String>) delegate.invoke(null, "a", ImmutableList.of("b", "c")); assertEquals(ImmutableList.of("a", "b", "c"), ImmutableList.copyOf(result)); }
public void testMethod_declaredBySuperclass() throws Exception { Method toStringMethod = Object.class.getMethod("toString"); ImmutableList<String> list = ImmutableList.of("foo"); assertEquals(list.toString(), TypeToken.of(List.class).method(toStringMethod).invoke(list)); }