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