Example usage for org.springframework.util ClassUtils getMostSpecificMethod

List of usage examples for org.springframework.util ClassUtils getMostSpecificMethod

Introduction

In this page you can find the example usage for org.springframework.util ClassUtils 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 reflective invocation, find the corresponding target method if there is one.

Usage

From source file:com.gradecak.alfresco.mvc.aop.RunAsAdvice.java

public Object invoke(final MethodInvocation invocation) throws Throwable {

    Class<?> targetClass = invocation.getThis() != null ? invocation.getThis().getClass() : null;

    Method specificMethod = ClassUtils.getMostSpecificMethod(invocation.getMethod(), targetClass);
    // If we are dealing with method with generic parameters, find the original
    // method./*from  ww w. j a  v a 2 s .  com*/
    specificMethod = BridgeMethodResolver.findBridgedMethod(specificMethod);
    AlfrescoRunAs alfrescounRunAs = parseRunAsAnnotation(specificMethod);
    if (alfrescounRunAs != null) {
        String runAs = alfrescounRunAs.value();
        if (StringUtils.hasText(runAs)) {
            RunAsWork<Object> getUserNameRunAsWork = new RunAsWork<Object>() {
                public Object doWork() throws Exception {
                    try {
                        return invocation.proceed();
                    } catch (Throwable e) {
                        throw new Exception(e.getMessage(), e);
                    }
                }
            };
            return AuthenticationUtil.runAs(getUserNameRunAsWork, runAs);
        }
    }

    return invocation.proceed();
}

From source file:com.gradecak.alfresco.mvc.aop.TransactionalAdvice.java

public Object invoke(final MethodInvocation invocation) throws Throwable {
    Class<?> targetClass = invocation.getThis() != null ? invocation.getThis().getClass() : null;

    Method specificMethod = ClassUtils.getMostSpecificMethod(invocation.getMethod(), targetClass);
    // If we are dealing with method with generic parameters, find the original
    // method.// w ww  .  j  a va2 s . com
    specificMethod = BridgeMethodResolver.findBridgedMethod(specificMethod);
    AlfrescoTransaction alfrescoTransaction = parseAnnotation(specificMethod);

    if (alfrescoTransaction != null) {
        RetryingTransactionCallback<Object> exampleWork = new RetryingTransactionCallback<Object>() {
            public Object execute() throws Throwable {
                return invocation.proceed();
            }
        };
        boolean readonly = alfrescoTransaction.readOnly();
        Propagation propagation = alfrescoTransaction.propagation();

        boolean requiresNew = Propagation.REQUIRES_NEW.equals(propagation);
        return serviceRegistry.getTransactionService().getRetryingTransactionHelper()
                .doInTransaction(exampleWork, readonly, requiresNew);
    } else {
        return invocation.proceed();
    }

}

From source file:com.gradecak.alfresco.mvc.aop.AuthenticationAdvice.java

public Object invoke(final MethodInvocation invocation) throws Throwable {

    Class<?> targetClass = invocation.getThis() != null ? invocation.getThis().getClass() : null;

    Method specificMethod = ClassUtils.getMostSpecificMethod(invocation.getMethod(), targetClass);
    // If we are dealing with method with generic parameters, find the original
    // method.//from   ww  w . j  a  v a 2  s  . c  om
    specificMethod = BridgeMethodResolver.findBridgedMethod(specificMethod);

    AlfrescoAuthentication alfrescoAuthentication = parseAnnotation(specificMethod);

    if (alfrescoAuthentication != null) {

        AuthenticationType authenticationType = alfrescoAuthentication.value();

        if (authenticationType != null && !AuthenticationType.NONE.equals(authenticationType)) {
            AuthenticationService authenticationService = serviceRegistry.getAuthenticationService();
            AuthorityService authorityService = serviceRegistry.getAuthorityService();

            String ticket = getTicket();
            if (StringUtils.hasText(ticket)) {
                authenticationService.validate(ticket);
                if (AuthenticationType.USER.equals(authenticationType)
                        && authorityService.hasGuestAuthority()) {
                    throw new AuthenticationException(
                            "User has guest authority where at least a user authentication is required.");
                } else if (AuthenticationType.ADMIN.equals(authenticationType)
                        && !authorityService.hasAdminAuthority()) {
                    throw new AuthenticationException(
                            "User does not have admin authority where at least named admin authentication is required .");
                }
            } else if (AuthenticationType.GUEST.equals(authenticationType)
                    && authenticationService.guestUserAuthenticationAllowed()) {
                authenticationService.authenticateAsGuest();
            } else {
                throw new AuthenticationException(
                        "\nUnable to authenticate due to one of the following reasons:\n"
                                + "Credentials are not provided in HTTP request where at least named user or admin authentication is required.\n"
                                + "Guest user authentication is not allowed where at least guest authentication is required.\n");
            }
        }
    }

    return invocation.proceed();
}

From source file:org.zkybase.kite.throttle.interceptor.AnnotationThrottleSource.java

public ThrottleTemplate getThrottle(Method method, Class<?> targetClass) {
    Assert.notNull(method, "method can't be null");

    // Method may be on an interface, but we need annotations from the
    // target class. If target class is null, method will be unchanged.
    Method specificMethod = ClassUtils.getMostSpecificMethod(method, targetClass);

    // If we are dealing with a method with generic parameters, find the
    // original method.
    specificMethod = BridgeMethodResolver.findBridgedMethod(specificMethod);

    ThrottleTemplate throttle = parseAnnotation(specificMethod);
    if (throttle != null) {
        return throttle;
    } else {// w w w . j  a  va  2  s.c o  m
        return parseAnnotation(method);
    }
}

From source file:org.zkybase.kite.circuitbreaker.interceptor.AnnotationCircuitBreakerSource.java

public CircuitBreakerTemplate getBreaker(Method method, Class<?> targetClass) {
    Assert.notNull(method, "method can't be null");

    // Method may be on an interface, but we need annotations from the
    // target class. If target class is null, method will be unchanged.
    Method specificMethod = ClassUtils.getMostSpecificMethod(method, targetClass);

    // If we are dealing with a method with generic parameters, find the
    // original, bridged method.
    specificMethod = BridgeMethodResolver.findBridgedMethod(specificMethod);

    CircuitBreakerTemplate breaker = parseAnnotation(specificMethod);
    return (breaker != null ? breaker : parseAnnotation(method));
}

From source file:com.springinpractice.ch14.kite.interceptor.AnnotationGuardListSource.java

@Override
public List<Guard> getGuards(Method method, Class<?> targetClass) {
    notNull(method, "method can't be null");

    // Method may be on an interface, but we need annotations from the target class. If target class is null, method
    // will be unchanged.
    Method specificMethod = ClassUtils.getMostSpecificMethod(method, targetClass);

    // If we are dealing with a method with generic parameters, find the original method.
    specificMethod = BridgeMethodResolver.findBridgedMethod(specificMethod);

    List<Guard> guards = parseAnnotation(specificMethod);
    return (guards != null ? guards : parseAnnotation(method));
}

From source file:com.ivanzhangwb.interpose.core.InterposeBootStrap.java

@Override
public Object postProcessAfterInitialization(final Object bean, String beanName) throws BeansException {
    if (interposeClassPathApplicationContext == null) {
        interposeClassPathApplicationContext = new InterposeClassPathApplicationContext(
                this.applicationContext);
    }//  w w  w  .  java 2  s  .  c o  m
    boolean needInterpose = false;
    Class beanClass = bean.getClass();
    do {
        Method[] methods = beanClass.getDeclaredMethods();
        for (int i = 0; i < methods.length; i++) {
            if (Interpose.class != null && methods[i].isAnnotationPresent(Interpose.class)
                    && methods[i].equals(ClassUtils.getMostSpecificMethod(methods[i], bean.getClass()))) {
                needInterpose = true;
                handleMethodAnnotation(methods[i]);
            }
        }
        beanClass = beanClass.getSuperclass();
    } while (beanClass != null);

    if (needInterpose) {
        ConfigurableApplicationContext atx = (ConfigurableApplicationContext) interposeClassPathApplicationContext;
        DefaultListableBeanFactory beanFactory = (DefaultListableBeanFactory) atx.getBeanFactory();
        ProxyFactoryBean factoryBean = new ProxyFactoryBean();
        factoryBean.setTarget(bean);
        factoryBean.setInterceptorNames(new String[] { InterposeConstants.INTERPOSE_CORE_INTERCEPTOR });
        factoryBean.setBeanFactory(beanFactory);
        return factoryBean.getObject();
    } else {
        return bean;
    }
}

From source file:org.icescrum.core.security.MethodScrumEvaluationContext.java

private void addArgumentsAsVariables() {
    Object[] args = mi.getArguments();
    Object targetObject = mi.getThis();
    Method method = ClassUtils.getMostSpecificMethod(mi.getMethod(), targetObject.getClass());
    String[] paramNames = parameterNameDiscoverer.getParameterNames(method);

    for (int i = 0; i < args.length; i++) {
        super.setVariable(paramNames[i], args[i]);
    }/*from ww  w.  j  a  v  a2 s.c  o m*/
}

From source file:org.craftercms.commons.ebus.config.EBusBeanAutoConfiguration.java

private static Set<Method> findHandlerMethods(final Class<?> handlerType,
        final ReflectionUtils.MethodFilter listenerMethodFilter) {

    final Set<Method> handlerMethods = new LinkedHashSet<Method>();

    if (handlerType == null) {
        return handlerMethods;
    }/*from  ww  w . ja v  a2s .  com*/

    Set<Class<?>> handlerTypes = new LinkedHashSet<Class<?>>();
    Class<?> specifiedHandlerType = null;
    if (!Proxy.isProxyClass(handlerType)) {
        handlerTypes.add(handlerType);
        specifiedHandlerType = handlerType;
    }
    handlerTypes.addAll(Arrays.asList(handlerType.getInterfaces()));
    for (Class<?> currentHandlerType : handlerTypes) {
        final Class<?> targetClass = (specifiedHandlerType != null ? specifiedHandlerType : currentHandlerType);
        ReflectionUtils.doWithMethods(currentHandlerType, new ReflectionUtils.MethodCallback() {
            @Override
            public void doWith(final Method method) throws IllegalArgumentException, IllegalAccessException {
                Method specificMethod = ClassUtils.getMostSpecificMethod(method, targetClass);
                Method bridgeMethod = BridgeMethodResolver.findBridgedMethod(specificMethod);
                if (listenerMethodFilter.matches(specificMethod)
                        && (bridgeMethod == specificMethod || !listenerMethodFilter.matches(bridgeMethod))) {
                    handlerMethods.add(specificMethod);
                }
            }
        }, ReflectionUtils.USER_DECLARED_METHODS);
    }

    return handlerMethods;
}

From source file:org.vbossica.springbox.annotation.AnnotatedMethodResolver.java

/**
 * Traverses the {@code bean} in search for annotated methods. Calls the
 * {@link #doWithAnnotatedMethod(String, Object, Method, Annotation)} for every method found.
 *
 * @param beanName the name of the Spring bean
 * @param bean the Spring bean itself/*  ww  w.  ja v a2 s  .c o  m*/
 * @see #doWithAnnotatedMethod(String, Object, java.lang.reflect.Method, java.lang.annotation.Annotation)
 */
public void traverse(final String beanName, final Object bean) {
    logger.info("traversing bean " + beanName);

    Class<?> handlerType = bean.getClass();

    Class<?>[] handlerTypes = Proxy.isProxyClass(handlerType) ? handlerType.getInterfaces()
            : new Class<?>[] { handlerType };
    for (final Class<?> currentHandlerType : handlerTypes) {
        ReflectionUtils.doWithMethods(currentHandlerType, new ReflectionUtils.MethodCallback() {
            public void doWith(Method method) {
                Method specificMethod = ClassUtils.getMostSpecificMethod(method, currentHandlerType);
                A annotation = AnnotationUtils.findAnnotation(method, annotationClass);
                if (annotation != null) {
                    doWithAnnotatedMethod(beanName, bean, specificMethod, annotation);
                }
            }
        }, ReflectionUtils.NON_BRIDGED_METHODS);
    }
}