Search in sources :

Example 66 with InvocationHandler

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);
}
Also used : InvocationHandler(java.lang.reflect.InvocationHandler)

Example 67 with 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);
    }
}
Also used : Consumer(java.util.function.Consumer) Proxy(java.lang.reflect.Proxy) Logger(org.slf4j.Logger) Application(com.apple.eawt.Application) LoggerFactory(org.slf4j.LoggerFactory) InvocationHandler(java.lang.reflect.InvocationHandler) Method(java.lang.reflect.Method) InvocationTargetException(java.lang.reflect.InvocationTargetException) InvocationHandler(java.lang.reflect.InvocationHandler) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 68 with InvocationHandler

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);
    }
}
Also used : Consumer(java.util.function.Consumer) Proxy(java.lang.reflect.Proxy) Logger(org.slf4j.Logger) Application(com.apple.eawt.Application) LoggerFactory(org.slf4j.LoggerFactory) InvocationHandler(java.lang.reflect.InvocationHandler) Method(java.lang.reflect.Method) InvocationTargetException(java.lang.reflect.InvocationTargetException) InvocationHandler(java.lang.reflect.InvocationHandler) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 69 with InvocationHandler

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();
        }
    }));
}
Also used : Receiver(org.gradle.process.internal.worker.request.Receiver) RequestProtocol(org.gradle.process.internal.worker.request.RequestProtocol) Method(java.lang.reflect.Method) InvocationHandler(java.lang.reflect.InvocationHandler) ObjectConnection(org.gradle.internal.remote.ObjectConnection)

Example 70 with InvocationHandler

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;
        }
    });
}
Also used : DefaultClassPath(org.gradle.internal.classpath.DefaultClassPath) ClassPath(org.gradle.internal.classpath.ClassPath) Method(java.lang.reflect.Method) ScalaMethod(org.gradle.scala.internal.reflect.ScalaMethod) InvocationHandler(java.lang.reflect.InvocationHandler) UncheckedException(org.gradle.internal.UncheckedException) IOException(java.io.IOException) InvocationTargetException(java.lang.reflect.InvocationTargetException) URLClassLoader(java.net.URLClassLoader) URLClassLoader(java.net.URLClassLoader) DefaultClassPath(org.gradle.internal.classpath.DefaultClassPath)

Aggregations

InvocationHandler (java.lang.reflect.InvocationHandler)411 Method (java.lang.reflect.Method)232 Test (org.junit.Test)70 InvocationTargetException (java.lang.reflect.InvocationTargetException)54 Proxy (java.lang.reflect.Proxy)28 ArrayList (java.util.ArrayList)25 Map (java.util.Map)23 IOException (java.io.IOException)19 Field (java.lang.reflect.Field)19 HashMap (java.util.HashMap)18 AccessibleObject (java.lang.reflect.AccessibleObject)16 List (java.util.List)16 BindingProvider (javax.xml.ws.BindingProvider)14 PersistentClass (org.hibernate.mapping.PersistentClass)12 RootClass (org.hibernate.mapping.RootClass)12 Before (org.junit.Before)10 Connection (java.sql.Connection)9 LinkedHashMap (java.util.LinkedHashMap)9 AbstractQueryFacade (org.jboss.tools.hibernate.runtime.common.AbstractQueryFacade)8 DexMakerTest (com.android.dx.DexMakerTest)7