@Override Object interfaceMethodCalled(Class<?> interfaceType, Method method) { return getDummyValue(TypeToken.of(interfaceType).method(method).getReturnType()); } };
private static Invokable<?, ?> invokable(@Nullable Object instance, Method method) { if (instance == null) { return Invokable.from(method); } else { return TypeToken.of(instance.getClass()).method(method); } }
@Override protected Object handleInvocation(Object proxy, Method method, Object[] args) { Invokable<?, ?> invokable = interfaceType.method(method); ImmutableList<Parameter> params = invokable.getParameters(); for (int i = 0; i < args.length; i++) { Parameter param = params.get(i); if (!isNullable(param)) { checkNotNull(args[i]); } } return dummyReturnValue(interfaceType.resolveType(method.getGenericReturnType())); }
public void testMethod_notDeclaredByType() throws NoSuchMethodException { Method sizeMethod = Map.class.getMethod("size"); try { TypeToken.of(List.class).method(sizeMethod); fail(); } catch (IllegalArgumentException expected) { } }
@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); } });
List<Invokable<?, ? extends T>> factories = Lists.newArrayList(); for (Method method : type.getRawType().getDeclaredMethods()) { Invokable<?, ?> invokable = type.method(method); if (!invokable.isPrivate() && !invokable.isSynthetic()
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)); }
private static <T> void callAllPublicMethods(TypeToken<T> type, T object) throws InvocationTargetException { for (Method method : type.getRawType().getMethods()) { if ((method.getModifiers() & STATIC) != 0) { continue; } ImmutableList<Parameter> parameters = type.method(method).getParameters(); Object[] args = new Object[parameters.size()]; for (int i = 0; i < parameters.size(); i++) { args[i] = getDefaultValue(parameters.get(i).getType()); } try { try { method.invoke(object, args); } catch (InvocationTargetException ex) { try { throw ex.getCause(); } catch (UnsupportedOperationException unsupported) { // this is a legit exception } } } catch (Throwable cause) { throw new InvocationTargetException( cause, method + " with args: " + Arrays.toString(args)); } } } }
public void testMethod_getOwnerType() throws NoSuchMethodException { Method sizeMethod = List.class.getMethod("size"); assertEquals( TypeToken.of(List.class), TypeToken.of(List.class).method(sizeMethod).getOwnerType()); assertEquals( new TypeToken<List<String>>() {}, new TypeToken<List<String>>() {}.method(sizeMethod).getOwnerType()); }
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 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(); }
@Override Object interfaceMethodCalled(Class<?> interfaceType, Method method) { return getDummyValue(TypeToken.of(interfaceType).method(method).getReturnType()); } };
private void addPropertyName(Method method, ComponentBeanMethod bm, String propertyName, boolean computed, boolean defaulted) { IComponent.EqualsKey a = method.getAnnotation(IComponent.EqualsKey.class); boolean equalsKey = a != null; TypeToken<?> typeToken = componentTypeToken.method(method).getReturnType(); PropertyDescriptor cf = new PropertyDescriptor(method, propertyName, typeToken, typeToken.getRawType(), equalsKey, computed, defaulted); componentFieldMap.put(propertyName, cf); propertyDescriptors.add(cf); propertyNames.add(propertyName); if (equalsKey) { equalsKeyPropertyNames.add(propertyName); nullEqualsKeyMap.put(propertyName, a.nullEquals()); } }
private static Invokable<?, ?> invokable(@Nullable Object instance, Method method) { if (instance == null) { return Invokable.from(method); } else { return TypeToken.of(instance.getClass()).method(method); } }
@Override protected Object handleInvocation(Object proxy, Method method, Object[] args) { Invokable<?, ?> invokable = interfaceType.method(method); ImmutableList<Parameter> params = invokable.getParameters(); for (int i = 0; i < args.length; i++) { Parameter param = params.get(i); if (!isNullable(param)) { checkNotNull(args[i]); } } return dummyReturnValue(interfaceType.resolveType(method.getGenericReturnType())); }
private static Invokable<?, ?> invokable(@Nullable final Object instance, final Method method) { if (instance == null) { return Invokable.from(method); } else { return TypeToken.of(instance.getClass()).method(method); } }
public void testMethod_notDeclaredByType() throws NoSuchMethodException { Method sizeMethod = Map.class.getMethod("size"); try { TypeToken.of(List.class).method(sizeMethod); fail(); } catch (IllegalArgumentException expected) {} }
@Override protected Object handleInvocation( Object proxy, Method method, Object[] args) { Invokable<?, ?> invokable = interfaceType.method(method); ImmutableList<Parameter> params = invokable.getParameters(); for (int i = 0; i < args.length; i++) { Parameter param = params.get(i); if (!param.isAnnotationPresent(Nullable.class)) { checkNotNull(args[i]); } } return dummyReturnValue(interfaceType.resolveType(method.getGenericReturnType())); }