001/*
002 * Copyright 2002-2013 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      https://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.springframework.beans;
018
019import java.beans.BeanInfo;
020import java.beans.IntrospectionException;
021import java.beans.Introspector;
022import java.lang.reflect.Method;
023
024import org.springframework.core.Ordered;
025
026/**
027 * {@link BeanInfoFactory} implementation that evaluates whether bean classes have
028 * "non-standard" JavaBeans setter methods and are thus candidates for introspection
029 * by Spring's (package-visible) {@code ExtendedBeanInfo} implementation.
030 *
031 * <p>Ordered at {@link Ordered#LOWEST_PRECEDENCE} to allow other user-defined
032 * {@link BeanInfoFactory} types to take precedence.
033 *
034 * @author Chris Beams
035 * @since 3.2
036 * @see BeanInfoFactory
037 * @see CachedIntrospectionResults
038 */
039public class ExtendedBeanInfoFactory implements BeanInfoFactory, Ordered {
040
041        /**
042         * Return an {@link ExtendedBeanInfo} for the given bean class, if applicable.
043         */
044        @Override
045        public BeanInfo getBeanInfo(Class<?> beanClass) throws IntrospectionException {
046                return (supports(beanClass) ? new ExtendedBeanInfo(Introspector.getBeanInfo(beanClass)) : null);
047        }
048
049        /**
050         * Return whether the given bean class declares or inherits any non-void
051         * returning bean property or indexed property setter methods.
052         */
053        private boolean supports(Class<?> beanClass) {
054                for (Method method : beanClass.getMethods()) {
055                        if (ExtendedBeanInfo.isCandidateWriteMethod(method)) {
056                                return true;
057                        }
058                }
059                return false;
060        }
061
062        @Override
063        public int getOrder() {
064                return Ordered.LOWEST_PRECEDENCE;
065        }
066
067}