I\'m trying to try out eclipse jdt/ast following this article.
This is the java code as an input:
class Hello
{
int hello()
{
int a =
I tried solving this using the following piece of code
protected static CompilationUnit parseStatements(String source) {
ASTParser parser = ASTParser.newParser(AST.JLS8);
parser.setSource(source.toCharArray());
parser.setKind(ASTParser.K_COMPILATION_UNIT);
parser.setResolveBindings(true);
parser.setEnvironment( // apply classpath
new String[] { "//home//user//Projects//SmartCopy//ASTParser_Test//bin" }, //
null, null, true);
parser.setUnitName("any_name");
final CompilationUnit cu = (CompilationUnit) parser.createAST(null);
return cu;
}
static void newcheckVariableDeclaration(){
String source ="package javaproject;" // package for all classes
+ "class Dummy {"
+ "int j;" //
+ " public void add(){"
+ "int x=0,y=0;"
+ "j=x+y;\n" //
+ " }" //
+ "}";
final CompilationUnit root = parseStatements(source);
root.accept(new ASTVisitor() {
public boolean visit(SimpleName node) {
System.out.println(node.toString());
if(node.resolveBinding() == null){
System.out.println(node.toString()+" is not declared");
}
else{
System.out.println(node.toString() + " is declared");
}
System.out.println();
return true;
}
});