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:
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