Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Language IDEs » Java Development Tools (JDT) » Dynamic Compilation and Class Loader (file deletion during runtime without delay...)
Dynamic Compilation and Class Loader (file deletion during runtime without delay...) [message #1725240] Wed, 02 March 2016 03:32
Jia Guo is currently offline Jia GuoFriend
Messages: 2
Registered: March 2016
Junior Member
I am trying to dynamically compile a java program which includes three classes (one main with other two). After rewriting the code using ASTRewriter and pass the rewritten source code to Dynamic Compiler, I successfully compiled the code and output the new .class file to a local storage. Since the output file has the same package as the original file, when I tried to load the class using classLoader, it actually loads the original file rather than the newly output one. In this case, the rewritten part of the source could not be implemented.

I set a breakpoint right before classLoader, and manually delete the original files at that moment, then the program successfully load the newly output files as I expect.

My thought is I can immediately delete the source file right after the rewrite is applied, so that the classLoader could not find the original files when it tries to load, but will load the newly output files. I did so, but it seems there is a delay in eclipse when I delete the file. The files will not disappear from the project view until about 5 seconds after the program is terminated. So, even though the file is supposed to have been deleted, the program can still load the files. That's not what I expect.

I have two questions regarding this issue:
1. is there any way that I can use to make file deletion take effect immediately during the runtime without delay?
2. In addition to the method I mentioned, is there any other way to force the classLoader load newly output files instead of the original files?

Part of control method (main):
directory = args[0];
		file = new File(directory);
		File moveTo = new File("/Users/JP/Desktop/root");
		File[] files = file.listFiles();
		for (File testfile : files) {
			if (testfile.isFile()) {
				source = demo.readJavaFile(testfile.toString());
				unit = demo.parse(source);
				document = new Document(source);
				visitor = new MyVisitor(unit);
				unit.accept(visitor);
				edit = visitor.rewrite.rewriteAST(document, null);
				edit.apply(document);
				String fileName = testfile.getName();
				testfile.delete();
				new DynamicCompiler(document.get(), fileName.substring(0, fileName.length()-5));
			 	DynamicCompiler.main(null);
			}
		}
		DynamicCompiler.runIt();


runIt() Method:
public static void runIt()
    {
        // Create a File object on the root of the directory
        // containing the class file
        File file = new File(classOutputFolder); 
 
        try
        {
        	// Convert File to a URL
            URL url = file.toURL(); // file:/classes/demo
            URL[] urls = new URL[] { url };
 
            // Create a new class loader with the directory
            ClassLoader loader = new URLClassLoader(urls);
 
            // Load in the class; Class.childclass should be located in
            // the directory file:/class/demo/
            Class mainClass = loader.loadClass("pa3.MainTest.PA3_Main");
            Class params[] = {};
            Method mainMethod = mainClass.getMethod("main", String[].class);
            mainMethod.invoke(null,  new Object[]{null});
        }
        catch (MalformedURLException e)
        {
        }
        catch (ClassNotFoundException e)
        {
        }
        catch (Exception ex)
        {
            ex.printStackTrace();
        }
    }
Previous Topic:Obtaining method bindings for types not explicitly declaring them
Next Topic:What is the current state of the RAT project?
Goto Forum:
  


Current Time: Fri Mar 29 05:44:35 GMT 2024

Powered by FUDForum. Page generated in 0.01477 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top