我有一个Java应用程序,做结果的文件,并释放输出一些操作。 我想补充一个属性,每个人都在使用我的应用程序可以添加他/她的具体操作。 并知道我要加载的类不知道它的名称或方法。
这是我的课,我知道可能被添加的类和方法名:
public class PathGetter {
public static void main(String[] args) {
try{
String AbsolutePath = args[0];
AbsolutePath += "\\in.txt";
String SecondFilePath = args[1];
SecondFilePath += "\\out.txt";
File file = new File(SecondFilePath);
file.createNewFile();
FileWriter writer = new FileWriter(file);
FileReader fr = new FileReader();
Object newOp = null;
String newOpSign="!@#$%^&*";
Method m = null;
MathOperations mathOperations = new MathOperationsImpl();
System.out.println("Do you have new operator? ");
Scanner scanner1 = new Scanner(System.in);
if(scanner1.nextBoolean()==true){
Scanner scanner2 = new Scanner(System.in);
System.out.println("Enter operator sign: ");
newOpSign = scanner2.nextLine();
File addedClass= new File("E:\\workspace\\940420\\bin");
URI uri = addedClass.toURI();
URL[] urls = new URL[]{uri.toURL()};
ClassLoader classLoader = new URLClassLoader(urls);
Class clazz = classLoader.loadClass("second.MathOperationsUpdateImpl"); \\I don't know the class name might be added
newOp = clazz.newInstance();
m = newOp.getClass().getMethod("mathOperate", String.class); \\I don't know the method name in added class
}
ArrayList<String> answer = new ArrayList<String>();
for (int i = 0; i < fr.readFile(AbsolutePath).size(); i++) {
if((fr.readFile(AbsolutePath).get(i)).contains(newOpSign)){
answer.add(i, String.valueOf(m.invoke(newOp,(fr.readFile(AbsolutePath).get(i)))));
}
else{
answer.add(i, String.valueOf(mathOperations.mathOperate(fr.readFile(AbsolutePath).get(i))));
}
}
for (int i = 0; i < fr.readFile(AbsolutePath).size(); i++) {
writer.write(answer.get(i)+ "\r\n");
}
writer.close();
System.out.print("Your File Successfully Created In: " + SecondFilePath);
} catch (Exception e) {
System.err.println("Error: " + e.getMessage());
}
}
}