[
Date Prev
][
Date Next
][
Thread Prev
][
Thread Next
][
Date Index
][
Thread Index
]
[news.eclipse.tools.jdt] Re: Using the ASTParser for parsing an expression...
From
:
judith.wiesinger@xxxxxxxxxxxxxxx
(Judith Wiesinger)
Date
: Fri, 7 Apr 2006 10:57:35 +0000 (UTC)
Newsgroups
:
eclipse.tools.jdt
Organization
: Eclipse
User-agent
: NewsPortal/0.36 (http://florian-amrhein.de/newsportal)
Problem solved:
I forgot that further things had to be set in order that the source is treated as compilation unit.
- setProject
- setCompilerOptions
- setUnitName
not to forget to setKind to ASTParser.K_COMPILATION_UNIT.
public void testASTParsing() throws JavaModelException {
ASTParser parser = ASTParser.newParser(AST.JLS3);
// set source
String source = "import java.util.ArrayList;"
+ "import java.util.List;"
+ "class MyClass {\n" + "public void myMethod() { \n"
+ " List<Object> myList = new ArrayList<Object>(); \n"
+ " String s; \n"
+ " s = myList.get(0).toString(); \n"
+ "}"
+ "}"
; parser.setSource(source.toCharArray());
// set project
IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(MY_PROJECT_NAME);
IJavaProject javaProject = JavaCore.create(project);
boolean exists = javaProject.exists();
parser.setProject(javaProject);
parser.setCompilerOptions(javaProject.getOptions(true));
parser.setUnitName("MyClass");
parser.setKind(ASTParser.K_COMPILATION_UNIT);
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();
List<Statement> statements = block.statements();
for (Statement stmt : statements) {
if (stmt instanceof ExpressionStatement) {
ExpressionStatement exprStmt = (ExpressionStatement) stmt;
ITypeBinding typeBind = exprStmt.getExpression().resolveTypeBinding();
String typeName = typeBind.getQualifiedName();
}
}
}
References
:
[news.eclipse.tools.jdt] Using the ASTParser for parsing an expression...
From:
Judith Wiesinger
[news.eclipse.tools.jdt] Re: Using the ASTParser for parsing an expression...
From:
Olivier Thomann
Prev by Date:
[news.eclipse.tools.jdt] Re: Using the ASTParser for parsing an expression...
Next by Date:
[news.eclipse.tools.jdt] Re: Newbie needs help: How does javadoc capture highlighting??? NEED HELP REAL B
Previous by thread:
[news.eclipse.tools.jdt] Re: Using the ASTParser for parsing an expression...
Next by thread:
[news.eclipse.tools.jdt] window history
Index(es):
Date
Thread