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

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

Introduction

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

Prototype

public static Method getMostSpecificMethod(Method method, @Nullable Class<?> targetClass) 

Source Link

Document

Given a method, which may come from an interface, and a target class used in the current AOP invocation, find the corresponding target method if there is one.

Usage

From source file:com.inspiresoftware.lib.dto.geda.interceptor.impl.TransferableUtils.java

private static Map<Occurrence, AdviceConfig> resolveConfiguration(final Method method,
        final Class<?> targetClass, final boolean trySpecific) {

    Method specificMethod = method;
    Transferable annotation = specificMethod.getAnnotation(Transferable.class);

    if (annotation == null && trySpecific) {

        specificMethod = AopUtils.getMostSpecificMethod(method, targetClass);
        annotation = specificMethod.getAnnotation(Transferable.class);

    }//  w ww  . ja v  a  2s.  co m

    if (annotation == null) {
        return Collections.emptyMap();
    }

    final Map<Occurrence, AdviceConfig> cfg = new HashMap<Occurrence, AdviceConfig>();
    resolveConfiguration(cfg, specificMethod.getName(), specificMethod.getParameterTypes(),
            specificMethod.getReturnType(), annotation);
    return cfg;

}

From source file:org.activiti.spring.components.aop.util.MetaAnnotationMethodMatcher.java

@Override
@SuppressWarnings("rawtypes")
public boolean matches(Method method, Class targetClass) {
    if (AnnotationUtils.getAnnotation(method, this.annotationType) != null) {
        return true;
    }/*from  ww w .  j  a  va 2  s . c o  m*/
    // The method may be on an interface, so let's check on the target class as well.
    Method specificMethod = AopUtils.getMostSpecificMethod(method, targetClass);
    return (specificMethod != method
            && (AnnotationUtils.getAnnotation(specificMethod, this.annotationType) != null));
}

From source file:org.springmodules.cache.interceptor.MetadataCacheAttributeSource.java

private CacheAttribute retrieve(Method m, Class t) {
    Method specificMethod = AopUtils.getMostSpecificMethod(m, t);
    CacheAttribute attribute = finder.find(specificMethod);
    if (attribute != null)
        return attribute;
    if (specificMethod != m)
        return finder.find(m);
    return null;//from  w  w w .  ja va2 s . c  o  m
}

From source file:grails.plugin.cache.web.filter.LazyParamAwareEvaluationContext.java

protected void loadArgsAsVariables() {
    // shortcut if no args need to be loaded
    if (ObjectUtils.isEmpty(args)) {
        return;/*from www  .  j  a va 2s  . c  om*/
    }

    String key = toString(method);
    Method targetMethod = methodCache.get(key);
    if (targetMethod == null) {
        targetMethod = AopUtils.getMostSpecificMethod(method, targetClass);
        if (targetMethod == null) {
            targetMethod = method;
        }
        methodCache.put(key, targetMethod);
    }

    // save arguments as indexed variables
    for (int i = 0; i < args.length; i++) {
        setVariable("p" + i, args[i]);
    }

    String[] parameterNames = paramDiscoverer.getParameterNames(targetMethod);
    // save parameter names (if discovered)
    if (parameterNames != null) {
        for (int i = 0; i < parameterNames.length; i++) {
            setVariable(parameterNames[i], args[i]);
        }
    }
}

From source file:org.apache.rave.synchronization.SynchronizingAspect.java

private Method getTargetMethod(Class<?> targetClass, Method method) {
    Method targetMethod = targetMethodCache.get(method);
    if (targetMethod == null) {
        targetMethod = AopUtils.getMostSpecificMethod(method, targetClass);
        targetMethodCache.put(method, targetMethod);
    }//from   ww  w.  j av a 2  s. c  om
    return targetMethod;
}

From source file:org.springframework.aop.aspectj.AspectJExpressionPointcut.java

@Override
public boolean matches(Method method, @Nullable Class<?> targetClass, boolean beanHasIntroductions) {
    obtainPointcutExpression();//from www. j  a v a  2 s  .  com
    Method targetMethod = AopUtils.getMostSpecificMethod(method, targetClass);
    ShadowMatch shadowMatch = getShadowMatch(targetMethod, method);

    // Special handling for this, target, @this, @target, @annotation
    // in Spring - we can optimize since we know we have exactly this class,
    // and there will never be matching subclass at runtime.
    if (shadowMatch.alwaysMatches()) {
        return true;
    } else if (shadowMatch.neverMatches()) {
        return false;
    } else {
        // the maybe case
        if (beanHasIntroductions) {
            return true;
        }
        // A match test returned maybe - if there are any subtype sensitive variables
        // involved in the test (this, target, at_this, at_target, at_annotation) then
        // we say this is not a match as in Spring there will never be a different
        // runtime subtype.
        RuntimeTestWalker walker = getRuntimeTestWalker(shadowMatch);
        return (!walker.testsSubtypeSensitiveVars()
                || (targetClass != null && walker.testTargetInstanceOfResidue(targetClass)));
    }
}

From source file:org.springframework.aop.aspectj.AspectJExpressionPointcut.java

@Override
public boolean matches(Method method, @Nullable Class<?> targetClass, Object... args) {
    obtainPointcutExpression();/*from   ww w.  j  ava 2  s.c om*/
    ShadowMatch shadowMatch = getShadowMatch(AopUtils.getMostSpecificMethod(method, targetClass), method);
    ShadowMatch originalShadowMatch = getShadowMatch(method, method);

    // Bind Spring AOP proxy to AspectJ "this" and Spring AOP target to AspectJ target,
    // consistent with return of MethodInvocationProceedingJoinPoint
    ProxyMethodInvocation pmi = null;
    Object targetObject = null;
    Object thisObject = null;
    try {
        MethodInvocation mi = ExposeInvocationInterceptor.currentInvocation();
        targetObject = mi.getThis();
        if (!(mi instanceof ProxyMethodInvocation)) {
            throw new IllegalStateException("MethodInvocation is not a Spring ProxyMethodInvocation: " + mi);
        }
        pmi = (ProxyMethodInvocation) mi;
        thisObject = pmi.getProxy();
    } catch (IllegalStateException ex) {
        // No current invocation...
        if (logger.isDebugEnabled()) {
            logger.debug("Could not access current invocation - matching with limited context: " + ex);
        }
    }

    try {
        JoinPointMatch joinPointMatch = shadowMatch.matchesJoinPoint(thisObject, targetObject, args);

        /*
         * Do a final check to see if any this(TYPE) kind of residue match. For
         * this purpose, we use the original method's (proxy method's) shadow to
         * ensure that 'this' is correctly checked against. Without this check,
         * we get incorrect match on this(TYPE) where TYPE matches the target
         * type but not 'this' (as would be the case of JDK dynamic proxies).
         * <p>See SPR-2979 for the original bug.
         */
        if (pmi != null && thisObject != null) { // there is a current invocation
            RuntimeTestWalker originalMethodResidueTest = getRuntimeTestWalker(originalShadowMatch);
            if (!originalMethodResidueTest.testThisInstanceOfResidue(thisObject.getClass())) {
                return false;
            }
            if (joinPointMatch.matches()) {
                bindParameters(pmi, joinPointMatch);
            }
        }

        return joinPointMatch.matches();
    } catch (Throwable ex) {
        if (logger.isDebugEnabled()) {
            logger.debug("Failed to evaluate join point for arguments " + Arrays.asList(args)
                    + " - falling back to non-match", ex);
        }
        return false;
    }
}

From source file:org.springframework.cloud.sleuth.annotation.SleuthAdvisorConfig.java

@Override
public Object invoke(MethodInvocation invocation) throws Throwable {
    Method method = invocation.getMethod();
    if (method == null) {
        return invocation.proceed();
    }/*  ww w .  j a v a 2  s .c  o  m*/
    Method mostSpecificMethod = AopUtils.getMostSpecificMethod(method, invocation.getThis().getClass());
    NewSpan newSpan = SleuthAnnotationUtils.findAnnotation(mostSpecificMethod, NewSpan.class);
    ContinueSpan continueSpan = SleuthAnnotationUtils.findAnnotation(mostSpecificMethod, ContinueSpan.class);
    if (newSpan == null && continueSpan == null) {
        return invocation.proceed();
    }
    Span span = tracer().getCurrentSpan();
    String log = log(continueSpan);
    boolean hasLog = StringUtils.hasText(log);
    try {
        if (newSpan != null) {
            span = spanCreator().createSpan(invocation, newSpan);
        }
        if (hasLog) {
            logEvent(span, log + ".before");
        }
        spanTagAnnotationHandler().addAnnotatedParameters(invocation);
        return invocation.proceed();
    } catch (Exception e) {
        if (logger.isDebugEnabled()) {
            logger.debug("Exception occurred while trying to continue the pointcut", e);
        }
        if (hasLog) {
            logEvent(span, log + ".afterFailure");
        }
        tracer().addTag(Span.SPAN_ERROR_TAG_NAME, ExceptionUtils.getExceptionMessage(e));
        throw e;
    } finally {
        if (span != null) {
            if (hasLog) {
                logEvent(span, log + ".after");
            }
            if (newSpan != null) {
                tracer().close(span);
            }
        }
    }
}

From source file:org.springframework.cloud.sleuth.annotation.SpanTagAnnotationHandler.java

void addAnnotatedParameters(MethodInvocation pjp) {
    try {//from   www.j  a v  a  2s.co m
        Method method = pjp.getMethod();
        Method mostSpecificMethod = AopUtils.getMostSpecificMethod(method, pjp.getThis().getClass());
        List<SleuthAnnotatedParameter> annotatedParameters = SleuthAnnotationUtils
                .findAnnotatedParameters(mostSpecificMethod, pjp.getArguments());
        getAnnotationsFromInterfaces(pjp, mostSpecificMethod, annotatedParameters);
        mergeAnnotatedMethodsIfNecessary(pjp, method, mostSpecificMethod, annotatedParameters);
        addAnnotatedArguments(annotatedParameters);
    } catch (SecurityException e) {
        log.error("Exception occurred while trying to add annotated parameters", e);
    }
}

From source file:org.springframework.integration.handler.support.MessagingMethodInvokerHelper.java

private void checkSpelInvokerRequired(final Class<?> targetClass, Method methodArg,
        HandlerMethod handlerMethod) {//from w  w w . j  ava  2s.  co m
    Method method = AopUtils.getMostSpecificMethod(methodArg, targetClass);
    UseSpelInvoker useSpel = AnnotationUtils.findAnnotation(method, UseSpelInvoker.class);
    if (useSpel == null) {
        useSpel = AnnotationUtils.findAnnotation(targetClass, UseSpelInvoker.class);
    }
    if (useSpel != null) {
        handlerMethod.spelOnly = true;
        handlerMethod.useSpelInvoker = useSpel;
    }
}