Search in sources :

Example 71 with InvocationHandler

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)));
}
Also used : MongoDbFactory(org.springframework.data.mongodb.MongoDbFactory) InvocationHandler(java.lang.reflect.InvocationHandler) Test(org.junit.Test)

Example 72 with InvocationHandler

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)));
}
Also used : ReactiveMongoDatabaseFactory(org.springframework.data.mongodb.ReactiveMongoDatabaseFactory) InvocationHandler(java.lang.reflect.InvocationHandler) Test(org.junit.Test)

Example 73 with InvocationHandler

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;
}
Also used : Arrays(java.util.Arrays) Proxy(java.lang.reflect.Proxy) Object_(suite.util.Object_) Collection(java.util.Collection) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Set(java.util.Set) Fun(suite.util.FunUtil.Fun) InvocationTargetException(java.lang.reflect.InvocationTargetException) HashSet(java.util.HashSet) Objects(java.util.Objects) List(java.util.List) Map(java.util.Map) InvocationHandler(java.lang.reflect.InvocationHandler) Method(java.lang.reflect.Method) InvocationHandler(java.lang.reflect.InvocationHandler) InvocationTargetException(java.lang.reflect.InvocationTargetException) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 74 with InvocationHandler

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

Example 75 with InvocationHandler

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

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