- import java.io.FileReader;
- import java.io.IOException;
- import java.io.Reader;
- import java.lang.reflect.Constructor;
- import java.lang.reflect.Method;
- import java.util.Properties;
- /*
- * 写一个配置(properties)文件按记录该类的包名,以及要运行的方法
- * 思路:
- * 首先通过properties文件读取该文件, 1
- * 只要通过keys将得到相应的值即可(className,methdeName)2
- * 获得字节码 3
- * 获得构造器,创建实例 4
- * 获得方法 5
- * 设置语法忽略语法检查6
- * 执行方法 7
- *
- *
- *
- * */
- @SuppressWarnings("all")
- public class Text {
- public static void main(String[] args)throws IOException,Exception {
- Properties prop=new Properties();
- prop.load(new FileReader("text.properties"));//1
- String className= prop.getProperty("className");//2
- String methdeName=prop.getProperty("methdeName");
- Class clazz=Class.forName(className);//3
- Constructor con=clazz.getConstructor(String.class,int.class,int.class,int.class);//4
- Object ob=con.newInstance("李四",90,90,100);
- Method sum=clazz.getDeclaredMethod(methdeName, null);//5
- sum.setAccessible(true); //6
- System.out.println(sum.invoke(ob,null));sum.invoke(ob,null);//7
- }
- }
复制代码
|
|