Skip to content
This repository was archived by the owner on May 12, 2025. It is now read-only.

Commit 181a00d

Browse files
authored
int literals should return an Integer value (#624)
* `int` literals should return an `Integer` value * Change `Long` value to `Integer` for `int` expressions
1 parent b01642a commit 181a00d

File tree

3 files changed

+76
-2
lines changed

3 files changed

+76
-2
lines changed

gradle/licenseHeader.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
Copyright 2023 the original author or authors.
1+
Copyright ${year} the original author or authors.
22
<p>
33
Licensed under the Apache License, Version 2.0 (the "License");
44
you may not use this file except in compliance with the License.

src/main/java/org/openrewrite/kotlin/internal/KotlinTreeParserVisitor.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2096,9 +2096,13 @@ JContainer<Expression> mapTypeArguments(@Nullable KtTypeArgumentList ktTypeArgum
20962096
@Override
20972097
public J visitConstantExpression(KtConstantExpression expression, ExecutionContext data) {
20982098
IElementType elementType = expression.getElementType();
2099+
JavaType.Primitive type = primitiveType(expression);
20992100
Object value;
21002101
if (elementType == KtNodeTypes.INTEGER_CONSTANT || elementType == KtNodeTypes.FLOAT_CONSTANT) {
21012102
value = ParseUtilsKt.parseNumericLiteral(expression.getText(), elementType);
2103+
if (type == JavaType.Primitive.Int && value instanceof Long) {
2104+
value = ((Long) value).intValue();
2105+
}
21022106
} else if (elementType == KtNodeTypes.BOOLEAN_CONSTANT) {
21032107
value = ParseUtilsKt.parseBoolean(expression.getText());
21042108
} else if (elementType == KtNodeTypes.CHARACTER_CONSTANT) {
@@ -2115,7 +2119,7 @@ public J visitConstantExpression(KtConstantExpression expression, ExecutionConte
21152119
value,
21162120
expression.getText(),
21172121
null,
2118-
primitiveType(expression)
2122+
type
21192123
);
21202124
}
21212125

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/*
2+
* Copyright 2025 the original author or authors.
3+
* <p>
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
* <p>
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
* <p>
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.openrewrite.kotlin.tree;
17+
18+
import org.junit.jupiter.api.Test;
19+
import org.openrewrite.Issue;
20+
import org.openrewrite.java.tree.J;
21+
import org.openrewrite.test.RewriteTest;
22+
23+
import static org.assertj.core.api.Assertions.assertThat;
24+
import static org.openrewrite.kotlin.Assertions.kotlin;
25+
26+
class PrimitiveTest implements RewriteTest {
27+
28+
@Issue("https://github.com/openrewrite/rewrite-spring/pull/663")
29+
@Test
30+
void intZeroValueIsInteger() {
31+
rewriteRun(
32+
kotlin(
33+
"""
34+
fun foo() {
35+
val bar = 0
36+
}
37+
""",
38+
spec -> spec.afterRecipe(cu -> {
39+
J.MethodDeclaration foo = (J.MethodDeclaration) cu.getStatements().get(0);
40+
J.VariableDeclarations bar = (J.VariableDeclarations) foo.getBody().getStatements().get(0);
41+
J.Literal zero = (J.Literal) bar.getVariables().get(0).getInitializer();
42+
assertThat(zero.getType().getKeyword()).isEqualTo("int");
43+
assertThat(zero.getValue()).isInstanceOf(Integer.class);
44+
})
45+
)
46+
);
47+
}
48+
49+
@Issue("https://github.com/openrewrite/rewrite-spring/pull/663")
50+
@Test
51+
void longZeroValueIsLong() {
52+
rewriteRun(
53+
kotlin(
54+
"""
55+
fun foo() {
56+
val bar = 0L
57+
}
58+
""",
59+
spec -> spec.afterRecipe(cu -> {
60+
J.MethodDeclaration foo = (J.MethodDeclaration) cu.getStatements().get(0);
61+
J.VariableDeclarations bar = (J.VariableDeclarations) foo.getBody().getStatements().get(0);
62+
J.Literal zero = (J.Literal) bar.getVariables().get(0).getInitializer();
63+
assertThat(zero.getType().getKeyword()).isEqualTo("long");
64+
assertThat(zero.getValue()).isInstanceOf(Long.class);
65+
})
66+
)
67+
);
68+
}
69+
70+
}

0 commit comments

Comments
 (0)