/** * @param lineSeparator characters which will be printed as End of line. * By default there is System.getProperty("line.separator") */ public DefaultJavaPrettyPrinter setLineSeparator(String lineSeparator) { getPrinterHelper().setLineSeparator(lineSeparator); return this; }
/** * @return current line separator. By default there is CR LF, LF or CR depending on the Operation system * defined by System.getProperty("line.separator") */ public String getLineSeparator() { return getPrinterHelper().getLineSeparator(); }
@Override public Map<Integer, Integer> getLineNumberMapping() { return getPrinterHelper().getLineNumberMapping(); }
private <T> void visitCtType(CtType<T> type) { elementPrinterHelper.writeComment(type, CommentOffset.BEFORE); getPrinterHelper().mapLine(type, sourceCompilationUnit); if (type.isTopLevel()) { context.currentTopLevel = type; } elementPrinterHelper.visitCtNamedElement(type, sourceCompilationUnit); elementPrinterHelper.writeModifiers(type); }
protected void printTypes(List<CtType<?>> types) { for (CtType<?> t : types) { scan(t); if (!env.isPreserveLineNumbers()) { // saving lines and chars printer.writeln().writeln(); } else { getPrinterHelper().adjustEndPosition(t); } } this.writeFooter(types); }
/** * The generic scan method for an element. */ public DefaultJavaPrettyPrinter scan(CtElement e) { if (e != null) { enter(e); context.elementStack.push(e); if (env.isPreserveLineNumbers()) { if (!(e instanceof CtNamedElement)) { getPrinterHelper().adjustStartPosition(e); } } try { e.accept(this); } catch (SpoonException ex) { throw ex; } catch (Exception ex) { String elementInfo = e.getClass().getName(); elementInfo += " on path " + getPath(e) + "\n"; if (e.getPosition().isValidPosition()) { elementInfo += "at position " + e.getPosition().toString() + " "; } throw new SpoonException("Printing of " + elementInfo + "failed", ex); } context.elementStack.pop(); exit(e); } return this; }
/** * Enters an expression. */ protected void enterCtExpression(CtExpression<?> e) { if (!(e instanceof CtStatement)) { elementPrinterHelper.writeComment(e, CommentOffset.BEFORE); } getPrinterHelper().mapLine(e, sourceCompilationUnit); if (shouldSetBracket(e)) { context.parenthesedExpression.push(e); printer.writeSeparator("("); } if (!e.getTypeCasts().isEmpty()) { for (CtTypeReference<?> r : e.getTypeCasts()) { printer.writeSeparator("("); scan(r); printer.writeSeparator(")").writeSpace(); printer.writeSeparator("("); context.parenthesedExpression.push(e); } } }
@Override public <T> void visitCtCatchVariable(CtCatchVariable<T> catchVariable) { if (env.isPreserveLineNumbers()) { getPrinterHelper().adjustStartPosition(catchVariable); } elementPrinterHelper.writeModifiers(catchVariable); scan(catchVariable.getType()); printer.writeSpace(); printer.writeIdentifier(catchVariable.getSimpleName()); }
if (m.getBody().getPosition().getCompilationUnit() == sourceCompilationUnit) { if (m.getBody().getStatements().isEmpty() || !(m.getBody().getStatements().get(m.getBody().getStatements().size() - 1) instanceof CtReturn)) { getPrinterHelper().putLineNumberMapping(m.getBody().getPosition().getEndLine()); getPrinterHelper().undefineLine(); getPrinterHelper().undefineLine();
/** * Enters a statement. */ protected void enterCtStatement(CtStatement s) { elementPrinterHelper.writeComment(s, CommentOffset.BEFORE); getPrinterHelper().mapLine(s, sourceCompilationUnit); if (!context.isNextForVariable()) { //TODO AnnotationLoopTest#testAnnotationDeclaredInForInit expects that annotations of next for variables are not printed //but may be correct is that the next variables are not annotated, because they might have different annotation then first param! elementPrinterHelper.writeAnnotations(s); } if (!context.isFirstForVariable() && !context.isNextForVariable()) { if (s.getLabel() != null) { printer.writeIdentifier(s.getLabel()).writeSpace().writeSeparator(":").writeSpace(); } } }
@Override public <T> void visitCtClass(CtClass<T> ctClass) { context.pushCurrentThis(ctClass); if (ctClass.getSimpleName() != null && !CtType.NAME_UNKNOWN.equals(ctClass.getSimpleName()) && !ctClass.isAnonymous()) { visitCtType(ctClass); if (ctClass.isLocalType()) { printer.writeKeyword("class").writeSpace().writeIdentifier(ctClass.getSimpleName().replaceAll("^[0-9]*", "")); } else { printer.writeKeyword("class").writeSpace().writeIdentifier(ctClass.getSimpleName()); } elementPrinterHelper.writeFormalTypeParameters(ctClass); elementPrinterHelper.writeExtendsClause(ctClass); elementPrinterHelper.writeImplementsClause(ctClass); } printer.writeSpace().writeSeparator("{").incTab(); elementPrinterHelper.writeElementList(ctClass.getTypeMembers()); getPrinterHelper().adjustEndPosition(ctClass); printer.decTab().writeSeparator("}"); context.popCurrentThis(); }
@Override public <R> void visitCtBlock(CtBlock<R> block) { enterCtStatement(block); if (!block.isImplicit()) { printer.writeSeparator("{"); } printer.incTab(); for (CtStatement statement : block.getStatements()) { if (!statement.isImplicit()) { printer.writeln(); elementPrinterHelper.writeStatement(statement); } } printer.decTab(); getPrinterHelper().adjustEndPosition(block); if (env.isPreserveLineNumbers()) { if (!block.isImplicit()) { printer.writeSeparator("}"); } } else { printer.writeln(); if (!block.isImplicit()) { printer.writeSeparator("}"); } } exitCtStatement(block); }
@Override public <T> void visitCtLocalVariable(CtLocalVariable<T> localVariable) { enterCtStatement(localVariable); if (env.isPreserveLineNumbers()) { getPrinterHelper().adjustStartPosition(localVariable); } if (!context.isNextForVariable()) { elementPrinterHelper.writeModifiers(localVariable); if (localVariable.isInferred() && this.env.getComplianceLevel() >= 10) { getPrinterTokenWriter().writeKeyword("var"); } else { scan(localVariable.getType()); } printer.writeSpace(); } printer.writeIdentifier(localVariable.getSimpleName()); if (localVariable.getDefaultExpression() != null) { printer.writeSpace().writeOperator("=").writeSpace(); scan(localVariable.getDefaultExpression()); } exitCtStatement(localVariable); }
getPrinterHelper().adjustStartPosition(invocation);