我有一个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());
}
}
}
总结的评论给出最终答案:
1:如果您不知道类名,则可以使用File获取目录的内容:
File d = new File("<knowDirectory>");
File[] f = d.listFiles();
当然,.class
目录中应该只有一个文件。
2 ::这样您就知道类文件的名称和路径。加载它
Class c = Class.forName("<classname>").
比起您还可以通过反射获得所有可用的方法:
Methods[] m = c.getMethods();
3(A)::如果您不知道要调用的方法的名称,但是知道其签名(即返回类型和参数),则可以使用m.getReturnType()
和m.getParameterTypes()
查找具有指定签名的方法。如果有多个,则无法区分它们。如果只有一个带有签名的方法,请使用它(通过反射调用)。这样的东西(对于返回类型Double
和单个参数String
):
Class<?>[] params = m.getParameterTypes();
Class<?> r = m.getReturnType();
if(r.getName().equals(Double.class.getName())
&& params.length == 1
&& params[0].getName().equals(String.class.getName()))
{
// invoke
}
3(B)::如果您强迫用户实现抽象类(或接口),那么您知道要实现的方法的名称,请加载该类并将其强制转换为接口:
Class childClass = Class.forName("DerivedClassName");
MyAbstractClass userOperator = (MyAbstractClass)childClass.newInstance();
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句