use of java.lang.reflect.InvocationHandler in project byte-buddy by raphw.
the class AnnotationDescriptionAnnotationInvocationHandlerTest method testEqualsToOtherHandlerIsTrue.
@Test
public void testEqualsToOtherHandlerIsTrue() throws Throwable {
when(loadedAnnotationValue.getState()).thenReturn(AnnotationValue.Loaded.State.RESOLVED);
when(loadedAnnotationValue.resolve()).thenReturn(FOO);
InvocationHandler invocationHandler = Proxy.getInvocationHandler(AnnotationDescription.AnnotationInvocationHandler.of(getClass().getClassLoader(), Foo.class, Collections.<String, AnnotationValue<?, ?>>singletonMap(FOO, annotationValue)));
assertThat(Proxy.getInvocationHandler(AnnotationDescription.AnnotationInvocationHandler.of(getClass().getClassLoader(), Foo.class, Collections.<String, AnnotationValue<?, ?>>singletonMap(FOO, annotationValue))).invoke(new Object(), Object.class.getDeclaredMethod("equals", Object.class), new Object[] { Proxy.newProxyInstance(getClass().getClassLoader(), new Class<?>[] { Foo.class }, invocationHandler) }), is((Object) true));
verify(loadedAnnotationValue, never()).resolve();
}
use of java.lang.reflect.InvocationHandler in project redisson by redisson.
the class BaseRemoteService method async.
private <T> T async(final Class<T> remoteInterface, final RemoteInvocationOptions options, final Class<?> syncInterface) {
// local copy of the options, to prevent mutation
final RemoteInvocationOptions optionsCopy = new RemoteInvocationOptions(options);
final String toString = getClass().getSimpleName() + "-" + remoteInterface.getSimpleName() + "-proxy-" + generateRequestId();
InvocationHandler handler = new InvocationHandler() {
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
if (method.getName().equals("toString")) {
return toString;
} else if (method.getName().equals("equals")) {
return proxy == args[0];
} else if (method.getName().equals("hashCode")) {
return toString.hashCode();
}
if (!optionsCopy.isResultExpected() && !(method.getReturnType().equals(Void.class) || method.getReturnType().equals(Void.TYPE) || method.getReturnType().equals(RFuture.class))) {
throw new IllegalArgumentException("The noResult option only supports void return value");
}
final String requestId = generateRequestId();
final String requestQueueName = getRequestQueueName(syncInterface);
final String responseName = getResponseQueueName(syncInterface, requestId);
final String ackName = getAckName(syncInterface, requestId);
final RBlockingQueue<RemoteServiceRequest> requestQueue = redisson.getBlockingQueue(requestQueueName, getCodec());
final RemoteServiceRequest request = new RemoteServiceRequest(requestId, method.getName(), getMethodSignatures(method), args, optionsCopy, System.currentTimeMillis());
final RemotePromise<Object> result = new RemotePromise<Object>(commandExecutor.getConnectionManager().newPromise()) {
@Override
public boolean cancel(boolean mayInterruptIfRunning) {
if (isCancelled()) {
return true;
}
if (isDone()) {
return false;
}
if (optionsCopy.isAckExpected()) {
RFuture<Boolean> future = commandExecutor.evalWriteAsync(responseName, LongCodec.INSTANCE, RedisCommands.EVAL_BOOLEAN, "if redis.call('setnx', KEYS[1], 1) == 1 then " + "redis.call('pexpire', KEYS[1], ARGV[2]);" + "redis.call('lrem', KEYS[3], 1, ARGV[1]);" + "redis.call('pexpire', KEYS[2], ARGV[2]);" + "return 1;" + "end;" + "return 0;", Arrays.<Object>asList(ackName, responseName, requestQueueName), encode(request), request.getOptions().getAckTimeoutInMillis());
boolean ackNotSent = commandExecutor.get(future);
if (ackNotSent) {
super.cancel(mayInterruptIfRunning);
return true;
}
return cancel(syncInterface, requestId, request, mayInterruptIfRunning);
}
boolean removed = remove(requestQueue, request);
if (removed) {
super.cancel(mayInterruptIfRunning);
return true;
}
return cancel(syncInterface, requestId, request, mayInterruptIfRunning);
}
private boolean cancel(Class<?> remoteInterface, String requestId, RemoteServiceRequest request, boolean mayInterruptIfRunning) {
if (isCancelled()) {
return true;
}
if (isDone()) {
return false;
}
String canceRequestName = getCancelRequestQueueName(remoteInterface, requestId);
cancelExecution(optionsCopy, responseName, request, mayInterruptIfRunning, canceRequestName, this);
awaitUninterruptibly(60, TimeUnit.SECONDS);
return isCancelled();
}
};
result.setRequestId(requestId);
RFuture<Boolean> addFuture = addAsync(requestQueue, request, result);
addFuture.addListener(new FutureListener<Boolean>() {
@Override
public void operationComplete(Future<Boolean> future) throws Exception {
if (!future.isSuccess()) {
result.tryFailure(future.cause());
return;
}
if (optionsCopy.isAckExpected()) {
final RBlockingQueue<RemoteServiceAck> responseQueue = redisson.getBlockingQueue(responseName, getCodec());
RFuture<RemoteServiceAck> ackFuture = responseQueue.pollAsync(optionsCopy.getAckTimeoutInMillis(), TimeUnit.MILLISECONDS);
ackFuture.addListener(new FutureListener<RemoteServiceAck>() {
@Override
public void operationComplete(Future<RemoteServiceAck> future) throws Exception {
if (!future.isSuccess()) {
result.tryFailure(future.cause());
return;
}
RemoteServiceAck ack = future.getNow();
if (ack == null) {
RFuture<RemoteServiceAck> ackFutureAttempt = tryPollAckAgainAsync(optionsCopy, responseQueue, ackName);
ackFutureAttempt.addListener(new FutureListener<RemoteServiceAck>() {
@Override
public void operationComplete(Future<RemoteServiceAck> future) throws Exception {
if (!future.isSuccess()) {
result.tryFailure(future.cause());
return;
}
if (future.getNow() == null) {
Exception ex = new RemoteServiceAckTimeoutException("No ACK response after " + optionsCopy.getAckTimeoutInMillis() + "ms for request: " + request);
result.tryFailure(ex);
return;
}
awaitResultAsync(optionsCopy, result, request, responseName, ackName);
}
});
} else {
awaitResultAsync(optionsCopy, result, request, responseName);
}
}
});
} else {
awaitResultAsync(optionsCopy, result, request, responseName);
}
}
});
return result;
}
};
return (T) Proxy.newProxyInstance(remoteInterface.getClassLoader(), new Class[] { remoteInterface }, handler);
}
use of java.lang.reflect.InvocationHandler in project robovm by robovm.
the class ProxyTest method test_newProxyInstanceLjava_lang_ClassLoader$Ljava_lang_ClassLjava_lang_reflect_InvocationHandler.
/**
* java.lang.reflect.Proxy#newProxyInstance(java.lang.ClassLoader,
* java.lang.Class[], java.lang.reflect.InvocationHandler)
*/
public void test_newProxyInstanceLjava_lang_ClassLoader$Ljava_lang_ClassLjava_lang_reflect_InvocationHandler() {
Object p = Proxy.newProxyInstance(Support_Proxy_I1.class.getClassLoader(), new Class[] { Support_Proxy_I1.class, Support_Proxy_I2.class }, new InvocationHandler() {
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
if (method.getName().equals("equals"))
return new Boolean(proxy == args[0]);
if (method.getName().equals("array"))
return new int[] { (int) ((long[]) args[0])[1], -1 };
if (method.getName().equals("string")) {
if ("".equals(args[0]))
throw new Support_Proxy_SubException();
if ("clone".equals(args[0]))
throw new Support_Proxy_ParentException();
if ("error".equals(args[0]))
throw new ArrayStoreException();
if ("any".equals(args[0]))
throw new IllegalAccessException();
}
return null;
}
});
Support_Proxy_I1 proxy = (Support_Proxy_I1) p;
assertTrue("Failed identity test ", proxy.equals(proxy));
assertTrue("Failed not equals test ", !proxy.equals(""));
int[] result = (int[]) proxy.array(new long[] { 100L, -200L });
assertEquals("Failed primitive type conversion test ", -200, result[0]);
boolean worked = false;
try {
proxy.string("");
} catch (Support_Proxy_SubException e) {
worked = true;
} catch (Support_Proxy_ParentException e) {
// is never thrown
}
assertTrue("Problem converting exception ", worked);
worked = false;
try {
proxy.string("clone");
} catch (Support_Proxy_ParentException e) {
// is never thrown
} catch (UndeclaredThrowableException e) {
worked = true;
}
assertTrue("Problem converting exception ", worked);
worked = false;
try {
proxy.string("error");
} catch (Support_Proxy_ParentException e) {
// is never thrown
} catch (UndeclaredThrowableException e) {
} catch (RuntimeException e) {
worked = e.getClass() == ArrayStoreException.class;
}
assertTrue("Problem converting exception ", worked);
worked = false;
try {
proxy.string("any");
} catch (Support_Proxy_ParentException e) {
// is never thrown
} catch (UndeclaredThrowableException e) {
worked = true;
}
assertTrue("Problem converting exception ", worked);
Broken1 proxyObject = null;
try {
proxyObject = (Broken1) Proxy.newProxyInstance(Broken1.class.getClassLoader(), new Class[] { Broken1.class }, new Broken1Invoke());
} catch (Throwable e) {
fail("Failed to create proxy for class: " + Broken1.class + " - " + e);
}
float brokenResult = proxyObject.method(2.1f, 5.8f);
assertTrue("Invalid invoke result", brokenResult == 5.8f);
}
use of java.lang.reflect.InvocationHandler in project robovm by robovm.
the class Clash3InvocationHandler method main.
public static void main(String[] args) {
InvocationHandler handler = new Clash3InvocationHandler();
try {
Proxy.newProxyInstance(Clash.class.getClassLoader(), new Class[] { Interface3a.class, Interface3base.class, Interface3aa.class, Interface3b.class }, handler);
System.err.println("Clash3 did not throw expected exception");
} catch (IllegalArgumentException iae) {
System.out.println("Clash3 threw expected exception");
}
}
use of java.lang.reflect.InvocationHandler in project robovm by robovm.
the class Clash4InvocationHandler method main.
public static void main(String[] args) {
InvocationHandler handler = new Clash4InvocationHandler();
try {
Proxy.newProxyInstance(Clash.class.getClassLoader(), new Class[] { Interface4a.class, Interface4aa.class, Interface4base.class, Interface4b.class, Interface4bb.class }, handler);
System.err.println("Clash4 did not throw expected exception");
} catch (IllegalArgumentException iae) {
System.out.println("Clash4 threw expected exception");
//System.out.println(iae);
}
}
Aggregations