use of java.lang.reflect.InvocationHandler in project spring-data-mongodb by spring-projects.
the class SimpleMongoDbFactoryUnitTests method cascadedWithSessionUsesRootFactory.
// DATAMONGO-1880
@Test
public void cascadedWithSessionUsesRootFactory() {
when(mongo.getDatabase("foo")).thenReturn(database);
MongoDbFactory factory = new SimpleMongoDbFactory(mongo, "foo");
MongoDbFactory wrapped = factory.withSession(clientSession).withSession(clientSession);
InvocationHandler invocationHandler = Proxy.getInvocationHandler(wrapped.getDb());
Object singletonTarget = AopProxyUtils.getSingletonTarget(ReflectionTestUtils.getField(invocationHandler, "advised"));
assertThat(singletonTarget, is(sameInstance(database)));
}
use of java.lang.reflect.InvocationHandler in project spring-data-mongodb by spring-projects.
the class SimpleReactiveMongoDatabaseFactoryUnitTests method cascadedWithSessionUsesRootFactory.
// DATAMONGO-1880
@Test
public void cascadedWithSessionUsesRootFactory() {
when(mongoClient.getDatabase("foo")).thenReturn(database);
ReactiveMongoDatabaseFactory factory = new SimpleReactiveMongoDatabaseFactory(mongoClient, "foo");
ReactiveMongoDatabaseFactory wrapped = factory.withSession(clientSession).withSession(clientSession);
InvocationHandler invocationHandler = Proxy.getInvocationHandler(wrapped.getMongoDatabase());
Object singletonTarget = AopProxyUtils.getSingletonTarget(ReflectionTestUtils.getField(invocationHandler, "advised"));
assertThat(singletonTarget, is(sameInstance(database)));
}
use of java.lang.reflect.InvocationHandler in project suite by stupidsing.
the class CacheUtil method proxy.
public <I> I proxy(Class<I> interface_, I object, Collection<Method> methods) {
InvocationHandler handler = (proxy, method, ps) -> {
Key key = methods.contains(method) ? new Key(object, method, ps) : null;
boolean isCached = key != null && results.containsKey(key);
Object result;
if (!isCached)
try {
results.put(key, result = method.invoke(object, ps));
} catch (InvocationTargetException ite) {
Throwable th = ite.getTargetException();
throw th instanceof Exception ? (Exception) th : ite;
}
else
result = results.get(key);
return result;
};
@SuppressWarnings("unchecked") Class<I> clazz = (Class<I>) object.getClass();
ClassLoader classLoader = clazz.getClassLoader();
Class<?>[] classes = { interface_ };
@SuppressWarnings("unchecked") I proxied = (I) Proxy.newProxyInstance(classLoader, classes, handler);
return proxied;
}
use of java.lang.reflect.InvocationHandler in project suite by stupidsing.
the class SynchronizeUtil method proxy.
public static <I> I proxy(Class<I> interface_, I object) {
@SuppressWarnings("unchecked") Class<I> clazz = (Class<I>) object.getClass();
ClassLoader classLoader = clazz.getClassLoader();
Class<?>[] classes = { interface_ };
InvocationHandler handler = (proxy, method, ps) -> {
synchronized (object) {
try {
return method.invoke(object, ps);
} catch (InvocationTargetException ite) {
Throwable th = ite.getTargetException();
throw th instanceof Exception ? (Exception) th : ite;
}
}
};
@SuppressWarnings("unchecked") I proxied = (I) Proxy.newProxyInstance(classLoader, classes, handler);
return proxied;
}
use of java.lang.reflect.InvocationHandler in project jdbc-shards by wplatform.
the class SmartConnection method newInstance.
/**
* Creates a exception trace version of a connection
*
* @param conn - the original connection
* @return - the connection with exception trace
* @throws SQLException
*/
public static Connection newInstance(DataSourceRepository database, SmartDataSource dataSource, String username, String password) {
InvocationHandler handler = new SmartConnection(database, dataSource, username, password);
ClassLoader cl = Connection.class.getClassLoader();
return (Connection) Proxy.newProxyInstance(cl, new Class[] { Connection.class }, handler);
}
Aggregations