1
说明:
1.以下的代码亲测直接可用,
2.设计思路来自博客园的 张飞_ :http://www.cnblogs.com/zhangfei/p/5330994.html / http://www.cnblogs.com/zhangfei/p/
3.重点在于理解 当中的设计思路 ,如果问题需要讨论可以联系我QQ:316567803 或者给我留言;
7.person 和 MoveDisk类
1 package keyWordsDriver; 2 3 /** 4 * 5 */ 6 public class Person { 7 void getTheCommandFromTeacher(){} 8 void findTheDeskAtA(){} 9 void moveDeskToB(){} 10 void notifyTeacherHadFinished(){} 11 } 12 13 package keyWordsDriver; 14 15 /** 16 * 17 */ 18 public class MoveDesk { 19 20 private Person zhangsan; 21 22 23 public void setZhangsan(Person zhangsan) { 24 this.zhangsan = zhangsan; 25 } 26 27 public void setZhangsan() { 28 System.out.println(" "); 29 System.out.println("大家好我是张三,你现在执行的方法为:setZhangsan()"); 30 this.zhangsan = new Person(); 31 System.out.println(" "); 32 } 33 34 public void moveDeskFromA2B() { 35 zhangsan.getTheCommandFromTeacher(); 36 zhangsan.findTheDeskAtA(); 37 zhangsan.moveDeskToB(); 38 zhangsan.notifyTeacherHadFinished(); 39 } 40 }
8.反射需要用的关键字对象整理:
1 package keyWordsDriver; 2 3 import java.util.HashMap; 4 import java.util.Map; 5 6 /** 7 * 1.根据张三 ,得到张三的className,通过className得到张三这个对象,然后再执行张三所要执行的方法(setZhangsan) 8 */ 9 public class KeywordReflect { 10 11 public static Map<String, Map<String, String>> KEYWORD_POOLS = new HashMap<String, Map<String, String>>(); 12 static{ 13 KEYWORD_POOLS.put("张三", KeywordReflect.methodInfo(MoveDesk.class.getName(), "setZhangsan")); 14 KEYWORD_POOLS.put("把桌子从A地点搬到B地点", KeywordReflect.methodInfo(MoveDesk.class.getName(), "moveDeskFromA2B")); 15 } 16 17 public static Map<String, String> methodInfo(String className, String methodName){ 18 Map<String, String> methodInfo = new HashMap<String, String>(); 19 methodInfo.put("class", className); 20 methodInfo.put("method", methodName); 21 return methodInfo; 22 23 } 24 25 static void print() { 26 for (Map.Entry<String, Map<String, String>> entrys : KEYWORD_POOLS.entrySet() 27 ) { 28 System.out.println("entrys.getKey()="+entrys.getKey()); 29 Map<String,String> values = entrys.getValue(); 30 for (Map.Entry<String, String> entyr2 : values.entrySet() 31 ) { 32 System.out.println(entyr2); 33 } 34 } 35 } 36 37 public static void main(String[] args) { 38 KeywordReflect.print(); 39 } 40 41 42 }
9.对象库负责初始化所有需要用到的对象;
1 package keyWordsDriver; 2 3 import java.util.HashMap; 4 import java.util.Map; 5 6 /** 7 * 这是对象的注册中心 ; 8 */ 9 public class RegisterCenter { 10 public static Map<String, Object> OBJ_POOLS = new HashMap<String, Object>(); 11 static{ 12 OBJ_POOLS.put(MoveDesk.class.getName(), new MoveDesk()); 13 } 14 15 }
10.解析关键
1 package keyWordsDriver; 2 3 import java.util.ArrayList; 4 import java.util.List; 5 import java.util.regex.Matcher; 6 import java.util.regex.Pattern; 7 8 /** 9 * 10 */ 11 public class RegExp { 12 public boolean match(String reg, String str) { 13 return Pattern.matches(reg, str); 14 } 15 16 public List<String> find(String reg, String str) { 17 Matcher matcher = Pattern.compile(reg).matcher(str); 18 List<String> list = new ArrayList<String>(); 19 while (matcher.find()) { 20 list.add(matcher.group()); 21 } 22 return list; 23 } 24 25 } 26 27 package keyWordsDriver; 28 29 import java.util.List; 30 31 /** 32 * ParseKeyword 用于解析关键字,需要指定的格式 ; 33 */ 34 public class ParseKeyword { 35 public List<String> getKeywords(String p) { 36 String reg = "(?<=(?<!\\)\$\{)(.*?)(?=(?<!\\)\})"; 37 RegExp re = new RegExp(); 38 List<String> list = re.find(reg, p); 39 return list; 40 41 } 42 43 public static void main(String[] args) { 44 45 ParseKeyword p = new ParseKeyword(); 46 System.out.println(p.getKeywords("${张三},去${把桌子从A地点搬到B地点}")); 47 System.out.println(p.getKeywords("a${a}a")); 48 System.out.println(p.getKeywords("a\${a}a")); 49 System.out.println(p.getKeywords("a${a\}a")); 50 System.out.println(p.getKeywords("a${a\}a}a")); 51 System.out.println(p.getKeywords("a${a}a${")); 52 System.out.println(p.getKeywords("a${ab}a${a}")); 53 54 } 55 56 }
11,方法执行的过程:
1 package keyWordsDriver; 2 3 import java.io.BufferedReader; 4 import java.io.File; 5 import java.io.FileInputStream; 6 import java.io.InputStreamReader; 7 import java.lang.reflect.InvocationTargetException; 8 import java.lang.reflect.Method; 9 import java.util.ArrayList; 10 import java.util.List; 11 import java.util.Map; 12 13 /** 14 * 利用反射进行方法的执行 ; 15 */ 16 17 public class Executor { 18 private ParseKeyword pk = new ParseKeyword(); 19 20 public List<String> readTxtFile(String filePath) { 21 List<String> list = new ArrayList<String>(); 22 try { 23 String encoding = "UTF8"; 24 File file = new File(filePath); 25 if (file.isFile() && file.exists()) { 26 InputStreamReader read = new InputStreamReader(new FileInputStream(file), encoding); 27 BufferedReader bufferedReader = new BufferedReader(read); 28 String lineTxt = null; 29 while ((lineTxt = bufferedReader.readLine()) != null) { 30 list.add(lineTxt); 31 // System.out.println(" : "+lineTxt); 32 } 33 read.close(); 34 bufferedReader.close(); 35 } else { 36 System.out.println("找不到指定的文件"); 37 } 38 } catch (Exception e) { 39 System.out.println("读取文件内容出错"); 40 e.printStackTrace(); 41 } 42 return list; 43 } 44 45 void print() { 46 String path = ClassLoader.getSystemClassLoader().getResource("command.txt").getPath(); 47 List<String> lists = this.readTxtFile(path); 48 System.out.print("lists 总共有:"+lists.size()+"行 内容为: "); 49 for (String vaule : lists 50 ) { 51 System.out.print(vaule); 52 } 53 } 54 55 56 public void executor() { 57 String path = ClassLoader.getSystemClassLoader().getResource("command.txt").getPath(); 58 List<String> commands = this.readTxtFile(path); 59 for (String command : commands) { 60 System.out.println("---------------------------------------------"); 61 System.out.println("%%%%%%%%%%%%%%%%%%"+"command="+command+" %%%%%%%%%%%%%%%%%%%%%%"); 62 List<String> keywords = pk.getKeywords(command); 63 64 for (String keyword : keywords) { 65 this.invoke(keyword); 66 } 67 System.out.println("---------------------------------------------"); 68 } 69 } 70 71 72 public void invoke(String keyword) { 73 System.out.println("keyword="+keyword); 74 Map<String, String> keyMethod = KeywordReflect.KEYWORD_POOLS.get(keyword); 75 System.out.println("keyMethod="+keyMethod); 76 String className = keyMethod.get("class"); 77 System.out.println("className="+className); 78 String methodName = keyMethod.get("method"); 79 System.out.println("methodName="+methodName); 80 Object obj = RegisterCenter.OBJ_POOLS.get(className); 81 System.out.println("obj="+obj); 82 Method method = this.getMethod(methodName, obj); 83 System.out.println("method="+method); 84 try { 85 method.invoke(obj); 86 } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) { 87 e.printStackTrace(); 88 89 } 90 91 } 92 93 94 private Method getMethod(String methodName, Object obj) { 95 try { 96 System.out.println("methodName="+methodName+"; obj="+obj.getClass().getName()); 97 Method[] methods = obj.getClass().getMethods(); 98 for (Method m : methods) { 99 if (m.getName().equals(methodName)) { 100 return m; 101 } 102 } 103 } catch (SecurityException e) { 104 return null; 105 } 106 return null; 107 } 108 109 110 public static void main(String[] args) { 111 Executor e = new Executor(); 112 e.print(); 113 e.executor(); 114 } 115 116 117 }
12. 代码运行的结果:其实仔细的话从结果红你能看到你想看到的东西 ,上面有部分内容省略了 ;
1 "C:Program FilesJavajdk1.8.0_65injava" -Didea.launcher.port=7532 "-Didea.launcher.bin.path=C:CustomSofterdevelopIDEAIntelliJ IDEA Community Edition 2016.2.4in" -Dfile.encoding=UTF-8 -classpath "C:Program FilesJavajdk1.8.0_65jrelibcharsets.jar;C:Program FilesJavajdk1.8.0_65jrelibdeploy.jar;C:Program FilesJavajdk1.8.0_65jrelibextaccess-bridge-64.jar;C:Program FilesJavajdk1.8.0_65jrelibextcldrdata.jar;C:Program FilesJavajdk1.8.0_65jrelibextdnsns.jar;C:Program FilesJavajdk1.8.0_65jrelibextjaccess.jar;C:Program FilesJavajdk1.8.0_65jrelibextjfxrt.jar;C:Program FilesJavajdk1.8.0_65jrelibextlocaledata.jar;C:Program FilesJavajdk1.8.0_65jrelibext ashorn.jar;C:Program FilesJavajdk1.8.0_65jrelibextsunec.jar;C:Program FilesJavajdk1.8.0_65jrelibextsunjce_provider.jar;C:Program FilesJavajdk1.8.0_65jrelibextsunmscapi.jar;C:Program FilesJavajdk1.8.0_65jrelibextsunpkcs11.jar;C:Program FilesJavajdk1.8.0_65jrelibextzipfs.jar;C:Program FilesJavajdk1.8.0_65jrelibjavaws.jar;C:Program FilesJavajdk1.8.0_65jrelibjce.jar;C:Program FilesJavajdk1.8.0_65jrelibjfr.jar;C:Program FilesJavajdk1.8.0_65jrelibjfxswt.jar;C:Program FilesJavajdk1.8.0_65jrelibjsse.jar;C:Program FilesJavajdk1.8.0_65jrelibmanagement-agent.jar;C:Program FilesJavajdk1.8.0_65jrelibplugin.jar;C:Program FilesJavajdk1.8.0_65jrelib esources.jar;C:Program FilesJavajdk1.8.0_65jrelib t.jar;C:CustomSofterdevelopIDEAIdeaWorkspace_1JaraProoutproductionJaraPro;C:CustomSofterdevelopIDEAsofterwareJARseleniumJarselenium-2.47.0selenium-java-2.47.0.jar;C:CustomSofterdevelopIDEAsofterwareJARseleniumJarselenium-2.47.0libsapache-mime4j-0.6.jar;C:CustomSofterdevelopIDEAsofterwareJARseleniumJarselenium-2.47.0libssh-1.3.0.jar;C:CustomSofterdevelopIDEAsofterwareJARseleniumJarselenium-2.47.0libscglib-nodep-2.1_3.jar;C:CustomSofterdevelopIDEAsofterwareJARseleniumJarselenium-2.47.0libscommons-codec-1.10.jar;C:CustomSofterdevelopIDEAsofterwareJARseleniumJarselenium-2.47.0libscommons-collections-3.2.1.jar;C:CustomSofterdevelopIDEAsofterwareJARseleniumJarselenium-2.47.0libscommons-exec-1.3.jar;C:CustomSofterdevelopIDEAsofterwareJARseleniumJarselenium-2.47.0libscommons-io-2.4.jar;C:CustomSofterdevelopIDEAsofterwareJARseleniumJarselenium-2.47.0libscommons-lang3-3.4.jar;C:CustomSofterdevelopIDEAsofterwareJARseleniumJarselenium-2.47.0libscommons-logging-1.1.3.jar;C:CustomSofterdevelopIDEAsofterwareJARseleniumJarselenium-2.47.0libscssparser-0.9.16.jar;C:CustomSofterdevelopIDEAsofterwareJARseleniumJarselenium-2.47.0libsgson-2.3.1.jar;C:CustomSofterdevelopIDEAsofterwareJARseleniumJarselenium-2.47.0libsguava-18.0.jar;C:CustomSofterdevelopIDEAsofterwareJARseleniumJarselenium-2.47.0libshamcrest-core-1.3.jar;C:CustomSofterdevelopIDEAsofterwareJARseleniumJarselenium-2.47.0libshamcrest-library-1.3.jar;C:CustomSofterdevelopIDEAsofterwareJARseleniumJarselenium-2.47.0libshtmlunit-2.17.jar;C:CustomSofterdevelopIDEAsofterwareJARseleniumJarselenium-2.47.0libshtmlunit-core-js-2.17.jar;C:CustomSofterdevelopIDEAsofterwareJARseleniumJarselenium-2.47.0libshttpclient-4.4.1.jar;C:CustomSofterdevelopIDEAsofterwareJARseleniumJarselenium-2.47.0libshttpcore-4.4.1.jar;C:CustomSofterdevelopIDEAsofterwareJARseleniumJarselenium-2.47.0libshttpmime-4.4.1.jar;C:CustomSofterdevelopIDEAsofterwareJARseleniumJarselenium-2.47.0libsjcommander-1.29.jar;C:CustomSofterdevelopIDEAsofterwareJARseleniumJarselenium-2.47.0libsjetty-io-9.2.11.v20150529.jar;C:CustomSofterdevelopIDEAsofterwareJARseleniumJarselenium-2.47.0libsjetty-util-9.2.11.v20150529.jar;C:CustomSofterdevelopIDEAsofterwareJARseleniumJarselenium-2.47.0libsjna-4.1.0.jar;C:CustomSofterdevelopIDEAsofterwareJARseleniumJarselenium-2.47.0libsjna-platform-4.1.0.jar;C:CustomSofterdevelopIDEAsofterwareJARseleniumJarselenium-2.47.0libsjunit-4.12.jar;C:CustomSofterdevelopIDEAsofterwareJARseleniumJarselenium-2.47.0libs ekohtml-1.9.22.jar;C:CustomSofterdevelopIDEAsofterwareJARseleniumJarselenium-2.47.0libs etty-3.5.7.Final.jar;C:CustomSofterdevelopIDEAsofterwareJARseleniumJarselenium-2.47.0libsphantomjsdriver-1.2.1.jar;C:CustomSofterdevelopIDEAsofterwareJARseleniumJarselenium-2.47.0libssac-1.3.jar;C:CustomSofterdevelopIDEAsofterwareJARseleniumJarselenium-2.47.0libsserializer-2.7.2.jar;C:CustomSofterdevelopIDEAsofterwareJARseleniumJarselenium-2.47.0libs estng-6.8.5.jar;C:CustomSofterdevelopIDEAsofterwareJARseleniumJarselenium-2.47.0libswebsocket-api-9.2.11.v20150529.jar;C:CustomSofterdevelopIDEAsofterwareJARseleniumJarselenium-2.47.0libswebsocket-client-9.2.11.v20150529.jar;C:CustomSofterdevelopIDEAsofterwareJARseleniumJarselenium-2.47.0libswebsocket-common-9.2.11.v20150529.jar;C:CustomSofterdevelopIDEAsofterwareJARseleniumJarselenium-2.47.0libsxalan-2.7.2.jar;C:CustomSofterdevelopIDEAsofterwareJARseleniumJarselenium-2.47.0libsxercesImpl-2.11.0.jar;C:CustomSofterdevelopIDEAsofterwareJARseleniumJarselenium-2.47.0libsxml-apis-1.4.01.jar;C:CustomSofterdevelopIDEAsofterwareJARothersJARjyamljyaml-1.3.jar;C:CustomSofterdevelopIDEAIntelliJ IDEA Community Edition 2016.2.4libidea_rt.jar" com.intellij.rt.execution.application.AppMain keyWordsDriver.Executor 2 lists 总共有:1行 内容为: ${张三},去${把桌子从A地点搬到B地点}”,--------------------------------------------- 3 %%%%%%%%%%%%%%%%%%command=${张三},去${把桌子从A地点搬到B地点}”, %%%%%%%%%%%%%%%%%%%%%% 4 keyword=张三 5 keyMethod={method=setZhangsan, class=keyWordsDriver.MoveDesk} 6 className=keyWordsDriver.MoveDesk 7 methodName=setZhangsan 8 obj=keyWordsDriver.MoveDesk@5674cd4d 9 methodName=setZhangsan; obj=keyWordsDriver.MoveDesk 10 method=public void keyWordsDriver.MoveDesk.setZhangsan() 11 12 13 大家好我是张三,你现在执行的方法为:setZhangsan() 14 15 16 keyword=把桌子从A地点搬到B地点 17 keyMethod={method=moveDeskFromA2B, class=keyWordsDriver.MoveDesk} 18 className=keyWordsDriver.MoveDesk 19 methodName=moveDeskFromA2B 20 obj=keyWordsDriver.MoveDesk@5674cd4d 21 methodName=moveDeskFromA2B; obj=keyWordsDriver.MoveDesk 22 method=public void keyWordsDriver.MoveDesk.moveDeskFromA2B() 23 --------------------------------------------- 24 25 Process finished with exit code 0