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

Hi,

I have tried to parse the expression wrapped in a compilation unit. See the following code but the type binding is still null. Does anybody know why? How can I get to the type binding?

Thanks for your help.

Judith


public void testASTParsing() throws JavaModelException {
ASTParser parser = ASTParser.newParser(AST.JLS3);
String projectName = "at.allianz.test.test1";
IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(projectName);
IJavaProject javaProject = JavaCore.create(project);
boolean exists = javaProject.exists(); // project does exist
parser.setProject(javaProject);
String source = "class MyClass {\n" + "public static void myMethod() { \n"
+ " int i; \n"
+ " i = 5 + 4; \n"
+ "}"
+ "}"
; parser.setSource(source.toCharArray());
parser.setResolveBindings(true);
ASTNode node = parser.createAST(null);
CompilationUnit cu = (CompilationUnit) node;
TypeDeclaration type = (TypeDeclaration) cu.types().get(0);
MethodDeclaration method = type.getMethods()[0];
Block block = method.getBody();
VariableDeclarationStatement stmt1 = (VariableDeclarationStatement) block.statements().get(0); // stmt1 = "int i;"
ExpressionStatement stmt2 = (ExpressionStatement) block.statements().get(1); // stmt2 = "i=5 + 4;"
ITypeBinding typeBind = stmt2.getExpression().resolveTypeBinding();
// typeBind == null ????
}