Skip to content

Commit 7e72422

Browse files
cushongoogle-java-format Team
authored andcommitted
Merge Java21InputAstVisitor into JavaInputAstVisitor
In preparation for raising the minimum supported JDK version to JDK 21. PiperOrigin-RevId: 811349632
1 parent f4aaf0f commit 7e72422

File tree

5 files changed

+60
-167
lines changed

5 files changed

+60
-167
lines changed

core/pom.xml

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -223,31 +223,6 @@
223223
</build>
224224

225225
<profiles>
226-
<profile>
227-
<id>jdk17</id>
228-
<activation>
229-
<jdk>[17,21)</jdk>
230-
</activation>
231-
<build>
232-
<plugins>
233-
<plugin>
234-
<groupId>org.apache.maven.plugins</groupId>
235-
<artifactId>maven-compiler-plugin</artifactId>
236-
<configuration>
237-
<excludes>
238-
<exclude>**/Java21InputAstVisitor.java</exclude>
239-
</excludes>
240-
</configuration>
241-
</plugin>
242-
<plugin>
243-
<artifactId>maven-javadoc-plugin</artifactId>
244-
<configuration>
245-
<excludePackageNames>com.google.googlejavaformat.java.java21</excludePackageNames>
246-
</configuration>
247-
</plugin>
248-
</plugins>
249-
</build>
250-
</profile>
251226
<profile>
252227
<id>native</id>
253228
<build>

core/src/main/java/com/google/googlejavaformat/java/Formatter.java

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -150,14 +150,7 @@ public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOExcept
150150
}
151151
OpsBuilder builder = new OpsBuilder(javaInput, javaOutput);
152152
// Output the compilation unit.
153-
JavaInputAstVisitor visitor;
154-
if (Runtime.version().feature() >= 21) {
155-
visitor =
156-
createVisitor(
157-
"com.google.googlejavaformat.java.java21.Java21InputAstVisitor", builder, options);
158-
} else {
159-
visitor = new JavaInputAstVisitor(builder, options.indentationMultiplier());
160-
}
153+
JavaInputAstVisitor visitor = new JavaInputAstVisitor(builder, options.indentationMultiplier());
161154
visitor.scan(unit, null);
162155
builder.sync(javaInput.getText().length());
163156
builder.drain();
@@ -167,18 +160,6 @@ public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOExcept
167160
javaOutput.flush();
168161
}
169162

170-
private static JavaInputAstVisitor createVisitor(
171-
final String className, final OpsBuilder builder, final JavaFormatterOptions options) {
172-
try {
173-
return Class.forName(className)
174-
.asSubclass(JavaInputAstVisitor.class)
175-
.getConstructor(OpsBuilder.class, int.class)
176-
.newInstance(builder, options.indentationMultiplier());
177-
} catch (ReflectiveOperationException e) {
178-
throw new LinkageError(e.getMessage(), e);
179-
}
180-
}
181-
182163
static boolean errorDiagnostic(Diagnostic<?> input) {
183164
if (input.getKind() != Diagnostic.Kind.ERROR) {
184165
return false;

core/src/main/java/com/google/googlejavaformat/java/JavaInputAstVisitor.java

Lines changed: 59 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,10 @@
9797
import com.sun.source.tree.CompilationUnitTree;
9898
import com.sun.source.tree.CompoundAssignmentTree;
9999
import com.sun.source.tree.ConditionalExpressionTree;
100+
import com.sun.source.tree.ConstantCaseLabelTree;
100101
import com.sun.source.tree.ContinueTree;
102+
import com.sun.source.tree.DeconstructionPatternTree;
103+
import com.sun.source.tree.DefaultCaseLabelTree;
101104
import com.sun.source.tree.DirectiveTree;
102105
import com.sun.source.tree.DoWhileLoopTree;
103106
import com.sun.source.tree.EmptyStatementTree;
@@ -125,6 +128,8 @@
125128
import com.sun.source.tree.OpensTree;
126129
import com.sun.source.tree.ParameterizedTypeTree;
127130
import com.sun.source.tree.ParenthesizedTree;
131+
import com.sun.source.tree.PatternCaseLabelTree;
132+
import com.sun.source.tree.PatternTree;
128133
import com.sun.source.tree.PrimitiveTypeTree;
129134
import com.sun.source.tree.ProvidesTree;
130135
import com.sun.source.tree.RequiresTree;
@@ -359,6 +364,12 @@ private boolean inExpression() {
359364

360365
@Override
361366
public Void scan(Tree tree, Void unused) {
367+
// Pre-visit AST for preview features, since com.sun.source.tree.AnyPattern can't be
368+
// accessed directly without --enable-preview.
369+
if (tree instanceof JCTree.JCAnyPattern) {
370+
visitJcAnyPattern((JCTree.JCAnyPattern) tree);
371+
return null;
372+
}
362373
inExpression.addLast(tree instanceof ExpressionTree || inExpression.peekLast());
363374
int previous = builder.depth();
364375
try {
@@ -2001,7 +2012,7 @@ public Void visitCase(CaseTree node, Void unused) {
20012012
builder.close();
20022013
}
20032014

2004-
final ExpressionTree guard = getGuard(node);
2015+
final ExpressionTree guard = node.getGuard();
20052016
if (guard != null) {
20062017
builder.breakToFill(" ");
20072018
token("when");
@@ -2041,10 +2052,6 @@ public Void visitCase(CaseTree node, Void unused) {
20412052
return null;
20422053
}
20432054

2044-
protected ExpressionTree getGuard(final CaseTree node) {
2045-
return null;
2046-
}
2047-
20482055
@Override
20492056
public Void visitSwitch(SwitchTree node, Void unused) {
20502057
sync(node);
@@ -3775,7 +3782,11 @@ protected int declareOne(
37753782
}
37763783

37773784
protected void variableName(Name name) {
3778-
visit(name);
3785+
if (name.isEmpty()) {
3786+
token("_");
3787+
} else {
3788+
visit(name);
3789+
}
37793790
}
37803791

37813792
private void maybeAddDims(Deque<List<? extends AnnotationTree>> annotations) {
@@ -4153,4 +4164,46 @@ public Void visitSwitchExpression(SwitchExpressionTree node, Void aVoid) {
41534164
visitSwitch(node.getExpression(), node.getCases());
41544165
return null;
41554166
}
4167+
4168+
@Override
4169+
public Void visitDefaultCaseLabel(DefaultCaseLabelTree node, Void unused) {
4170+
token("default");
4171+
return null;
4172+
}
4173+
4174+
@Override
4175+
public Void visitPatternCaseLabel(PatternCaseLabelTree node, Void unused) {
4176+
scan(node.getPattern(), null);
4177+
return null;
4178+
}
4179+
4180+
@Override
4181+
public Void visitConstantCaseLabel(ConstantCaseLabelTree node, Void aVoid) {
4182+
scan(node.getConstantExpression(), null);
4183+
return null;
4184+
}
4185+
4186+
@Override
4187+
public Void visitDeconstructionPattern(DeconstructionPatternTree node, Void unused) {
4188+
scan(node.getDeconstructor(), null);
4189+
builder.open(plusFour);
4190+
token("(");
4191+
builder.breakOp();
4192+
boolean afterFirstToken = false;
4193+
for (PatternTree pattern : node.getNestedPatterns()) {
4194+
if (afterFirstToken) {
4195+
token(",");
4196+
builder.breakOp(" ");
4197+
}
4198+
afterFirstToken = true;
4199+
scan(pattern, null);
4200+
}
4201+
builder.close();
4202+
token(")");
4203+
return null;
4204+
}
4205+
4206+
private void visitJcAnyPattern(JCTree.JCAnyPattern unused) {
4207+
token("_");
4208+
}
41564209
}

core/src/main/java/com/google/googlejavaformat/java/java21/Java21InputAstVisitor.java

Lines changed: 0 additions & 107 deletions
This file was deleted.

core/src/main/resources/META-INF/native-image/reflect-config.json

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,5 @@
22
{
33
"name": "com.sun.tools.javac.parser.UnicodeReader",
44
"allDeclaredMethods": true
5-
},
6-
{
7-
"name": "com.google.googlejavaformat.java.java21.Java21InputAstVisitor",
8-
"methods": [
9-
{
10-
"name": "<init>",
11-
"parameterTypes": ["com.google.googlejavaformat.OpsBuilder", "int"]
12-
}
13-
]
145
}
156
]

0 commit comments

Comments
 (0)