Search code examples
javaeclipseeclipse-pluginabstract-syntax-treeeclipse-jdt

Writing Java interpreter Plugin


A few days back I wrote a Textinterpreter plugin in Eclipse which basically takes a text file and simply printout it's content in the console. It does this by first taking a text file and converts it to a string. then it makes an Arraylist out of it from which each line is printed out in the console.

    List<String> mLines = new LinkedList<String>(Arrays.asList(string)

while(!mLines.isEmpty())) {
String line = mLines.remove(0);
if(line.equals("Stop...")){
                debug(DebugAction.Suspend);
}
System.out.println(">>> " + line + " <<<");

}

You can see an if statement in code above which checks whether "Stop..." is written on any line in the text file and if it is then the debug() funtion is called(which suspends running unless the user press resume() button in debugmode.)

Now I want to do the same for .java files. i.e write a Java interpreter plugin which execute a java file normally until it finds "Stop..." written in code.

Any Suggestions?


Solution

  • I don't think you really want to implement a Java interpreter, that'd be a huge project keeping you busy for some years maybe.

    The most natural solution for you task might be to scan the Java source file and automatically create breakpoints at each Stop statement. Then run the application in debug mode and you get the desired behaviour. Since you only need the line number for creating the breakpoint you can actually keep reading/scanning files line-by-line. To get additional statements executed (like calling debug(..)) add your snippet as a breakpoint condition (followed by return true; to tell the debug to stop indeed).