Skip to content

Commit

Permalink
Remove Java17InputAstVisitor
Browse files Browse the repository at this point in the history
JDk 17 is now the lowest supported version.

#1159

PiperOrigin-RevId: 697756606
  • Loading branch information
cushon authored and google-java-format Team committed Nov 18, 2024
1 parent 7e5fb80 commit 3356bd3
Show file tree
Hide file tree
Showing 5 changed files with 186 additions and 323 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -155,10 +155,6 @@ public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOExcept
visitor =
createVisitor(
"com.google.googlejavaformat.java.java21.Java21InputAstVisitor", builder, options);
} else if (Runtime.version().feature() >= 17) {
visitor =
createVisitor(
"com.google.googlejavaformat.java.java17.Java17InputAstVisitor", builder, options);
} else {
visitor = new JavaInputAstVisitor(builder, options.indentationMultiplier());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

package com.google.googlejavaformat.java;

import static com.google.common.collect.ImmutableList.toImmutableList;
import static com.google.common.collect.Iterables.getLast;
import static com.google.common.collect.Iterables.getOnlyElement;
import static com.google.googlejavaformat.Doc.FillMode.INDEPENDENT;
Expand Down Expand Up @@ -84,8 +85,10 @@
import com.sun.source.tree.AssertTree;
import com.sun.source.tree.AssignmentTree;
import com.sun.source.tree.BinaryTree;
import com.sun.source.tree.BindingPatternTree;
import com.sun.source.tree.BlockTree;
import com.sun.source.tree.BreakTree;
import com.sun.source.tree.CaseLabelTree;
import com.sun.source.tree.CaseTree;
import com.sun.source.tree.CatchTree;
import com.sun.source.tree.ClassTree;
Expand Down Expand Up @@ -125,6 +128,7 @@
import com.sun.source.tree.RequiresTree;
import com.sun.source.tree.ReturnTree;
import com.sun.source.tree.StatementTree;
import com.sun.source.tree.SwitchExpressionTree;
import com.sun.source.tree.SwitchTree;
import com.sun.source.tree.SynchronizedTree;
import com.sun.source.tree.ThrowTree;
Expand All @@ -138,11 +142,13 @@
import com.sun.source.tree.VariableTree;
import com.sun.source.tree.WhileLoopTree;
import com.sun.source.tree.WildcardTree;
import com.sun.source.tree.YieldTree;
import com.sun.source.util.TreePath;
import com.sun.source.util.TreePathScanner;
import com.sun.tools.javac.code.Flags;
import com.sun.tools.javac.tree.JCTree;
import com.sun.tools.javac.tree.JCTree.JCMethodDecl;
import com.sun.tools.javac.tree.TreeInfo;
import com.sun.tools.javac.tree.TreeScanner;
import java.util.ArrayDeque;
import java.util.ArrayList;
Expand Down Expand Up @@ -410,7 +416,17 @@ public Void visitCompilationUnit(CompilationUnitTree node, Void unused) {
return null;
}

protected void handleModule(boolean afterFirstToken, CompilationUnitTree node) {}
protected void handleModule(boolean afterFirstToken, CompilationUnitTree node) {
ModuleTree module = node.getModule();
if (module != null) {
if (afterFirstToken) {
builder.blankLineWanted(YES);
}
markForPartialFormat();
visitModule(module, null);
builder.forcedBreak();
}
}

/** Skips over extra semi-colons at the top-level, or in a class member declaration lists. */
protected void dropEmptyDeclarations() {
Expand All @@ -436,6 +452,9 @@ public Void visitClass(ClassTree tree, Void unused) {
case ENUM:
visitEnumDeclaration(tree);
break;
case RECORD:
visitRecordDeclaration(tree);
break;
default:
throw new AssertionError(tree.getKind());
}
Expand Down Expand Up @@ -928,6 +947,69 @@ public boolean visitEnumDeclaration(ClassTree node) {
return false;
}

public void visitRecordDeclaration(ClassTree node) {
sync(node);
typeDeclarationModifiers(node.getModifiers());
Verify.verify(node.getExtendsClause() == null);
boolean hasSuperInterfaceTypes = !node.getImplementsClause().isEmpty();
token("record");
builder.space();
visit(node.getSimpleName());
if (!node.getTypeParameters().isEmpty()) {
token("<");
}
builder.open(plusFour);
{
if (!node.getTypeParameters().isEmpty()) {
typeParametersRest(node.getTypeParameters(), hasSuperInterfaceTypes ? plusFour : ZERO);
}
ImmutableList<JCTree.JCVariableDecl> parameters = JavaInputAstVisitor.recordVariables(node);
token("(");
if (!parameters.isEmpty()) {
// Break before args.
builder.breakToFill("");
}
// record headers can't declare receiver parameters
visitFormals(/* receiver= */ Optional.empty(), parameters);
token(")");
if (hasSuperInterfaceTypes) {
builder.breakToFill(" ");
builder.open(node.getImplementsClause().size() > 1 ? plusFour : ZERO);
token("implements");
builder.space();
boolean afterFirstToken = false;
for (Tree superInterfaceType : node.getImplementsClause()) {
if (afterFirstToken) {
token(",");
builder.breakOp(" ");
}
scan(superInterfaceType, null);
afterFirstToken = true;
}
builder.close();
}
}
builder.close();
if (node.getMembers() == null) {
token(";");
} else {
ImmutableList<Tree> members =
node.getMembers().stream()
.filter(t -> (TreeInfo.flags((JCTree) t) & Flags.GENERATED_MEMBER) == 0)
.collect(toImmutableList());
addBodyDeclarations(members, BracesOrNot.YES, FirstDeclarationsOrNot.YES);
}
dropEmptyDeclarations();
}

private static ImmutableList<JCTree.JCVariableDecl> recordVariables(ClassTree node) {
return node.getMembers().stream()
.filter(JCTree.JCVariableDecl.class::isInstance)
.map(JCTree.JCVariableDecl.class::cast)
.filter(m -> (m.mods.flags & RECORD) == RECORD)
.collect(toImmutableList());
}

@Override
public Void visitMemberReference(MemberReferenceTree node, Void unused) {
builder.open(plusFour);
Expand Down Expand Up @@ -1199,7 +1281,11 @@ public Void visitInstanceOf(InstanceOfTree node, Void unused) {
builder.open(ZERO);
token("instanceof");
builder.breakOp(" ");
scan(node.getType(), null);
if (node.getPattern() != null) {
scan(node.getPattern(), null);
} else {
scan(node.getType(), null);
}
builder.close();
builder.close();
return null;
Expand Down Expand Up @@ -1874,18 +1960,69 @@ public Void visitCase(CaseTree node, Void unused) {
sync(node);
markForPartialFormat();
builder.forcedBreak();
if (node.getExpression() == null) {
List<? extends CaseLabelTree> labels = node.getLabels();
boolean isDefault =
labels.size() == 1 && getOnlyElement(labels).getKind().name().equals("DEFAULT_CASE_LABEL");
builder.open(node.getCaseKind().equals(CaseTree.CaseKind.RULE) ? plusFour : ZERO);
if (isDefault) {
token("default", ZERO);
token(":");
} else {
token("case", ZERO);
builder.open(ZERO);
builder.space();
scan(node.getExpression(), null);
token(":");
boolean afterFirstToken = false;
for (Tree expression : labels) {
if (afterFirstToken) {
token(",");
builder.breakOp(" ");
}
scan(expression, null);
afterFirstToken = true;
}
builder.close();
}

final ExpressionTree guard = getGuard(node);
if (guard != null) {
builder.breakToFill(" ");
token("when");
builder.space();
scan(guard, null);
}

switch (node.getCaseKind()) {
case STATEMENT:
token(":");
builder.open(plusTwo);
visitStatements(node.getStatements());
builder.close();
builder.close();
break;
case RULE:
builder.space();
token("-");
token(">");
if (node.getBody().getKind() == BLOCK) {
builder.close();
builder.space();
// Explicit call with {@link CollapseEmptyOrNot.YES} to handle empty case blocks.
visitBlock(
(BlockTree) node.getBody(),
CollapseEmptyOrNot.YES,
AllowLeadingBlankLine.NO,
AllowTrailingBlankLine.NO);
} else {
builder.breakOp(" ");
scan(node.getBody(), null);
builder.close();
}
builder.guessToken(";");
break;
}
builder.open(plusTwo);
visitStatements(node.getStatements());
builder.close();
return null;
}

protected ExpressionTree getGuard(final CaseTree node) {
return null;
}

Expand Down Expand Up @@ -2022,7 +2159,7 @@ public Void visitTry(TryTree node, Void unused) {
public void visitClassDeclaration(ClassTree node) {
sync(node);
typeDeclarationModifiers(node.getModifiers());
List<? extends Tree> permitsTypes = getPermitsClause(node);
List<? extends Tree> permitsTypes = node.getPermitsClause();
boolean hasSuperclassType = node.getExtendsClause() != null;
boolean hasSuperInterfaceTypes = !node.getImplementsClause().isEmpty();
boolean hasPermitsTypes = !permitsTypes.isEmpty();
Expand Down Expand Up @@ -3800,11 +3937,6 @@ protected void addBodyDeclarations(
}
}

/** Gets the permits clause for the given node. This is only available in Java 15 and later. */
protected List<? extends Tree> getPermitsClause(ClassTree node) {
return ImmutableList.of();
}

private void classDeclarationTypeList(String token, List<? extends Tree> types) {
if (types.isEmpty()) {
return;
Expand Down Expand Up @@ -3966,4 +4098,40 @@ final BreakTag genSym() {
public final String toString() {
return MoreObjects.toStringHelper(this).add("builder", builder).toString();
}

@Override
public Void visitBindingPattern(BindingPatternTree node, Void unused) {
sync(node);
VariableTree variableTree = node.getVariable();
declareOne(
DeclarationKind.PARAMETER,
Direction.HORIZONTAL,
Optional.of(variableTree.getModifiers()),
variableTree.getType(),
variableTree.getName(),
/* op= */ "",
/* equals= */ "",
/* initializer= */ Optional.empty(),
/* trailing= */ Optional.empty(),
/* receiverExpression= */ Optional.empty(),
/* typeWithDims= */ Optional.empty());
return null;
}

@Override
public Void visitYield(YieldTree node, Void aVoid) {
sync(node);
token("yield");
builder.space();
scan(node.getValue(), null);
token(";");
return null;
}

@Override
public Void visitSwitchExpression(SwitchExpressionTree node, Void aVoid) {
sync(node);
visitSwitch(node.getExpression(), node.getCases());
return null;
}
}
Loading

0 comments on commit 3356bd3

Please sign in to comment.