Search in sources :

Example 11 with InvocationHandler

use of java.lang.reflect.InvocationHandler in project qi4j-sdk by Qi4j.

the class SideEffectsModel method newInstance.

// Context
public SideEffectsInstance newInstance(Method method, ModuleInstance moduleInstance, InvocationHandler invoker) {
    ProxyReferenceInvocationHandler proxyHandler = new ProxyReferenceInvocationHandler();
    SideEffectInvocationHandlerResult result = new SideEffectInvocationHandlerResult();
    List<InvocationHandler> sideEffects = new ArrayList<InvocationHandler>(sideEffectModels.size());
    for (SideEffectModel sideEffectModel : sideEffectModels) {
        InvocationHandler sideEffect = sideEffectModel.newInstance(moduleInstance, result, proxyHandler, method);
        sideEffects.add(sideEffect);
    }
    return new SideEffectsInstance(sideEffects, result, proxyHandler, invoker);
}
Also used : ArrayList(java.util.ArrayList) InvocationHandler(java.lang.reflect.InvocationHandler)

Example 12 with InvocationHandler

use of java.lang.reflect.InvocationHandler in project qi4j-sdk by Qi4j.

the class AbstractModifierModel method newInstance.

// Context
public InvocationHandler newInstance(ModuleInstance moduleInstance, InvocationHandler next, ProxyReferenceInvocationHandler proxyHandler, Method method) {
    InjectionContext injectionContext = new InjectionContext(moduleInstance, wrapNext(next), proxyHandler);
    Object modifier = constructorsModel.newInstance(injectionContext);
    try {
        if (FragmentClassLoader.isGenerated(modifier)) {
            modifier.getClass().getField("_instance").set(modifier, proxyHandler);
        }
    } catch (IllegalAccessException | NoSuchFieldException e) {
        e.printStackTrace();
    }
    injectedFieldsModel.inject(injectionContext, modifier);
    injectedMethodsModel.inject(injectionContext, modifier);
    if (isGeneric()) {
        return (InvocationHandler) modifier;
    } else {
        try {
            Method invocationMethod = modifierClass.getMethod("_" + method.getName(), method.getParameterTypes());
            TypedModifierInvocationHandler handler = new TypedModifierInvocationHandler();
            handler.setFragment(modifier);
            handler.setMethod(invocationMethod);
            return handler;
        } catch (NoSuchMethodException e) {
            throw new ConstructionException("Could not find modifier method", e);
        }
    }
}
Also used : Method(java.lang.reflect.Method) ConstructionException(org.qi4j.api.common.ConstructionException) InvocationHandler(java.lang.reflect.InvocationHandler) InjectionContext(org.qi4j.runtime.injection.InjectionContext)

Example 13 with InvocationHandler

use of java.lang.reflect.InvocationHandler in project qi4j-sdk by Qi4j.

the class DataSources method wrapWithCircuitBreaker.

public static DataSource wrapWithCircuitBreaker(final String dataSourceIdentity, final DataSource pool, final CircuitBreaker circuitBreaker) {
    // Create wrapper
    InvocationHandler handler = new InvocationHandler() {

        @Override
        public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
            if (!circuitBreaker.isOn()) {
                Throwable throwable = circuitBreaker.lastThrowable();
                if (throwable != null) {
                    throw throwable;
                } else {
                    throw new ServiceImporterException("Circuit breaker for DataSource " + dataSourceIdentity + " is not on");
                }
            }
            try {
                Object result = method.invoke(pool, args);
                circuitBreaker.success();
                return result;
            } catch (IllegalAccessException e) {
                circuitBreaker.throwable(e);
                throw e;
            } catch (IllegalArgumentException e) {
                circuitBreaker.throwable(e);
                throw e;
            } catch (InvocationTargetException e) {
                circuitBreaker.throwable(e.getCause());
                throw e.getCause();
            }
        }
    };
    // Create proxy with circuit breaker
    return (DataSource) Proxy.newProxyInstance(DataSource.class.getClassLoader(), new Class[] { DataSource.class }, handler);
}
Also used : ServiceImporterException(org.qi4j.api.service.ServiceImporterException) Method(java.lang.reflect.Method) InvocationHandler(java.lang.reflect.InvocationHandler) InvocationTargetException(java.lang.reflect.InvocationTargetException) DataSource(javax.sql.DataSource)

Example 14 with InvocationHandler

use of java.lang.reflect.InvocationHandler in project XPrivacy by M66B.

the class XGoogleApiClient method getPendingResult.

public static Object getPendingResult(ClassLoader loader) throws Throwable {
    InvocationHandler ih = new PendingResultHandler(loader);
    Class<?> pr = Class.forName("com.google.android.gms.common.api.PendingResult", false, loader);
    return Proxy.newProxyInstance(loader, new Class<?>[] { pr }, ih);
}
Also used : InvocationHandler(java.lang.reflect.InvocationHandler)

Example 15 with InvocationHandler

use of java.lang.reflect.InvocationHandler in project XPrivacy by M66B.

the class XLocationManager method proxyLocationListener.

private void proxyLocationListener(XParam param, int arg, Class<?> interfaze, boolean client) throws Throwable {
    if (param.args.length > arg)
        if (param.args[arg] instanceof PendingIntent)
            param.setResult(null);
        else if (param.args[arg] != null && param.thisObject != null) {
            if (client) {
                Object key = param.args[arg];
                synchronized (mMapProxy) {
                    // Reuse existing proxy
                    if (mMapProxy.containsKey(key)) {
                        Util.log(this, Log.INFO, "Reuse existing proxy uid=" + Binder.getCallingUid());
                        param.args[arg] = mMapProxy.get(key);
                        return;
                    }
                    // Already proxied
                    if (mMapProxy.containsValue(key)) {
                        Util.log(this, Log.INFO, "Already proxied uid=" + Binder.getCallingUid());
                        return;
                    }
                }
                // Create proxy
                Util.log(this, Log.INFO, "Creating proxy uid=" + Binder.getCallingUid());
                Object proxy = new ProxyLocationListener(Binder.getCallingUid(), (LocationListener) param.args[arg]);
                // Use proxy
                synchronized (mMapProxy) {
                    mMapProxy.put(key, proxy);
                }
                param.args[arg] = proxy;
            } else {
                // Create proxy
                ClassLoader cl = param.thisObject.getClass().getClassLoader();
                InvocationHandler ih = new OnLocationChangedHandler(Binder.getCallingUid(), param.args[arg]);
                Object proxy = Proxy.newProxyInstance(cl, new Class<?>[] { interfaze }, ih);
                Object key = param.args[arg];
                if (key instanceof IInterface)
                    key = ((IInterface) key).asBinder();
                // Use proxy
                synchronized (mMapProxy) {
                    mMapProxy.put(key, proxy);
                }
                param.args[arg] = proxy;
            }
        }
}
Also used : IInterface(android.os.IInterface) PendingIntent(android.app.PendingIntent) 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