Skip to content

Commit

Permalink
int literals should return an Integer value (#624)
Browse files Browse the repository at this point in the history
* `int` literals should return an `Integer` value

* Change `Long` value to `Integer` for `int` expressions
  • Loading branch information
timtebeek authored Jan 11, 2025
1 parent b01642a commit 181a00d
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 2 deletions.
2 changes: 1 addition & 1 deletion gradle/licenseHeader.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Copyright 2023 the original author or authors.
Copyright ${year} the original author or authors.
<p>
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2096,9 +2096,13 @@ JContainer<Expression> mapTypeArguments(@Nullable KtTypeArgumentList ktTypeArgum
@Override
public J visitConstantExpression(KtConstantExpression expression, ExecutionContext data) {
IElementType elementType = expression.getElementType();
JavaType.Primitive type = primitiveType(expression);
Object value;
if (elementType == KtNodeTypes.INTEGER_CONSTANT || elementType == KtNodeTypes.FLOAT_CONSTANT) {
value = ParseUtilsKt.parseNumericLiteral(expression.getText(), elementType);
if (type == JavaType.Primitive.Int && value instanceof Long) {
value = ((Long) value).intValue();
}
} else if (elementType == KtNodeTypes.BOOLEAN_CONSTANT) {
value = ParseUtilsKt.parseBoolean(expression.getText());
} else if (elementType == KtNodeTypes.CHARACTER_CONSTANT) {
Expand All @@ -2115,7 +2119,7 @@ public J visitConstantExpression(KtConstantExpression expression, ExecutionConte
value,
expression.getText(),
null,
primitiveType(expression)
type
);
}

Expand Down
70 changes: 70 additions & 0 deletions src/test/java/org/openrewrite/kotlin/tree/PrimitiveTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
* Copyright 2025 the original author or authors.
* <p>
* 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
* <p>
* https://www.apache.org/licenses/LICENSE-2.0
* <p>
* 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.
*/
package org.openrewrite.kotlin.tree;

import org.junit.jupiter.api.Test;
import org.openrewrite.Issue;
import org.openrewrite.java.tree.J;
import org.openrewrite.test.RewriteTest;

import static org.assertj.core.api.Assertions.assertThat;
import static org.openrewrite.kotlin.Assertions.kotlin;

class PrimitiveTest implements RewriteTest {

@Issue("https://github.com/openrewrite/rewrite-spring/pull/663")
@Test
void intZeroValueIsInteger() {
rewriteRun(
kotlin(
"""
fun foo() {
val bar = 0
}
""",
spec -> spec.afterRecipe(cu -> {
J.MethodDeclaration foo = (J.MethodDeclaration) cu.getStatements().get(0);
J.VariableDeclarations bar = (J.VariableDeclarations) foo.getBody().getStatements().get(0);
J.Literal zero = (J.Literal) bar.getVariables().get(0).getInitializer();
assertThat(zero.getType().getKeyword()).isEqualTo("int");
assertThat(zero.getValue()).isInstanceOf(Integer.class);
})
)
);
}

@Issue("https://github.com/openrewrite/rewrite-spring/pull/663")
@Test
void longZeroValueIsLong() {
rewriteRun(
kotlin(
"""
fun foo() {
val bar = 0L
}
""",
spec -> spec.afterRecipe(cu -> {
J.MethodDeclaration foo = (J.MethodDeclaration) cu.getStatements().get(0);
J.VariableDeclarations bar = (J.VariableDeclarations) foo.getBody().getStatements().get(0);
J.Literal zero = (J.Literal) bar.getVariables().get(0).getInitializer();
assertThat(zero.getType().getKeyword()).isEqualTo("long");
assertThat(zero.getValue()).isInstanceOf(Long.class);
})
)
);
}

}

0 comments on commit 181a00d

Please sign in to comment.