java利用反射执行字符串方法
来源:互联网 发布:马航mh370终结篇 知乎 编辑:程序博客网 时间:2024/06/06 01:17
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
public class TestMethod {
public static void main(String[] args) throws NoSuchMethodException,
SecurityException, IllegalAccessException,
IllegalArgumentException, InvocationTargetException {
TestMethod testMethod = new TestMethod();
TestMethod.City city = testMethod.new City();
String name = "setName(成都)";
String code = "setCode(chengdu)";
testMethod.dotMethod(name, city);
testMethod.dotMethod(code, city);
System.out.println(city.getName() + "," + city.getCode());
}
private void dotMethod(String methodStr, Object obj)
throws NoSuchMethodException, SecurityException,
IllegalAccessException, IllegalArgumentException,
InvocationTargetException {
// 拆解方法字符串,找出方法名
String methodName = methodStr.substring(0, methodStr.indexOf('('));
// 找出参数
String[] args = null;
String paramStr = methodStr.substring(methodStr.indexOf('(') + 1,
methodStr.indexOf(')'));
if (!paramStr.isEmpty()) {
String[] tmp = paramStr.split(",");
args = tmp;
}
Class c = obj.getClass();
Method method = c.getMethod(methodName, String.class);
method.invoke(obj, args[0]);
}
public class City {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
private String code;
}
}
import java.lang.reflect.Method;
public class TestMethod {
public static void main(String[] args) throws NoSuchMethodException,
SecurityException, IllegalAccessException,
IllegalArgumentException, InvocationTargetException {
TestMethod testMethod = new TestMethod();
TestMethod.City city = testMethod.new City();
String name = "setName(成都)";
String code = "setCode(chengdu)";
testMethod.dotMethod(name, city);
testMethod.dotMethod(code, city);
System.out.println(city.getName() + "," + city.getCode());
}
private void dotMethod(String methodStr, Object obj)
throws NoSuchMethodException, SecurityException,
IllegalAccessException, IllegalArgumentException,
InvocationTargetException {
// 拆解方法字符串,找出方法名
String methodName = methodStr.substring(0, methodStr.indexOf('('));
// 找出参数
String[] args = null;
String paramStr = methodStr.substring(methodStr.indexOf('(') + 1,
methodStr.indexOf(')'));
if (!paramStr.isEmpty()) {
String[] tmp = paramStr.split(",");
args = tmp;
}
Class c = obj.getClass();
Method method = c.getMethod(methodName, String.class);
method.invoke(obj, args[0]);
}
public class City {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
private String code;
}
}
阅读全文
0 0
- java利用反射执行字符串方法
- Java利用反射,将字符串拼成方法并执行
- java利用反射执行对象的方法
- 利用一下java的反射根据字符串找类,方法,然后执行方法
- Java 利用反射获取方法
- java利用反射原理:根据某个对象的名称和方法去执行该方法。
- java 利用反射获取类,实例化,并执行其内部方法
- Java反射学习(利用反射遍历链表并执行所有对象的所有get方法)
- java中利用反射机制实现调用给定为字符串的方法名
- java中利用反射机制实现调用给定为字符串的方法名
- 通过字符串创建对象并访问类中的方法(利用java的反射)
- JAVA 反射方式执行类方法
- 利用反射动态加载Assembly动态执行类型方法
- 利用java反射访问私有方法
- 利用Java反射机制调用类方法
- Java利用反射操作成员变量,方法
- Java反射(四)利用反射调用方法
- 利用java 反射类自动执行类函数
- win7系统,打开office出现错误代码0x8007007B的解决办法
- efm32低功耗时钟模块cmuClock_CORELE
- eclipse 导入 spring 源码
- 增加Hokuyo Laser到你的turtlebot注意事项
- yii2 常用组件 -- 导航组件(Nav)
- java利用反射执行字符串方法
- idea+maven中的jar包缺失的解决办法(提示没有add library)
- 稀疏分布式存储(Sparse Distributed Memory)的golang简单实现
- eclipse运行maven项目报错:找不到ContextLoaderListener、IntrospectorCleanupListener
- 【2017今日头条】头条校招
- js回车事件
- 四大线程池详解
- 读取文本文件的所有行并写入到数据库
- python 高阶函数:sort,filter,reduce,map