Skip to content

Commit d5e744c

Browse files
authored
Merge pull request #1028 from Simulant87/fix-sonarqube-reliability-issues
Refactoring: Fix sonarqube reliability issues
2 parents e0c4086 + 8cbb4d5 commit d5e744c

File tree

2 files changed

+16
-10
lines changed

2 files changed

+16
-10
lines changed

src/main/java/org/json/XML.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import java.math.BigDecimal;
1010
import java.math.BigInteger;
1111
import java.util.Iterator;
12+
import java.util.NoSuchElementException;
1213

1314
/**
1415
* This provides static methods to convert an XML text into a JSONObject, and to
@@ -80,7 +81,7 @@ private static Iterable<Integer> codePointIterator(final String string) {
8081
public Iterator<Integer> iterator() {
8182
return new Iterator<Integer>() {
8283
private int nextIndex = 0;
83-
private int length = string.length();
84+
private final int length = string.length();
8485

8586
@Override
8687
public boolean hasNext() {
@@ -89,6 +90,9 @@ public boolean hasNext() {
8990

9091
@Override
9192
public Integer next() {
93+
if (!hasNext()) {
94+
throw new NoSuchElementException();
95+
}
9296
int result = string.codePointAt(this.nextIndex);
9397
this.nextIndex += Character.charCount(result);
9498
return result;

src/test/java/org/json/junit/JSONObjectTest.java

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3117,12 +3117,13 @@ public void testJSONWriterException() {
31173117

31183118
// test a more complex object
31193119
writer = new StringWriter();
3120-
try {
3121-
new JSONObject()
3120+
3121+
JSONObject object = new JSONObject()
31223122
.put("somethingElse", "a value")
31233123
.put("someKey", new JSONArray()
3124-
.put(new JSONObject().put("key1", new BrokenToString())))
3125-
.write(writer).toString();
3124+
.put(new JSONObject().put("key1", new BrokenToString())));
3125+
try {
3126+
object.write(writer).toString();
31263127
fail("Expected an exception, got a String value");
31273128
} catch (JSONException e) {
31283129
assertEquals("Unable to write JSONObject value for key: someKey", e.getMessage());
@@ -3133,17 +3134,18 @@ public void testJSONWriterException() {
31333134
writer.close();
31343135
} catch (Exception e) {}
31353136
}
3136-
3137+
31373138
// test a more slightly complex object
31383139
writer = new StringWriter();
3139-
try {
3140-
new JSONObject()
3140+
3141+
object = new JSONObject()
31413142
.put("somethingElse", "a value")
31423143
.put("someKey", new JSONArray()
31433144
.put(new JSONObject().put("key1", new BrokenToString()))
31443145
.put(12345)
3145-
)
3146-
.write(writer).toString();
3146+
);
3147+
try {
3148+
object.write(writer).toString();
31473149
fail("Expected an exception, got a String value");
31483150
} catch (JSONException e) {
31493151
assertEquals("Unable to write JSONObject value for key: someKey", e.getMessage());

0 commit comments

Comments
 (0)