com.github.javaparser.printer.PrettyPrinterConfiguration Java Examples

The following examples show how to use com.github.javaparser.printer.PrettyPrinterConfiguration. 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: JavaMethodParser.java    From Siamese with GNU General Public License v3.0 6 votes vote down vote up
@Override
public void visit(MethodDeclaration n, Object arg) {
    ArrayList<crest.siamese.document.Parameter> paramsList = new ArrayList<>();
    // do not include comments in the indexed code
    PrettyPrinterConfiguration ppc = new PrettyPrinterConfiguration();
    ppc.setPrintComments(false);
    ppc.setPrintJavaDoc(false);
    // retrieve the comments separately
    String comment = retrieveComments(n);
    int begin = -1;
    int end = -1;
    if (n.getBegin().isPresent()) begin = n.getBegin().get().line;
    if (n.getEnd().isPresent()) end = n.getEnd().get().line;
    methodList.add(createNewMethod(n.getName().asString(), comment, n.toString(ppc), begin,
            end, paramsList, n.getDeclarationAsString()));
    super.visit(n, arg);
}
 
Example #3
Source File: JavaMethodParser.java    From Siamese with GNU General Public License v3.0 6 votes vote down vote up
@Override
public void visit(ConstructorDeclaration c, Object arg) {
    ArrayList<crest.siamese.document.Parameter> paramsList = new ArrayList<>();
    // do not include comments in the indexed code
    PrettyPrinterConfiguration ppc = new PrettyPrinterConfiguration();
    ppc.setPrintComments(false);
    ppc.setPrintJavaDoc(false);
    String comment = retrieveComments(c);
    int begin = -1;
    int end = -1;
    if (c.getBegin().isPresent()) begin = c.getBegin().get().line;
    if (c.getEnd().isPresent()) end = c.getEnd().get().line;
    methodList.add(createNewMethod(c.getName().asString(), comment, c.toString(ppc), begin,
            end, paramsList, c.getDeclarationAsString()));
    super.visit(c, arg);
}
 
Example #4
Source File: TestCodeVisitor.java    From CodeDefenders with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Override
public void visit(ExpressionStmt stmt, Void args) {
    if (!isValid) {
        return;
    }
    String stringStmt = stmt.toString( new PrettyPrinterConfiguration().setPrintComments(false));
    for (String prohibited : CodeValidator.PROHIBITED_CALLS) {
        // This might be a bit too strict... We shall use typeSolver otherwise.
        if (stringStmt.contains(prohibited)) {
            messages.add("Test contains a prohibited call to " + prohibited);
            isValid = false;
            return;
        }
    }
    stmtCount++;
    super.visit(stmt, args);
}
 
Example #5
Source File: CodeValidator.java    From CodeDefenders with GNU Lesser General Public License v3.0 5 votes vote down vote up
public static String getMD5FromText(String code) {
    // Parse the code and output the string without the comment.
    // This string should be already normalized
    try {
        return org.apache.commons.codec.digest.DigestUtils
                .md5Hex(getCompilationUnitFromText(code).toString( new PrettyPrinterConfiguration().setPrintComments(false)));
    } catch ( IOException | ParseException e) {
        // Ignore this
    }
    // if the code does not compile there's no point to try to remove the comments
    return org.apache.commons.codec.digest.DigestUtils.md5Hex(code);
}
 
Example #6
Source File: CodeValidator.java    From CodeDefenders with GNU Lesser General Public License v3.0 5 votes vote down vote up
private static Set<String> extractImportStatements(CompilationUnit cu) {
        final PrettyPrinterConfiguration p = new PrettyPrinterConfiguration().setPrintComments( false );
        Set<String> result = new HashSet<>();
        for( ImportDeclaration id : cu.getImports() ){
            result.add( id.toString( p ) );
        }
        return result;
        // I have no idea on how to use stream with map and paramenters
//        return cu.getImports()
//                .stream()
//                .map(ImportDeclaration::toString(p))
//                .collect(Collectors.toSet());
    }