这里举个例子,编译时候添加参数-parameters:
public class Main11 { public static void main(String[] args) throws Exception { Class<Supper> supperClass = Supper.class; Method sayHello = supperClass.getDeclaredMethod("sayHello", String.class); sayHello.setAccessible(Boolean.TRUE); Parameter[] parameters = sayHello.getParameters(); for (Parameter parameter : parameters) { System.out.println("isNamePresent->" + parameter.isNamePresent()); System.out.println("isImplicit->" + parameter.isImplicit()); System.out.println("getName->" + parameter.getName()); System.out.println("====================="); } } public static class Supper { private void sayHello(String name) { System.out.println(String.format("%s say hello!", name)); } } }输出结果:
isNamePresent->true isImplicit->false getName->name =====================如果不设置编译参数-parameters,会输出下面的结果:
isNamePresent->false isImplicit->false getName->arg0 ===================== 小结这篇文章开篇对反射的基本进行介绍,后面花大量篇幅列举了相关类库的API和API使用,掌握这些类库,才能轻松地进行反射编程。
个人博客Throwable's Blog