Open mP1 opened 4 years ago
I faced the same problem. I used the code snippet below to solve it. Java supports string literals of size 65534. So basically split the string by length 65534 and initialise the string with string builder using CodeBlock's formatting.
String longString = "long string";
String[] argsArray = longString.split("(?<=\\G.{65534})");
StringBuilder stringBuilderString = new StringBuilder().append("new StringBuilder()");
for (String s : argsArray) {
stringBuilderString.append(".append($S)");
}
stringBuilderString.append(".toString()");
FieldSpec stringBuilder = FieldSpec.builder(String.class, "variableName", Modifier.PUBLIC, Modifier.STATIC, Modifier.FINAL)
.initializer(CodeBlock.of(stringBuilderString.toString(), argsArray))
.build();
@amalrajmani
Your code will fail if the long String being split contains a unicode escape (aka backslash u hex hex hex hex) or a backslash escape sequence (like backslash tee for tab) that starts at 65534 -1.
Our group are interested in this issue, we will try to fix it. ---- SE_Sustech
If you need strings that long, is compiled code the right format? Loading a resource is probably a more appropriate option.
If you really need those strings, this is a really niche feature request and the perfect thing for another library to supply.
On Thu, Mar 11, 2021 at 5:59 PM CharlotteE67 @.***> wrote:
Our group are interested in this issue, we will try to fix it. ---- SE_Sustech
— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/square/javapoet/issues/776#issuecomment-796841474, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAGBRXK2OGHR7CB6OE7QFQ3TDDSEPANCNFSM4M522IEQ .
GWT/J2Cl are two examples where this is might be the only or best choice out of many others.
https://github.com/square/javapoet/blob/master/src/main/java/com/squareup/javapoet/Util.java
If attempting to "write" a string literal greater than 64k, the resulting source product that will fail to compile. Javac will of course complain that said string literal is too longer than 64k.
My suggestion would be to split the string into
new StringBuilder()
withappend
statements each holding a string literal that isnt over the limit.My own mini tool that accomplishes this:
https://github.com/mP1/j2cl-locale/blob/master/src/main/java/walkingkooka/j2cl/locale/annotationprocessor/LocaleAwareAnnotationProcessor.java#L171