背景
从简单工厂到工厂方法(参考大话设计模式)
将实际的业务封装到了具体的子类中,但是如果无法生成各个子工厂的代码,这种封装还是缺点意思。于是现在想直接根据operation子类生成工厂部分的代码。
实操
1、扫描方式
package PassivehotLoad; import javax.tools.JavaCompiler; import javax.tools.ToolProvider; import java.io.BufferedWriter; import java.io.File; import java.io.FileWriter; import java.io.IOException; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.nio.file.*; import java.util.*; /** * @BelongsProject: JAVAtest * @BelongsPackage: hotLoad * @Author: GuoYuan.Zhao * @CreateTime: 2023-03-18 10:17 * @Description: TODO * @Version: 1.0 */ public class MyManagerHot { static String className = ""; static String operation = ""; static List<String>allName ; public static void main(String[] args) throws ClassNotFoundException, InvocationTargetException, InstantiationException, IllegalAccessException, NoSuchMethodException, IOException { //首先去扫描OperationAll包下是不是多了类,如果类变多了,从类中提起关键字 如Add,然后将值赋值给变量,然后按照下面的步骤进行 String pathPackage = "E:\\zy\\TGB-zgy-2022\\米老师设计模式课相关资料必须留着\\米老师设计模式课小例子\\JAVAtest\\Factory\\src\\main\\java\\MoreAutoFactory\\OperationAll"; File file = new File(pathPackage); if (file.isDirectory()){ List<String>allNameNew = null; File[] files = file.listFiles(); for (File f : files) { String fileName = f.getAbsolutePath(); if (fileName.endsWith(".java")) { String className = fileName.substring(fileName.indexOf("MoreAutoFactory"), fileName.indexOf(".java")); String replacedName = className.replace("MoreAutoFactory\\OperationAll\\Operation", ""); // allNameNew.add(replacedName); create(replacedName); reflect(replacedName); } } } } public static void create(String className) throws IOException { Scanner scanner = new Scanner(System.in); // System.out.println("请输入要创建的算法的模式:"); // className = scanner.nextLine(); if(className.equals("Add")){ operation = "+"; }else if (className.equals("Sub")){ operation = "-"; }else if (className.equals("Mul")){ operation = "*"; }else if (className.equals("Div")){ operation = "/"; } String srcOperationCode = "package MoreAutoFactory.OperationAll;\n" + "\n" + "\n" + "import MoreAutoFactory.Operation;\n" + "\n" + "public class Operation"+className+" extends Operation {\n" + " @Override\n" + " public double getResult(){\n" + " double result = 0;\n" + " result = getNumberA()"+operation+"getNumberB();\n" + " return result;\n" + " }\n" + "\n" + "}"; //要创建文件的路径 String path1 = "E:\\zy\\TGB-zgy-2022\\米老师设计模式课相关资料必须留着\\米老师设计模式课小例子\\JAVAtest\\Factory\\src\\main\\java\\MoreAutoFactory\\OperationAll\\Operation"+ className+".java"; File file1 = new File(path1); if (!file1.exists()) { if (file1.createNewFile()) { System.out.println("新类型算法文件创建成功"); FileWriter fileWriter = new FileWriter(path1); BufferedWriter bufferedWriter = new BufferedWriter(fileWriter); bufferedWriter.write(srcOperationCode); bufferedWriter.close(); //编译文件 Compiler(System.getProperty("user.dir")+"\\Factory\\target\\classes",path1); } } String srcFactoryCode = "package MoreAutoFactory.FactoryAll;\n" + "\n" + "import MoreAutoFactory.IFactory;\n" + "import MoreAutoFactory.Operation;\n" + "import MoreAutoFactory.OperationAll.Operation"+className+";\n" + "\n" + "public class "+className+"Factory implements IFactory {\n" + " @Override\n" + " public Operation createOperation() {\n" + " return new Operation"+className+"();\n" + " }\n" + "}"; //要创建文件的路径 String path = "E:\\zy\\TGB-zgy-2022\\米老师设计模式课相关资料必须留着\\米老师设计模式课小例子\\JAVAtest\\Factory\\src\\main\\java\\MoreAutoFactory\\FactoryAll\\" + className + "Factory.java"; File file = new File(path); if (!file.exists()) { if (file.createNewFile()) { System.out.println("新类型工厂文件创建成功"); FileWriter fileWriter = new FileWriter(path); BufferedWriter bufferedWriter = new BufferedWriter(fileWriter); bufferedWriter.write(srcFactoryCode); bufferedWriter.close(); //编译文件 Compiler(System.getProperty("user.dir")+"\\Factory\\target\\classes",path); } } String srcClientCode = "package MoreAutoFactory.ClientAll;\n" + "\n" + "import MoreAutoFactory.FactoryAll."+className+"Factory;\n" + "import MoreAutoFactory.IFactory;\n" + "import MoreAutoFactory.Operation;\n" + "\n" + "public class "+className+"Client {\n" + " public void getEndResult() throws Exception {\n" + " IFactory operFactory = new "+className+"Factory();\n" + " Operation oper = operFactory.createOperation();\n" + " oper.setNumberA(2);\n" + " oper.setNumberB(2);\n" + " double result = oper.getResult();\n" + // " System.out.println("+className+");\n" + " System.out.println(result);\n" + " }\n" + "}"; //要创建文件的路径 String pathClient = "E:\\zy\\TGB-zgy-2022\\米老师设计模式课相关资料必须留着\\米老师设计模式课小例子\\JAVAtest\\Factory\\src\\main\\java\\MoreAutoFactory\\ClientAll\\" + className + "Client.java"; File fileClient = new File(pathClient); if (!fileClient.exists()) { if (fileClient.createNewFile()) { System.out.println("新类型客户端文件创建成功"); FileWriter fileWriter = new FileWriter(pathClient); BufferedWriter bufferedWriter = new BufferedWriter(fileWriter); bufferedWriter.write(srcClientCode); bufferedWriter.close(); //编译文件 Compiler(System.getProperty("user.dir")+"\\Factory\\target\\classes",pathClient); } } } public static void reflect(String className) throws ClassNotFoundException, InstantiationException, IllegalAccessException, InvocationTargetException, NoSuchMethodException { // String str = "MoreAutoFactory.Client"+className+"Client"; // System.out.println(str); Class classClient = Class.forName("MoreAutoFactory.ClientAll."+className+"Client"); Object object = classClient.newInstance(); Method getEndResultMethod = classClient.getMethod("getEndResult"); getEndResultMethod.invoke(object,null); } public static void Compiler(String compilerPath,String javaPath){ JavaCompiler javac = ToolProvider.getSystemJavaCompiler(); int status = javac.run(null, null, null, "-d", compilerPath,javaPath); if(status!=0){ System.out.println("没有编译成功!"); } } }
2、注册方式
package twoMethodCreateClass.Register; import hotLoad.BaseManager; import javax.tools.JavaCompiler; import javax.tools.ToolProvider; import java.io.BufferedWriter; import java.io.File; import java.io.FileWriter; import java.io.IOException; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.util.Scanner; /** * @BelongsProject: JAVAtest * @BelongsPackage: hotLoad * @Author: GuoYuan.Zhao * @CreateTime: 2023-03-18 10:17 * @Description: TODO * @Version: 1.0 */ public class MyManagerHot { public static void main(String[] args) throws IOException, ClassNotFoundException, InvocationTargetException, InstantiationException, IllegalAccessException, NoSuchMethodException { create(); reflect(); } static String className = ""; static String operation = ""; public static void create() throws IOException { Scanner scanner = new Scanner(System.in); System.out.println("请输入要创建的算法的模式:"); className = scanner.nextLine(); if(className.equals("Add")){ operation = "+"; }else if (className.equals("Sub")){ operation = "-"; }else if (className.equals("Mul")){ operation = "*"; }else if (className.equals("Div")){ operation = "/"; } String srcOperationCode = "package MoreAutoFactory.OperationAll;\n" + "\n" + "\n" + "import MoreAutoFactory.Operation;\n" + "\n" + "public class Operation"+className+" extends Operation {\n" + " @Override\n" + " public double getResult(){\n" + " double result = 0;\n" + " result = getNumberA()"+operation+"getNumberB();\n" + " return result;\n" + " }\n" + "\n" + "}"; //要创建文件的路径 String path1 = "E:\\zy\\TGB-zgy-2022\\米老师设计模式课相关资料必须留着\\米老师设计模式课小例子\\JAVAtest\\Factory\\src\\main\\java\\MoreAutoFactory\\OperationAll\\Operation"+ className+".java"; File file1 = new File(path1); if (!file1.exists()) { if (file1.createNewFile()) { System.out.println("新类型算法文件创建成功"); FileWriter fileWriter = new FileWriter(path1); BufferedWriter bufferedWriter = new BufferedWriter(fileWriter); bufferedWriter.write(srcOperationCode); bufferedWriter.close(); //编译文件 Compiler(System.getProperty("user.dir")+"\\Factory\\target\\classes",path1); } } String srcFactoryCode = "package MoreAutoFactory.FactoryAll;\n" + "\n" + "import MoreAutoFactory.IFactory;\n" + "import MoreAutoFactory.Operation;\n" + "import MoreAutoFactory.OperationAll.Operation"+className+";\n" + "\n" + "public class "+className+"Factory implements IFactory {\n" + " @Override\n" + " public Operation createOperation() {\n" + " return new Operation"+className+"();\n" + " }\n" + "}"; //要创建文件的路径 String path = "E:\\zy\\TGB-zgy-2022\\米老师设计模式课相关资料必须留着\\米老师设计模式课小例子\\JAVAtest\\Factory\\src\\main\\java\\MoreAutoFactory\\FactoryAll\\" + className + "Factory.java"; File file = new File(path); if (!file.exists()) { if (file.createNewFile()) { System.out.println("新类型工厂文件创建成功"); FileWriter fileWriter = new FileWriter(path); BufferedWriter bufferedWriter = new BufferedWriter(fileWriter); bufferedWriter.write(srcFactoryCode); bufferedWriter.close(); //编译文件 Compiler(System.getProperty("user.dir")+"\\Factory\\target\\classes",path); } } String srcClientCode = "package MoreAutoFactory.ClientAll;\n" + "\n" + "import MoreAutoFactory.FactoryAll."+className+"Factory;\n" + "import MoreAutoFactory.IFactory;\n" + "import MoreAutoFactory.Operation;\n" + "\n" + "public class "+className+"Client {\n" + " public void getEndResult() throws Exception {\n" + " IFactory operFactory = new "+className+"Factory();\n" + " Operation oper = operFactory.createOperation();\n" + " oper.setNumberA(2);\n" + " oper.setNumberB(2);\n" + " double result = oper.getResult();\n" + " System.out.println(result);\n" + " }\n" + "}"; //要创建文件的路径 String pathClient = "E:\\zy\\TGB-zgy-2022\\米老师设计模式课相关资料必须留着\\米老师设计模式课小例子\\JAVAtest\\Factory\\src\\main\\java\\MoreAutoFactory\\ClientAll\\" + className + "Client.java"; File fileClient = new File(pathClient); if (!fileClient.exists()) { if (fileClient.createNewFile()) { System.out.println("新类型客户端文件创建成功"); FileWriter fileWriter = new FileWriter(pathClient); BufferedWriter bufferedWriter = new BufferedWriter(fileWriter); bufferedWriter.write(srcClientCode); bufferedWriter.close(); //编译文件 Compiler(System.getProperty("user.dir")+"\\Factory\\target\\classes",pathClient); } } } public static void reflect() throws ClassNotFoundException, InstantiationException, IllegalAccessException, InvocationTargetException, NoSuchMethodException { // String str = "MoreAutoFactory.Client"+className+"Client"; // System.out.println(str); Class classClient = Class.forName("MoreAutoFactory.ClientAll."+className+"Client"); Object object = classClient.newInstance(); Method getEndResultMethod = classClient.getMethod("getEndResult"); getEndResultMethod.invoke(object,null); } public static void Compiler(String compilerPath,String javaPath){ JavaCompiler javac = ToolProvider.getSystemJavaCompiler(); int status = javac.run(null, null, null, "-d", compilerPath,javaPath); if(status!=0){ System.out.println("没有编译成功!"); } } }
总结
代码分为三种
源码
字节码
机器码
我们可以通过改变源码的方式去操纵机器码,这也是实现自动化的关键