com.github.javaparser.printer.PrettyPrinter Java Examples

The following examples show how to use com.github.javaparser.printer.PrettyPrinter. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: SourceProjectImpl.java    From chrome-devtools-java-client with Apache License 2.0 6 votes vote down vote up
@Override
public void saveAll() {
  PrettyPrinterConfiguration prettyPrinterConfiguration = new PrettyPrinterConfiguration();
  prettyPrinterConfiguration.setIndent("\t");
  prettyPrinterConfiguration.setPrintComments(true);
  prettyPrinterConfiguration.setPrintJavadoc(true);
  prettyPrinterConfiguration.setOrderImports(true);

  PrettyPrinter prettyPrinter = new PrettyPrinter(prettyPrinterConfiguration);

  JavaFormatterOptions javaFormatterOptions =
      JavaFormatterOptions.builder().style(JavaFormatterOptions.Style.GOOGLE).build();

  sourceRoot.setPrinter(googleCodeFormatter(javaFormatterOptions, prettyPrinter::print));
  sourceRoot.saveAll(sourceRoot.getRoot());
}
 
Example #2
Source File: BaseModelTest.java    From jeddict with Apache License 2.0 4 votes vote down vote up
private void testClass(JavaClass javaClass, ClassGenerator generator, EntityMappings entityMappings, String fileName) throws Exception {
        PrettyPrinter prettyPrinter = new PrettyPrinter();

        String existingSource;
        CompilationUnit existingUnit;

        String newSource = null;
        CompilationUnit newUnit;

        try (InputStream existingSourceStream
                = this.getClass().getResourceAsStream(javaClass.getClazz() + JAVA_EXT_SUFFIX)) {
            existingSource = readString(existingSourceStream);
            existingUnit = new JavaParser().parse(existingSource).getResult().get();
            assertNotNull(existingUnit);
            existingSource = prettyPrinter.print(existingUnit);
        }

        JavaClassSyncHandler
                .getInstance(javaClass)
                .syncExistingSnippet(existingUnit);

        if (fileName == null) {
            fileName = wrap("Reverse Engineering", FG_DARK_MAGENTA);
        } else {
            fileName = wrap(fileName, FG_DARK_BLUE);
        }

        try {
            ClassDefSnippet classDef = generator.getClassDef();
            classDef.setJaxbSupport(entityMappings.getJaxbSupport());
            newSource = classDef.getSnippet();
            assertNotNull(newSource);
            newUnit = new JavaParser().parse(newSource).getResult().get();
            assertNotNull(newUnit);
            newSource = prettyPrinter.print(newUnit);

//            System.out.println("newSource " + newSource);
            try (BufferedReader existingSourceReader = new BufferedReader(new StringReader(existingSource));
                    BufferedReader newSourceReader = new BufferedReader(new StringReader(newSource));) {

                String existingSourceLine;
                String newSourceLine;
                int lineNumber = 0;
                while ((existingSourceLine = existingSourceReader.readLine()) != null && (newSourceLine = newSourceReader.readLine()) != null) {
                    ++lineNumber;
                    assertEquals(existingSourceLine, newSourceLine,
                            '\n'
                            + wrap("Failed : " + javaClass.getFQN() + " [" + fileName + "]", FG_DARK_RED, BOLD)
                            + '\n'
                            + wrap("Line number : " + lineNumber, FG_RED, BOLD)
                            + '\n'
                            + wrap("existingSourceLine : " + existingSourceLine, FG_DARK_RED)
                            + '\n'
                            + wrap("newSourceLine : " + newSourceLine, FG_DARK_RED)
                            + '\n'
                            + newSource
                    );
                }
            }

            System.out.println(wrap("Passed : ", FG_DARK_GREEN, BOLD)
                    + wrap(javaClass.getFQN(), FG_DARK_CYAN)
                    + " [" + fileName + "]"
            );
        } catch (ParseProblemException ex) {
            fail(wrap(
                    "Compilation Failed : "
                    + javaClass.getFQN() + " [" + fileName + "]"
                    + '\n'
                    + "---------------------"
                    + '\n'
                    + newSource
                    + '\n'
                    + "---------------------"
                    + ex.getMessage()
                    + "---------------------",
                    FG_RED
            ));
        }
    }