private void writeInitializerElements(final AstNodeCollection<Expression> elements) { if (elements.isEmpty()) { writeToken(Roles.LEFT_BRACE); writeToken(Roles.RIGHT_BRACE); return; final BraceStyle style = wrapElements ? BraceStyle.NextLine : BraceStyle.BannerStyle; openBrace(style); if (isFirst) { if (style == BraceStyle.BannerStyle) { space(); comma(node, wrapElements); newLine(); optionalComma(); newLine(); space(); closeBrace(style);
@Override public Void visitPackageDeclaration(final PackageDeclaration node, final Void ignored) { startNode(node); writeKeyword(Roles.PACKAGE_KEYWORD); writeQualifiedIdentifier(node.getIdentifiers()); semicolon(); newLine(); for (int i = 0; i < policy.BlankLinesAfterPackageDeclaration; i++) { newLine(); } endNode(node); return null; }
@Override public Void visitMethodGroupExpression(final MethodGroupExpression node, final Void ignored) { startNode(node); node.getTarget().acceptVisitor(this, ignored); writeToken(MethodGroupExpression.DOUBLE_COLON_ROLE); if (isKeyword(node.getMethodName())) { writeKeyword(node.getMethodName()); } else { writeIdentifier(node.getMethodName()); } endNode(node); return null; }
@Override public Void visitPatternPlaceholder(final AstNode node, final Pattern pattern, final Void ignored) { startNode(node); visitNodeInPattern(pattern); endNode(node); return null; }
@Override public Void visitSwitchStatement(final SwitchStatement node, final Void ignored) { startNode(node); writeKeyword(SwitchStatement.SWITCH_KEYWORD_ROLE); space(policy.SpaceBeforeSwitchParentheses); leftParenthesis(); space(policy.SpacesWithinSwitchParentheses); node.getExpression().acceptVisitor(this, ignored); space(policy.SpacesWithinSwitchParentheses); rightParenthesis(); openBrace(policy.StatementBraceStyle); if (policy.IndentSwitchBody) { formatter.indent(); } for (final SwitchSection section : node.getSwitchSections()) { section.acceptVisitor(this, ignored); } if (policy.IndentSwitchBody) { formatter.unindent(); } closeBrace(policy.StatementBraceStyle); newLine(); endNode(node); return null; }
@Override public Void visitEnumValueDeclaration(final EnumValueDeclaration node, final Void ignored) { startNode(node); writeAnnotations(node.getAnnotations(), true); writeIdentifier(node.getName()); writeCommaSeparatedListInParenthesis(arguments, policy.SpaceWithinEnumDeclarationParentheses); final BraceStyle braceStyle = policy.AnonymousClassBraceStyle; openBrace(braceStyle); closeBrace(braceStyle); semicolon(); comma(node.getNextSibling()); endNode(node); return null;
@Override public Void visitTypeDeclaration(final TypeDeclaration node, final Void ignored) { startNode(node); writeAnnotations(node.getAnnotations(), true); writeModifiers(node.getModifiers()); writeKeyword(Roles.ENUM_KEYWORD); break; case INTERFACE: writeKeyword(Roles.INTERFACE_KEYWORD); break; case ANNOTATION: writeKeyword(Roles.ANNOTATION_KEYWORD); break; default: writeKeyword(Roles.CLASS_KEYWORD); break; writeTypeParameters(node.getTypeParameters()); space(); writeKeyword(Roles.EXTENDS_KEYWORD); space(); node.getBaseType().acceptVisitor(this, ignored); space(); writeKeyword(Roles.EXTENDS_KEYWORD);
@Override public Void visitAnonymousObjectCreationExpression(final AnonymousObjectCreationExpression node, final Void ignored) { startNode(node); final Expression target = node.getTarget(); if (target != null && !target.isNull()) { target.acceptVisitor(this, ignored); writeToken(Roles.DOT); } writeKeyword(ObjectCreationExpression.NEW_KEYWORD_ROLE); node.getType().acceptVisitor(this, ignored); space(policy.SpaceBeforeMethodCallParentheses); writeCommaSeparatedListInParenthesis(node.getArguments(), policy.SpaceWithinMethodCallParentheses); node.getTypeDeclaration().acceptVisitor(new JavaOutputVisitor(output, settings), ignored); endNode(node); return null; }
void writeMethodBody(final AstNodeCollection<TypeDeclaration> declaredTypes, final BlockStatement body) { if (body.isNull()) { semicolon(); return; startNode(body); openBrace(style); for (final TypeDeclaration declaredType : declaredTypes) { if (needNewLine) { newLine(); declaredType.acceptVisitor(new JavaOutputVisitor(output, settings), null); needNewLine = true; newLine(); closeBrace(style); newLine(); endNode(body);
private void visitMemberReferenceTypeNode(final MemberReferenceTypeNode node) { writeKeyword("memberReference"); writeToken(Roles.LEFT_BRACKET); writeIdentifier(node.getReferenceType().getSimpleName()); writeToken(Roles.RIGHT_BRACKET); leftParenthesis(); visitNodeInPattern(node.getTarget()); rightParenthesis(); }
@Override public Void visitLambdaExpression(final LambdaExpression node, final Void ignored) { startNode(node); if (lambdaNeedsParenthesis(node)) { writeCommaSeparatedListInParenthesis(node.getParameters(), policy.SpaceWithinMethodDeclarationParentheses); } else { node.getParameters().firstOrNullObject().acceptVisitor(this, ignored); } space(); writeToken(LambdaExpression.ARROW_ROLE); if (!(node.getBody() instanceof BlockStatement)) { space(); } node.getBody().acceptVisitor(this, ignored); endNode(node); return null; }
@Override public Void visitReturnStatement(final ReturnStatement node, final Void ignored) { startNode(node); writeKeyword(ReturnStatement.RETURN_KEYWORD_ROLE); if (!node.getExpression().isNull()) { space(); node.getExpression().acceptVisitor(this, null); } semicolon(); endNode(node); return null; }
@Override public Void visitGotoStatement(final GotoStatement node, final Void ignored) { startNode(node); writeKeyword(GotoStatement.GOTO_KEYWORD_ROLE); writeIdentifier(node.getLabel(), Roles.LABEL); semicolon(); endNode(node); return null; }
@Override public Void visitBlockStatement(final BlockStatement node, final Void ignored) { startNode(node); openBrace(style); closeBrace(style); newLine(); endNode(node);
private Void writeVariableDeclaration(final VariableDeclarationStatement node, final boolean semicolon) { startNode(node); writeModifiers(node.getChildrenByRole(VariableDeclarationStatement.MODIFIER_ROLE)); node.getType().acceptVisitor(this, null); space(); writeCommaSeparatedList(node.getVariables()); if (semicolon) { semicolon(); } endNode(node); return null; }
@Override public Void visitParenthesizedExpression(final ParenthesizedExpression node, final Void ignored) { startNode(node); leftParenthesis(); space(policy.SpacesWithinParentheses); node.getExpression().acceptVisitor(this, ignored); space(policy.SpacesWithinParentheses); rightParenthesis(); endNode(node); return null; }
private void visitOptionalNode(final OptionalNode optionalNode) { writeKeyword("optional"); leftParenthesis(); visitNodeInPattern(optionalNode.getNode()); rightParenthesis(); }
private void writeCommaSeparatedListInParenthesis( final Iterable<? extends AstNode> list, final boolean spaceWithin) { leftParenthesis(); if (any(list)) { space(spaceWithin); writeCommaSeparatedList(list); space(spaceWithin); } rightParenthesis(); }
@Override public Void visitPrimitiveExpression(final PrimitiveExpression node, final Void ignored) { node.setStartLocation(new TextLocation(output.getRow(), output.getColumn())); startNode(node); if (!StringUtilities.isNullOrEmpty(node.getLiteralValue())) { formatter.writeLiteral(node.getLiteralValue()); } else if (node.getValue() instanceof Number) { final long longValue = ((Number) node.getValue()).longValue(); if (longValue != -1L && isBitwiseContext(node.getParent(), node)) { formatter.writeLiteral( String.format( node.getValue() instanceof Long ? "0x%1$XL" : "0x%1$X", node.getValue() ) ); } else { writePrimitiveValue(node.getValue()); } } else { writePrimitiveValue(node.getValue()); } endNode(node); return null; }