1 package com.github.javaparser.utils; 2 3 import com.github.javaparser.JavaParser; 4 import com.github.javaparser.ParseResult; 5 import com.github.javaparser.ParserConfiguration; 6 import com.github.javaparser.ast.CompilationUnit; 7 import com.github.javaparser.ast.body.BodyDeclaration; 8 import com.github.javaparser.ast.expr.Expression; 9 import com.github.javaparser.ast.stmt.Statement; 10 11 import static com.github.javaparser.ParserConfiguration.LanguageLevel.BLEEDING_EDGE; 12 import static org.junit.jupiter.api.Assertions.fail; 13 14 public class TestParser { 15 16 private static final JavaParser parser = new JavaParser(new ParserConfiguration().setLanguageLevel(BLEEDING_EDGE)); 17 parseCompilationUnit(String stmt)18 public static CompilationUnit parseCompilationUnit(String stmt) { 19 return unpack(parser.parse(stmt)); 20 } 21 parseStatement(String stmt)22 public static Statement parseStatement(String stmt) { 23 return unpack(parser.parseStatement(stmt)); 24 } 25 unpack(ParseResult<T> result)26 private static <T> T unpack(ParseResult<T> result) { 27 if (!result.isSuccessful()) { 28 fail(result.getProblems().toString()); 29 } 30 return result.getResult().get(); 31 } 32 parseExpression(String expr)33 public static Expression parseExpression(String expr) { 34 return unpack(parser.parseExpression(expr)); 35 } 36 parseBodyDeclaration(String bd)37 public static BodyDeclaration<?> parseBodyDeclaration(String bd) { 38 return unpack(parser.parseBodyDeclaration(bd)); 39 } 40 } 41