Skip to content

Commit ecaa571

Browse files
committed
Consolidate location and path rendering for exceptions.
1 parent 0333764 commit ecaa571

File tree

1 file changed

+27
-47
lines changed

1 file changed

+27
-47
lines changed

gson/src/main/java/com/google/gson/stream/JsonReader.java

Lines changed: 27 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -347,8 +347,7 @@ public void beginArray() throws IOException {
347347
pathIndices[stackSize - 1] = 0;
348348
peeked = PEEKED_NONE;
349349
} else {
350-
throw new IllegalStateException("Expected BEGIN_ARRAY but was " + peek()
351-
+ " at line " + getLineNumber() + " column " + getColumnNumber() + " path " + getPath());
350+
throw new IllegalStateException("Expected BEGIN_ARRAY but was " + peek() + locationString());
352351
}
353352
}
354353

@@ -366,8 +365,7 @@ public void endArray() throws IOException {
366365
pathIndices[stackSize - 1]++;
367366
peeked = PEEKED_NONE;
368367
} else {
369-
throw new IllegalStateException("Expected END_ARRAY but was " + peek()
370-
+ " at line " + getLineNumber() + " column " + getColumnNumber() + " path " + getPath());
368+
throw new IllegalStateException("Expected END_ARRAY but was " + peek() + locationString());
371369
}
372370
}
373371

@@ -384,8 +382,7 @@ public void beginObject() throws IOException {
384382
push(JsonScope.EMPTY_OBJECT);
385383
peeked = PEEKED_NONE;
386384
} else {
387-
throw new IllegalStateException("Expected BEGIN_OBJECT but was " + peek()
388-
+ " at line " + getLineNumber() + " column " + getColumnNumber() + " path " + getPath());
385+
throw new IllegalStateException("Expected BEGIN_OBJECT but was " + peek() + locationString());
389386
}
390387
}
391388

@@ -404,8 +401,7 @@ public void endObject() throws IOException {
404401
pathIndices[stackSize - 1]++;
405402
peeked = PEEKED_NONE;
406403
} else {
407-
throw new IllegalStateException("Expected END_OBJECT but was " + peek()
408-
+ " at line " + getLineNumber() + " column " + getColumnNumber() + " path " + getPath());
404+
throw new IllegalStateException("Expected END_OBJECT but was " + peek() + locationString());
409405
}
410406
}
411407

@@ -790,8 +786,7 @@ public String nextName() throws IOException {
790786
} else if (p == PEEKED_DOUBLE_QUOTED_NAME) {
791787
result = nextQuotedValue('"');
792788
} else {
793-
throw new IllegalStateException("Expected a name but was " + peek()
794-
+ " at line " + getLineNumber() + " column " + getColumnNumber() + " path " + getPath());
789+
throw new IllegalStateException("Expected a name but was " + peek() + locationString());
795790
}
796791
peeked = PEEKED_NONE;
797792
pathNames[stackSize - 1] = result;
@@ -827,8 +822,7 @@ public String nextString() throws IOException {
827822
result = new String(buffer, pos, peekedNumberLength);
828823
pos += peekedNumberLength;
829824
} else {
830-
throw new IllegalStateException("Expected a string but was " + peek()
831-
+ " at line " + getLineNumber() + " column " + getColumnNumber() + " path " + getPath());
825+
throw new IllegalStateException("Expected a string but was " + peek() + locationString());
832826
}
833827
peeked = PEEKED_NONE;
834828
pathIndices[stackSize - 1]++;
@@ -856,8 +850,7 @@ public boolean nextBoolean() throws IOException {
856850
pathIndices[stackSize - 1]++;
857851
return false;
858852
}
859-
throw new IllegalStateException("Expected a boolean but was " + peek()
860-
+ " at line " + getLineNumber() + " column " + getColumnNumber() + " path " + getPath());
853+
throw new IllegalStateException("Expected a boolean but was " + peek() + locationString());
861854
}
862855

863856
/**
@@ -876,8 +869,7 @@ public void nextNull() throws IOException {
876869
peeked = PEEKED_NONE;
877870
pathIndices[stackSize - 1]++;
878871
} else {
879-
throw new IllegalStateException("Expected null but was " + peek()
880-
+ " at line " + getLineNumber() + " column " + getColumnNumber() + " path " + getPath());
872+
throw new IllegalStateException("Expected null but was " + peek() + locationString());
881873
}
882874
}
883875

@@ -910,15 +902,14 @@ public double nextDouble() throws IOException {
910902
} else if (p == PEEKED_UNQUOTED) {
911903
peekedString = nextUnquotedValue();
912904
} else if (p != PEEKED_BUFFERED) {
913-
throw new IllegalStateException("Expected a double but was " + peek()
914-
+ " at line " + getLineNumber() + " column " + getColumnNumber() + " path " + getPath());
905+
throw new IllegalStateException("Expected a double but was " + peek() + locationString());
915906
}
916907

917908
peeked = PEEKED_BUFFERED;
918909
double result = Double.parseDouble(peekedString); // don't catch this NumberFormatException.
919910
if (!lenient && (Double.isNaN(result) || Double.isInfinite(result))) {
920-
throw new MalformedJsonException("JSON forbids NaN and infinities: " + result
921-
+ " at line " + getLineNumber() + " column " + getColumnNumber() + " path " + getPath());
911+
throw new MalformedJsonException(
912+
"JSON forbids NaN and infinities: " + result + locationString());
922913
}
923914
peekedString = null;
924915
peeked = PEEKED_NONE;
@@ -966,16 +957,14 @@ public long nextLong() throws IOException {
966957
// Fall back to parse as a double below.
967958
}
968959
} else {
969-
throw new IllegalStateException("Expected a long but was " + peek()
970-
+ " at line " + getLineNumber() + " column " + getColumnNumber() + " path " + getPath());
960+
throw new IllegalStateException("Expected a long but was " + peek() + locationString());
971961
}
972962

973963
peeked = PEEKED_BUFFERED;
974964
double asDouble = Double.parseDouble(peekedString); // don't catch this NumberFormatException.
975965
long result = (long) asDouble;
976966
if (result != asDouble) { // Make sure no precision was lost casting to 'long'.
977-
throw new NumberFormatException("Expected a long but was " + peekedString
978-
+ " at line " + getLineNumber() + " column " + getColumnNumber() + " path " + getPath());
967+
throw new NumberFormatException("Expected a long but was " + peekedString + locationString());
979968
}
980969
peekedString = null;
981970
peeked = PEEKED_NONE;
@@ -1172,8 +1161,7 @@ public int nextInt() throws IOException {
11721161
if (p == PEEKED_LONG) {
11731162
result = (int) peekedLong;
11741163
if (peekedLong != result) { // Make sure no precision was lost casting to 'int'.
1175-
throw new NumberFormatException("Expected an int but was " + peekedLong
1176-
+ " at line " + getLineNumber() + " column " + getColumnNumber() + " path " + getPath());
1164+
throw new NumberFormatException("Expected an int but was " + peekedLong + locationString());
11771165
}
11781166
peeked = PEEKED_NONE;
11791167
pathIndices[stackSize - 1]++;
@@ -1198,16 +1186,14 @@ public int nextInt() throws IOException {
11981186
// Fall back to parse as a double below.
11991187
}
12001188
} else {
1201-
throw new IllegalStateException("Expected an int but was " + peek()
1202-
+ " at line " + getLineNumber() + " column " + getColumnNumber() + " path " + getPath());
1189+
throw new IllegalStateException("Expected an int but was " + peek() + locationString());
12031190
}
12041191

12051192
peeked = PEEKED_BUFFERED;
12061193
double asDouble = Double.parseDouble(peekedString); // don't catch this NumberFormatException.
12071194
result = (int) asDouble;
12081195
if (result != asDouble) { // Make sure no precision was lost casting to 'int'.
1209-
throw new NumberFormatException("Expected an int but was " + peekedString
1210-
+ " at line " + getLineNumber() + " column " + getColumnNumber() + " path " + getPath());
1196+
throw new NumberFormatException("Expected an int but was " + peekedString + locationString());
12111197
}
12121198
peekedString = null;
12131199
peeked = PEEKED_NONE;
@@ -1315,14 +1301,6 @@ private boolean fillBuffer(int minimum) throws IOException {
13151301
return false;
13161302
}
13171303

1318-
int getLineNumber() {
1319-
return lineNumber + 1;
1320-
}
1321-
1322-
int getColumnNumber() {
1323-
return pos - lineStart + 1;
1324-
}
1325-
13261304
/**
13271305
* Returns the next character in the stream that is neither whitespace nor a
13281306
* part of a comment. When this returns, the returned character is always at
@@ -1412,8 +1390,7 @@ private int nextNonWhitespace(boolean throwOnEof) throws IOException {
14121390
}
14131391
}
14141392
if (throwOnEof) {
1415-
throw new EOFException("End of input"
1416-
+ " at line " + getLineNumber() + " column " + getColumnNumber());
1393+
throw new EOFException("End of input" + locationString());
14171394
} else {
14181395
return -1;
14191396
}
@@ -1465,8 +1442,13 @@ private boolean skipTo(String toFind) throws IOException {
14651442
}
14661443

14671444
@Override public String toString() {
1468-
return getClass().getSimpleName()
1469-
+ " at line " + getLineNumber() + " column " + getColumnNumber();
1445+
return getClass().getSimpleName() + locationString();
1446+
}
1447+
1448+
private String locationString() {
1449+
int line = lineNumber + 1;
1450+
int column = pos - lineStart + 1;
1451+
return " at line " + line + " column " + column + " path " + getPath();
14701452
}
14711453

14721454
/**
@@ -1571,8 +1553,7 @@ private char readEscapeCharacter() throws IOException {
15711553
* with this reader's content.
15721554
*/
15731555
private IOException syntaxError(String message) throws IOException {
1574-
throw new MalformedJsonException(message
1575-
+ " at line " + getLineNumber() + " column " + getColumnNumber() + " path " + getPath());
1556+
throw new MalformedJsonException(message + locationString());
15761557
}
15771558

15781559
/**
@@ -1615,9 +1596,8 @@ private void consumeNonExecutePrefix() throws IOException {
16151596
} else if (p == PEEKED_UNQUOTED_NAME) {
16161597
reader.peeked = PEEKED_UNQUOTED;
16171598
} else {
1618-
throw new IllegalStateException("Expected a name but was " + reader.peek() + " "
1619-
+ " at line " + reader.getLineNumber() + " column " + reader.getColumnNumber()
1620-
+ " path " + reader.getPath());
1599+
throw new IllegalStateException(
1600+
"Expected a name but was " + reader.peek() + reader.locationString());
16211601
}
16221602
}
16231603
};

0 commit comments

Comments
 (0)