Example usage for org.springframework.aop.support AopUtils isJdkDynamicProxy

List of usage examples for org.springframework.aop.support AopUtils isJdkDynamicProxy

Introduction

In this page you can find the example usage for org.springframework.aop.support AopUtils isJdkDynamicProxy.

Prototype

public static boolean isJdkDynamicProxy(@Nullable Object object) 

Source Link

Document

Check whether the given object is a JDK dynamic proxy.

Usage

From source file:cn.guoyukun.spring.utils.AopProxyUtils.java

/**
 * ??//  w  w w  .  ja v a 2s.c  om
 * see http://jinnianshilongnian.iteye.com/blog/1894465
 * @param proxy
 * @return
 */
public static boolean isMultipleProxy(Object proxy) {
    try {
        ProxyFactory proxyFactory = null;
        if (AopUtils.isJdkDynamicProxy(proxy)) {
            proxyFactory = findJdkDynamicProxyFactory(proxy);
        }
        if (AopUtils.isCglibProxy(proxy)) {
            proxyFactory = findCglibProxyFactory(proxy);
        }
        TargetSource targetSource = (TargetSource) ReflectionUtils.getField(ProxyFactory_targetSource_FIELD,
                proxyFactory);
        return AopUtils.isAopProxy(targetSource.getTarget());
    } catch (Exception e) {
        throw new IllegalArgumentException(
                "proxy args maybe not proxy with cglib or jdk dynamic proxy. this method not support", e);
    }
}

From source file:com.dangdang.ddframe.job.lite.spring.util.AopTargetUtils.java

/**
 * ?.//from  w  w w  .ja va  2s.c  om
 * 
 * @param proxy ?
 * @return 
 */
public static Object getTarget(final Object proxy) {
    if (!AopUtils.isAopProxy(proxy)) {
        return proxy;
    }
    if (AopUtils.isJdkDynamicProxy(proxy)) {
        return getProxyTargetObject(proxy, "h");
    } else {
        return getProxyTargetObject(proxy, "CGLIB$CALLBACK_0");
    }
}

From source file:com.brienwheeler.lib.test.spring.aop.AopTestUtils.java

@SuppressWarnings("unchecked")
public static <T> T getTarget(Object object) {
    if (AopUtils.isJdkDynamicProxy(object)) {
        try {//from w  w  w  .  j  a v  a 2s . c  o  m
            return (T) getTarget(((Advised) object).getTargetSource().getTarget());
        } catch (Exception e) {
            throw new RuntimeException("error getting proxy target", e);
        }
    } else {
        return (T) object;
    }
}

From source file:com.github.tddts.jet.util.SpringUtil.java

/**
 * Checks if given bean is a dynamic proxy and if it is so returns actual bean behind proxy and it's type.
 *
 * @param bean bean object//from ww  w.  j  a  v a 2  s.  c  o m
 * @return pair containing of bean and it's class
 * @throws BeanInitializationException in case of any exception
 */
public static Pair<Class<?>, Object> checkForDinamicProxy(Object bean) throws BeanInitializationException {
    try {
        Class<?> type = bean.getClass();
        if (AopUtils.isJdkDynamicProxy(bean)) {
            Advised advised = (Advised) bean;
            type = advised.getTargetClass();
            bean = advised.getTargetSource().getTarget();
        }
        return Pair.of(type, bean);
    } catch (Exception e) {
        throw new BeanInitializationException(e.getMessage(), e);
    }
}

From source file:org.horizontaldb.util.BeanUtils.java

public Object unpackDynamicProxy(Object bean) {
    Object target = bean;/*from   w  w  w  .j a va 2  s . c o  m*/

    if (AopUtils.isJdkDynamicProxy(bean)) {
        try {
            target = ((Advised) bean).getTargetSource().getTarget();
        } catch (Exception e) {
            throw new IllegalStateException(e);
        }
    }

    return target;
}

From source file:org.bigtester.ate.GlobalUtils.java

/**
 * Gets the target object.//  ww w .  j a va2 s.  com
 *
 * @param <T>
 *            the generic type
 * @param proxy
 *            the proxy
 * @return the target object
 * @throws Exception 
 */

@SuppressWarnings("unchecked")
public static <T> T getTargetObject(@Nullable Object proxy) { //NOPMD
    if (proxy == null)
        throw GlobalUtils.createNotInitializedException("proxy");
    while (AopUtils.isJdkDynamicProxy(proxy)) {
        try {
            return (T) getTargetObject(((Advised) proxy).getTargetSource()//NOPMD
                    .getTarget());
        } catch (Exception e) {//NOPMD
            throw GlobalUtils.createInternalError("proxied object error", e);
        }
    }
    return (T) proxy; // expected to be cglib proxy then, which is simply a
                      // specialized class
}

From source file:cn.guoyukun.spring.utils.AopProxyUtils.java

private static void removeAdvisor(Object proxy, Class<? extends Advice> adviceClass) {
    if (!AopUtils.isAopProxy(proxy)) {
        return;/*from w w w  . j  a v a  2s . c om*/
    }
    ProxyFactory proxyFactory = null;
    if (AopUtils.isJdkDynamicProxy(proxy)) {
        proxyFactory = findJdkDynamicProxyFactory(proxy);
    }
    if (AopUtils.isCglibProxy(proxy)) {
        proxyFactory = findCglibProxyFactory(proxy);
    }

    Advisor[] advisors = proxyFactory.getAdvisors();

    if (advisors == null || advisors.length == 0) {
        return;
    }

    for (Advisor advisor : advisors) {
        if (adviceClass.isAssignableFrom(advisor.getAdvice().getClass())) {
            proxyFactory.removeAdvisor(advisor);
            break;
        }
    }
}

From source file:tools.xor.util.ClassUtil.java

public static boolean isEnhanced(Object proxy) {
    boolean result = false;

    /*/*  w  w w.  j  a v  a  2  s .  c  o m*/
     * CGLIB support is deprecated so we won't support it.
    if (Enhancer.isEnhanced(proxy.getClass()) || isJavassistEnhanced(proxy.getClass()) ||
    AopUtils.isJdkDynamicProxy(proxy)) {
       result = true;
    }
    */

    if (isJavassistEnhanced(proxy.getClass()) || AopUtils.isJdkDynamicProxy(proxy)) {
        result = true;
    }

    return result;
}

From source file:cn.guoyukun.spring.utils.AopProxyUtils.java

private static boolean hasAdvice(Object proxy, Class<? extends Advice> adviceClass) {
    if (!AopUtils.isAopProxy(proxy)) {
        return false;
    }//from  w  w  w .j a  v  a  2s .c  o m
    ProxyFactory proxyFactory = null;
    if (AopUtils.isJdkDynamicProxy(proxy)) {
        proxyFactory = findJdkDynamicProxyFactory(proxy);
    }
    if (AopUtils.isCglibProxy(proxy)) {
        proxyFactory = findCglibProxyFactory(proxy);
    }

    Advisor[] advisors = proxyFactory.getAdvisors();

    if (advisors == null || advisors.length == 0) {
        return false;
    }

    for (Advisor advisor : advisors) {
        if (adviceClass.isAssignableFrom(advisor.getAdvice().getClass())) {
            return true;
        }
    }
    return false;
}

From source file:tools.xor.util.ClassUtil.java

public static Object getTargetObject(Object object) {
    Object result = object;// w  w  w.  ja  v  a 2s.c  om

    // Hibernate based implementation, safe to initialize the proxy since we need its data
    // Allow user to override this behavior
    if (object instanceof HibernateProxy) {
        while (object instanceof HibernateProxy) {
            HibernateProxy proxy = (HibernateProxy) object;
            LazyInitializer li = proxy.getHibernateLazyInitializer();
            object = li.getImplementation();
        }
        return object;
    } else if (AopUtils.isJdkDynamicProxy(object)) {
        try {
            return ((Advised) object).getTargetSource().getTarget();
        } catch (Exception e) {
            throw wrapRun(e);
        }
    }

    return result;
}