IndexOutOfBoundsException - Wrong formatted content when dealing with latex String
Nobody has claimed this yet.
Assessment
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Newbie friendliness
- 55/100
Research direction
Reproduce the failure with the supplied Java class, then inspect StringWrapper#applyReplacements and FormatterException#formatDiagnostics. Check the generated source and diagnostic line handling against the original input. Done means formatting no longer produces uncompilable Java and the formatter reports the error without IndexOutOfBoundsException.
Written by the indexing model from the issue text.
Description
Give this class
package question;
class T {
private static final String JSON =
"{\n"
+ " \"a\": \"AAAAA AAA AAA AAA AA AAA AA AAAAA AA AAAAA AAA AAAA AAAA AA AAAA AAAA AAAAA AAAAA AAAAA AAAAA AAAAAA AAAAA AAAAAA AAAAA AAA at $-15.4{^{\\\\,\\\\circ}\\\\text{C}}$ BBB BBBBB BB BBB $42.7{^{\\\\,\\\\circ}\\\\text{C}}$?\\n\\n(CC CC CCC CC CCCCC CCC CCC CCC CCC CCC CC = $1.95\\\\;{\\\\text{J}/\\\\text{g}\\\\cdot{^{\\\\circ}\\\\text{C}}}$; DDD DDD DDDD DDDD DDDDD DDDD DDD DD DDDD = $1.18\\\\;{\\\\text{J}/\\\\text{g}\\\\cdot{^{\\\\circ}\\\\text{C}}}$C; $\\\\Delta H_\\\\text{fus}$ = 5.72 kJ/mol; normal freezing point, $T_f$ = $28.3{^{\\\\,\\\\circ}\\\\text{C}}$)\",\n"
+ " \"b\": \"*EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EEE.*\\n\\nFF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF F $-15.4^\\\\circ\\\\text C$ to $28.3^\\\\circ\\\\text C$.\\n\\\\[q=mc\\\\Delta T=(10.1\\\\;\\\\mathrm{g})(1.95\\\\;\\\\mathrm{J/g\\\\cdot °C})(28.3\\\\;\\\\mathrm{ °C}-(-15.4)\\\\;\\\\mathrm{ °C})=860.6715\\\\;\\\\mathrm{J} \\\\]\\n\\nGG GG GG GG GG GG GG GG GG GG GG GG GG GG GG GG GG GG GG GG GG GG GG GG GG GG GG GG GG GG GG GG GG GG GG GG GG GG GG GG GG GG GG GG GG GG GG GGGGGGGGGGGGGGGGGGGGG.\\n\\\\[q=nH_{fus}=(\\\\frac{10.1\\\\;\\\\mathrm{g}}{67.44\\\\;\\\\mathrm{g/mol}})(5720\\\\;\\\\mathrm{J})=856.6429\\\\;\\\\mathrm{J}\\\\]\\n\\nHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH $28.3^\\\\circ\\\\text C$ to $42.7^\\\\circ\\\\text C$.\\n\\\\[q=mc\\\\Delta T=(10.1\\\\;\\\\mathrm{g})(1.18\\\\;\\\\mathrm{J/g\\\\cdot °C})(42.7\\\\;\\\\mathrm{ °C}-28.3\\\\;\\\\mathrm{ °C})=171.6192\\\\;\\\\mathrm{J}\\\\]\\n\\nIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII.\\n\\\\[860.6715\\\\;\\\\mathrm{J}+856.6429\\\\;\\\\mathrm{J}+171.6192\\\\;\\\\mathrm{J}=1888.9336\\\\;\\\\mathrm{J}=1.89\\\\;\\\\mathrm{kJ}\\\\]\",\n"
+ "}";
private static final String JSON2 =
"{\n"
+ " \"a\": \"AAAAA AAA AAA AAA AA AAA AA AAAAA AA AAAAA AAA AAAA AAAA AA AAAA AAAA AAAAA AAAAA AAAAA AAAAA AAAAAA AAAAA AAAAAA AAAAA AAA at $-15.4{^{\\\\,\\\\circ}\\\\text{C}}$ BBB BBBBB BB BBB $42.7{^{\\\\,\\\\circ}\\\\text{C}}$?\\n\\n(CC CC CCC CC CCCCC CCC CCC CCC CCC CCC CC = $1.95\\\\;{\\\\text{J}/\\\\text{g}\\\\cdot{^{\\\\circ}\\\\text{C}}}$; DDD DDD DDDD DDDD DDDDD DDDD DDD DD DDDD = $1.18\\\\;{\\\\text{J}/\\\\text{g}\\\\cdot{^{\\\\circ}\\\\text{C}}}$C; $\\\\Delta H_\\\\text{fus}$ = 5.72 kJ/mol; normal freezing point, $T_f$ = $28.3{^{\\\\,\\\\circ}\\\\text{C}}$)\",\n"
+ " \"b\": \"*EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EEE.*\\n\\nFF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF F $-15.4^\\\\circ\\\\text C$ to $28.3^\\\\circ\\\\text C$.\\n\\\\[q=mc\\\\Delta T=(10.1\\\\;\\\\mathrm{g})(1.95\\\\;\\\\mathrm{J/g\\\\cdot °C})(28.3\\\\;\\\\mathrm{ °C}-(-15.4)\\\\;\\\\mathrm{ °C})=860.6715\\\\;\\\\mathrm{J} \\\\]\\n\\nGG GG GG GG GG GG GG GG GG GG GG GG GG GG GG GG GG GG GG GG GG GG GG GG GG GG GG GG GG GG GG GG GG GG GG GG GG GG GG GG GG GG GG GG GG GG GG GGGGGGGGGGGGGGGGGGGGG.\\n\\\\[q=nH_{fus}=(\\\\frac{10.1\\\\;\\\\mathrm{g}}{67.44\\\\;\\\\mathrm{g/mol}})(5720\\\\;\\\\mathrm{J})=856.6429\\\\;\\\\mathrm{J}\\\\]\\n\\nHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH $28.3^\\\\circ\\\\text C$ to $42.7^\\\\circ\\\\text C$.\\n\\\\[q=mc\\\\Delta T=(10.1\\\\;\\\\mathrm{g})(1.18\\\\;\\\\mathrm{J/g\\\\cdot °C})(42.7\\\\;\\\\mathrm{ °C}-28.3\\\\;\\\\mathrm{ °C})=171.6192\\\\;\\\\mathrm{J}\\\\]\\n\\nIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII.\\n\\\\[860.6715\\\\;\\\\mathrm{J}+856.6429\\\\;\\\\mathrm{J}+171.6192\\\\;\\\\mathrm{J}=1888.9336\\\\;\\\\mathrm{J}=1.89\\\\;\\\\mathrm{kJ}\\\\]\",\n"
+ "}";
public static void main(String[] args) {
System.out.println(JSON);
}
}
I ran the main method to ensure it is valid java source code
But when trying to format it using GJF, I got
Exception in thread "main" java.lang.IndexOutOfBoundsException: Index 42 out of bounds for length 22
at java.base/jdk.internal.util.Preconditions.outOfBounds(Preconditions.java:100)
at java.base/jdk.internal.util.Preconditions.outOfBoundsCheckIndex(Preconditions.java:106)
at java.base/jdk.internal.util.Preconditions.checkIndex(Preconditions.java:302)
at java.base/java.util.Objects.checkIndex(Objects.java:385)
at java.base/java.util.ArrayList.get(ArrayList.java:427)
at java.base/java.util.Collections$UnmodifiableList.get(Collections.java:1502)
at com.google.googlejavaformat.java.FormatterException.formatDiagnostics(FormatterException.java:70)
at com.google.googlejavaformat.java.Main.formatFiles(Main.java:177)
at com.google.googlejavaformat.java.Main.format(Main.java:128)
at com.google.googlejavaformat.java.Main.main(Main.java:91)
at com.google.googlejavaformat.java.Main.main(Main.java:81)
at com.google.googlejavaformat.java.Main.main(Main.java:70)
Digging into the GJF, I found 2 issues:
- The
com.google.googlejavaformat.java.StringWrapper#applyReplacementsgive wrong result:
package question;
class T {
private static final String JSON =
"{\n"
+ " \"a\": \"AAAAA AAA AAA AAA AA AAA AA AAAAA AA AAAAA AAA AAAA AAAA AA AAAA AAAA AAAAA"
+ " AAAAA AAAAA AAAAA AAAAAA AAAAA AAAAAA AAAAA AAA at $-15.4{^{\\\\,\\\\circ}\\\"
+ "\text{C}}$ BBB BBBBB BB BBB $42.7{^{\\\\,\\\\circ}\\\\text{C}}$?\\n"
+ "\\n"
+ "(CC CC CCC CC CCCCC CCC CCC CCC CCC CCC CC = $1.95\\\\;{\\\\text{J}/\\\"
+ "\text{g}\\\\cdot{^{\\\\circ}\\\\text{C}}}$; DDD DDD DDDD DDDD DDDDD DDDD DDD DD DDDD ="
+ " $1.18\\\\;{\\\\text{J}/\\\\text{g}\\\\cdot{^{\\\\circ}\\\\text{C}}}$C; $\\\\Delta"
+ " H_\\\\text{fus}$ = 5.72 kJ/mol; normal freezing point, $T_f$ ="
+ " $28.3{^{\\\\,\\\\circ}\\\\text{C}}$)\",\n"
+ " \"b\": \"*EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE"
+ " EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE"
+ " EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EEE.*\\n"
+ "\\n"
+ "FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF F"
+ " $-15.4^\\\\circ\\\\text C$ to $28.3^\\\\circ\\\\text C$.\\n"
+ "\\\\[q=mc\\\\Delta T=(10.1\\\\;\\\\mathrm{g})(1.95\\\\;\\\\mathrm{J/g\\\\cdot"
+ " °C})(28.3\\\\;\\\\mathrm{ °C}-(-15.4)\\\\;\\\\mathrm{ °C})=860.6715\\\\;\\\\mathrm{J}"
+ " \\\\]\\n"
+ "\\n"
+ "GG GG GG GG GG GG GG GG GG GG GG GG GG GG GG GG GG GG GG GG GG GG GG GG GG GG GG GG GG"
+ " GG GG GG GG GG GG GG GG GG GG GG GG GG GG GG GG GG GG GGGGGGGGGGGGGGGGGGGGG.\\n"
+ "\\\\[q=nH_{fus}=(\\\\frac{10.1\\\\;\\\\mathrm{g}}{67.44\\\\;\\\\mathrm{g/mol}})(5720\\\\;\\\\mathrm{J})=856.6429\\\\;\\\\mathrm{J}\\\\]\\n"
+ "\\n"
+ "HHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH $28.3^\\\\circ\\\\text C$ to"
+ " $42.7^\\\\circ\\\\text C$.\\n"
+ "\\\\[q=mc\\\\Delta T=(10.1\\\\;\\\\mathrm{g})(1.18\\\\;\\\\mathrm{J/g\\\\cdot"
+ " °C})(42.7\\\\;\\\\mathrm{ °C}-28.3\\\\;\\\\mathrm{"
+ " °C})=171.6192\\\\;\\\\mathrm{J}\\\\]\\n"
+ "\\n"
+ "IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII.\\n"
+ "\\\\[860.6715\\\\;\\\\mathrm{J}+856.6429\\\\;\\\\mathrm{J}+171.6192\\\\;\\\\mathrm{J}=1888.9336\\\\;\\\\mathrm{J}=1.89\\\\;\\\\mathrm{kJ}\\\\]\",\n"
+ "}";
private static final String JSON2 =
"{\n"
+ " \"a\": \"AAAAA AAA AAA AAA AA AAA AA AAAAA AA AAAAA AAA AAAA AAAA AA AAAA AAAA AAAAA"
+ " AAAAA AAAAA AAAAA AAAAAA AAAAA AAAAAA AAAAA AAA at $-15.4{^{\\\\,\\\\circ}\\\"
+ "\text{C}}$ BBB BBBBB BB BBB $42.7{^{\\\\,\\\\circ}\\\\text{C}}$?\\n"
+ "\\n"
+ "(CC CC CCC CC CCCCC CCC CCC CCC CCC CCC CC = $1.95\\\\;{\\\\text{J}/\\\"
+ "\text{g}\\\\cdot{^{\\\\circ}\\\\text{C}}}$; DDD DDD DDDD DDDD DDDDD DDDD DDD DD DDDD ="
+ " $1.18\\\\;{\\\\text{J}/\\\\text{g}\\\\cdot{^{\\\\circ}\\\\text{C}}}$C; $\\\\Delta"
+ " H_\\\\text{fus}$ = 5.72 kJ/mol; normal freezing point, $T_f$ ="
+ " $28.3{^{\\\\,\\\\circ}\\\\text{C}}$)\",\n"
+ " \"b\": \"*EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE"
+ " EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE"
+ " EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EEE.*\\n"
+ "\\n"
+ "FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF F"
+ " $-15.4^\\\\circ\\\\text C$ to $28.3^\\\\circ\\\\text C$.\\n"
+ "\\\\[q=mc\\\\Delta T=(10.1\\\\;\\\\mathrm{g})(1.95\\\\;\\\\mathrm{J/g\\\\cdot"
+ " °C})(28.3\\\\;\\\\mathrm{ °C}-(-15.4)\\\\;\\\\mathrm{ °C})=860.6715\\\\;\\\\mathrm{J}"
+ " \\\\]\\n"
+ "\\n"
+ "GG GG GG GG GG GG GG GG GG GG GG GG GG GG GG GG GG GG GG GG GG GG GG GG GG GG GG GG GG"
+ " GG GG GG GG GG GG GG GG GG GG GG GG GG GG GG GG GG GG GGGGGGGGGGGGGGGGGGGGG.\\n"
+ "\\\\[q=nH_{fus}=(\\\\frac{10.1\\\\;\\\\mathrm{g}}{67.44\\\\;\\\\mathrm{g/mol}})(5720\\\\;\\\\mathrm{J})=856.6429\\\\;\\\\mathrm{J}\\\\]\\n"
+ "\\n"
+ "HHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH $28.3^\\\\circ\\\\text C$ to"
+ " $42.7^\\\\circ\\\\text C$.\\n"
+ "\\\\[q=mc\\\\Delta T=(10.1\\\\;\\\\mathrm{g})(1.18\\\\;\\\\mathrm{J/g\\\\cdot"
+ " °C})(42.7\\\\;\\\\mathrm{ °C}-28.3\\\\;\\\\mathrm{"
+ " °C})=171.6192\\\\;\\\\mathrm{J}\\\\]\\n"
+ "\\n"
+ "IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII.\\n"
+ "\\\\[860.6715\\\\;\\\\mathrm{J}+856.6429\\\\;\\\\mathrm{J}+171.6192\\\\;\\\\mathrm{J}=1888.9336\\\\;\\\\mathrm{J}=1.89\\\\;\\\\mathrm{kJ}\\\\]\",\n"
+ "}";
public static void main(String[] args) {
System.out.println(JSON);
}
}
This result is completely not compilable
com.google.googlejavaformat.java.FormatterException#formatDiagnosticsusing wrong param to format the diagnostics: It uses the original java content (22 lines intotal) instead of formatted java content (79 lines in total). That is why we getting IndexOutOfBoundsException
- Dominant language
- Java
- Stars
- 6.2k
- Forks
- 936
- Avg merge
- 6m
- Merged PRs (30d)
- 3
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from google/google-java-format
-
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
google/google-java-format#1094 · 1 comment ·
-
Difficulty 5/5 Over a week Newbie friendliness 15/100
google/google-java-format#1450 · 1 reaction ·
-
Difficulty 3/5 1-2 days Newbie friendliness 62/100
google/google-java-format#1436 · 2 comments ·
-
Difficulty 3/5 1-2 days Newbie friendliness 48/100
google/google-java-format#1428 · 3 comments ·
-
Eclipse
Difficulty 3/5 1-2 days Newbie friendliness 55/100
google/google-java-format#1417 ·
All issues in google/google-java-format
Similar issues
-
bug untriaged
Difficulty 2/5 1-3 hours Newbie friendliness 84/100
opensearch-project/ml-commons#5094 ·
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 85/100
-
emitter:client:csharp feature
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
-
affects/8.10 affects/8.9 component/clients kind/bug likelihood/mid severity/mid
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
-
Two open-case totals on one screen: the Programs tile says 15,858 and the nav badge says 15,868 Openbug frontend maui-pilot
Difficulty 2/5 1-3 hours Newbie friendliness 72/100