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 @@ -101,6 +101,21 @@ private J.Literal toStringLiteral(J.Literal input) {
return input;
}

// Handle character literals by stripping single quotes and wrapping in double quotes
if (input.getType() == JavaType.Primitive.Char) {
String charSource = input.getValueSource();
if (charSource != null && charSource.length() >= 2 &&
charSource.startsWith("'") && charSource.endsWith("'")) {
String charContent = charSource.substring(1, charSource.length() - 1);
String stringValue = input.getValue() != null ? String.valueOf(input.getValue()) : charContent;
String valueSource = "\"" + charContent + "\"";
return input
.withType(JavaType.Primitive.String)
.withValue(stringValue)
.withValueSource(valueSource);
}
}

String value = input.getValueSource();
return new J.Literal(randomId(), Space.EMPTY, Markers.EMPTY, value,
"\"" + value + "\"", null, JavaType.Primitive.String);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -390,4 +390,35 @@ void print(String str, String str2) {
)
);
}

@Test
void characterLiterals() {
rewriteRun(
//language=java
java(
"""
class A {
String track(Object consumer) {
return "tracked";
}

String method(String text, Object consumer) {
return new StringBuilder(50).append('[').append(track(consumer)).append("]: ").append(text).toString();
}
}
""",
"""
class A {
String track(Object consumer) {
return "tracked";
}

String method(String text, Object consumer) {
return "[" + track(consumer) + "]: " + text;
}
}
"""
)
);
}
}