Community
Participate
Working Groups
I tried to use ASTParser to parse text into expressions. i.e after calling ASTParser.setKind(K_EXPRESSION); It works successfully in almost all cases, except for text which is an array initializer such as: String text = "{\"red\", \"yellow\"}"; The createAST method returns a CompilationUnit node instead of an ArrayInitializer node. The problem string in the CompilationUnit node is: 'Syntax error on tokens, delete these tokens' Note: If the entire Java file is parsed, instead of just the code snippet, then the ArrayInitializer node is correctly created. So, this problem seems to be only for the ASTParser.setKind(K_EXPRESSION) case. The code for the test case is as follows: ******************TEST_CASE **************************************** import org.eclipse.jdt.core.compiler.IProblem; import org.eclipse.jdt.core.dom.AST; import org.eclipse.jdt.core.dom.ASTNode; import org.eclipse.jdt.core.dom.ASTParser; import org.eclipse.jdt.core.dom.CompilationUnit; import junit.framework.TestCase; public class ArrayInitializerTest extends TestCase { public void testASTParser() { String text = "{\"red\", \"yellow\"}"; ASTParser parser = ASTParser.newParser(AST.JLS3); parser.setKind(ASTParser.K_EXPRESSION); parser.setSource(text.toCharArray()); ASTNode node = parser.createAST(null); if (node instanceof CompilationUnit) { CompilationUnit compilationUnit = (CompilationUnit) node; IProblem[] problems = compilationUnit.getProblems(); StringBuffer buffer = new StringBuffer(); for (int i=0; i<problems.length; i++) { buffer.append("Problem = " + problems[i].getMessage()); } fail(buffer.toString()); } } }
*** This bug has been marked as a duplicate of 125270 ***