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)); }
/** Returns the type of {@code T}. */ // Overridden in TypeToken#method() and TypeToken#constructor() @SuppressWarnings("unchecked") // The declaring class is T. @Override public TypeToken<T> getOwnerType() { return (TypeToken<T>) TypeToken.of(getDeclaringClass()); }
public void testInnerClassWithAnnotatedConstructorParameter() { Constructor<?> constructor = InnerWithAnnotatedConstructorParameter.class.getDeclaredConstructors()[0]; Invokable<?, ?> invokable = Invokable.from(constructor); assertEquals(1, invokable.getParameters().size()); assertEquals(TypeToken.of(String.class), invokable.getParameters().get(0).getType()); }
public void testNestedInnerClassDefaultConstructor() { Constructor<?> constructor = InnerWithDefaultConstructor.NestedInner.class.getDeclaredConstructors()[0]; assertEquals(0, Invokable.from(constructor).getParameters().size()); }
public void testGetOwnerType_constructor() throws Exception { Invokable<String, String> invokable = Invokable.from(String.class.getConstructor()); assertEquals(TypeToken.of(String.class), invokable.getOwnerType()); }
@Override public Set<String> apply(Invocation invocation) { Optional<Consumes> accept = Optional.fromNullable(invocation.getInvokable().getAnnotation(Consumes.class)).or( Optional.fromNullable(invocation.getInvokable().getOwnerType().getRawType().getAnnotation(Consumes.class))); return (accept.isPresent()) ? ImmutableSet.copyOf(accept.get().value()) : ImmutableSet.<String> of(); } }
private static Optional<String> getAnnotatedApiVersion(Invocation invocation) { final Invokable<?, ?> invokable = invocation.getInvokable(); if (invokable.isAnnotationPresent(ApiVersionOverride.class)) { return Optional.fromNullable(invokable.getAnnotation(ApiVersionOverride.class).value()); } else { final Class<?> owner = invokable.getOwnerType().getRawType(); if (owner.isAnnotationPresent(ApiVersionOverride.class)) { return Optional.fromNullable(owner.getAnnotation(ApiVersionOverride.class).value()); } } return Optional.absent(); }
/** Returns all declared exception types of this {@code Invokable}. */ public final ImmutableList<TypeToken<? extends Throwable>> getExceptionTypes() { ImmutableList.Builder<TypeToken<? extends Throwable>> builder = ImmutableList.builder(); for (Type type : getGenericExceptionTypes()) { // getGenericExceptionTypes() will never return a type that's not exception @SuppressWarnings("unchecked") TypeToken<? extends Throwable> exceptionType = (TypeToken<? extends Throwable>) TypeToken.of(type); builder.add(exceptionType); } return builder.build(); }
@Override public org.jclouds.Fallback<?> getFallback(Invocation invocation) { Fallback fallback = invocation.getInvokable().getAnnotation(Fallback.class); if (fallback != null) { return injector.getInstance(fallback.value()); } return defaultFallback; }
public static String getBucketName(HttpRequest req) { checkArgument(req instanceof GeneratedHttpRequest, "this should be a generated http request"); GeneratedHttpRequest request = GeneratedHttpRequest.class.cast(req); String bucketName = null; for (int i = 0; i < request.getInvocation().getInvokable().getParameters().size(); i++) { if (any(Arrays.asList(request.getInvocation().getInvokable().getParameters().get(i).getAnnotations()), ANNOTATIONTYPE_BUCKET)) { bucketName = (String) request.getInvocation().getArgs().get(i); break; } } return bucketName; }
public void testMethodFromClassAndParams() { @SuppressWarnings("rawtypes") Invokable<Set, Object> methodInSuper = method(Set.class, "equals", Object.class); assertEquals(methodInSuper.getOwnerType(), typeToken(Set.class)); assertEquals(methodInSuper.getParameters().get(0).getType().getRawType(), Object.class); }
protected static void assertInvokedCommand(Invokable<?, ?> actual, Invokable<?, ?> expected) { // Invokables can be constructed by different meanings and be of different types. Since we // only want to verify the "wrapped" types, let's ignore the invokable type and compare the // types of the underlying methods assertEquals(actual.getDeclaringClass(), expected.getDeclaringClass()); assertEquals(actual.getName(), expected.getName()); assertEquals(actual.getParameters().size(), expected.getParameters().size()); for (int i = 0; i < actual.getParameters().size(); i++) { assertEquals(actual.getParameters().get(i).getType(), expected.getParameters().get(i).getType()); } assertEquals(actual.getReturnType(), expected.getReturnType()); } }
private boolean checkPresentOrNullable(Invocation invocation, String paramKey, int argIndex, Object arg) { if (arg == null && !invocation.getInvokable().getParameters().get(argIndex).isAnnotationPresent(Nullable.class)) throw new NullPointerException(format("param{%s} for invocation %s.%s", paramKey, invocation.getInvokable() .getOwnerType().getRawType().getSimpleName(), invocation.getInvokable().getName())); return true; }
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 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(); }
@Override public String getCommandName(Invocation invocation) { Invokable<?, ?> invoked = invocation.getInvokable(); if (invoked.isAnnotationPresent(Named.class)) { return invoked.getAnnotation(Named.class).value(); } else { // TODO: remove old logic once Named annotations are on all methods String className = invoked.getOwnerType().getRawType().getSimpleName().replace("AsyncClient", "Client") .replace("AsyncApi", "Api"); return className + "." + invoked.getName(); } }
private boolean checkPresentOrNullable(Invocation invocation, String paramKey, int argIndex, Object arg) { if (arg == null && !getInvokableParameters(invocation.getInvokable()).get(argIndex).isAnnotationPresent(Nullable.class)) throw new NullPointerException(format("param{%s} for invocation %s.%s", paramKey, invocation.getInvokable() .getOwnerType().getRawType().getSimpleName(), invocation.getInvokable().getName())); return true; }
@Override Object interfaceMethodCalled(Class<?> interfaceType, Method method) { return getDummyValue(TypeToken.of(interfaceType).method(method).getReturnType()); } };
public static void putInvokables(Class<?> sync, Class<?> async, Cache<Invokable<?, ?>, Invokable<?, ?>> cache) { for (Invokable<?, ?> invoked : methods(sync)) { Invokable<?, ?> delegatedMethod = method(async, invoked.getName(), getParameterTypes(invoked)); checkArgument(delegatedMethod.getExceptionTypes().equals(invoked.getExceptionTypes()) || isCloseable(delegatedMethod), "invoked %s has different typed exceptions than target %s", invoked, delegatedMethod); cache.put(invoked, delegatedMethod); } }
/** * 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)); }