如果在运行时多次编译类,则通过反射调用方法将返回不一致的结果



我在运行时编译一个类,然后调用该类方法。如果类被编译了多次,布尔方法总是返回 true,而不是适合参数的响应。

奇怪的是,当我调试时,我可以看到编译的类是相同且正确的,在正确的类路径上找到了正确的方法并且参数是相同的。

编译代码的方法:

public static void compile(String conditionClass){
try {
String fullPath =  getClassPath() + "/" + target;
File file = new File(fullPath + ".java");
if (file.exists())
file.delete();
file.createNewFile();
PrintWriter pw = new PrintWriter(file);
pw.print(conditionClass);
pw.flush();
pw.close();
JavaCompiler javac = ToolProvider.getSystemJavaCompiler();
String[] methodArgs = {fullPath + ".java"};
javac.run(null, null, null, methodArgs);
targetClass = Class.forName(target);
file.delete();
} catch (Exception e){
e.printStackTrace();
}
}
public static String getClassPath() throws Exception {
for (String s : System.getProperty("java.class.path").split(":"))
if (s.indexOf("/target/classes") + "/target/classes".length() == s.length())
return s;
}

调用代码的方法:

public boolean call(String methodName, String[][][] arg1, String[] arg2){
try {
Method m = targetClass.getMethod(methodName, new Class[]{String[][][].class, String[].class});
Object[] args = new Object[]{arg1, arg2};
Object response = m.invoke(null, args);
return (boolean)response;
} catch (Exception e){
e.printStackTrace();
}
return true;
}

正在编译的类详细信息: 类主体,作为条件类传递:

public class TestClass{
public static boolean method1(String[][][] arg1, String[] arg2){
boolean res = true;
if(path[0][0][0].equals("test target"))
return false;
return res;
}
}

正如T.J. Crowder所建议的那样,问题是具有相同路径和名称的类不会再次加载。

对于我的任务,只需更改类的名称并将其重新加载到 JVM 就足够了,但这是一种粗制滥造的方法,应该避免。

最新更新