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

Util. stringLiteralWithDoubleQuotes should split strings > 64k #776

Open
mP1 opened this issue May 11, 2020 · 5 comments
Open

Util. stringLiteralWithDoubleQuotes should split strings > 64k #776

mP1 opened this issue May 11, 2020 · 5 comments

Comments

@mP1
Copy link

mP1 commented May 11, 2020

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() with append 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

@amalrajmani
Copy link

amalrajmani commented Jul 29, 2020

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();

@mP1
Copy link
Author

mP1 commented Jul 29, 2020

@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.

@CharlotteE67
Copy link

Our group are interested in this issue, we will try to fix it.
---- SE_Sustech

@ronshapiro
Copy link
Contributor

ronshapiro commented Mar 12, 2021 via email

@mP1
Copy link
Author

mP1 commented Mar 12, 2021

GWT/J2Cl are two examples where this is might be the only or best choice out of many others.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants