/** * The given options should at least provide the source level (JavaCore.COMPILER_SOURCE), the compiler compliance * level (JavaCore.COMPILER_COMPLIANCE) and the target platform (JavaCore.COMPILER_CODEGEN_TARGET_PLATFORM). * * Without these options, it is not possible for the code formatter to know what kind of source it needs to format. */ private static Properties readConfigInternal() { Properties properties = new Properties(); properties.setProperty(JavaCore.COMPILER_SOURCE, JDTOptions.getOption(JavaCore.COMPILER_SOURCE)); properties.setProperty(JavaCore.COMPILER_COMPLIANCE, JDTOptions.getOption(JavaCore.COMPILER_COMPLIANCE)); properties.setProperty(JavaCore.COMPILER_CODEGEN_TARGET_PLATFORM, JDTOptions.getOption(JavaCore.COMPILER_CODEGEN_TARGET_PLATFORM)); // ROASTER-96: Add a blank line after imports. "1" is equivalent to TRUE in the formatter XML file properties.setProperty(DefaultCodeFormatterConstants.FORMATTER_BLANK_LINES_AFTER_IMPORTS, "1"); return properties; }
@Override public List<Problem> validateSnippet(String snippet) { CodeSnippetParsingUtil codeSnippetParsingUtil = new CodeSnippetParsingUtil(false); ConstructorDeclaration constructorDeclaration = codeSnippetParsingUtil.parseStatements(snippet.toCharArray(), 0, snippet.length(), JDTOptions.getJDTOptions(), true, false); CompilationResult compilationResult = constructorDeclaration.compilationResult(); List<Problem> problems = new ArrayList<>(); if (compilationResult.hasErrors()) { for (CategorizedProblem problem : compilationResult.getErrors()) { Problem p = new Problem(problem.getMessage(), problem.getSourceStart(), problem.getSourceEnd(), problem.getSourceLineNumber()); problems.add(p); } } return problems; } }
@Override public String toUnformattedString() { Document documentLocal = new Document(this.document.get()); try { Map<String, String> options = JDTOptions.getJDTOptions(); TextEdit edit = unit.rewrite(documentLocal, options); edit.apply(documentLocal); } catch (Exception e) { throw new ParserException("Could not modify source: " + unit.toString(), e); } return documentLocal.get(); }
parser.setCompilerOptions(JDTOptions.getJDTOptions());