Example usage for java.lang.reflect Proxy isProxyClass

List of usage examples for java.lang.reflect Proxy isProxyClass

Introduction

In this page you can find the example usage for java.lang.reflect Proxy isProxyClass.

Prototype

public static boolean isProxyClass(Class<?> cl) 

Source Link

Document

Returns true if the given class is a proxy class.

Usage

From source file:com.lambdaworks.redis.support.ConnectionPoolSupportTest.java

@Test
public void wrappedConnectionShouldUseWrappers() throws Exception {

    GenericObjectPool<StatefulRedisConnection<String, String>> pool = ConnectionPoolSupport
            .createGenericObjectPool(() -> client.connect(), new GenericObjectPoolConfig());

    StatefulRedisConnection<String, String> connection = pool.borrowObject();
    RedisCommands<String, String> sync = connection.sync();

    assertThat(connection).isInstanceOf(StatefulRedisConnection.class)
            .isNotInstanceOf(StatefulRedisClusterConnectionImpl.class);
    assertThat(Proxy.isProxyClass(connection.getClass())).isTrue();

    assertThat(sync).isInstanceOf(RedisCommands.class);
    assertThat(connection.async()).isInstanceOf(RedisAsyncCommands.class)
            .isNotInstanceOf(RedisAsyncCommandsImpl.class);
    assertThat(connection.reactive()).isInstanceOf(RedisReactiveCommands.class)
            .isNotInstanceOf(RedisReactiveCommandsImpl.class);
    assertThat(sync.getStatefulConnection()).isInstanceOf(StatefulRedisConnection.class)
            .isNotInstanceOf(StatefulRedisConnectionImpl.class).isSameAs(connection);

    sync.close();//www .  j av  a  2  s  . c  om
    pool.close();
}

From source file:com.interface21.aop.framework.AopProxy.java

/**
 * Equality means interceptors and interfaces are ==.
 * This will only work with J2SE dynamic proxies,   not with CGLIB ones
 * (as CGLIB doesn't delegate equals calls to proxies).
 * @see java.lang.Object#equals(java.lang.Object)
 * @param other may be a dynamic proxy wrapping an instance
 * of this class/*ww w .j a  v a  2  s  .  c  om*/
 */
public boolean equals(Object other) {
    if (other == null)
        return false;
    if (other == this)
        return true;

    AopProxy aopr2 = null;
    if (other instanceof AopProxy) {
        aopr2 = (AopProxy) other;
    } else if (Proxy.isProxyClass(other.getClass())) {
        InvocationHandler ih = Proxy.getInvocationHandler(other);
        if (!(ih instanceof AopProxy))
            return false;
        aopr2 = (AopProxy) ih;
    } else {
        // Not a valid comparison
        return false;
    }

    // If we get here, aopr2 is the other AopProxy
    if (this == aopr2)
        return true;

    if (!Arrays.equals(aopr2.config.getProxiedInterfaces(), this.config.getProxiedInterfaces()))
        return false;

    // List equality is cool
    if (!aopr2.config.getMethodPointcuts().equals(this.config.getMethodPointcuts()))
        return false;

    return true;
}

From source file:io.lettuce.core.support.ConnectionPoolSupportTest.java

@Test
public void wrappedConnectionShouldUseWrappers() throws Exception {

    GenericObjectPool<StatefulRedisConnection<String, String>> pool = ConnectionPoolSupport
            .createGenericObjectPool(() -> client.connect(), new GenericObjectPoolConfig());

    StatefulRedisConnection<String, String> connection = pool.borrowObject();
    RedisCommands<String, String> sync = connection.sync();

    assertThat(connection).isInstanceOf(StatefulRedisConnection.class)
            .isNotInstanceOf(StatefulRedisClusterConnectionImpl.class);
    assertThat(Proxy.isProxyClass(connection.getClass())).isTrue();

    assertThat(sync).isInstanceOf(RedisCommands.class);
    assertThat(connection.async()).isInstanceOf(RedisAsyncCommands.class)
            .isNotInstanceOf(RedisAsyncCommandsImpl.class);
    assertThat(connection.reactive()).isInstanceOf(RedisReactiveCommands.class)
            .isNotInstanceOf(RedisReactiveCommandsImpl.class);
    assertThat(sync.getStatefulConnection()).isInstanceOf(StatefulRedisConnection.class)
            .isNotInstanceOf(StatefulRedisConnectionImpl.class).isSameAs(connection);

    connection.close();/*from   w w  w.  j  av a  2 s.  c o m*/
    pool.close();
}

From source file:com.lambdaworks.redis.support.ConnectionPoolSupportTest.java

@Test
public void wrappedMasterSlaveConnectionShouldUseWrappers() throws Exception {

    GenericObjectPool<StatefulRedisMasterSlaveConnection<String, String>> pool = ConnectionPoolSupport
            .createGenericObjectPool(/*from   w  w  w. j ava 2  s  . c o  m*/
                    () -> MasterSlave.connect(client, new StringCodec(), RedisURI.create(host, port)),
                    new GenericObjectPoolConfig());

    StatefulRedisMasterSlaveConnection<String, String> connection = pool.borrowObject();
    RedisCommands<String, String> sync = connection.sync();

    assertThat(connection).isInstanceOf(StatefulRedisMasterSlaveConnection.class);
    assertThat(Proxy.isProxyClass(connection.getClass())).isTrue();

    assertThat(sync).isInstanceOf(RedisCommands.class);
    assertThat(connection.async()).isInstanceOf(RedisAsyncCommands.class)
            .isNotInstanceOf(RedisAsyncCommandsImpl.class);
    assertThat(connection.reactive()).isInstanceOf(RedisReactiveCommands.class)
            .isNotInstanceOf(RedisReactiveCommandsImpl.class);
    assertThat(sync.getStatefulConnection()).isInstanceOf(StatefulRedisConnection.class)
            .isNotInstanceOf(StatefulRedisConnectionImpl.class).isSameAs(connection);

    sync.close();
    pool.close();
}

From source file:org.pushio.webapp.helper.json.SerializeConfig.java

public ObjectSerializer getObjectWriter(Class<?> clazz) {
    ObjectSerializer writer = get(clazz);

    if (writer == null) {
        try {/* w w  w.j a v a 2 s . c o m*/
            final ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
            for (Object o : ServiceLoader.load(AutowiredObjectSerializer.class, classLoader)) {
                if (!(o instanceof AutowiredObjectSerializer)) {
                    continue;
                }
                AutowiredObjectSerializer autowired = (AutowiredObjectSerializer) o;
                for (Type forType : autowired.getAutowiredFor()) {
                    put(forType, autowired);
                }
            }
        } catch (ClassCastException ex) {
            // skip
        }
        writer = get(clazz);
    }

    if (writer == null) {
        final ClassLoader classLoader = JSON.class.getClassLoader();
        if (classLoader != Thread.currentThread().getContextClassLoader()) {
            try {
                for (Object o : ServiceLoader.load(AutowiredObjectSerializer.class, classLoader)) {

                    if (!(o instanceof AutowiredObjectSerializer)) {
                        continue;
                    }

                    AutowiredObjectSerializer autowired = (AutowiredObjectSerializer) o;
                    for (Type forType : autowired.getAutowiredFor()) {
                        put(forType, autowired);
                    }
                }
            } catch (ClassCastException ex) {
                // skip
            }

            writer = get(clazz);
        }
    }

    if (writer == null) {
        if (Map.class.isAssignableFrom(clazz)) {
            put(clazz, MapSerializer.instance);
        } else if (List.class.isAssignableFrom(clazz)) {
            put(clazz, MyListSerializer.instance);
        } else if (Collection.class.isAssignableFrom(clazz)) {
            put(clazz, MyCollectionSerializer.instance);
        } else if (Date.class.isAssignableFrom(clazz)) {
            put(clazz, DateSerializer.instance);
        } else if (JSONAware.class.isAssignableFrom(clazz)) {
            put(clazz, JSONAwareSerializer.instance);
        } else if (JSONSerializable.class.isAssignableFrom(clazz)) {
            put(clazz, JSONSerializableSerializer.instance);
        } else if (JSONStreamAware.class.isAssignableFrom(clazz)) {
            put(clazz, JSONStreamAwareSerializer.instance);
        } else if (clazz.isEnum() || (clazz.getSuperclass() != null && clazz.getSuperclass().isEnum())) {
            put(clazz, EnumSerializer.instance);
        } else if (clazz.isArray()) {
            Class<?> componentType = clazz.getComponentType();
            ObjectSerializer compObjectSerializer = getObjectWriter(componentType);
            put(clazz, new ArraySerializer(componentType, compObjectSerializer));
        } else if (Throwable.class.isAssignableFrom(clazz)) {
            put(clazz, new ExceptionSerializer(clazz));
        } else if (TimeZone.class.isAssignableFrom(clazz)) {
            put(clazz, TimeZoneCodec.instance);
        } else if (Appendable.class.isAssignableFrom(clazz)) {
            put(clazz, AppendableSerializer.instance);
        } else if (Charset.class.isAssignableFrom(clazz)) {
            put(clazz, CharsetCodec.instance);
        } else if (Enumeration.class.isAssignableFrom(clazz)) {
            put(clazz, EnumerationSeriliazer.instance);
        } else if (Calendar.class.isAssignableFrom(clazz)) {
            put(clazz, CalendarCodec.instance);
        } else if (Clob.class.isAssignableFrom(clazz)) {
            put(clazz, ClobSeriliazer.instance);
        } else {
            boolean isCglibProxy = false;
            boolean isJavassistProxy = false;
            for (Class<?> item : clazz.getInterfaces()) {
                if (item.getName().equals("net.sf.cglib.proxy.Factory")
                        || item.getName().equals("org.springframework.cglib.proxy.Factory")) {
                    isCglibProxy = true;
                    break;
                } else if (item.getName().equals("javassist.util.proxy.ProxyObject")) {
                    isJavassistProxy = true;
                    break;
                }
            }

            if (isCglibProxy || isJavassistProxy) {
                Class<?> superClazz = clazz.getSuperclass();

                ObjectSerializer superWriter = getObjectWriter(superClazz);
                put(clazz, superWriter);

                return superWriter;
            }

            if (Proxy.isProxyClass(clazz)) {
                put(clazz, createJavaBeanSerializer(clazz));
            } else {
                put(clazz, createJavaBeanSerializer(clazz));
            }

        }

        writer = get(clazz);
    }
    return writer;
}

From source file:gov.nih.nci.firebird.proxy.FaultTolerantProxyFactoryTest.java

private void verifyProxyChain(TestClientI wrappedClient) throws Exception {
    assertTrue(Proxy.isProxyClass(wrappedClient.getClass()));
    RetryHandler retryHandler = (RetryHandler) Proxy.getInvocationHandler(wrappedClient);
    Provider<TestClientI> provider = (Provider<TestClientI>) retryHandler.getClientProvider();

    TestClientI pooledClient = provider.get();
    assertTrue(Proxy.isProxyClass(pooledClient.getClass()));
    PoolingHandler poolingHandler = (PoolingHandler) Proxy.getInvocationHandler(pooledClient);
    ObjectPool<Object> pool = poolingHandler.getPool();
    TestClientI client = (TestClientI) pool.borrowObject();
    assertNotNull(client);//from  www.ja v  a  2 s.c om

    assertTrue(retryHandler.getValidExceptions().containsAll(VALID_EXCEPTIONS));
    assertTrue(poolingHandler.getValidExceptions().containsAll(VALID_EXCEPTIONS));
}

From source file:org.springframework.aop.framework.OptimizedJdkDynamicAopProxy.java

/**
 * Equality means interceptors and interfaces and
 * TargetSource are equal./* w w w .  ja  v  a 2 s. c  o  m*/
 * @see java.lang.Object#equals(java.lang.Object)
 * @param other may be a dynamic proxy wrapping an instance
 * of this class
 */
public boolean equals(Object other) {
    if (other == null)
        return false;
    if (other == this)
        return true;

    OptimizedJdkDynamicAopProxy aopr2 = null;
    if (other instanceof OptimizedJdkDynamicAopProxy) {
        aopr2 = (OptimizedJdkDynamicAopProxy) other;
    } else if (Proxy.isProxyClass(other.getClass())) {
        InvocationHandler ih = Proxy.getInvocationHandler(other);
        if (!(ih instanceof OptimizedJdkDynamicAopProxy))
            return false;
        aopr2 = (OptimizedJdkDynamicAopProxy) ih;
    } else {
        // Not a valid comparison
        return false;
    }

    // If we get here, aopr2 is the other AopProxy
    return AopProxyUtils.equalsInProxy(this.advised, aopr2.advised);
}

From source file:io.lettuce.core.support.ConnectionPoolSupportTest.java

@Test
public void wrappedMasterSlaveConnectionShouldUseWrappers() throws Exception {

    GenericObjectPool<StatefulRedisMasterSlaveConnection<String, String>> pool = ConnectionPoolSupport
            .createGenericObjectPool(/*from  www  .j  a v a 2 s .c  o  m*/
                    () -> MasterSlave.connect(client, new StringCodec(), RedisURI.create(host, port)),
                    new GenericObjectPoolConfig());

    StatefulRedisMasterSlaveConnection<String, String> connection = pool.borrowObject();
    RedisCommands<String, String> sync = connection.sync();

    assertThat(connection).isInstanceOf(StatefulRedisMasterSlaveConnection.class);
    assertThat(Proxy.isProxyClass(connection.getClass())).isTrue();

    assertThat(sync).isInstanceOf(RedisCommands.class);
    assertThat(connection.async()).isInstanceOf(RedisAsyncCommands.class)
            .isNotInstanceOf(RedisAsyncCommandsImpl.class);
    assertThat(connection.reactive()).isInstanceOf(RedisReactiveCommands.class)
            .isNotInstanceOf(RedisReactiveCommandsImpl.class);
    assertThat(sync.getStatefulConnection()).isInstanceOf(StatefulRedisConnection.class)
            .isNotInstanceOf(StatefulRedisConnectionImpl.class).isSameAs(connection);

    connection.close();
    pool.close();
}

From source file:io.coala.enterprise.Fact.java

/**
 * @param subtype the type of {@link Fact} to mimic
 * @param callObserver an {@link Observer} of method call, or {@code null}
 * @return the {@link Proxy} instance//w  ww  . ja  va  2 s .  c om
 */
@SuppressWarnings("unchecked")
static <F extends Fact> F proxyAs(final Fact impl, final Class<F> subtype,
        final Observer<Method> callObserver) {
    final F proxy = (F) Proxy.newProxyInstance(subtype.getClassLoader(), new Class<?>[] { subtype },
            (self, method, args) -> {
                try {
                    final Object result = method.isDefault() && Proxy.isProxyClass(self.getClass())
                            ? ReflectUtil.invokeDefaultMethod(self, method, args)
                            : method.invoke(impl, args);
                    if (callObserver != null)
                        callObserver.onNext(method);
                    return result;
                } catch (Throwable e) {
                    if (e instanceof IllegalArgumentException)
                        try {
                            return ReflectUtil.invokeAsBean(impl.properties(), subtype, method, args);
                        } catch (final Exception ignore) {
                            LogUtil.getLogger(Fact.class).warn("{}method call failed: {}",
                                    method.isDefault() ? "default " : "", method, ignore);
                        }
                    if (e instanceof InvocationTargetException)
                        e = e.getCause();
                    if (callObserver != null)
                        callObserver.onError(e);
                    throw e;
                }
            });
    return proxy;
}

From source file:com.lambdaworks.redis.support.ConnectionPoolSupportTest.java

@Test
public void wrappedClusterConnectionShouldUseWrappers() throws Exception {

    RedisClusterClient redisClusterClient = RedisClusterClient.create(TestClientResources.create(),
            RedisURI.create(TestSettings.host(), 7379));

    GenericObjectPool<StatefulRedisClusterConnection<String, String>> pool = ConnectionPoolSupport
            .createGenericObjectPool(redisClusterClient::connect, new GenericObjectPoolConfig());

    StatefulRedisClusterConnection<String, String> connection = pool.borrowObject();
    RedisAdvancedClusterCommands<String, String> sync = connection.sync();

    assertThat(connection).isInstanceOf(StatefulRedisClusterConnection.class)
            .isNotInstanceOf(StatefulRedisClusterConnectionImpl.class);
    assertThat(Proxy.isProxyClass(connection.getClass())).isTrue();

    assertThat(sync).isInstanceOf(RedisAdvancedClusterCommands.class);
    assertThat(connection.async()).isInstanceOf(RedisAdvancedClusterAsyncCommands.class)
            .isNotInstanceOf(RedisAdvancedClusterAsyncCommandsImpl.class);
    assertThat(connection.reactive()).isInstanceOf(RedisAdvancedClusterReactiveCommands.class)
            .isNotInstanceOf(RedisAdvancedClusterReactiveCommandsImpl.class);
    assertThat(sync.getStatefulConnection()).isInstanceOf(StatefulRedisClusterConnection.class)
            .isNotInstanceOf(StatefulRedisClusterConnectionImpl.class).isSameAs(connection);

    sync.close();//  w ww . j  av  a2 s  .  c  o m
    pool.close();

    FastShutdown.shutdown(redisClusterClient);
}