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

[492 byte] By [Andre-Dantas-Rocha] at [2007-9-30 21:42:58]
# 1
java.lang.reflect, and classes associated with reflection, like the Class class in java.lang
Adeodatus at 2007-7-7 3:12:24 > top of Java-index,Other Topics,Algorithms...
# 2
It's not possible using reflection because I'd like to change the java source code.
Andre-Dantas-Rocha at 2007-7-7 3:12:24 > top of Java-index,Other Topics,Algorithms...
# 3
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...
Adeodatus at 2007-7-7 3:12:24 > top of Java-index,Other Topics,Algorithms...
# 4
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]
Franck_Lefevre at 2007-7-7 3:12:24 > top of Java-index,Other Topics,Algorithms...
# 5
> 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.
jschell at 2007-7-7 3:12:24 > top of Java-index,Other Topics,Algorithms...
# 6

>

> 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.)

jschell at 2007-7-7 3:12:24 > top of Java-index,Other Topics,Algorithms...
# 7
why don't you use the refactoring methods of eclipse?
koen.dejonghe at 2007-7-7 3:12:24 > top of Java-index,Other Topics,Algorithms...
# 8
> why don't you use the refactoring methods of eclipse?4 month old thread.And exactly how does that particular part of eclipse help to find those particular methods?And how does that particular part of eclipse help to replace to the code?
jschell at 2007-7-7 3:12:24 > top of Java-index,Other Topics,Algorithms...