r/javahelp • u/wildwombat3 • Mar 25 '20
I’m looking to write a program that modified another source file, recompiles that file, and uses the new functionality.
I’ve tried using ToolProvider’s getSystemJavaCompiler() method but I’m still not noticing the new functionality after I recompile. Has anyone done something like this and could offer suggestions?
1
u/Kambz22 Intermediate Brewer Mar 25 '20
I would think that's because the old class is already loaded in the JVM. I've never edited and compiled a class before without restarting the application. Not sure the best way.
Is this for school or for a personal project or are you just fooling around? If it's for school, they probably shouldn't be teaching you this.
1
u/wildwombat3 Mar 25 '20
It’s for school. I’m trying in implement a delta debugging algorithm. The algorithm itself is straightforward, but having to constantly make changes to a file and test it’s new functionality is proving to be difficult.
1
u/erjiin Mar 25 '20
I'm unsure of what is doing a delta debugger but if it's something like git bisect, i can imagine that the correct way to test this should be (of course) to do a unit test using a set of your file compiled at different steps. Are you doing your tests with junit (or other test framework) or "at hand" ?
1
u/daCrimsonSnasher Mar 25 '20
You'll need to implement a custom class loader since you cannot (AFAIK) reload classes using the default class loader. It's not complicated - just quite a bit of work. Look for some resources online, and we can help out if you get stuck.
4
u/hephaestusness Mar 25 '20
This is a job for Groovy! Groovy is a Jvm language that is syntactically identical to java 7+ some groovy additional syntactic sugar. you can pass a string of source code and it will compile it and return the resulting object instance. Since all valid java is valid groovy, once done with the source, the final algo can be dumped into a file for permanent integration.
0
u/daCrimsonSnasher Mar 26 '20
I wouldn't recommend this at all - it simply introduces a whole new language as a dependency. Why not Clojure then - you'll get infinitely better metaprogramming at least.
2
u/[deleted] Mar 25 '20
What are you trying to accomplish? A java repl?