[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[news.eclipse.tools.jdt] Using the ASTParser for parsing an expression...

Hello there,

I want to use the ASTParser for parsing an expression in order to get the return type.

String expr1 = "5 + 4";
//String expr2 = "myVar.myMethod(myParam)";
ASTParser parser = ASTParser.newParser(AST.JLS3);
parser.setKind(ASTParser.K_EXPRESSION);
parser.setResolveBindings(true);
parser.setSource(expr1.toCharArray());
ASTNode astNode = parser.createAST(null); Expression expr = (Expression) astNode;
ITypeBinding typeBinding = expr.resolveTypeBinding();


The problem is that typeBinding is null. How can I get the return type then?


2nd question: If I have got a more complex expression (like expr2) how do I add the declaration of those variables to the expression?


AST ast = astNode.getAST();
SingleVariableDeclaration varDecl = ast.newSingleVariableDeclaration();
varDecl.setType(ast.newSimpleType(ast.newSimpleName("MyType")));
varDecl.setName(ast.newSimpleName("myVar"));


Thanks a lot for your consideration!

Judith