A simple parser
Hi,
I'd like to get/replace the methods implementation of a java file. Like this:
public class A {
public void myMethod(String s) { XXXX }
}
getMethod("public void myMethod(String)") > returns "public void myMethod(String s) { XXXX }"
replaceMethod("public void myMethod(String)", "public void myMethod(String s) { YYYY }") >
replace current implementation by the new one.
Is there a library to do this?
Thanks!
Andre
It is possible to use reflection, but not to change the source...You could have your parser rewrite some lines of the original, recompile it, and reload it...Or use LISP...
The problems seems simple enough to avoid reflection, and focus on using a parser.This parser would follow the grammar of Java, as defined in [url http://java.sun.com/docs/books/jls/second_edition/html/syntax.doc.html#44467]The Grammar of the Java Programming Language[/url]
> It is possible to use reflection, but not to change> the source...The problem is not solvable using reflection.The problem is not to create new functionality (or load it) but rather to replace what is already there. And reflection does not allow that.
>
> Is there a library to do this?
>
No.
Mainly because the problem is too complex.
You can certainly write a parser that does that. But you need to use the existing source so you can determine exceptions.
And unless you have had experience doing this it is just going to be easier to do a search and replace by hand (unless you have the time to learn.)