Skip to content

Commit 4b2c500

Browse files
authored
fix(jdk-codemodel): replace raw String operator fields with typed enums (#12)
1 parent 03fabfc commit 4b2c500

10 files changed

Lines changed: 360 additions & 69 deletions

File tree

jdk-codemodel/src/main/java/build/codemodel/jdk/JdkExpressionConverter.java

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,9 @@
4747
import build.codemodel.imperative.Block;
4848
import build.codemodel.imperative.Statement;
4949
import build.codemodel.jdk.expression.ArrayAccess;
50+
import build.codemodel.jdk.expression.AssignmentOperator;
5051
import build.codemodel.jdk.expression.BitwiseBinary;
52+
import build.codemodel.jdk.expression.BitwiseOperator;
5153
import build.codemodel.jdk.expression.CharLiteral;
5254
import build.codemodel.jdk.expression.CompoundAssignment;
5355
import build.codemodel.jdk.expression.FieldAccess;
@@ -61,7 +63,9 @@
6163
import build.codemodel.jdk.expression.NewObject;
6264
import build.codemodel.jdk.expression.NullLiteral;
6365
import build.codemodel.jdk.expression.Parenthesized;
66+
import build.codemodel.jdk.expression.PostfixOperator;
6467
import build.codemodel.jdk.expression.PostfixUnary;
68+
import build.codemodel.jdk.expression.PrefixOperator;
6569
import build.codemodel.jdk.expression.PrefixUnary;
6670
import build.codemodel.jdk.expression.SwitchExpression;
6771
import build.codemodel.jdk.expression.Ternary;
@@ -326,15 +330,28 @@ public Expression visitConditionalExpression(final ConditionalExpressionTree t,
326330

327331
@Override
328332
public Expression visitAssignment(final AssignmentTree t, final Void v) {
329-
return CompoundAssignment.of(codeModel, "ASSIGNMENT",
333+
return CompoundAssignment.of(codeModel, AssignmentOperator.ASSIGN,
330334
convert(t.getVariable()),
331335
convert(t.getExpression()));
332336
}
333337

334338
@Override
335339
public Expression visitCompoundAssignment(final CompoundAssignmentTree t, final Void v) {
336-
return CompoundAssignment.of(codeModel,
337-
t.getKind().toString(),
340+
final var op = switch (t.getKind()) {
341+
case PLUS_ASSIGNMENT -> AssignmentOperator.PLUS;
342+
case MINUS_ASSIGNMENT -> AssignmentOperator.MINUS;
343+
case MULTIPLY_ASSIGNMENT -> AssignmentOperator.MULTIPLY;
344+
case DIVIDE_ASSIGNMENT -> AssignmentOperator.DIVIDE;
345+
case REMAINDER_ASSIGNMENT -> AssignmentOperator.REMAINDER;
346+
case AND_ASSIGNMENT -> AssignmentOperator.AND;
347+
case OR_ASSIGNMENT -> AssignmentOperator.OR;
348+
case XOR_ASSIGNMENT -> AssignmentOperator.XOR;
349+
case LEFT_SHIFT_ASSIGNMENT -> AssignmentOperator.LEFT_SHIFT;
350+
case RIGHT_SHIFT_ASSIGNMENT -> AssignmentOperator.RIGHT_SHIFT;
351+
case UNSIGNED_RIGHT_SHIFT_ASSIGNMENT -> AssignmentOperator.UNSIGNED_RIGHT_SHIFT;
352+
default -> throw new IllegalArgumentException("Unexpected compound assignment kind: " + t.getKind());
353+
};
354+
return CompoundAssignment.of(codeModel, op,
338355
convert(t.getVariable()),
339356
convert(t.getExpression()));
340357
}
@@ -357,8 +374,12 @@ public Expression visitBinary(final BinaryTree t, final Void v) {
357374
case GREATER_THAN_EQUAL -> GreaterThanOrEqualTo.of(left, right);
358375
case CONDITIONAL_AND -> Conjunction.of(left, right);
359376
case CONDITIONAL_OR -> Disjunction.of(left, right);
360-
case AND, OR, XOR, LEFT_SHIFT, RIGHT_SHIFT, UNSIGNED_RIGHT_SHIFT ->
361-
BitwiseBinary.of(codeModel, t.getKind().toString(), left, right);
377+
case AND -> BitwiseBinary.of(codeModel, BitwiseOperator.AND, left, right);
378+
case OR -> BitwiseBinary.of(codeModel, BitwiseOperator.OR, left, right);
379+
case XOR -> BitwiseBinary.of(codeModel, BitwiseOperator.XOR, left, right);
380+
case LEFT_SHIFT -> BitwiseBinary.of(codeModel, BitwiseOperator.LEFT_SHIFT, left, right);
381+
case RIGHT_SHIFT -> BitwiseBinary.of(codeModel, BitwiseOperator.RIGHT_SHIFT, left, right);
382+
case UNSIGNED_RIGHT_SHIFT -> BitwiseBinary.of(codeModel, BitwiseOperator.UNSIGNED_RIGHT_SHIFT, left, right);
362383
default -> UnknownExpression.of(codeModel);
363384
};
364385
}
@@ -367,10 +388,12 @@ public Expression visitBinary(final BinaryTree t, final Void v) {
367388
public Expression visitUnary(final UnaryTree t, final Void v) {
368389
final var operand = convert(t.getExpression());
369390
return switch (t.getKind()) {
370-
case PREFIX_INCREMENT, PREFIX_DECREMENT, BITWISE_COMPLEMENT, UNARY_PLUS ->
371-
PrefixUnary.of(codeModel, t.getKind().toString(), operand);
372-
case POSTFIX_INCREMENT, POSTFIX_DECREMENT ->
373-
PostfixUnary.of(codeModel, t.getKind().toString(), operand);
391+
case PREFIX_INCREMENT -> PrefixUnary.of(codeModel, PrefixOperator.INCREMENT, operand);
392+
case PREFIX_DECREMENT -> PrefixUnary.of(codeModel, PrefixOperator.DECREMENT, operand);
393+
case BITWISE_COMPLEMENT -> PrefixUnary.of(codeModel, PrefixOperator.BITWISE_COMPLEMENT, operand);
394+
case UNARY_PLUS -> PrefixUnary.of(codeModel, PrefixOperator.UNARY_PLUS, operand);
395+
case POSTFIX_INCREMENT -> PostfixUnary.of(codeModel, PostfixOperator.INCREMENT, operand);
396+
case POSTFIX_DECREMENT -> PostfixUnary.of(codeModel, PostfixOperator.DECREMENT, operand);
374397
case UNARY_MINUS -> Negative.of(operand);
375398
case LOGICAL_COMPLEMENT -> Negation.of(operand);
376399
default -> UnknownExpression.of(codeModel);
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package build.codemodel.jdk.expression;
2+
3+
/*-
4+
* #%L
5+
* JDK Code Model
6+
* %%
7+
* Copyright (C) 2026 Workday, Inc.
8+
* %%
9+
* Licensed under the Apache License, Version 2.0 (the "License");
10+
* you may not use this file except in compliance with the License.
11+
* You may obtain a copy of the License at
12+
*
13+
* http://www.apache.org/licenses/LICENSE-2.0
14+
*
15+
* Unless required by applicable law or agreed to in writing, software
16+
* distributed under the License is distributed on an "AS IS" BASIS,
17+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18+
* See the License for the specific language governing permissions and
19+
* limitations under the License.
20+
* #L%
21+
*/
22+
23+
/**
24+
* The operator of a {@link CompoundAssignment} expression.
25+
*
26+
* @author reed.vonredwitz
27+
* @since Apr-2026
28+
*/
29+
public enum AssignmentOperator {
30+
/** Simple assignment: {@code x = y} */
31+
ASSIGN,
32+
/** Addition assignment: {@code x += y} */
33+
PLUS,
34+
/** Subtraction assignment: {@code x -= y} */
35+
MINUS,
36+
/** Multiplication assignment: {@code x *= y} */
37+
MULTIPLY,
38+
/** Division assignment: {@code x /= y} */
39+
DIVIDE,
40+
/** Remainder assignment: {@code x %= y} */
41+
REMAINDER,
42+
/** Bitwise AND assignment: {@code x &= y} */
43+
AND,
44+
/** Bitwise OR assignment: {@code x |= y} */
45+
OR,
46+
/** Bitwise XOR assignment: {@code x ^= y} */
47+
XOR,
48+
/** Left shift assignment: {@code x <<= y} */
49+
LEFT_SHIFT,
50+
/** Signed right shift assignment: {@code x >>= y} */
51+
RIGHT_SHIFT,
52+
/** Unsigned right shift assignment: {@code x >>>= y} */
53+
UNSIGNED_RIGHT_SHIFT
54+
}

jdk-codemodel/src/main/java/build/codemodel/jdk/expression/BitwiseBinary.java

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@
99
* Licensed under the Apache License, Version 2.0 (the "License");
1010
* you may not use this file except in compliance with the License.
1111
* You may obtain a copy of the License at
12-
*
12+
*
1313
* http://www.apache.org/licenses/LICENSE-2.0
14-
*
14+
*
1515
* Unless required by applicable law or agreed to in writing, software
1616
* distributed under the License is distributed on an "AS IS" BASIS,
1717
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -47,9 +47,9 @@ public final class BitwiseBinary
4747
extends AbstractExpression {
4848

4949
/**
50-
* The operator kind string (e.g. {@code "AND"}, {@code "LEFT_SHIFT"}).
50+
* The operator.
5151
*/
52-
private final String operator;
52+
private final BitwiseOperator operator;
5353

5454
/**
5555
* The left-hand-side operand.
@@ -62,7 +62,7 @@ public final class BitwiseBinary
6262
private final Expression right;
6363

6464
private BitwiseBinary(final CodeModel codeModel,
65-
final String operator,
65+
final BitwiseOperator operator,
6666
final Expression left,
6767
final Expression right) {
6868
super(codeModel);
@@ -79,7 +79,7 @@ public BitwiseBinary(@Bound final CodeModel codeModel,
7979
final Marshalled<Expression> left,
8080
final Marshalled<Expression> right) {
8181
super(codeModel, marshaller, traits);
82-
this.operator = operator;
82+
this.operator = BitwiseOperator.valueOf(operator);
8383
this.left = marshaller.unmarshal(left);
8484
this.right = marshaller.unmarshal(right);
8585
}
@@ -91,17 +91,17 @@ public void destructor(final Marshaller marshaller,
9191
final Out<Marshalled<Expression>> left,
9292
final Out<Marshalled<Expression>> right) {
9393
super.destructor(marshaller, traits);
94-
operator.set(this.operator);
94+
operator.set(this.operator.name());
9595
left.set(marshaller.marshal(this.left));
9696
right.set(marshaller.marshal(this.right));
9797
}
9898

9999
/**
100-
* Obtains the operator kind string.
100+
* Obtains the operator.
101101
*
102-
* @return the operator kind string
102+
* @return the {@link BitwiseOperator}
103103
*/
104-
public String operator() {
104+
public BitwiseOperator operator() {
105105
return this.operator;
106106
}
107107

@@ -126,7 +126,7 @@ public Expression right() {
126126
@Override
127127
public boolean equals(final Object object) {
128128
return object instanceof BitwiseBinary other
129-
&& Objects.equals(this.operator, other.operator)
129+
&& this.operator == other.operator
130130
&& Objects.equals(this.left, other.left)
131131
&& Objects.equals(this.right, other.right)
132132
&& super.equals(other);
@@ -136,13 +136,13 @@ public boolean equals(final Object object) {
136136
* Creates a {@link BitwiseBinary} expression.
137137
*
138138
* @param codeModel the {@link CodeModel}
139-
* @param operator the operator kind string
140-
* @param left the left-hand-side {@link Expression}
141-
* @param right the right-hand-side {@link Expression}
139+
* @param operator the {@link BitwiseOperator}
140+
* @param left the left-hand-side {@link Expression}
141+
* @param right the right-hand-side {@link Expression}
142142
* @return a new {@link BitwiseBinary}
143143
*/
144144
public static BitwiseBinary of(final CodeModel codeModel,
145-
final String operator,
145+
final BitwiseOperator operator,
146146
final Expression left,
147147
final Expression right) {
148148
return new BitwiseBinary(codeModel, operator, left, right);
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package build.codemodel.jdk.expression;
2+
3+
/*-
4+
* #%L
5+
* JDK Code Model
6+
* %%
7+
* Copyright (C) 2026 Workday, Inc.
8+
* %%
9+
* Licensed under the Apache License, Version 2.0 (the "License");
10+
* you may not use this file except in compliance with the License.
11+
* You may obtain a copy of the License at
12+
*
13+
* http://www.apache.org/licenses/LICENSE-2.0
14+
*
15+
* Unless required by applicable law or agreed to in writing, software
16+
* distributed under the License is distributed on an "AS IS" BASIS,
17+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18+
* See the License for the specific language governing permissions and
19+
* limitations under the License.
20+
* #L%
21+
*/
22+
23+
/**
24+
* The operator of a {@link BitwiseBinary} expression.
25+
*
26+
* @author reed.vonredwitz
27+
* @since Apr-2026
28+
*/
29+
public enum BitwiseOperator {
30+
/** Bitwise AND: {@code a & b} */
31+
AND,
32+
/** Bitwise OR: {@code a | b} */
33+
OR,
34+
/** Bitwise XOR: {@code a ^ b} */
35+
XOR,
36+
/** Left shift: {@code a << b} */
37+
LEFT_SHIFT,
38+
/** Signed right shift: {@code a >> b} */
39+
RIGHT_SHIFT,
40+
/** Unsigned right shift: {@code a >>> b} */
41+
UNSIGNED_RIGHT_SHIFT
42+
}

jdk-codemodel/src/main/java/build/codemodel/jdk/expression/CompoundAssignment.java

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@
99
* Licensed under the Apache License, Version 2.0 (the "License");
1010
* you may not use this file except in compliance with the License.
1111
* You may obtain a copy of the License at
12-
*
12+
*
1313
* http://www.apache.org/licenses/LICENSE-2.0
14-
*
14+
*
1515
* Unless required by applicable law or agreed to in writing, software
1616
* distributed under the License is distributed on an "AS IS" BASIS,
1717
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -37,8 +37,7 @@
3737
import java.util.stream.Stream;
3838

3939
/**
40-
* A compound assignment expression: {@code var OP= value} (e.g. {@code x += 1}).
41-
* Also used for simple assignment {@code var = value} when the operator is {@code "ASSIGNMENT"}.
40+
* An assignment expression: simple {@code x = y} or compound {@code x += y}, {@code x <<= y}, etc.
4241
*
4342
* @author reed.vonredwitz
4443
* @since Mar-2026
@@ -47,9 +46,9 @@ public final class CompoundAssignment
4746
extends AbstractExpression {
4847

4948
/**
50-
* The operator kind string (e.g. {@code "PLUS_ASSIGNMENT"}, {@code "ASSIGNMENT"}).
49+
* The operator.
5150
*/
52-
private final String operator;
51+
private final AssignmentOperator operator;
5352

5453
/**
5554
* The left-hand-side variable expression.
@@ -62,7 +61,7 @@ public final class CompoundAssignment
6261
private final Expression value;
6362

6463
private CompoundAssignment(final CodeModel codeModel,
65-
final String operator,
64+
final AssignmentOperator operator,
6665
final Expression variable,
6766
final Expression value) {
6867
super(codeModel);
@@ -79,7 +78,7 @@ public CompoundAssignment(@Bound final CodeModel codeModel,
7978
final Marshalled<Expression> variable,
8079
final Marshalled<Expression> value) {
8180
super(codeModel, marshaller, traits);
82-
this.operator = operator;
81+
this.operator = AssignmentOperator.valueOf(operator);
8382
this.variable = marshaller.unmarshal(variable);
8483
this.value = marshaller.unmarshal(value);
8584
}
@@ -91,17 +90,17 @@ public void destructor(final Marshaller marshaller,
9190
final Out<Marshalled<Expression>> variable,
9291
final Out<Marshalled<Expression>> value) {
9392
super.destructor(marshaller, traits);
94-
operator.set(this.operator);
93+
operator.set(this.operator.name());
9594
variable.set(marshaller.marshal(this.variable));
9695
value.set(marshaller.marshal(this.value));
9796
}
9897

9998
/**
100-
* Obtains the operator kind string.
99+
* Obtains the operator.
101100
*
102-
* @return the operator kind string
101+
* @return the {@link AssignmentOperator}
103102
*/
104-
public String operator() {
103+
public AssignmentOperator operator() {
105104
return this.operator;
106105
}
107106

@@ -126,7 +125,7 @@ public Expression value() {
126125
@Override
127126
public boolean equals(final Object object) {
128127
return object instanceof CompoundAssignment other
129-
&& Objects.equals(this.operator, other.operator)
128+
&& this.operator == other.operator
130129
&& Objects.equals(this.variable, other.variable)
131130
&& Objects.equals(this.value, other.value)
132131
&& super.equals(other);
@@ -136,13 +135,13 @@ public boolean equals(final Object object) {
136135
* Creates a {@link CompoundAssignment} expression.
137136
*
138137
* @param codeModel the {@link CodeModel}
139-
* @param operator the operator kind string
140-
* @param variable the left-hand-side variable {@link Expression}
141-
* @param value the right-hand-side value {@link Expression}
138+
* @param operator the {@link AssignmentOperator}
139+
* @param variable the left-hand-side variable {@link Expression}
140+
* @param value the right-hand-side value {@link Expression}
142141
* @return a new {@link CompoundAssignment}
143142
*/
144143
public static CompoundAssignment of(final CodeModel codeModel,
145-
final String operator,
144+
final AssignmentOperator operator,
146145
final Expression variable,
147146
final Expression value) {
148147
return new CompoundAssignment(codeModel, operator, variable, value);

0 commit comments

Comments
 (0)