Search in sources :

Example 6 with InvocationHandler

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);
}
Also used : Arrays(java.util.Arrays) Proxy(java.lang.reflect.Proxy) Preconditions(com.google.common.base.Preconditions) InvocationHandler(java.lang.reflect.InvocationHandler) Nullable(javax.annotation.Nullable) InvocationHandler(java.lang.reflect.InvocationHandler)

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

Example 8 with 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"));
}
Also used : InvocationHandler(java.lang.reflect.InvocationHandler) AbstractQi4jTest(org.qi4j.test.AbstractQi4jTest) Test(org.junit.Test)

Example 9 with InvocationHandler

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

Example 10 with InvocationHandler

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

Aggregations

InvocationHandler (java.lang.reflect.InvocationHandler)179 Method (java.lang.reflect.Method)97 InvocationTargetException (java.lang.reflect.InvocationTargetException)24 Test (org.junit.Test)19 IOException (java.io.IOException)11 Proxy (java.lang.reflect.Proxy)10 Field (java.lang.reflect.Field)9 AccessibleObject (java.lang.reflect.AccessibleObject)7 ArrayList (java.util.ArrayList)6 NotNull (org.jetbrains.annotations.NotNull)6 Support_Proxy_I1 (tests.support.Support_Proxy_I1)6 Callable (java.util.concurrent.Callable)5 MBeanServerInvocationHandler (javax.management.MBeanServerInvocationHandler)5 LinkedHashMap (java.util.LinkedHashMap)4 Map (java.util.Map)4 UndeclaredThrowableException (java.lang.reflect.UndeclaredThrowableException)3 RemoteObjectInvocationHandler (java.rmi.server.RemoteObjectInvocationHandler)3 Connection (java.sql.Connection)3 SQLException (java.sql.SQLException)3 HashMap (java.util.HashMap)3