在 Java 里,“Parameter(参数)”这个词可能出现在不同场景,下面按常见情况给你说明如何获取。
Method / Constructor)import java.lang.reflect.Method;
import java.lang.reflect.Parameter;
public class Test {
public void hello(String name, int age) {}
public static void main(String[] args) throws Exception {
Method method = Test.class.getMethod("hello", String.class, int.class);
Parameter[] parameters = method.getParameters();
for (Parameter p : parameters) {
System.out.println("参数名: " + p.getName());
System.out.println("参数类型: " + p.getType());
System.out.println("是否是可变参数: " + p.isVarArgs());
}
}
}
⚠️ 注意
p.getName() 可能是 arg0, arg1javac -parameters Test.java
或使用 Maven:
<compilerArgs>
<arg>-parameters</arg>
</compilerArgs>
@MyAnnotation(value = "test")
public class Demo {}
MyAnnotation anno = Demo.class.getAnnotation(MyAnnotation.class);
String value = anno.value(); // 获取参数
@Around("execution(* com.xxx.*.*(..))")
public Object around(ProceedingJoinPoint pjp) throws Throwable {
Object[] args = pjp.getArgs(); // 获取参数值
for (Object arg : args) {
System.out.println(arg);
}
return pjp.proceed();
}
HttpServletRequest 中的参数(Web)String name = request.getParameter("name");
String[] values = request.getParameterValues("hobby");
public static void main(String[] args) {
for (String arg : args) {
System.out.println(arg);
}
}
| 场景 | 获取方式 |
|---|---|
| 方法定义参数 | Method.getParameters() |
| 方法实际参数 | AOP / 反射调用 |
| 注解参数 | annotation.xxx() |
| Web 参数 | request.getParameter() |
| 命令行参数 | main(String[] args) |
如果你指的是 某一个具体场景(比如 MyBatis、Spring、反射工具类、IDEA 插件等),可以告诉我,我可以给你更精准的示例。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。