Community
Participate
Working Groups
Sometimes it's nice to be able to parse a String representing just an Expression or just a Statement. Here's some code originally written by Konstantin for EclipseColorer that I think should be considered for inclusion into Eclipse JDT Core: package org.eclipse.jdt.core.dom; import java.util.ArrayList; import org.eclipse.jdt.core.IJavaProject; import org.eclipse.jdt.core.JavaModelException; import org.eclipse.jdt.internal.compiler.AbstractSyntaxTreeVisitorAdapter; import org.eclipse.jdt.internal.compiler.ast.CompilationUnitDeclaration; /** * @author scheglov_ke * @author daveo - Updated to use StringBuffer; provided 2.0.2 hack */ public class JavaCoreUtils { public static Expression parseExpression(AST ast, final int start, String src, IJavaProject project) throws JavaModelException { StringBuffer source = new StringBuffer("class Fake { public void foo() {System.out.println("); final int baseLength = source.length(); source.append(src); source.append(");}}"); // final ArrayList resultList = new ArrayList(); CompilationUnit cu = parseCompilationUnit(ast, source.toString(), project); cu.accept(new ASTVisitor() { public void preVisit(ASTNode node) { if ((node instanceof Expression) && (node.getStartPosition() == baseLength)) { resultList.add(node); } int pos = node.getStartPosition(); int length = node.getLength(); if (pos >= baseLength) node.setSourceRange(start + pos - baseLength, length); } }); // Expression expression = (Expression) resultList.get(0); expression.setParent(null); //((Block) expression.getParent()).statements().remove(expression); return expression; } public static Statement parseStatement(AST ast, final int start, String src, IJavaProject project) throws JavaModelException { StringBuffer source = new StringBuffer("class Fake { public void foo() {"); final int baseLength = source.length(); source.append(src); source.append(";}}"); // final ArrayList resultList = new ArrayList(); CompilationUnit cu = parseCompilationUnit(ast, source.toString(), project); cu.accept(new ASTVisitor() { public void preVisit(ASTNode node) { if ((node instanceof Statement) && (node.getStartPosition() == baseLength)) { resultList.add(node); } int pos = node.getStartPosition(); int length = node.getLength(); if (pos >= baseLength) node.setSourceRange(start + pos - baseLength, length); } }); // Statement statement = (Statement) resultList.get(0); ((Block) statement.getParent()).statements().remove(statement); return statement; } public static CompilationUnit parseCompilationUnit(AST ast, String source, IJavaProject project) throws JavaModelException { if (source == null) throw new IllegalArgumentException(); if (project == null) throw new IllegalArgumentException(); CompilationUnitDeclaration compilationUnitDeclaration = CompilationUnitResolver.resolve( source.toCharArray(), "fakeUnit", project, new AbstractSyntaxTreeVisitorAdapter()); //////////////////////////////////////////// // 2.0.2 version here // ASTConverter converter = new ASTConverter(true); //////////////////////////////////////////// // 2.1 version here ASTConverter converter = new ASTConverter(project.getOptions(true), true); //AST ast = new AST(); BindingResolver resolver = new DefaultBindingResolver(compilationUnitDeclaration.scope); ast.setBindingResolver(resolver); converter.setAST(ast); CompilationUnit cu = converter.convert(compilationUnitDeclaration, source.toCharArray()); cu.setLineEndTable(compilationUnitDeclaration.compilationResult.lineSeparatorPositions); resolver.storeModificationCount(ast.modificationCount()); return cu; } }
Parsing is likely easy, but binding resolution will require proper context.
That is correct. The submitted code does not do binding resolution.
Let me know if you think that the support for bug 48489 is enough.
Ths is a dupe of Bug 48489. I needed the API for exactly the same reason. *** This bug has been marked as a duplicate of 48489 ***
Could you please try it when next integration build is out and let me know by annotating this PR or bug 48489 that it works as expected? Thanks.