Example usage for org.springframework.aop.framework Advised getTargetClass

List of usage examples for org.springframework.aop.framework Advised getTargetClass

Introduction

In this page you can find the example usage for org.springframework.aop.framework Advised getTargetClass.

Prototype

@Nullable
Class<?> getTargetClass();

Source Link

Document

Return the target class behind the implementing object (typically a proxy configuration or an actual proxy).

Usage

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/*w  w w  .  j  av a 2  s .c om*/
 * @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.springmodules.validation.valang.javascript.taglib.ValangRulesExportInterceptor.java

/**
 * Convert the specified <code>handler</code> to a {@link BaseCommandController} if possible.
 * // w w  w  .j av  a 2 s  .  c  o  m
 * <p>
 * If the <code>handler</code> is type compatible with a <code>BaseCommandController</code> then it will simply be
 * cast.
 * </p>
 * <p>
 * If the <code>handler</code> is a Spring JDK (or CGLIB) proxy, then it will unravelled and returned (assuming the
 * target is a <code>BaseCommandController</code>.
 * </p>
 * If neither of the above strategies work, <code>null</code> will be returned.
 * 
 * @param handler
 *            the handler to convert to a <code>BaseCommandController</code>.
 * @return a <code>BaseCommandController</code> or <code>null</code> if <code>handler</code> cannot be converted.
 * @throws Exception
 */
private BaseCommandController retrieveBaseCommandControllerIfPossible(Object handler) throws Exception {
    BaseCommandController baseCommandController = null;

    if (BaseCommandController.class.isAssignableFrom(handler.getClass())) {
        if (logger.isDebugEnabled()) {
            logger.debug("handler is type compatible, simply cast");
        }
        baseCommandController = (BaseCommandController) handler;
    } else if (AopUtils.isAopProxy(handler)) {
        if (logger.isDebugEnabled()) {
            logger.debug("handler is AOP proxy");
        }

        Advised advisedObject = (Advised) handler;
        Class proxiedClass = advisedObject.getTargetClass();
        Object target = advisedObject.getTargetSource().getTarget();

        // convert (if possible) the target.
        baseCommandController = retrieveBaseCommandControllerIfPossible(target);
    }

    if (logger.isDebugEnabled()) {
        logger.debug("Cannot convert handler to BaseCommandController");
    }
    return baseCommandController;
}