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);
}
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);
}
}
}
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);
}
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);
}
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;
}
}
}
Aggregations