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;
025import org.springframework.lang.Nullable;
026
027/**
028 * {@link BeanInfoFactory} implementation that evaluates whether bean classes have
029 * "non-standard" JavaBeans setter methods and are thus candidates for introspection
030 * by Spring's (package-visible) {@code ExtendedBeanInfo} implementation.
031 *
032 * <p>Ordered at {@link Ordered#LOWEST_PRECEDENCE} to allow other user-defined
033 * {@link BeanInfoFactory} types to take precedence.
034 *
035 * @author Chris Beams
036 * @since 3.2
037 * @see BeanInfoFactory
038 * @see CachedIntrospectionResults
039 */
040public class ExtendedBeanInfoFactory implements BeanInfoFactory, Ordered {
041
042        /**
043         * Return an {@link ExtendedBeanInfo} for the given bean class, if applicable.
044         */
045        @Override
046        @Nullable
047        public BeanInfo getBeanInfo(Class<?> beanClass) throws IntrospectionException {
048                return (supports(beanClass) ? new ExtendedBeanInfo(Introspector.getBeanInfo(beanClass)) : null);
049        }
050
051        /**
052         * Return whether the given bean class declares or inherits any non-void
053         * returning bean property or indexed property setter methods.
054         */
055        private boolean supports(Class<?> beanClass) {
056                for (Method method : beanClass.getMethods()) {
057                        if (ExtendedBeanInfo.isCandidateWriteMethod(method)) {
058                                return true;
059                        }
060                }
061                return false;
062        }
063
064        @Override
065        public int getOrder() {
066                return Ordered.LOWEST_PRECEDENCE;
067        }
068
069}