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.lang.reflect.Parameter; 022 023import org.springframework.lang.Nullable; 024 025/** 026 * {@link ParameterNameDiscoverer} implementation which uses JDK 8's reflection facilities 027 * for introspecting parameter names (based on the "-parameters" compiler flag). 028 * 029 * @author Juergen Hoeller 030 * @since 4.0 031 * @see java.lang.reflect.Method#getParameters() 032 * @see java.lang.reflect.Parameter#getName() 033 */ 034public class StandardReflectionParameterNameDiscoverer implements ParameterNameDiscoverer { 035 036 @Override 037 @Nullable 038 public String[] getParameterNames(Method method) { 039 return getParameterNames(method.getParameters()); 040 } 041 042 @Override 043 @Nullable 044 public String[] getParameterNames(Constructor<?> ctor) { 045 return getParameterNames(ctor.getParameters()); 046 } 047 048 @Nullable 049 private String[] getParameterNames(Parameter[] parameters) { 050 String[] parameterNames = new String[parameters.length]; 051 for (int i = 0; i < parameters.length; i++) { 052 Parameter param = parameters[i]; 053 if (!param.isNamePresent()) { 054 return null; 055 } 056 parameterNames[i] = param.getName(); 057 } 058 return parameterNames; 059 } 060 061}