Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Language IDEs » Java Development Tools (JDT) » Detect Compilation Errors with ASTParser
Detect Compilation Errors with ASTParser [message #1115164] Mon, 23 September 2013 19:09
Nick Collier is currently offline Nick Collier
Messages: 20
Registered: July 2009
Junior Member
I'm trying to use the ASTParser to detect compilation errors, but getProblems when called on the CompilationUnit that results from the parse seems to always return an empty array. My code:

ASTParser parser = ASTParser.newParser(AST.JLS4);
String source = doc.get();
 source = source.substring(0, data.getOffset()) + "return " + source.substring(data.getOffset());          
 parser.setKind(ASTParser.K_COMPILATION_UNIT);
 parser.setSource(source.toCharArray());
parser.setProject(project);   
 CompilationUnit result = (CompilationUnit)parser.createAST(null);


The intention here is to see if adding a return in some appropriate spot will eliminate a known compilation error. For example, if the original source looks like:

public class Foo {
  public double getVal() {
     0.5;
  }
}


Then adding the return will create a valid return statement:

return 0.5;


However, I've noticed that adding the return to prior to an unresolved variable, and parsing that source doesn't produce any problems. In that case, the source passed to the ASTParser is:

public class Foo {
  public double getVal() {
    return abc;
  }
}


Calling getProblems() on the result of the parse of this source returns an empty array. Is there someway to get this to work?

thanks,

Nick

[Updated on: Mon, 23 September 2013 19:14]

Report message to a moderator

Previous Topic:Text Search function failing b/c "Resource out of sync with the file system"
Next Topic:JSF Support
Goto Forum:
  


Current Time: Fri Oct 31 05:01:19 GMT 2014

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

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