java本身提供的方法不能获取方法的参数名的,只能获取每个参数的类型
比如:
public String concatString(String param1,String param2){
return param1+param2;
}
想获取”param1”,和”param2”这个参数名,肯定是不行的。关于获取方法的参数名字(不是参数类型),一般的做法是借助第三方包javassist或asm来实现。参见下面的文章:
java反射:使用asm 获取方法的参数名
javassist:增强型的java反照工具,获取方法参数名
不过如果你的项目是基于axis2环境的,其实还有第三种方法:
我做基于axis2的项目时,需要获取方法的参数名字,当时也找到了上述的两种办法,都可以实现,没啥问题,但是系统要多一个jar包,感觉不爽。想到axis2在生成wsdl文件时需要获取方法的名字,所以我认为axis2的jar包中肯定有办法获取方法的参数名。于是研究了axis2 关于生成wsdl文件的相关代码,总算找到了,用法也很简单。
在axsi2生成wsdl文件时,使用了一个叫org.apache.axis2.description.java2wsdl.bytecode.MethodTable(axis2-kernel-1.6.2.jar)的类来获取方法的参数名,代码如下:
/* * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */
package org.apache.axis2.description.java2wsdl.bytecode;
import java.lang.reflect.Method;
import java.util.HashMap;
public class MethodTable {
private HashMap nameToMethodMap;
//关键的实现都在这个类中
private ChainedParamReader cpr;
public MethodTable(Class cls) throws Exception {
cpr = new ChainedParamReader(cls);
nameToMethodMap = new HashMap();
loadMethods(cls);
}
/** * To load all the methods in the given class by Java reflection * * @param cls * @throws Exception */
private void loadMethods(Class cls) throws Exception {
Method [] methods = cls.getMethods();
for (int i = 0; i < methods.length; i++) {
Method method = methods[i];
if (method.isBridge()) {
continue;
}
nameToMethodMap.put(method.getName(), method);
}
}
public String [] getParameterNames(String methodName) {
Method method = (Method) nameToMethodMap.get(methodName);
if (method == null) {
return null;
}
return cpr.getParameterNames(method);
}
}
看上面的代码,这个类其实很简单,实际实现获取参数名的工作都是在org.apache.axis2.description.java2wsdl.bytecode.ChainedParamReader下完成的
/* * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */
package org.apache.axis2.description.java2wsdl.bytecode;
import java.io.IOException;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
/** * Description: In ParamReader class, user cannot get inherited method parameter * from the class they passed in for performance reasons This class * is walks up the inheritance chain. If the method is not found in * the derived class, search in super class. If not found in the immedidate super * class, search super class's super class, until the root, which is java.lang.Object, * is reached. This is not an eager load since it only start searching the super class * when it is asked to. */
public class ChainedParamReader {
private List chain = new ArrayList();
private List clsChain = new ArrayList();
private Map methodToParamMap = new HashMap();
/** * Processes a given class's parameter names. * * @param cls the class which user wants to get parameter info from * @throws IOException */
public ChainedParamReader(Class cls) throws IOException {
//具体解析每个class文件是由这个ParamReader实现的
ParamReader reader = new ParamReader(cls);
chain.add(reader);
clsChain.add(cls);
}
//now I need to create deligate methods
/** * Returns the names of the declared parameters for the given constructor. * If we cannot determine the names, return null. The returned array will * have one name per parameter. The length of the array will be the same * as the length of the Class[] array returned by Constructor.getParameterTypes(). * * @param ctor * @return Returns array of names, one per parameter, or null */
public String[] getParameterNames(Constructor ctor) {
//there is no need for the constructor chaining.
return ((ParamReader) chain.get(0)).getParameterNames(ctor);
}
/** * Returns the names of the declared parameters for the given method. * If cannot determine the names in the current class, search its parent * class until we reach java.lang.Object. If still can not find the method, * returns null. The returned array has one name per parameter. The length * of the array will be the same as the length of the Class[] array * returned by Method.getParameterTypes(). * * @param method * @return String[] Returns array of names, one per parameter, or null */
public String[] getParameterNames(Method method) {
//go find the one from the cache first
if (methodToParamMap.containsKey(method)) {
return (String[]) methodToParamMap.get(method);
}
String[] ret = null;
//在当前类中寻找指定的方法,找到就返回
for (Iterator it = chain.iterator(); it.hasNext();) {
ParamReader reader = (ParamReader) it.next();
ret = reader.getParameterNames(method);
if (ret != null) {
methodToParamMap.put(method, ret);
return ret;
}
}
//if we here, it means we need to create new chain.
Class cls = (Class) clsChain.get(chain.size() - 1);
//如果在当前类中找不到指定的方法,就到父类中寻找,找不到就返回null
while (cls != null && cls != java.lang.Object.class && cls.getSuperclass() != null) {
Class superClass = cls.getSuperclass();
try {
ParamReader _reader = new ParamReader(superClass);
chain.add(_reader);
clsChain.add(cls);
ret = _reader.getParameterNames(method);
if (ret != null) { //we found it so just return it.
methodToParamMap.put(method, ret);
return ret;
}
} catch (IOException e) {
//can not find the super class in the class path, abort here
return null;
}
cls = superClass;
}
methodToParamMap.put(method, ret);
return null;
}
}
仔细看代码,ChainedParamReader也只是一层级联封装,实现对父类的方法搜索,真正实现对每个class文件字节读取解析是在 org.apache.axis2.description.java2wsdl.bytecode.ParamReader(axis2-kernel-1.6.2.jar)里完成的,如果你想继续深入可以看axis2的源代码。
对于我来说,我只要关心org.apache.axis2.description.java2wsdl.bytecode.ChainedParamReader这个类就够了。
于是我根据自己的需要,基于ChainedParamReader写了这个下面这个类,来实现获取一般方法和构造方法的参数名。
import java.io.IOException;
import java.lang.reflect.Constructor;
import java.lang.reflect.Member;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;
import org.apache.axis2.description.java2wsdl.bytecode.ChainedParamReader;
/** * 获取构造函数或方法的参数名 * @author guyadong * */
public class ParameterNames {
private final Map, ChainedParamReader> readers = new HashMap, ChainedParamReader>();
private final Class> clazz;
/** * @param clazz 要构造函数或方法的参数名的类 */
public ParameterNames(Class> clazz) {
this.clazz = clazz;
try {
Class> c = clazz;
do {
readers.put(c, new ChainedParamReader(c));
} while (null != (c = c.getSuperclass()));
} catch (IOException e) {
throw new RuntimeException(e);
}
}
/** * 获取构造函数或方法的参数名 * @param reader * @param member 构造函数或方法对象 * @return */
private final String[] getParameterNames(ChainedParamReader reader, Member member) {
if (member instanceof Method)
return reader.getParameterNames((Method) member);
else if (member instanceof Constructor)
return reader.getParameterNames((Constructor>) member);
else
throw new IllegalArgumentException("member type must be Method or Constructor");
}
/** * 获取构造函数或方法的参数名 * @param member 构造函数或方法对象 * @return * @see #getParameterNames(ChainedParamReader, Member) */
public final String[] getParameterNames(Member member) {
Assert.notNull(member, "member");
Class> declaringClass = member.getDeclaringClass();
ChainedParamReader reader;
if (null == (reader = readers.get(declaringClass))) {
throw new IllegalArgumentException(String.format("%s is not member of %s", member.toString(),
declaringClass.getName()));
}
return getParameterNames(reader, member);
}
/** * 获取构造函数或方法的参数名
* {@code name}为{@code null}时,获取构造函数的参数名 * @param name 方法名 * @param parameterTypes 构造函数或方法的参数类型 * @return * @throws NoSuchMethodException * @see {@link #getParameterNames(String, Class)} */
public final String[] getParameterNames(String name, Class>[] parameterTypes) throws NoSuchMethodException {
try {
Member member = null == name ? clazz.getConstructor(parameterTypes) : clazz.getMethod(name, parameterTypes);
return getParameterNames(member);
} catch (SecurityException e) {
throw new IllegalArgumentException(e);
}
}
}
注意:
不论是使用asm,javassist,还是我上述的办法,都只能获取实体类中方法的参数名,对于抽象类或接口,是没有办法的。