/** * Explicitly specifies the return type of this {@code Invokable}. For example: * * <pre>{@code * Method factoryMethod = Person.class.getMethod("create"); * Invokable<?, Person> factory = Invokable.of(getNameMethod).returning(Person.class); * }</pre> */ public final <R1 extends R> Invokable<T, R1> returning(Class<R1> returnType) { return returning(TypeToken.of(returnType)); }
/** * Explicitly specifies the return type of this {@code Invokable}. For example: * * <pre>{@code * Method factoryMethod = Person.class.getMethod("create"); * Invokable<?, Person> factory = Invokable.of(getNameMethod).returning(Person.class); * }</pre> */ public final <R1 extends R> Invokable<T, R1> returning(Class<R1> returnType) { return returning(TypeToken.of(returnType)); }
/** Explicitly specifies the return type of this {@code Invokable}. */ public final <R1 extends R> Invokable<T, R1> returning(TypeToken<R1> returnType) { if (!returnType.isSupertypeOf(getReturnType())) { throw new IllegalArgumentException( "Invokable is known to return " + getReturnType() + ", not " + returnType); } @SuppressWarnings("unchecked") // guarded by previous check Invokable<T, R1> specialized = (Invokable<T, R1>) this; return specialized; }
public <T extends Number & List<String>> void testMethod_returnType_resolvedAgainstTypeBound() throws NoSuchMethodException { Method getMethod = List.class.getMethod("get", int.class); Invokable<T, String> invokable = new TypeToken<T>(getClass()) {}.method(getMethod).returning(String.class); assertEquals(TypeToken.of(String.class), invokable.getReturnType()); }
public void testStaticMethod_invalidReturning() throws Exception { Invokable<?, Object> delegate = Prepender.method("prepend", String.class, Iterable.class); try { delegate.returning(new TypeToken<Iterable<Integer>>() {}); fail(); } catch (IllegalArgumentException expected) { } }
public void testConstructor_invalidReturning() throws Exception { Invokable<?, Prepender> delegate = Prepender.constructor(String.class, int.class); try { delegate.returning(SubPrepender.class); fail(); } catch (IllegalArgumentException expected) { } }
public void testInstanceMethod_invalidReturning() throws Exception { Invokable<?, Object> delegate = Prepender.method("prepend", Iterable.class); try { delegate.returning(new TypeToken<Iterable<Integer>>() {}); fail(); } catch (IllegalArgumentException expected) { } }
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 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 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 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_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); }
/** * Explicitly specifies the return type of this {@code Invokable}. For example: * <pre> {@code * Method factoryMethod = Person.class.getMethod("create"); * Invokable<?, Person> factory = Invokable.of(getNameMethod).returning(Person.class);}</pre> */ public final <R1 extends R> Invokable<T, R1> returning(Class<R1> returnType) { return returning(TypeToken.of(returnType)); }
/** * Explicitly specifies the return type of this {@code Invokable}. For example: * <pre> {@code * Method factoryMethod = Person.class.getMethod("create"); * Invokable<?, Person> factory = Invokable.of(getNameMethod).returning(Person.class);}</pre> */ public final <R1 extends R> Invokable<T, R1> returning(Class<R1> returnType) { return returning(TypeToken.of(returnType)); }
/** * Explicitly specifies the return type of this {@code Invokable}. For example: * <pre> {@code * Method factoryMethod = Person.class.getMethod("create"); * Invokable<?, Person> factory = Invokable.of(getNameMethod).returning(Person.class);}</pre> */ public final <R1 extends R> Invokable<T, R1> returning(Class<R1> returnType) { return returning(TypeToken.of(returnType)); }
public void testStaticMethod_invalidReturning() throws Exception { Invokable<?, Object> delegate = Prepender.method("prepend", String.class, Iterable.class); try { delegate.returning(new TypeToken<Iterable<Integer>>() {}); fail(); } catch (IllegalArgumentException expected) {} }
public void testConstructor_invalidReturning() throws Exception { Invokable<?, Prepender> delegate = Prepender.constructor(String.class, int.class); try { delegate.returning(SubPrepender.class); fail(); } catch (IllegalArgumentException expected) {} }
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 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)); }