001/*
002 * Copyright 2002-2017 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.core;
018
019import java.lang.reflect.Constructor;
020import java.lang.reflect.Method;
021import java.util.LinkedList;
022import java.util.List;
023
024/**
025 * {@link ParameterNameDiscoverer} implementation that tries several discoverer
026 * delegates in succession. Those added first in the {@code addDiscoverer} method
027 * have highest priority. If one returns {@code null}, the next will be tried.
028 *
029 * <p>The default behavior is to return {@code null} if no discoverer matches.
030 *
031 * @author Rod Johnson
032 * @author Juergen Hoeller
033 * @since 2.0
034 */
035public class PrioritizedParameterNameDiscoverer implements ParameterNameDiscoverer {
036
037        private final List<ParameterNameDiscoverer> parameterNameDiscoverers =
038                        new LinkedList<ParameterNameDiscoverer>();
039
040
041        /**
042         * Add a further {@link ParameterNameDiscoverer} delegate to the list of
043         * discoverers that this {@code PrioritizedParameterNameDiscoverer} checks.
044         */
045        public void addDiscoverer(ParameterNameDiscoverer pnd) {
046                this.parameterNameDiscoverers.add(pnd);
047        }
048
049
050        @Override
051        public String[] getParameterNames(Method method) {
052                for (ParameterNameDiscoverer pnd : this.parameterNameDiscoverers) {
053                        String[] result = pnd.getParameterNames(method);
054                        if (result != null) {
055                                return result;
056                        }
057                }
058                return null;
059        }
060
061        @Override
062        public String[] getParameterNames(Constructor<?> ctor) {
063                for (ParameterNameDiscoverer pnd : this.parameterNameDiscoverers) {
064                        String[] result = pnd.getParameterNames(ctor);
065                        if (result != null) {
066                                return result;
067                        }
068                }
069                return null;
070        }
071
072}