use of com.github.javaparser.printer.DefaultPrettyPrinter in project checker-framework by typetools.
the class WholeProgramInferenceJavaParserStorage method writeResultsToFile.
@Override
public void writeResultsToFile(OutputFormat outputFormat, BaseTypeChecker checker) {
if (outputFormat != OutputFormat.AJAVA) {
throw new BugInCF("WholeProgramInferenceJavaParser used with format " + outputFormat);
}
File outputDir = new File(AJAVA_FILES_PATH);
if (!outputDir.exists()) {
outputDir.mkdirs();
}
for (String path : modifiedFiles) {
CompilationUnitAnnos root = sourceToAnnos.get(path);
prepareCompilationUnitForWriting(root);
root.transferAnnotations(checker);
String packageDir = AJAVA_FILES_PATH;
if (root.compilationUnit.getPackageDeclaration().isPresent()) {
packageDir += File.separator + root.compilationUnit.getPackageDeclaration().get().getNameAsString().replaceAll("\\.", File.separator);
}
File packageDirFile = new File(packageDir);
if (!packageDirFile.exists()) {
packageDirFile.mkdirs();
}
String name = new File(path).getName();
if (name.endsWith(".java")) {
name = name.substring(0, name.length() - ".java".length());
}
name += "-" + checker.getClass().getCanonicalName() + ".ajava";
String outputPath = packageDir + File.separator + name;
try {
FileWriter writer = new FileWriter(outputPath);
// JavaParser can output using lexical preserving printing, which writes the file such that
// its formatting is close to the original source file it was parsed from as
// possible. Currently, this feature is very buggy and crashes when adding annotations in
// certain locations. This implementation could be used instead if it's fixed in JavaParser.
// LexicalPreservingPrinter.print(root.declaration, writer);
DefaultPrettyPrinter prettyPrinter = new DefaultPrettyPrinter();
writer.write(prettyPrinter.print(root.compilationUnit));
writer.close();
} catch (IOException e) {
throw new BugInCF("Error while writing ajava file " + outputPath, e);
}
}
modifiedFiles.clear();
}
use of com.github.javaparser.printer.DefaultPrettyPrinter in project drools by kiegroup.
the class JavaParserCompiler method createPrettyPrinter.
private static DefaultPrettyPrinter createPrettyPrinter() {
DefaultPrinterConfiguration config = new DefaultPrinterConfiguration();
config.addOption(new DefaultConfigurationOption(DefaultPrinterConfiguration.ConfigOption.COLUMN_ALIGN_PARAMETERS, true));
config.addOption(new DefaultConfigurationOption(DefaultPrinterConfiguration.ConfigOption.COLUMN_ALIGN_FIRST_METHOD_CHAIN, true));
return new DefaultPrettyPrinter(config);
}
use of com.github.javaparser.printer.DefaultPrettyPrinter in project checker-framework by typetools.
the class BaseTypeVisitor method testAnnotationInsertion.
/**
* Tests {@link org.checkerframework.framework.ajava.InsertAjavaAnnotations} if the checker has
* the "ajavaChecks" option.
*
* <ol>
* <li>Parses the current file with JavaParser.
* <li>Removes all annotations.
* <li>Reinserts the annotations.
* <li>Throws an exception if the ASTs are not the same.
* </ol>
*
* <p>Subclasses may override this method to disable the test even if the option is provided.
*/
protected void testAnnotationInsertion() {
if (root == null || !checker.hasOption("ajavaChecks")) {
return;
}
CompilationUnit originalAst;
try (InputStream originalInputStream = root.getSourceFile().openInputStream()) {
originalAst = JavaParserUtil.parseCompilationUnit(originalInputStream);
} catch (IOException e) {
throw new BugInCF("Error while reading Java file: " + root.getSourceFile().toUri(), e);
}
CompilationUnit astWithoutAnnotations = originalAst.clone();
JavaParserUtil.clearAnnotations(astWithoutAnnotations);
String withoutAnnotations = new DefaultPrettyPrinter().print(astWithoutAnnotations);
String withAnnotations;
try (InputStream annotationInputStream = root.getSourceFile().openInputStream()) {
// This check only runs on files from the Checker Framework test suite, which should all use
// UNIX line separators. Using System.lineSeparator instead of "\n" could cause the test to
// fail on Mac or Windows.
withAnnotations = new InsertAjavaAnnotations(elements).insertAnnotations(annotationInputStream, withoutAnnotations, "\n");
} catch (IOException e) {
throw new BugInCF("Error while reading Java file: " + root.getSourceFile().toUri(), e);
}
CompilationUnit modifiedAst = null;
try {
modifiedAst = JavaParserUtil.parseCompilationUnit(withAnnotations);
} catch (ParseProblemException e) {
throw new BugInCF("Failed to parse annotation insertion:\n" + withAnnotations, e);
}
AnnotationEqualityVisitor visitor = new AnnotationEqualityVisitor();
originalAst.accept(visitor, modifiedAst);
if (!visitor.getAnnotationsMatch()) {
throw new BugInCF(String.join(System.lineSeparator(), "Sanity check of erasing then reinserting annotations produced a different AST.", "File: " + root.getSourceFile(), "Original node: " + visitor.getMismatchedNode1(), "Node with annotations re-inserted: " + visitor.getMismatchedNode2(), "Original annotations: " + visitor.getMismatchedNode1().getAnnotations(), "Re-inserted annotations: " + visitor.getMismatchedNode2().getAnnotations(), "Original AST:", originalAst.toString(), "Ast with annotations re-inserted: " + modifiedAst));
}
}
Aggregations