/** 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; }
@Override Object interfaceMethodCalled(Class<?> interfaceType, Method method) { return getDummyValue(TypeToken.of(interfaceType).method(method).getReturnType()); } };
private ImmutableList<Invokable<?, ?>> getFactoriesToTest() { ImmutableList.Builder<Invokable<?, ?>> builder = ImmutableList.builder(); for (Invokable<?, ?> factory : factories) { if (returnTypeToTest.isAssignableFrom(factory.getReturnType().getRawType())) { builder.add(factory); } } ImmutableList<Invokable<?, ?>> factoriesToTest = builder.build(); Assert.assertFalse( "No " + factoryMethodsDescription + " that return " + returnTypeToTest.getName() + " or subtype are found in " + declaringClass + ".", factoriesToTest.isEmpty()); return factoriesToTest; } }
/** 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; }
/** 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; }
@Override public Object handleInvocation(Object proxy, Method method, Object[] args) { // Crude, but acceptable until we can use Java 8. Other // methods have default implementations, and it is hard to // distinguish. if (method.getName().equals(JUF_METHODS.get(typeName))) { return getDefaultValue(type.method(method).getReturnType()); } throw new IllegalStateException("Unexpected " + method + " invoked on " + proxy); } });
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()); }
&& !invokable.isSynthetic() && invokable.isStatic() && type.isSupertypeOf(invokable.getReturnType())) { @SuppressWarnings("unchecked") // guarded by isAssignableFrom() Invokable<?, ? extends T> factory = (Invokable<?, ? extends T>) invokable;
public void testInstanceMethod_returnType() throws Exception { Invokable<?, ?> delegate = Prepender.method("prepend", Iterable.class); assertEquals(new TypeToken<Iterable<String>>() {}, delegate.getReturnType()); }
public void testStaticMethod_returnType() throws Exception { Invokable<?, ?> delegate = Prepender.method("prepend", String.class, Iterable.class); assertEquals(new TypeToken<Iterable<String>>() {}, delegate.getReturnType()); }
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_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)); }
public void testConstructor_returnType() throws Exception { assertEquals(Prepender.class, Prepender.constructor().getReturnType().getType()); }
public void testConstructor_returnType_hasTypeParameter() throws Exception { @SuppressWarnings("rawtypes") // Foo.class for Foo<T> is always raw type Class<WithConstructorAndTypeParameter> type = WithConstructorAndTypeParameter.class; @SuppressWarnings("rawtypes") // Foo.class Constructor<WithConstructorAndTypeParameter> constructor = type.getDeclaredConstructor(); Invokable<?, ?> factory = Invokable.from(constructor); assertThat(factory.getTypeParameters()).hasLength(2); assertEquals(type.getTypeParameters()[0], factory.getTypeParameters()[0]); assertEquals(constructor.getTypeParameters()[0], factory.getTypeParameters()[1]); ParameterizedType returnType = (ParameterizedType) factory.getReturnType().getType(); assertEquals(type, returnType.getRawType()); assertEquals( ImmutableList.copyOf(type.getTypeParameters()), ImmutableList.copyOf(returnType.getActualTypeArguments())); }
@Override Object interfaceMethodCalled(Class<?> interfaceType, Method method) { return getDummyValue(TypeToken.of(interfaceType).method(method).getReturnType()); } };
@SuppressWarnings("unchecked") private static Key<? extends Function<HttpResponse, ?>> getJAXBParserKeyForMethod(Invokable<?, ?> invoked) { Optional<Type> configuredReturnVal = Optional.absent(); if (invoked.isAnnotationPresent(JAXBResponseParser.class)) { Type configuredClass = invoked.getAnnotation(JAXBResponseParser.class).value(); configuredReturnVal = configuredClass.equals(NullType.class) ? Optional.<Type> absent() : Optional .<Type> of(configuredClass); } Type returnVal = configuredReturnVal.or(getReturnTypeFor(invoked.getReturnType())); Type parserType = newParameterizedType(ParseXMLWithJAXB.class, returnVal); return (Key<? extends Function<HttpResponse, ?>>) Key.get(parserType); }
@SuppressWarnings({ "unchecked" }) private static Key<? extends Function<HttpResponse, ?>> getJsonParserKeyForMethod(Invokable<?, ?> invoked) { ParameterizedType parserType; if (invoked.isAnnotationPresent(Unwrap.class)) { parserType = newParameterizedType(UnwrapOnlyJsonValue.class, getReturnTypeFor(invoked.getReturnType())); } else { parserType = newParameterizedType(ParseJson.class, getReturnTypeFor(invoked.getReturnType())); } return (Key<? extends Function<HttpResponse, ?>>) Key.get(parserType); }
@SuppressWarnings({ "unchecked" }) private static Key<? extends Function<HttpResponse, ?>> getJsonParserKeyForMethod(Invokable<?, ?> invoked) { ParameterizedType parserType; if (invoked.isAnnotationPresent(Unwrap.class)) { parserType = newParameterizedType(UnwrapOnlyJsonValue.class, getReturnTypeFor(invoked.getReturnType())); } else { parserType = newParameterizedType(ParseJson.class, getReturnTypeFor(invoked.getReturnType())); } return (Key<? extends Function<HttpResponse, ?>>) Key.get(parserType); }