Skip to content

Commit 3878bca

Browse files
committed
fix crash
1 parent 6bd140c commit 3878bca

File tree

4 files changed

+288
-28
lines changed

4 files changed

+288
-28
lines changed

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

Lines changed: 31 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,12 @@ public class JavaInputAstVisitor extends TreePathScanner<Void, Void> {
157157
*/
158158
private static final int METHOD_CHAIN_COLUMN_LIMIT = 80;
159159

160+
/**
161+
* Maximum column at which the annotated parameter of a record should start. This exists in particular to improve
162+
* readability when one or multiple annotations are added to the parameters of a record.
163+
*/
164+
private static final int ANNOTATION_RECORD_PARAMETER_COLUMN_LIMIT = 60;
165+
160166
/** Direction for Annotations (usually VERTICAL). */
161167
protected enum Direction {
162168
VERTICAL,
@@ -2245,30 +2251,24 @@ private List<Op> visitModifiers(
22452251
? forceBreakList(declarationAnnotationBreak)
22462252
: breakList(declarationAnnotationBreak));
22472253
}
2248-
AnnotationTree annotation = annotations.removeFirst();
2249-
if (!annotation.getArguments().isEmpty()) {
2250-
// ExpressionTree firstArg = annotation.getArguments().get(0);
2251-
// If it's an AssignmentTree, it's an explicit argument (name = value)
2252-
hasExplicitParameterizedAnnotation = true;
2253-
}
2254-
scan(annotation, null);
2254+
scan(annotations.removeFirst(), null);
22552255
first = false;
22562256
lastWasAnnotation = true;
22572257
}
22582258
builder.close();
22592259

22602260
// pjf specific: enforce breaking operatins for record parameters with annotations with explicit parameters
2261-
ImmutableList<Op> trailingBreak = annotationsDirection.isVertical() || isRecordParameter
2262-
? (isRecordParameter
2263-
? ImmutableList.of(Break.builder()
2264-
.fillMode(UNIFIED)
2265-
.flat(" ")
2266-
.plusIndent(ZERO)
2267-
.hasColumnLimit(true)
2268-
.optTag(declarationAnnotationBreak)
2269-
.build())
2270-
: forceBreakList(declarationAnnotationBreak))
2271-
: breakList(declarationAnnotationBreak);
2261+
ImmutableList<Op> trailingBreak = isRecordParameter
2262+
? ImmutableList.of(Break.builder()
2263+
.fillMode(FillMode.UNIFIED)
2264+
.flat(" ")
2265+
.plusIndent(ZERO)
2266+
.hasColumnLimit(true)
2267+
.optTag(declarationAnnotationBreak)
2268+
.build())
2269+
: annotationsDirection.isVertical()
2270+
? forceBreakList(declarationAnnotationBreak)
2271+
: breakList(declarationAnnotationBreak);
22722272
if (annotations.isEmpty() && !nextIsModifier()) {
22732273
return trailingBreak;
22742274
}
@@ -3505,16 +3505,20 @@ int declareOne(
35053505
int baseDims = 0;
35063506

35073507
if (kind == DeclarationKind.PARAMETER
3508-
&& (modifiers.isPresent()
3509-
&& !modifiers.get().getAnnotations().isEmpty()
3510-
&& !isInRecord(modifiers.get()))) {
3511-
builder.open(plusFour);
3508+
&& modifiers.isPresent()
3509+
&& !modifiers.get().getAnnotations().isEmpty()) {
3510+
if (!isInRecord(modifiers.get())) {
3511+
builder.open(plusFour);
3512+
} else {
3513+
// pjf specific: we are enforcing the column limit for annotated parameters of records
3514+
builder.open(OpenOp.builder()
3515+
.debugName("visitParam")
3516+
.plusIndent(ZERO)
3517+
.columnLimitBeforeLastBreak(ANNOTATION_RECORD_PARAMETER_COLUMN_LIMIT)
3518+
.build());
3519+
}
35123520
} else {
3513-
builder.open(OpenOp.builder()
3514-
.debugName("visitParam")
3515-
.plusIndent(ZERO)
3516-
.columnLimitBeforeLastBreak(60)
3517-
.build());
3521+
builder.open(ZERO);
35183522
}
35193523
{
35203524
if (modifiers.isPresent()) {

palantir-java-format/src/test/java/com/palantir/javaformat/java/FormatterIntegrationTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ public FormatterIntegrationTest(String name, String input, String expected) {
6464
* <p>Warning: don't turn this on for all tests. The debugger will always write to the same file.
6565
*/
6666
private static boolean isDebugMode() {
67-
return true; // Boolean.getBoolean("debugOutput");
67+
return Boolean.getBoolean("debugOutput");
6868
}
6969

7070
@TestTemplate
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
@Schema(description = "Type of quote being requested", example = "NEW_BUSINESS", somethingElse= "new", other="my other long string")
2+
public record QuoteRequest(
3+
@Schema(description = "US state of the product being quoted", example = "TX")
4+
RegulatoryState regulatoryState,
5+
@Schema(description = "Reason for a quote", example = "New Business") String amendmentReason,
6+
@Schema(description = "Type of quote being requested", example = "NEW_BUSINESS", somethingElse= "new", other="my other long string") QuoteType quoteType,
7+
@Schema(description = "Date the quoted changes become active", example = "2023-06-25")
8+
LocalDate quoteEffectiveDate
9+
) {}
10+
11+
public record QuoteRequest(
12+
13+
int value,
14+
15+
@SomeInput RegulatoryState regulatoryState,
16+
17+
@SomeInput
18+
@NotNull
19+
@Deprecated
20+
@JsonValue(name = "something") @Schema(description = "US state of the product being quoted", example = "TX") RegulatoryState regulatoryState,
21+
@Schema(description = "Reason for a quote", example = "New Business") String amendmentReason,
22+
int x,
23+
int j) {}
24+
25+
public record Url(
26+
int var1,
27+
@JsonValue("value") String var2,
28+
@SomeInput @JsonValue String var3,
29+
int var3,
30+
int var4,
31+
int var5,
32+
int var6) {}
33+
34+
public record Url(
35+
@NotNull
36+
@JsonValue
37+
@Deprecated
38+
@Annotation1
39+
@Annotation2
40+
@Annotation3
41+
@Annotation4
42+
@Annotation5
43+
@Annotation6
44+
@Annotation7
45+
@Annotation8
46+
@Annotation9
47+
@Annotation10
48+
@Annotation11
49+
@Annotation12
50+
String value) {}
51+
52+
public record Url(@NotNull String value, int number) {}
53+
54+
public record Url(
55+
int number,
56+
@JsonValue("value") String value) {}
57+
58+
public record Url(@New @JsonValue("value") String value) {}
59+
60+
@JsonIgnoreProperties(ignoreUnknown = true)
61+
@JsonInclude(JsonInclude.Include.NON_NULL)
62+
public record Game(
63+
@JsonProperty("accounting_group") @JsonPropertyDescription("The accounting group of the game.")
64+
String accountingGroup,
65+
@JsonProperty("accumulating")
66+
@JsonPropertyDescription("Marks which games with accumulating bonuses.") Boolean accumulating,
67+
@JsonProperty("bonus_buy") @JsonPropertyDescription("Games with purchasable bonuses.") Boolean bonusBuy,
68+
@JsonProperty("category")
69+
@JsonPropertyDescription(
70+
"Game's category. Allowed values: slots, roulette, card, "
71+
+ "casual, lottery, poker, craps, video_poker")
72+
String category,
73+
@JsonProperty("hd") @JsonPropertyDescription("HD format games.") Boolean hd,
74+
75+
@JsonProperty("new") @JsonPropertyDescription("New format games.") Boolean assa,
76+
@JsonProperty("hit_rate") @JsonPropertyDescription(
77+
"Frequency of wins per 100 bets. The higher, the hit rate the lower the volatility "
78+
+ "rating, and vice versa. Positive value. For slots only. "
79+
+ "Match pattern: ^\\d{1,18}(\\.\\d{1,12})?$") String hitRate,
80+
@JsonProperty("params") @JsonPropertyDescription("Game's custom parameters.")
81+
Map<String, String> params) {
82+
83+
private void get(@JsonNode("value") String value) {}
84+
85+
private void get2(@JsonNode(value="value") String value) {}
86+
}
87+
88+
record ApiEndpoint(@JsonValue("name") String name, HttpMethod method, String url) {
89+
ApiEndpoint {
90+
name = name.toUpperCase();
91+
}
92+
}
93+
94+
record ApiEndpoint(@JsonProperty("bonus_buy") @JsonValue(some_other_name_that_is_here="value", name_which_is_super_long_that_goes_beyond_the_limit="name", other="other") String name, HttpMethod method, String url) {
95+
ApiEndpoint {
96+
name = name.toUpperCase();
97+
}
98+
}
99+
100+
record ApiEndpoint(@JsonValue(name="name") String name, HttpMethod method, String url) {
101+
ApiEndpoint {
102+
name = name.toUpperCase();
103+
}
104+
}
105+
106+
record ApiEndpoint(@JsonValue(name="name") String name, HttpMethod method, String url, @JsonValue(name="name") String name) {
107+
ApiEndpoint {
108+
name = name.toUpperCase();
109+
}
110+
}
Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
@Schema(
2+
description = "Type of quote being requested",
3+
example = "NEW_BUSINESS",
4+
somethingElse = "new",
5+
other = "my other long string")
6+
public record QuoteRequest(
7+
@Schema(description = "US state of the product being quoted", example = "TX")
8+
RegulatoryState regulatoryState,
9+
10+
@Schema(description = "Reason for a quote", example = "New Business")
11+
String amendmentReason,
12+
13+
@Schema(
14+
description = "Type of quote being requested",
15+
example = "NEW_BUSINESS",
16+
somethingElse = "new",
17+
other = "my other long string")
18+
QuoteType quoteType,
19+
20+
@Schema(description = "Date the quoted changes become active", example = "2023-06-25")
21+
LocalDate quoteEffectiveDate) {}
22+
23+
public record QuoteRequest(
24+
int value,
25+
26+
@SomeInput RegulatoryState regulatoryState,
27+
28+
@SomeInput
29+
@NotNull
30+
@Deprecated
31+
@JsonValue(name = "something")
32+
@Schema(description = "US state of the product being quoted", example = "TX")
33+
RegulatoryState regulatoryState,
34+
35+
@Schema(description = "Reason for a quote", example = "New Business")
36+
String amendmentReason,
37+
38+
int x,
39+
int j) {}
40+
41+
public record Url(
42+
int var1,
43+
@JsonValue("value") String var2,
44+
@SomeInput @JsonValue String var3,
45+
int var3,
46+
int var4,
47+
int var5,
48+
int var6) {}
49+
50+
public record Url(
51+
@NotNull
52+
@JsonValue
53+
@Deprecated
54+
@Annotation1
55+
@Annotation2
56+
@Annotation3
57+
@Annotation4
58+
@Annotation5
59+
@Annotation6
60+
@Annotation7
61+
@Annotation8
62+
@Annotation9
63+
@Annotation10
64+
@Annotation11
65+
@Annotation12
66+
String value) {}
67+
68+
public record Url(@NotNull String value, int number) {}
69+
70+
public record Url(int number, @JsonValue("value") String value) {}
71+
72+
public record Url(@New @JsonValue("value") String value) {}
73+
74+
@JsonIgnoreProperties(ignoreUnknown = true)
75+
@JsonInclude(JsonInclude.Include.NON_NULL)
76+
public record Game(
77+
@JsonProperty("accounting_group") @JsonPropertyDescription("The accounting group of the game.")
78+
String accountingGroup,
79+
80+
@JsonProperty("accumulating") @JsonPropertyDescription("Marks which games with accumulating bonuses.")
81+
Boolean accumulating,
82+
83+
@JsonProperty("bonus_buy") @JsonPropertyDescription("Games with purchasable bonuses.")
84+
Boolean bonusBuy,
85+
86+
@JsonProperty("category")
87+
@JsonPropertyDescription("Game's category. Allowed values: slots, roulette, card, "
88+
+ "casual, lottery, poker, craps, video_poker")
89+
String category,
90+
91+
@JsonProperty("hd") @JsonPropertyDescription("HD format games.")
92+
Boolean hd,
93+
94+
@JsonProperty("new") @JsonPropertyDescription("New format games.")
95+
Boolean assa,
96+
97+
@JsonProperty("hit_rate")
98+
@JsonPropertyDescription("Frequency of wins per 100 bets. The higher, the hit rate the lower the volatility "
99+
+ "rating, and vice versa. Positive value. For slots only. "
100+
+ "Match pattern: ^\\d{1,18}(\\.\\d{1,12})?$")
101+
String hitRate,
102+
103+
@JsonProperty("params") @JsonPropertyDescription("Game's custom parameters.")
104+
Map<String, String> params) {
105+
106+
private void get(@JsonNode("value") String value) {}
107+
108+
private void get2(@JsonNode(value = "value") String value) {}
109+
}
110+
111+
record ApiEndpoint(@JsonValue("name") String name, HttpMethod method, String url) {
112+
ApiEndpoint {
113+
name = name.toUpperCase();
114+
}
115+
}
116+
117+
record ApiEndpoint(
118+
@JsonProperty("bonus_buy")
119+
@JsonValue(
120+
some_other_name_that_is_here = "value",
121+
name_which_is_super_long_that_goes_beyond_the_limit = "name",
122+
other = "other")
123+
String name,
124+
125+
HttpMethod method,
126+
String url) {
127+
ApiEndpoint {
128+
name = name.toUpperCase();
129+
}
130+
}
131+
132+
record ApiEndpoint(@JsonValue(name = "name") String name, HttpMethod method, String url) {
133+
ApiEndpoint {
134+
name = name.toUpperCase();
135+
}
136+
}
137+
138+
record ApiEndpoint(
139+
@JsonValue(name = "name") String name,
140+
HttpMethod method,
141+
String url,
142+
@JsonValue(name = "name") String name) {
143+
ApiEndpoint {
144+
name = name.toUpperCase();
145+
}
146+
}

0 commit comments

Comments
 (0)