Skip to content

Commit 4ed1716

Browse files
cushonabashev
authored andcommitted
Accept an annotated C-style array dimension on a parameter
A method parameter declared as "int a @anno []" crashed the formatter with a VerifyException from visitAnnotatedArrayType: the parameter's type was printed as a whole, dimensions included, before the name, and the annotated dimension that belongs after the name had nowhere to go. Fields and locals were fine, because their path already splits the dimensions off the type; parameters, resources and catch parameters go through visitToDeclare, which did not. This ports the fix for google/google-java-format#374, upstream commit ed40e472 by cushon, together with the null check that google/google-java-format#463 (upstream commit e19b7637) added next to it: a "var" declaration or an untyped lambda parameter has no type tree to split. The golden I374 comes from upstream, with the expected output in this project's style. The 15,747 files of the JDK 21 sources format exactly as before.
1 parent 746fb60 commit 4ed1716

3 files changed

Lines changed: 31 additions & 2 deletions

File tree

‎open-java-format/src/main/java/com/palantir/javaformat/java/JavaInputAstVisitor.java‎

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2669,18 +2669,29 @@ private void visitToDeclare(
26692669
Optional<String> trailing,
26702670
Optional<BreakTag> annotationBreakForRecords) {
26712671
sync(node);
2672+
Optional<TypeWithDims> typeWithDims;
2673+
Tree type;
2674+
if (node.getType() != null) {
2675+
TypeWithDims extractedDims = DimensionHelpers.extractDims(node.getType(), SortedDims.YES);
2676+
typeWithDims = Optional.of(extractedDims);
2677+
type = extractedDims.node;
2678+
} else {
2679+
// "var" and an untyped lambda parameter have no type tree
2680+
typeWithDims = Optional.empty();
2681+
type = null;
2682+
}
26722683
declareOne(
26732684
kind,
26742685
annotationsDirection,
26752686
Optional.of(node.getModifiers()),
2676-
node.getType(),
2687+
type,
26772688
node.getName(),
26782689
"",
26792690
equals,
26802691
initializer,
26812692
trailing,
26822693
/* receiverExpression= */ Optional.empty(),
2683-
/* typeWithDims= */ Optional.empty(),
2694+
typeWithDims,
26842695
annotationBreakForRecords);
26852696
}
26862697

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import java.lang.annotation.ElementType;
2+
import java.lang.annotation.Target;
3+
4+
@Target({ElementType.TYPE_USE, ElementType.TYPE_PARAMETER})
5+
@interface MyTypeAnno {}
6+
7+
public class GjfFailure {
8+
void m(int a @MyTypeAnno []) {}
9+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import java.lang.annotation.ElementType;
2+
import java.lang.annotation.Target;
3+
4+
@Target({ElementType.TYPE_USE, ElementType.TYPE_PARAMETER})
5+
@interface MyTypeAnno {}
6+
7+
public class GjfFailure {
8+
void m(int a @MyTypeAnno []) {}
9+
}

0 commit comments

Comments
 (0)