Example usage for org.springframework.aop.framework AopProxyUtils ultimateTargetClass

List of usage examples for org.springframework.aop.framework AopProxyUtils ultimateTargetClass

Introduction

In this page you can find the example usage for org.springframework.aop.framework AopProxyUtils ultimateTargetClass.

Prototype

public static Class<?> ultimateTargetClass(Object candidate) 

Source Link

Document

Determine the ultimate target class of the given bean instance, traversing not only a top-level proxy but any number of nested proxies as well — as long as possible without side effects, that is, just for singleton targets.

Usage

From source file:org.springframework.cache.jcache.interceptor.JCacheAspectSupport.java

protected Object execute(CacheOperationInvoker invoker, Object target, Method method, Object[] args) {
    // Check whether aspect is enabled to cope with cases where the AJ is pulled in automatically
    if (this.initialized) {
        Class<?> targetClass = AopProxyUtils.ultimateTargetClass(target);
        JCacheOperation<?> operation = getCacheOperationSource().getCacheOperation(method, targetClass);
        if (operation != null) {
            CacheOperationInvocationContext<?> context = createCacheOperationInvocationContext(target, args,
                    operation);//w ww.j  av  a2  s.  c o m
            return execute(context, invoker);
        }
    }

    return invoker.invoke();
}

From source file:org.springframework.jms.annotation.JmsListenerAnnotationBeanPostProcessor.java

@Override
public Object postProcessAfterInitialization(final Object bean, String beanName) throws BeansException {
    if (!this.nonAnnotatedClasses.contains(bean.getClass())) {
        Class<?> targetClass = AopProxyUtils.ultimateTargetClass(bean);
        Map<Method, Set<JmsListener>> annotatedMethods = MethodIntrospector.selectMethods(targetClass,
                (MethodIntrospector.MetadataLookup<Set<JmsListener>>) method -> {
                    Set<JmsListener> listenerMethods = AnnotatedElementUtils
                            .getMergedRepeatableAnnotations(method, JmsListener.class, JmsListeners.class);
                    return (!listenerMethods.isEmpty() ? listenerMethods : null);
                });/* w w w .  j av  a  2  s.c o m*/
        if (annotatedMethods.isEmpty()) {
            this.nonAnnotatedClasses.add(bean.getClass());
            if (logger.isTraceEnabled()) {
                logger.trace("No @JmsListener annotations found on bean type: " + bean.getClass());
            }
        } else {
            // Non-empty set of methods
            annotatedMethods.forEach((method, listeners) -> listeners
                    .forEach(listener -> processJmsListener(listener, method, bean)));
            if (logger.isDebugEnabled()) {
                logger.debug(annotatedMethods.size() + " @JmsListener methods processed on bean '" + beanName
                        + "': " + annotatedMethods);
            }
        }
    }
    return bean;
}

From source file:org.springframework.scheduling.annotation.ScheduledAnnotationBeanPostProcessor.java

@Override
public Object postProcessAfterInitialization(final Object bean, String beanName) {
    Class<?> targetClass = AopProxyUtils.ultimateTargetClass(bean);
    if (!this.nonAnnotatedClasses.contains(targetClass)) {
        Map<Method, Set<Scheduled>> annotatedMethods = MethodIntrospector.selectMethods(targetClass,
                (MethodIntrospector.MetadataLookup<Set<Scheduled>>) method -> {
                    Set<Scheduled> scheduledMethods = AnnotatedElementUtils
                            .getMergedRepeatableAnnotations(method, Scheduled.class, Schedules.class);
                    return (!scheduledMethods.isEmpty() ? scheduledMethods : null);
                });//from w  ww. ja  v a2s  .c  om
        if (annotatedMethods.isEmpty()) {
            this.nonAnnotatedClasses.add(targetClass);
            if (logger.isTraceEnabled()) {
                logger.trace("No @Scheduled annotations found on bean class: " + bean.getClass());
            }
        } else {
            // Non-empty set of methods
            annotatedMethods.forEach((method, scheduledMethods) -> scheduledMethods
                    .forEach(scheduled -> processScheduled(scheduled, method, bean)));
            if (logger.isDebugEnabled()) {
                logger.debug(annotatedMethods.size() + " @Scheduled methods processed on bean '" + beanName
                        + "': " + annotatedMethods);
            }
        }
    }
    return bean;
}

From source file:org.springframework.security.access.expression.method.MethodSecurityEvaluationContext.java

private void addArgumentsAsVariables() {
    Object[] args = mi.getArguments();

    if (args.length == 0) {
        return;/*from w ww . j  a  va  2  s.c  om*/
    }

    Object targetObject = mi.getThis();
    // SEC-1454
    Class<?> targetClass = AopProxyUtils.ultimateTargetClass(targetObject);

    if (targetClass == null) {
        // TODO: Spring should do this, but there's a bug in ultimateTargetClass()
        // which returns null
        targetClass = targetObject.getClass();
    }

    Method method = AopUtils.getMostSpecificMethod(mi.getMethod(), targetClass);
    String[] paramNames = parameterNameDiscoverer.getParameterNames(method);

    if (paramNames == null) {
        logger.warn("Unable to resolve method parameter names for method: " + method
                + ". Debug symbol information is required if you are using parameter names in expressions.");
        return;
    }

    for (int i = 0; i < args.length; i++) {
        if (paramNames[i] != null) {
            setVariable(paramNames[i], args[i]);
        }
    }
}

From source file:org.springframework.security.access.method.AbstractMethodSecurityMetadataSource.java

public final Collection<ConfigAttribute> getAttributes(Object object) {
    if (object instanceof MethodInvocation) {
        MethodInvocation mi = (MethodInvocation) object;
        Object target = mi.getThis();
        Class<?> targetClass = null;

        if (target != null) {
            targetClass = target instanceof Class<?> ? (Class<?>) target
                    : AopProxyUtils.ultimateTargetClass(target);
        }//from   w w  w .  j  a  v  a 2  s. c o m
        Collection<ConfigAttribute> attrs = getAttributes(mi.getMethod(), targetClass);
        if (attrs != null && !attrs.isEmpty()) {
            return attrs;
        }
        if (target != null && !(target instanceof Class<?>)) {
            attrs = getAttributes(mi.getMethod(), target.getClass());
        }
        return attrs;
    }

    throw new IllegalArgumentException("Object must be a non-null MethodInvocation");
}