Example usage for org.springframework.beans BeanUtils findPropertyForMethod

List of usage examples for org.springframework.beans BeanUtils findPropertyForMethod

Introduction

In this page you can find the example usage for org.springframework.beans BeanUtils findPropertyForMethod.

Prototype

@Nullable
public static PropertyDescriptor findPropertyForMethod(Method method) throws BeansException 

Source Link

Document

Find a JavaBeans PropertyDescriptor for the given method, with the method either being the read method or the write method for that bean property.

Usage

From source file:org.springbyexample.util.log.LoggerBeanPostProcessor.java

/**
 * Processes a property descriptor to inject a logger.
 *///from ww w  .  j ava2  s .  com
public void injectMethod(Object bean, Method method) {
    PropertyDescriptor pd = BeanUtils.findPropertyForMethod(method);

    if (pd != null) {
        String canonicalName = pd.getPropertyType().getCanonicalName();

        Object logger = getLogger(bean.getClass().getName(), canonicalName);

        if (logger != null) {
            try {
                pd.getWriteMethod().invoke(bean, new Object[] { logger });
            } catch (Throwable e) {
                throw new FatalBeanException("Problem injecting logger.  " + e.getMessage(), e);
            }
        }
    }
}

From source file:org.springframework.batch.core.jsr.configuration.support.SpringAutowiredAnnotationBeanPostProcessor.java

protected InjectionMetadata buildAutowiringMetadata(Class<?> clazz) {
    LinkedList<InjectionMetadata.InjectedElement> elements = new LinkedList<InjectionMetadata.InjectedElement>();
    Class<?> targetClass = clazz;

    do {//from w  w  w.  j  a  v a 2  s .  com
        LinkedList<InjectionMetadata.InjectedElement> currElements = new LinkedList<InjectionMetadata.InjectedElement>();
        for (Field field : targetClass.getDeclaredFields()) {
            Annotation annotation = findAutowiredAnnotation(field);
            if (annotation != null) {
                if (Modifier.isStatic(field.getModifiers())) {
                    if (logger.isWarnEnabled()) {
                        logger.warn("Autowired annotation is not supported on static fields: " + field);
                    }
                    continue;
                }
                boolean required = determineRequiredStatus(annotation);
                currElements.add(new AutowiredFieldElement(field, required));
            }
        }
        for (Method method : targetClass.getDeclaredMethods()) {
            Method bridgedMethod = BridgeMethodResolver.findBridgedMethod(method);
            Annotation annotation = BridgeMethodResolver.isVisibilityBridgeMethodPair(method, bridgedMethod)
                    ? findAutowiredAnnotation(bridgedMethod)
                    : findAutowiredAnnotation(method);
            if (annotation != null && method.equals(ClassUtils.getMostSpecificMethod(method, clazz))) {
                if (Modifier.isStatic(method.getModifiers())) {
                    if (logger.isWarnEnabled()) {
                        logger.warn("Autowired annotation is not supported on static methods: " + method);
                    }
                    continue;
                }
                if (method.getParameterTypes().length == 0) {
                    if (logger.isWarnEnabled()) {
                        logger.warn("Autowired annotation should be used on methods with actual parameters: "
                                + method);
                    }
                }
                boolean required = determineRequiredStatus(annotation);
                PropertyDescriptor pd = BeanUtils.findPropertyForMethod(method);
                currElements.add(new AutowiredMethodElement(method, required, pd));
            }
        }
        elements.addAll(0, currElements);
        targetClass = targetClass.getSuperclass();
    } while (targetClass != null && targetClass != Object.class);

    return new InjectionMetadata(clazz, elements);
}

From source file:org.springframework.jmx.access.MBeanClientInterceptor.java

/**
 * Route the invocation to the configured managed resource. Correctly routes JavaBean property
 * access to {@code MBeanServerConnection.get/setAttribute} and method invocation to
 * {@code MBeanServerConnection.invoke}.
 * @param invocation the {@code MethodInvocation} to re-route
 * @return the value returned as a result of the re-routed invocation
 * @throws Throwable an invocation error propagated to the user
 *//*ww w  .  j  a  v  a2s  . com*/
@Nullable
protected Object doInvoke(MethodInvocation invocation) throws Throwable {
    Method method = invocation.getMethod();
    try {
        Object result;
        if (this.invocationHandler != null) {
            result = this.invocationHandler.invoke(invocation.getThis(), method, invocation.getArguments());
        } else {
            PropertyDescriptor pd = BeanUtils.findPropertyForMethod(method);
            if (pd != null) {
                result = invokeAttribute(pd, invocation);
            } else {
                result = invokeOperation(method, invocation.getArguments());
            }
        }
        return convertResultValueIfNecessary(result, new MethodParameter(method, -1));
    } catch (MBeanException ex) {
        throw ex.getTargetException();
    } catch (RuntimeMBeanException ex) {
        throw ex.getTargetException();
    } catch (RuntimeErrorException ex) {
        throw ex.getTargetError();
    } catch (RuntimeOperationsException ex) {
        // This one is only thrown by the JMX 1.2 RI, not by the JDK 1.5 JMX code.
        RuntimeException rex = ex.getTargetException();
        if (rex instanceof RuntimeMBeanException) {
            throw ((RuntimeMBeanException) rex).getTargetException();
        } else if (rex instanceof RuntimeErrorException) {
            throw ((RuntimeErrorException) rex).getTargetError();
        } else {
            throw rex;
        }
    } catch (OperationsException ex) {
        if (ReflectionUtils.declaresException(method, ex.getClass())) {
            throw ex;
        } else {
            throw new InvalidInvocationException(ex.getMessage());
        }
    } catch (JMException ex) {
        if (ReflectionUtils.declaresException(method, ex.getClass())) {
            throw ex;
        } else {
            throw new InvocationFailureException("JMX access failed", ex);
        }
    } catch (IOException ex) {
        if (ReflectionUtils.declaresException(method, ex.getClass())) {
            throw ex;
        } else {
            throw new MBeanConnectFailureException("I/O failure during JMX access", ex);
        }
    }
}