一、主要方法
* 获取本类所有权限方法(Class): public Method[] getDeclaredMethods() throws SecurityException {}
* <p>
* 获取该类所有有权限方法,包含父类(Class): public Method[] getMethods() throws SecurityException {}
* <p>
* 获取指定方法(Class): public Method getDeclaredMethod(String name, Class<?>... parameterTypes)
* throws NoSuchMethodException, SecurityException {}
* <p>
* 调用方法(Method):public Object invoke(Object obj, Object... args)
* throws IllegalAccessException, IllegalArgumentException,
* InvocationTargetException{}
*
二、完整代码
package reflect.classhandle;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
/**
* 得到类方法,并调用
*
* 获取本类所有方法(Class): public Method[] getDeclaredMethods() throws SecurityException {}
* 获取该类所有public方法,包含父类(Class): public Method[] getMethods() throws SecurityException {}
* 获取指定方法(Class): public Method getDeclaredMethod(String name, Class<?>... parameterTypes)
* throws NoSuchMethodException, SecurityException {}
* 调用方法(Method):public Object invoke(Object obj, Object... args)
* throws IllegalAccessException, IllegalArgumentException,
* InvocationTargetException{}
*
* @ClassName: GetClassMethod
* @Author: Xlu
* @Date: 2020-06-14 13:00
* @Version 11
**/
public class GetClassMethod {
public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
Class<?> cls = Class.forName("reflect.classhandle.GetClassMethodDome");
Object obj = cls.getDeclaredConstructor().newInstance();
String value = "张三";
/**
* 获取本类中所有方法
* public java.lang.String reflect.classhandle.GetClassMethodDome.getName()
* public void reflect.classhandle.GetClassMethodDome.setName(java.lang.String)
* private void reflect.classhandle.GetClassMethodDome.getName1()
*/
Method[] declaredMethods = cls.getDeclaredMethods();
for (Method m : declaredMethods) {
System.out.println(m);
}
System.out.println("------------------------------------------------");
/**
* 获取该类所有public方法,包含父类
* public java.lang.String reflect.classhandle.GetClassMethodDome.getName()
* public void reflect.classhandle.GetClassMethodDome.setName(java.lang.String)
* public final native void java.lang.Object.wait(long) throws java.lang.InterruptedException
* public final void java.lang.Object.wait(long,int) throws java.lang.InterruptedException
* public final void java.lang.Object.wait() throws java.lang.InterruptedException
* public boolean java.lang.Object.equals(java.lang.Object)
* public java.lang.String java.lang.Object.toString()
* public native int java.lang.Object.hashCode()
* public final native java.lang.Class java.lang.Object.getClass()
* public final native void java.lang.Object.notify()
* public final native void java.lang.Object.notifyAll()
*/
Method[] methods = cls.getMethods();
for (Method m : methods) {
System.out.println(m);
}
//获取指定方法
Method setMethod = cls.getDeclaredMethod("setName" , String.class);
Method getMethod = cls.getDeclaredMethod("getName");
/**
* 调用方法
* 张三
*/
setMethod.invoke(obj , value);
System.out.println(getMethod.invoke(obj));
}
}
class GetClassMethodDome {
private String name;
public GetClassMethodDome(String name) {
this.name = name;
}
public GetClassMethodDome(){
}
public String getName() {
return name;
}
private void getName1() {
}
public void setName(String name) {
this.name = name;
}
}