JParser { public static CompilationUnitTree parse( String version, String unitName, String source, List<File> classpath ) { ASTParser astParser = ASTParser.newParser(AST.JLS14); Map<String, String> options = new HashMap<>(); options.put(JavaCore.COMPILER_COMPLIANCE, version); options.put(JavaCore.COMPILER_SOURCE, version); if (MAXIMUM_SUPPORTED_JAVA_VERSION.equals(version)) { options.put(JavaCore.COMPILER_PB_ENABLE_PREVIEW_FEATURES, "enabled"); } astParser.setCompilerOptions(options); astParser.setEnvironment( classpath.stream().map(File::getAbsolutePath).toArray(String[]::new), new String[]{}, new String[]{}, true ); astParser.setUnitName(unitName); astParser.setResolveBindings(true); astParser.setBindingsRecovery(true); char[] sourceChars = source.toCharArray(); astParser.setSource(sourceChars); CompilationUnit astNode; try { astNode = (CompilationUnit) astParser.createAST(null); } catch (Exception e) { LOG.error("ECJ: Unable to parse file", e); throw new RecognitionException(-1, "ECJ: Unable to parse file.", e); } for (IProblem problem : astNode.getProblems()) { if (!problem.isError()) { continue; } if ((problem.getID() & IProblem.Syntax) == 0) { continue; } final int line = problem.getSourceLineNumber(); final int column = astNode.getColumnNumber(problem.getSourceStart()); throw new RecognitionException(line, "Parse error at line " + line + " column " + column + ": " + problem.getMessage()); } JParser converter = new JParser(); converter.sema = new JSema(astNode.getAST()); converter.compilationUnit = astNode; converter.tokenManager = new TokenManager(lex(version, unitName, sourceChars), source, new DefaultCodeFormatterOptions(new HashMap<>())); JavaTree.CompilationUnitTreeImpl tree = converter.convertCompilationUnit(astNode); tree.sema = converter.sema; ASTUtils.mayTolerateMissingType(astNode.getAST()); setParents(tree); return tree; }  static CompilationUnitTree parse(
    String version,
    String unitName,
    String source,
    List<File> classpath
  ); static final String MAXIMUM_SUPPORTED_JAVA_VERSION; }
@Test void should_recover_if_parser_fails() { List<File> classpath = Collections.singletonList(new File("unknownFile")); assertThrows( RecognitionException.class, () -> JParser.parse( "12", "A", "class A { }", classpath)); }