Skip to content
This repository has been archived by the owner on Oct 10, 2024. It is now read-only.

Fix #604 Multiline Strings Forcefully Wrapping With Concatenation Causing Issues #902

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
27 changes: 27 additions & 0 deletions src/main/java/com/squareup/javapoet/CodeBlock.java
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,33 @@ private Object argToLiteral(Object o) {
}

private String argToString(Object o) {
if(o instanceof String){
int index = -1;
StringBuilder str= new StringBuilder();
char temp = '\\';
for(int i=0;i<((String) o).length();i++){
if(((String) o).charAt(i)==10){
if(i==0) {
str.append(temp).append("n");
index=i+1;
}else if(index==-1) {
str.append(((String) o).substring(0, i)).append(temp).append("n");
index=i+1;
}else {
str.append(((String) o).substring(index, i)).append(temp).append("n");
index=i+1;
}
}
}
if(index==-1){
return o != null ? String.valueOf(o) : null;
}
if(index!=((String) o).length() ){
str.append(((String) o).substring(index, ((String) o).length()));
}
return str.toString();

}
return o != null ? String.valueOf(o) : null;
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/squareup/javapoet/Util.java
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ static String characterLiteralWithoutSingleQuotes(char c) {
case '\r': return "\\r"; /* \u000d: carriage return (CR) */
case '\"': return "\""; /* \u0022: double quote (") */
case '\'': return "\\'"; /* \u0027: single quote (') */
case '\\': return "\\\\"; /* \u005c: backslash (\) */
case '\\': return "\\"; /* \u005c: backslash (\) */
default:
return isISOControl(c) ? String.format("\\u%04x", (int) c) : Character.toString(c);
}
Expand Down
Loading