java之反射(3)方法method(java反射的几种方法)
一、获取字节码文件
有一个User类,里面有一个login函数和eat函数。
public class User {
public boolean login(String cname, int password){
if(cname=="wyy")return true;
return false;
}
private void eat(){
System.out.println("eatting......");
}
}
在主函数中获取User字节码文件。
public class Client {
public static void main(String[] args) throws Exception {
Class cUser = Class.forName("org.example.dao.User");
}
}
二、获取字节码文件api
getDeclaredMethods是获取字节码文件中,所有的方法信息,包括私有函数和共有函数以及受保护的函数等。返回是一个Method[]数组.
public class Client {
public static void main(String[] args) throws Exception {
Class cUser = Class.forName("org.example.dao.User");
Method[] declaredMethods = cUser.getDeclaredMethods();
for (Method m:declaredMethods){
System.out.println("函数全名字:"+m);
System.out.println("函数简单名字:"+m.getName());
System.out.println("函数修饰符编号:" + m.getModifiers());
System.out.println("函数修饰符:" + Modifier.toString(m.getModifiers()));
}
}
输出结果:
函数全名字:public boolean org.example.dao.User.login(java.lang.String,int)
函数简单名字:login
函数修饰符编号:1
函数修饰符:public
函数全名字:private void org.example.dao.User.eat()
函数简单名字:eat
函数修饰符编号:2
函数修饰符:private
getReturnType获取返回值类型:
public class Client {
public static void main(String[] args) throws Exception {
Class cUser = Class.forName("org.example.dao.User");
Method[] declaredMethods = cUser.getDeclaredMethods();
for (Method m : declaredMethods) {
System.out.println("函数返回值类型:"+m.getReturnType());
}
}
}
输出结果:
函数返回值类型:boolean
函数返回值类型:void
getParameterTypes()获取函数参数类型,是一个数组。函数如果没有参数,则什么也不返回。
public class Client {
public static void main(String[] args) throws Exception {
Class cUser = Class.forName("org.example.dao.User");
Method[] declaredMethods = cUser.getDeclaredMethods();
for (Method m : declaredMethods) {
System.out.println("函数参数类型数组:"+m.getParameterTypes());
for (Class p : m.getParameterTypes()) {
System.out.println("函数参数类型名字:"+p);
}
}
}
输出结果:
函数参数类型数组:[Ljava.lang.Class;@378bf509
函数参数类型数组:[Ljava.lang.Class;@5fd0d5ae
函数参数类型名字:class java.lang.String
函数参数类型名字:int
getParameters获取函数参数名字,是一个数组。
public class Client {
public static void main(String[] args) throws Exception {
Class cUser = Class.forName("org.example.dao.User");
Method[] declaredMethods = cUser.getDeclaredMethods();
for (Method m : declaredMethods) {
System.out.println("函数参数名字数组:"+m.getParameters());
for (Parameter pa : m.getParameters()) {
System.out.println("函数参数名字:"+pa.getName());
}
}
}
}
函数参数名字数组:[Ljava.lang.reflect.Parameter;@378bf509
函数参数名字:arg0
函数参数名字:arg1
函数参数名字数组:[Ljava.lang.reflect.Parameter;@5fd0d5ae
嗯?为什么参数名字是arg0和arg1呢!而不是cname和password呢?
这是因为在设计时觉得获取名字是无意义的,随着发展,就有了获取参数名的需求。必须是java8之后的版本才能获取参数名字。
如果是在maven工程中:
。在pom.xml配置文件配置如下插件,我用的是java17版本的哈
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.10.1</version>
<configuration>
<source>17</source>
<target>17</target>
<encoding>utf8</encoding>
<compilerArgs>
<arg>-parameters</arg>
</compilerArgs>
</configuration>
</plugin>
</plugins>
</build>
如果是普通工程,在idea中设置:
file--setting--build,execution,deployment--compiler--java compiler中输入-parameters
运行结果:
函数参数名字数组:[Ljava.lang.reflect.Parameter;@27d6c5e0
函数参数名字数组:[Ljava.lang.reflect.Parameter;@4f3f5b24
函数参数名字:cname
函数参数名字:password
三、调用函数
用getDeclaredMethod获取单个函数,这里没有加s哈。getDeclaredMethod有两个参数:
- 第一个参数是要获取的函数名字,
- 第二个参数是函数参数的类型字节码。
- 如果函数没有参数,就不写。
函数调用用invoke,形式:
method.invoke(obj,...args)
。method要调用的函数名
。obj是要调用的函数在那个对象中,就写那个对象。
。...args是函数中的参数类型字节码。
public class Client {
public static void main(String[] args) throws Exception {
Class cUser = Class.forName("org.example.dao.User");
Object obj =cUser.newInstance();
Method login = cUser.getDeclaredMethod("login", String.class, int.class);
Method eat = cUser.getDeclaredMethod("eat");
login.invoke(obj,"wyy",123);
eat.setAccessible(true); // 因为eat函数是私有函数,只能爆破调用
eat.invoke(obj);
}
}
调用结果:
eatting......