use of java.lang.reflect.InvocationHandler in project buck by facebook.
the class Connection method setRemoteInterface.
@SuppressWarnings("unchecked")
public void setRemoteInterface(Class<REMOTE> remoteInterface, ClassLoader classLoader) {
checkNotClose();
InvocationHandler invocationHandler = (proxy, method, args) -> {
InvocationMessage invocation = new InvocationMessage(method.getName(), Arrays.asList(args));
ReturnResultMessage response = messageTransport.sendMessageAndWaitForResponse(invocation);
return response.getValue();
};
this.remoteObjectProxy = (REMOTE) Proxy.newProxyInstance(classLoader, new Class[] { remoteInterface }, invocationHandler);
}
use of java.lang.reflect.InvocationHandler in project elasticsearch by elastic.
the class FactoryProvider method get.
@Override
public F get() {
InvocationHandler invocationHandler = new InvocationHandler() {
@Override
public Object invoke(Object proxy, Method method, Object[] creationArgs) throws Throwable {
// pass methods from Object.class to the proxy
if (method.getDeclaringClass().equals(Object.class)) {
return method.invoke(this, creationArgs);
}
AssistedConstructor<?> constructor = factoryMethodToConstructor.get(method);
Object[] constructorArgs = gatherArgsForConstructor(constructor, creationArgs);
Object objectToReturn = constructor.newInstance(constructorArgs);
injector.injectMembers(objectToReturn);
return objectToReturn;
}
public Object[] gatherArgsForConstructor(AssistedConstructor<?> constructor, Object[] factoryArgs) {
int numParams = constructor.getAllParameters().size();
int argPosition = 0;
Object[] result = new Object[numParams];
for (int i = 0; i < numParams; i++) {
Parameter parameter = constructor.getAllParameters().get(i);
if (parameter.isProvidedByFactory()) {
result[i] = factoryArgs[argPosition];
argPosition++;
} else {
result[i] = parameter.getValue(injector);
}
}
return result;
}
};
// we imprecisely treat the class literal of T as a Class<T>
@SuppressWarnings("unchecked") Class<F> factoryRawType = (Class) factoryType.getRawType();
return factoryRawType.cast(Proxy.newProxyInstance(factoryRawType.getClassLoader(), new Class[] { factoryRawType }, invocationHandler));
}
use of java.lang.reflect.InvocationHandler in project qi4j-sdk by Qi4j.
the class DecoratorMixinTest method testDecorationWithGenericMixin.
// END SNIPPET: test
@Test
public void testDecorationWithGenericMixin() {
InvocationHandler handler = new FooModelInvocationHandler("Init");
ClassLoader cl = getClass().getClassLoader();
FooModel model = (FooModel) Proxy.newProxyInstance(cl, new Class[] { FooModel.class }, handler);
View1 view1 = createView1(model);
View2 view2 = createView2(model);
assertThat(view1.bar(), equalTo("Init"));
assertThat(view2.bar(), equalTo("Init"));
model.setBar("New Value");
assertThat(view1.bar(), equalTo("New Value"));
assertThat(view2.bar(), equalTo("New Value"));
}
use of java.lang.reflect.InvocationHandler in project qi4j-sdk by Qi4j.
the class ConcernsModel method newInstance.
// Context
public ConcernsInstance newInstance(Method method, ModuleInstance moduleInstance, FragmentInvocationHandler mixinInvocationHandler) {
ProxyReferenceInvocationHandler proxyHandler = new ProxyReferenceInvocationHandler();
InvocationHandler nextConcern = mixinInvocationHandler;
for (int i = concernsFor.size() - 1; i >= 0; i--) {
ConcernModel concernModel = concernsFor.get(i);
nextConcern = concernModel.newInstance(moduleInstance, nextConcern, proxyHandler, method);
}
return new ConcernsInstance(nextConcern, mixinInvocationHandler, proxyHandler);
}
use of java.lang.reflect.InvocationHandler in project qi4j-sdk by Qi4j.
the class SideEffectsInstance method invokeSideEffects.
private void invokeSideEffects(Object proxy, Method method, Object[] params, Object result, Throwable originalThrowable) throws Throwable {
proxyHandler.setProxy(proxy);
resultInvocationHandler.setResult(result, originalThrowable);
try {
for (InvocationHandler sideEffect : sideEffects) {
try {
sideEffect.invoke(proxy, method, params);
} catch (Throwable throwable) {
if (throwable != originalThrowable) {
throwable.printStackTrace();
}
}
}
} finally {
proxyHandler.clearProxy();
resultInvocationHandler.setResult(null, null);
}
}
Aggregations