use of java.lang.reflect.InvocationHandler in project jo-client-platform by jo-source.
the class GenericServiceAsyncDecorator method decorate.
@Override
public Object decorate(final Object original) {
Assert.paramNotNull(original, "original");
final InvocationHandler invocationHandler = new AsyncInvocationHandler(original);
return Proxy.newProxyInstance(serviceType.getClassLoader(), new Class[] { serviceType }, invocationHandler);
}
use of java.lang.reflect.InvocationHandler in project cayenne by apache.
the class OSXApplicationWrapper method setHandler.
private void setHandler(Method setMethod, Class<?> handlerClass, Runnable action) {
InvocationHandler handler = (proxy, method, args) -> {
action.run();
return null;
};
Object proxy = createProxy(handlerClass, handler);
try {
setMethod.invoke(application, proxy);
} catch (IllegalAccessException | InvocationTargetException ex) {
logger.warn("Unable to call " + setMethod.getName(), ex);
}
}
use of java.lang.reflect.InvocationHandler in project cayenne by apache.
the class OSXApplicationWrapper method setQuitHandler.
public void setQuitHandler(Consumer<OSXQuitResponseWrapper> action) {
InvocationHandler handler = (proxy, method, args) -> {
// args: 0 - event, 1 - quitResponse
action.accept(new OSXQuitResponseWrapper(args[1]));
return null;
};
Object proxy = createProxy(quitHandlerClass, handler);
try {
setQuitHandler.invoke(application, proxy);
} catch (IllegalAccessException | InvocationTargetException ex) {
logger.warn("Unable to call " + setQuitHandler.getName(), ex);
}
}
use of java.lang.reflect.InvocationHandler in project gradle by gradle.
the class DefaultSingleRequestWorkerProcessBuilder method build.
@Override
public PROTOCOL build() {
return protocolType.cast(Proxy.newProxyInstance(protocolType.getClassLoader(), new Class[] { protocolType }, new InvocationHandler() {
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
Receiver receiver = new Receiver(getBaseName());
try {
WorkerProcess workerProcess = builder.build();
workerProcess.start();
ObjectConnection connection = workerProcess.getConnection();
RequestProtocol requestProtocol = connection.addOutgoing(RequestProtocol.class);
connection.addIncoming(ResponseProtocol.class, receiver);
connection.useJavaSerializationForParameters(workerImplementation.getClassLoader());
connection.connect();
// TODO(ew): inject BuildOperationIdentifierRegistry instead of static use
requestProtocol.runThenStop(method.getName(), method.getParameterTypes(), args, CurrentBuildOperationRef.instance().get());
boolean hasResult = receiver.awaitNextResult();
workerProcess.waitForStop();
if (!hasResult) {
// Reached the end of input, worker has exited without failing
throw new IllegalStateException(String.format("No response was received from %s but the worker process has finished.", getBaseName()));
}
} catch (Exception e) {
throw WorkerProcessException.runFailed(getBaseName(), e);
}
return receiver.getNextResult();
}
}));
}
use of java.lang.reflect.InvocationHandler in project gradle by gradle.
the class DefaultVersionedPlayRunAdapter method getBuildLink.
@Override
public Object getBuildLink(final ClassLoader classLoader, final Reloader reloader, final File projectPath, final File applicationJar, final Iterable<File> changingClasspath, final File assetsJar, final Iterable<File> assetsDirs) throws ClassNotFoundException {
final ClassLoader assetsClassLoader = createAssetsClassLoader(assetsJar, assetsDirs, classLoader);
final Class<? extends Throwable> playExceptionClass = Cast.uncheckedCast(classLoader.loadClass(PLAY_EXCEPTION_CLASSNAME));
return Proxy.newProxyInstance(classLoader, new Class<?>[] { getBuildLinkClass(classLoader) }, new InvocationHandler() {
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
if (method.getName().equals("projectPath")) {
return projectPath;
} else if (method.getName().equals("reload")) {
Reloader.Result result = reloader.requireUpToDate();
// We can't close replaced loaders immediately, because their classes may be used during shutdown,
// after the return of the reload() call that caused the loader to be swapped out.
// We have no way of knowing when the loader is actually done with, so we use the request after the request
// that triggered the reload as the trigger point to close the replaced loader.
closeOldLoaders();
if (result.changed) {
ClassPath classpath = new DefaultClassPath(applicationJar).plus(new DefaultClassPath(changingClasspath));
URLClassLoader currentClassLoader = new URLClassLoader(classpath.getAsURLArray(), assetsClassLoader);
storeClassLoader(currentClassLoader);
return currentClassLoader;
} else {
Throwable failure = result.failure;
if (failure == null) {
return null;
} else {
try {
return DirectInstantiator.instantiate(playExceptionClass, "Gradle Build Failure", failure.getMessage(), failure);
} catch (Exception e) {
LOGGER.warn("Could not translate " + failure + " to " + PLAY_EXCEPTION_CLASSNAME, e);
return failure;
}
}
}
} else if (method.getName().equals("settings")) {
return new HashMap<String, String>();
}
// TODO: all methods
return null;
}
});
}
Aggregations