Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,9 @@
import build.codemodel.imperative.Block;
import build.codemodel.imperative.Statement;
import build.codemodel.jdk.expression.ArrayAccess;
import build.codemodel.jdk.expression.AssignmentOperator;
import build.codemodel.jdk.expression.BitwiseBinary;
import build.codemodel.jdk.expression.BitwiseOperator;
import build.codemodel.jdk.expression.CharLiteral;
import build.codemodel.jdk.expression.CompoundAssignment;
import build.codemodel.jdk.expression.FieldAccess;
Expand All @@ -61,7 +63,9 @@
import build.codemodel.jdk.expression.NewObject;
import build.codemodel.jdk.expression.NullLiteral;
import build.codemodel.jdk.expression.Parenthesized;
import build.codemodel.jdk.expression.PostfixOperator;
import build.codemodel.jdk.expression.PostfixUnary;
import build.codemodel.jdk.expression.PrefixOperator;
import build.codemodel.jdk.expression.PrefixUnary;
import build.codemodel.jdk.expression.SwitchExpression;
import build.codemodel.jdk.expression.Ternary;
Expand Down Expand Up @@ -326,15 +330,28 @@ public Expression visitConditionalExpression(final ConditionalExpressionTree t,

@Override
public Expression visitAssignment(final AssignmentTree t, final Void v) {
return CompoundAssignment.of(codeModel, "ASSIGNMENT",
return CompoundAssignment.of(codeModel, AssignmentOperator.ASSIGN,
convert(t.getVariable()),
convert(t.getExpression()));
}

@Override
public Expression visitCompoundAssignment(final CompoundAssignmentTree t, final Void v) {
return CompoundAssignment.of(codeModel,
t.getKind().toString(),
final var op = switch (t.getKind()) {
case PLUS_ASSIGNMENT -> AssignmentOperator.PLUS;
case MINUS_ASSIGNMENT -> AssignmentOperator.MINUS;
case MULTIPLY_ASSIGNMENT -> AssignmentOperator.MULTIPLY;
case DIVIDE_ASSIGNMENT -> AssignmentOperator.DIVIDE;
case REMAINDER_ASSIGNMENT -> AssignmentOperator.REMAINDER;
case AND_ASSIGNMENT -> AssignmentOperator.AND;
case OR_ASSIGNMENT -> AssignmentOperator.OR;
case XOR_ASSIGNMENT -> AssignmentOperator.XOR;
case LEFT_SHIFT_ASSIGNMENT -> AssignmentOperator.LEFT_SHIFT;
case RIGHT_SHIFT_ASSIGNMENT -> AssignmentOperator.RIGHT_SHIFT;
case UNSIGNED_RIGHT_SHIFT_ASSIGNMENT -> AssignmentOperator.UNSIGNED_RIGHT_SHIFT;
default -> throw new IllegalArgumentException("Unexpected compound assignment kind: " + t.getKind());
};
return CompoundAssignment.of(codeModel, op,
convert(t.getVariable()),
convert(t.getExpression()));
}
Expand All @@ -357,8 +374,12 @@ public Expression visitBinary(final BinaryTree t, final Void v) {
case GREATER_THAN_EQUAL -> GreaterThanOrEqualTo.of(left, right);
case CONDITIONAL_AND -> Conjunction.of(left, right);
case CONDITIONAL_OR -> Disjunction.of(left, right);
case AND, OR, XOR, LEFT_SHIFT, RIGHT_SHIFT, UNSIGNED_RIGHT_SHIFT ->
BitwiseBinary.of(codeModel, t.getKind().toString(), left, right);
case AND -> BitwiseBinary.of(codeModel, BitwiseOperator.AND, left, right);
case OR -> BitwiseBinary.of(codeModel, BitwiseOperator.OR, left, right);
case XOR -> BitwiseBinary.of(codeModel, BitwiseOperator.XOR, left, right);
case LEFT_SHIFT -> BitwiseBinary.of(codeModel, BitwiseOperator.LEFT_SHIFT, left, right);
case RIGHT_SHIFT -> BitwiseBinary.of(codeModel, BitwiseOperator.RIGHT_SHIFT, left, right);
case UNSIGNED_RIGHT_SHIFT -> BitwiseBinary.of(codeModel, BitwiseOperator.UNSIGNED_RIGHT_SHIFT, left, right);
default -> UnknownExpression.of(codeModel);
};
}
Expand All @@ -367,10 +388,12 @@ public Expression visitBinary(final BinaryTree t, final Void v) {
public Expression visitUnary(final UnaryTree t, final Void v) {
final var operand = convert(t.getExpression());
return switch (t.getKind()) {
case PREFIX_INCREMENT, PREFIX_DECREMENT, BITWISE_COMPLEMENT, UNARY_PLUS ->
PrefixUnary.of(codeModel, t.getKind().toString(), operand);
case POSTFIX_INCREMENT, POSTFIX_DECREMENT ->
PostfixUnary.of(codeModel, t.getKind().toString(), operand);
case PREFIX_INCREMENT -> PrefixUnary.of(codeModel, PrefixOperator.INCREMENT, operand);
case PREFIX_DECREMENT -> PrefixUnary.of(codeModel, PrefixOperator.DECREMENT, operand);
case BITWISE_COMPLEMENT -> PrefixUnary.of(codeModel, PrefixOperator.BITWISE_COMPLEMENT, operand);
case UNARY_PLUS -> PrefixUnary.of(codeModel, PrefixOperator.UNARY_PLUS, operand);
case POSTFIX_INCREMENT -> PostfixUnary.of(codeModel, PostfixOperator.INCREMENT, operand);
case POSTFIX_DECREMENT -> PostfixUnary.of(codeModel, PostfixOperator.DECREMENT, operand);
case UNARY_MINUS -> Negative.of(operand);
case LOGICAL_COMPLEMENT -> Negation.of(operand);
default -> UnknownExpression.of(codeModel);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package build.codemodel.jdk.expression;

/*-
* #%L
* JDK Code Model
* %%
* Copyright (C) 2026 Workday, Inc.
* %%
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* #L%
*/

/**
* The operator of a {@link CompoundAssignment} expression.
*
* @author reed.vonredwitz
* @since Apr-2026
*/
public enum AssignmentOperator {
/** Simple assignment: {@code x = y} */
ASSIGN,
/** Addition assignment: {@code x += y} */
PLUS,
/** Subtraction assignment: {@code x -= y} */
MINUS,
/** Multiplication assignment: {@code x *= y} */
MULTIPLY,
/** Division assignment: {@code x /= y} */
DIVIDE,
/** Remainder assignment: {@code x %= y} */
REMAINDER,
/** Bitwise AND assignment: {@code x &= y} */
AND,
/** Bitwise OR assignment: {@code x |= y} */
OR,
/** Bitwise XOR assignment: {@code x ^= y} */
XOR,
/** Left shift assignment: {@code x <<= y} */
LEFT_SHIFT,
/** Signed right shift assignment: {@code x >>= y} */
RIGHT_SHIFT,
/** Unsigned right shift assignment: {@code x >>>= y} */
UNSIGNED_RIGHT_SHIFT
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*
* http://www.apache.org/licenses/LICENSE-2.0
*
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Expand Down Expand Up @@ -47,9 +47,9 @@ public final class BitwiseBinary
extends AbstractExpression {

/**
* The operator kind string (e.g. {@code "AND"}, {@code "LEFT_SHIFT"}).
* The operator.
*/
private final String operator;
private final BitwiseOperator operator;

/**
* The left-hand-side operand.
Expand All @@ -62,7 +62,7 @@ public final class BitwiseBinary
private final Expression right;

private BitwiseBinary(final CodeModel codeModel,
final String operator,
final BitwiseOperator operator,
final Expression left,
final Expression right) {
super(codeModel);
Expand All @@ -79,7 +79,7 @@ public BitwiseBinary(@Bound final CodeModel codeModel,
final Marshalled<Expression> left,
final Marshalled<Expression> right) {
super(codeModel, marshaller, traits);
this.operator = operator;
this.operator = BitwiseOperator.valueOf(operator);
this.left = marshaller.unmarshal(left);
this.right = marshaller.unmarshal(right);
}
Expand All @@ -91,17 +91,17 @@ public void destructor(final Marshaller marshaller,
final Out<Marshalled<Expression>> left,
final Out<Marshalled<Expression>> right) {
super.destructor(marshaller, traits);
operator.set(this.operator);
operator.set(this.operator.name());
left.set(marshaller.marshal(this.left));
right.set(marshaller.marshal(this.right));
}

/**
* Obtains the operator kind string.
* Obtains the operator.
*
* @return the operator kind string
* @return the {@link BitwiseOperator}
*/
public String operator() {
public BitwiseOperator operator() {
return this.operator;
}

Expand All @@ -126,7 +126,7 @@ public Expression right() {
@Override
public boolean equals(final Object object) {
return object instanceof BitwiseBinary other
&& Objects.equals(this.operator, other.operator)
&& this.operator == other.operator
&& Objects.equals(this.left, other.left)
&& Objects.equals(this.right, other.right)
&& super.equals(other);
Expand All @@ -136,13 +136,13 @@ public boolean equals(final Object object) {
* Creates a {@link BitwiseBinary} expression.
*
* @param codeModel the {@link CodeModel}
* @param operator the operator kind string
* @param left the left-hand-side {@link Expression}
* @param right the right-hand-side {@link Expression}
* @param operator the {@link BitwiseOperator}
* @param left the left-hand-side {@link Expression}
* @param right the right-hand-side {@link Expression}
* @return a new {@link BitwiseBinary}
*/
public static BitwiseBinary of(final CodeModel codeModel,
final String operator,
final BitwiseOperator operator,
final Expression left,
final Expression right) {
return new BitwiseBinary(codeModel, operator, left, right);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package build.codemodel.jdk.expression;

/*-
* #%L
* JDK Code Model
* %%
* Copyright (C) 2026 Workday, Inc.
* %%
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* #L%
*/

/**
* The operator of a {@link BitwiseBinary} expression.
*
* @author reed.vonredwitz
* @since Apr-2026
*/
public enum BitwiseOperator {
/** Bitwise AND: {@code a & b} */
AND,
/** Bitwise OR: {@code a | b} */
OR,
/** Bitwise XOR: {@code a ^ b} */
XOR,
/** Left shift: {@code a << b} */
LEFT_SHIFT,
/** Signed right shift: {@code a >> b} */
RIGHT_SHIFT,
/** Unsigned right shift: {@code a >>> b} */
UNSIGNED_RIGHT_SHIFT
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*
* http://www.apache.org/licenses/LICENSE-2.0
*
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Expand All @@ -37,8 +37,7 @@
import java.util.stream.Stream;

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

/**
* The operator kind string (e.g. {@code "PLUS_ASSIGNMENT"}, {@code "ASSIGNMENT"}).
* The operator.
*/
private final String operator;
private final AssignmentOperator operator;

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

private CompoundAssignment(final CodeModel codeModel,
final String operator,
final AssignmentOperator operator,
final Expression variable,
final Expression value) {
super(codeModel);
Expand All @@ -79,7 +78,7 @@ public CompoundAssignment(@Bound final CodeModel codeModel,
final Marshalled<Expression> variable,
final Marshalled<Expression> value) {
super(codeModel, marshaller, traits);
this.operator = operator;
this.operator = AssignmentOperator.valueOf(operator);
this.variable = marshaller.unmarshal(variable);
this.value = marshaller.unmarshal(value);
}
Expand All @@ -91,17 +90,17 @@ public void destructor(final Marshaller marshaller,
final Out<Marshalled<Expression>> variable,
final Out<Marshalled<Expression>> value) {
super.destructor(marshaller, traits);
operator.set(this.operator);
operator.set(this.operator.name());
variable.set(marshaller.marshal(this.variable));
value.set(marshaller.marshal(this.value));
}

/**
* Obtains the operator kind string.
* Obtains the operator.
*
* @return the operator kind string
* @return the {@link AssignmentOperator}
*/
public String operator() {
public AssignmentOperator operator() {
return this.operator;
}

Expand All @@ -126,7 +125,7 @@ public Expression value() {
@Override
public boolean equals(final Object object) {
return object instanceof CompoundAssignment other
&& Objects.equals(this.operator, other.operator)
&& this.operator == other.operator
&& Objects.equals(this.variable, other.variable)
&& Objects.equals(this.value, other.value)
&& super.equals(other);
Expand All @@ -136,13 +135,13 @@ public boolean equals(final Object object) {
* Creates a {@link CompoundAssignment} expression.
*
* @param codeModel the {@link CodeModel}
* @param operator the operator kind string
* @param variable the left-hand-side variable {@link Expression}
* @param value the right-hand-side value {@link Expression}
* @param operator the {@link AssignmentOperator}
* @param variable the left-hand-side variable {@link Expression}
* @param value the right-hand-side value {@link Expression}
* @return a new {@link CompoundAssignment}
*/
public static CompoundAssignment of(final CodeModel codeModel,
final String operator,
final AssignmentOperator operator,
final Expression variable,
final Expression value) {
return new CompoundAssignment(codeModel, operator, variable, value);
Expand Down
Loading
Loading