public class BookController
{
public void init(String name,Integer age)
{ }
public static void main(String[] args)throws Exception
{
Class clazz = BookController.class;
Method[] methods = clazz.getMethods();
for(Method method : methods)
{
String name = method.getName();
if(name.equals("init"))
{
Class[] carray = method.getParameterTypes();
for(Class cla : carray)
{
System.out.println(cla.getSimpleName());
}
}
}
}
} 控制台输出的结果是:
String
Integer 我不光要这些参数类型,我还要参数的名字name,age
这两个参数的名字,请问,如何得到。 如果这个方法是这样的public void init(String userName,Integer userAge)
那得到的就是userName,userAge 请问,该如何的到这些参数 谢谢
{
public void init(String name,Integer age)
{ }
public static void main(String[] args)throws Exception
{
Class clazz = BookController.class;
Method[] methods = clazz.getMethods();
for(Method method : methods)
{
String name = method.getName();
if(name.equals("init"))
{
Class[] carray = method.getParameterTypes();
for(Class cla : carray)
{
System.out.println(cla.getSimpleName());
}
}
}
}
} 控制台输出的结果是:
String
Integer 我不光要这些参数类型,我还要参数的名字name,age
这两个参数的名字,请问,如何得到。 如果这个方法是这样的public void init(String userName,Integer userAge)
那得到的就是userName,userAge 请问,该如何的到这些参数 谢谢
比如在public void init(String userName,Integer userAge)之前加一个 Annotation定义,
并且在 Annotation中写入你的init方法的参数,然后再执行这个方法的时候先读取 Annotation就可以了
Annotation的用法可以google一下啦不过你的用途是什么呢?
从class文件中提取参数信息
分析class 文件 Eclipse 也自带了,不过不知道是哪个类或 jar 做的,期待楼下哪位研究一下。
import javax.servlet.http.HttpServletRequest;
public class MethodName
{
HttpServletRequest request;
public void list(String path)
{ }
public void addValue()throws Exception
{
Class clazz = MethodName.class;
Method[] methods = clazz.getMethods();
for(Method method : methods)
{
String name = method.getName();
if(name.equals("list"))
{
/*
* 此处得到list方法的参数的类型(String)和参数名称(path)
*/
String path = null;
String pathValue = request.getParameter(path);
method.invoke(null,pathValue);
}
}
}
}如果大家用了springmvc,就知道springmvc里面有这个功能。
import javax.servlet.http.HttpServletRequest;
public class MethodName
{
HttpServletRequest request;
public void list(String path)
{ }
public void addValue()throws Exception
{
Class clazz = MethodName.class;
Method[] methods = clazz.getMethods();
for(Method method : methods)
{
String name = method.getName();
if(name.equals("list"))
{
/*
* 此处得到list方法的参数的类型(String)和参数名称(path)
*/
String path = null;
String pathValue = request.getParameter(path);
method.invoke(null,pathValue);
}
}
}
}如果大家用了springmvc,就知道springmvc里面有这个功能。
先得到方法参数名称,假如说是path
然后用request.getParameter(path);
取得值,。。
这样实现啊,可能吗?class 上是不会保存方法的参数名称的,除非你用注解,也就是 2楼说的。eclipse 之所以能看到class 的方法参数名,是因为 eclipse 是去分析源码得来的,没有源码的话,eclipse 用的 是 arg0 arg1 ......
使用JDK自带的反射是不行的,但是借助第三方包javaassist就可以获得, 可以到如下网站下载相关jar文件http://ishare.iask.sina.com.cn/f/21122132.html?from=like如下是代码,你可以执行测试一下。绝对好用public static void main(String[] args) {
Class clazz = EmailServiceImpl.class;
try {
ClassPool pool = ClassPool.getDefault();
CtClass cc = pool.get(clazz.getName());
CtMethod cm = cc.getDeclaredMethod("removeEmails");
MethodInfo methodInfo = cm.getMethodInfo();
CodeAttribute codeAttribute = methodInfo.getCodeAttribute();
LocalVariableAttribute attr = (LocalVariableAttribute) codeAttribute.getAttribute(LocalVariableAttribute.tag);
if (attr == null) {
// exception
}
String[] paramNames = new String[cm.getParameterTypes().length];
int pos = Modifier.isStatic(cm.getModifiers()) ? 0 : 1;
for (int i = 0; i < paramNames.length; i++)
paramNames[i] = attr.variableName(i + pos);
for (int i = 0; i < paramNames.length; i++) {
System.out.println(paramNames[i]);
}
} catch (NotFoundException e) {
e.printStackTrace();
}
}
兄弟我找到了,快谢谢我