public static <T> T newProxy(Class<T> enclosingType, Function<Invocation, Object> invocationFunction) { checkNotNull(invocationFunction, "invocationFunction"); return newProxy(enclosingType, new FunctionalInvocationHandler<T>(typeToken(enclosingType), invocationFunction)); }
public static <T> T newProxy(Class<T> enclosingType, Function<Invocation, Object> invocationFunction) { checkNotNull(invocationFunction, "invocationFunction"); return newProxy(enclosingType, new FunctionalInvocationHandler<T>(typeToken(enclosingType), invocationFunction)); }
public static <T> T newProxy(Class<T> enclosingType, Function<Invocation, Object> invocationFunction) { checkNotNull(invocationFunction, "invocationFunction"); return newProxy(enclosingType, new FunctionalInvocationHandler<T>(typeToken(enclosingType), invocationFunction)); }
public static <T> T newProxy(Class<T> enclosingType, Function<Invocation, Object> invocationFunction) { checkNotNull(invocationFunction, "invocationFunction"); return newProxy(enclosingType, new FunctionalInvocationHandler<T>(typeToken(enclosingType), invocationFunction)); }
public static <T> T newProxy(Class<T> enclosingType, Function<Invocation, Object> invocationFunction) { checkNotNull(invocationFunction, "invocationFunction"); return newProxy(enclosingType, new FunctionalInvocationHandler<T>(typeToken(enclosingType), invocationFunction)); }
public static <T> T newProxy(Class<T> enclosingType, Function<Invocation, Object> invocationFunction) { checkNotNull(invocationFunction, "invocationFunction"); return newProxy(enclosingType, new FunctionalInvocationHandler<T>(typeToken(enclosingType), invocationFunction)); }
public void testToStringEqualsFunction() { final Function<Invocation, Object> test = new Function<Invocation, Object>() { public Object apply(Invocation e) { return "foo"; } public String toString() { return "bar"; } }; Closeable closeable = FunctionalReflection.newProxy(Closeable.class, test); assertEquals(closeable.toString(), "bar"); }
@Test(expectedExceptions = IOException.class, expectedExceptionsMessageRegExp = "io") public void testPropagatesDeclaredException() throws IOException { final Function<Invocation, Object> test = new Function<Invocation, Object>() { public Object apply(Invocation e) { throw new RuntimeException(new IOException("io")); } }; Closeable closeable = FunctionalReflection.newProxy(Closeable.class, test); closeable.close(); }
@SuppressWarnings("unchecked") @Test(expectedExceptions = UnsupportedOperationException.class) public void testNullArgsAreAllowedAndUnmodifiable() { final Function<Invocation, Object> test = new Function<Invocation, Object>() { public Object apply(Invocation e) { assertNotNull(e.getArgs()); assertNull(e.getArgs().get(0)); e.getArgs().add("foo"); throw new AssertionError("shouldn't be able to mutate the list!"); } }; FunctionalReflection.newProxy(Set.class, test).add(null); }
/** * for example, someone could have enabled assertions, or there could be a recoverable ServiceConfigurationError */ @Test(expectedExceptions = AssertionError.class, expectedExceptionsMessageRegExp = "assert") public void testPropagatesError() throws IOException { final Function<Invocation, Object> test = new Function<Invocation, Object>() { public Object apply(Invocation e) { throw new AssertionError("assert"); } }; Closeable closeable = FunctionalReflection.newProxy(Closeable.class, test); closeable.close(); }
@Test(expectedExceptions = IOException.class, expectedExceptionsMessageRegExp = "io") public void testPropagatesDeclaredException() throws IOException { final Function<Invocation, Object> test = new Function<Invocation, Object>() { public Object apply(Invocation e) { throw new RuntimeException(new IOException("io")); } }; Closeable closeable = FunctionalReflection.newProxy(Closeable.class, test); closeable.close(); }
public void testToStringEqualsFunction() { final Function<Invocation, Object> test = new Function<Invocation, Object>() { public Object apply(Invocation e) { return "foo"; } public String toString() { return "bar"; } }; Closeable closeable = FunctionalReflection.newProxy(Closeable.class, test); assertEquals(closeable.toString(), "bar"); }
@SuppressWarnings("unchecked") @Test(expectedExceptions = UnsupportedOperationException.class) public void testImmutableListWhenArgsAreNotNull() { final Function<Invocation, Object> test = new Function<Invocation, Object>() { public Object apply(Invocation e) { assertNotNull(e.getArgs()); assertTrue(e.getArgs() instanceof ImmutableList); assertEquals(e.getArgs().get(0), "foo"); e.getArgs().add("bar"); throw new AssertionError("shouldn't be able to mutate the list!"); } }; FunctionalReflection.newProxy(Set.class, test).add("foo"); }
/** * for example, someone could have enabled assertions, or there could be a recoverable ServiceConfigurationError */ @Test(expectedExceptions = AssertionError.class, expectedExceptionsMessageRegExp = "assert") public void testPropagatesError() throws IOException { final Function<Invocation, Object> test = new Function<Invocation, Object>() { public Object apply(Invocation e) { throw new AssertionError("assert"); } }; Closeable closeable = FunctionalReflection.newProxy(Closeable.class, test); closeable.close(); }
public void testHashCodeDifferentiatesOnInterface() { final Function<Invocation, Object> test = new Function<Invocation, Object>() { public Object apply(Invocation e) { return null; } public int hashCode() { return 1111; } }; Appendable appendable1 = FunctionalReflection.newProxy(Appendable.class, test); Appendable appendable2 = FunctionalReflection.newProxy(Appendable.class, test); assertEquals(appendable1.hashCode(), appendable2.hashCode()); Closeable closeable = FunctionalReflection.newProxy(Closeable.class, test); assertNotEquals(appendable1.hashCode(), closeable.hashCode()); } }
@SuppressWarnings("unchecked") @Test(expectedExceptions = UnsupportedOperationException.class) public void testNullArgsAreAllowedAndUnmodifiable() { final Function<Invocation, Object> test = new Function<Invocation, Object>() { public Object apply(Invocation e) { assertNotNull(e.getArgs()); assertNull(e.getArgs().get(0)); e.getArgs().add("foo"); throw new AssertionError("shouldn't be able to mutate the list!"); } }; FunctionalReflection.newProxy(Set.class, test).add(null); }
@SuppressWarnings("unchecked") @Test(expectedExceptions = UnsupportedOperationException.class) public void testImmutableListWhenArgsAreNotNull() { final Function<Invocation, Object> test = new Function<Invocation, Object>() { public Object apply(Invocation e) { assertNotNull(e.getArgs()); assertTrue(e.getArgs() instanceof ImmutableList); assertEquals(e.getArgs().get(0), "foo"); e.getArgs().add("bar"); throw new AssertionError("shouldn't be able to mutate the list!"); } }; FunctionalReflection.newProxy(Set.class, test).add("foo"); }
public void testHashCodeDifferentiatesOnInterface() { final Function<Invocation, Object> test = new Function<Invocation, Object>() { public Object apply(Invocation e) { return null; } public int hashCode() { return 1111; } }; Appendable appendable1 = FunctionalReflection.newProxy(Appendable.class, test); Appendable appendable2 = FunctionalReflection.newProxy(Appendable.class, test); assertEquals(appendable1.hashCode(), appendable2.hashCode()); Closeable closeable = FunctionalReflection.newProxy(Closeable.class, test); assertNotEquals(appendable1.hashCode(), closeable.hashCode()); } }
private Object propagateContextToDelegate(Invocation caller) { Class<?> returnType = unwrapIfOptional(caller.getInvokable().getReturnType()); Function<Invocation, Object> delegate; setCaller.enter(caller); try { @SuppressWarnings("unchecked") Key<Function<Invocation, Object>> delegateType = (Key<Function<Invocation, Object>>) methodInvokerFor(returnType); delegate = injector.getInstance(delegateType); } finally { setCaller.exit(); } Object result = FunctionalReflection.newProxy(returnType, delegate); if (isReturnTypeOptional(caller.getInvokable())) { result = optionalConverter.apply(InvocationSuccess.create(caller, result)); } return result; }
private Object propagateContextToDelegate(Invocation caller) { Class<?> returnType = unwrapIfOptional(caller.getInvokable().getReturnType()); Function<Invocation, Object> delegate; setCaller.enter(caller); try { @SuppressWarnings("unchecked") Key<Function<Invocation, Object>> delegateType = (Key<Function<Invocation, Object>>) methodInvokerFor(returnType); delegate = injector.getInstance(delegateType); } finally { setCaller.exit(); } Object result = FunctionalReflection.newProxy(returnType, delegate); if (isReturnTypeOptional(caller.getInvokable())) { result = optionalConverter.apply(InvocationSuccess.create(caller, result)); } return result; }