vendredi 29 juin 2018

Invoking method by reflection returns inconsistent results if the class is compiled more than once during run time

I compile a class during runtime and then later call that classes methods. If the class was compiled more than once the boolean methods always return true instead of the response appropriate to the arguments.

The weird part is that when I debug, I can see that the compiled class is the same and correct, the correct method at the correct class path is found and the arguments are the same.

Method that compiles the code:

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;
}

Method that calls the code:

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;
}





Aucun commentaire:

Enregistrer un commentaire