Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Addressed Java 17 compile warnings #769

Merged
merged 1 commit into from
Sep 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 22 additions & 22 deletions src/test/java/org/json/junit/JSONArrayTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -368,26 +368,26 @@ public void getArrayValues() {
"hello".equals(jsonArray.getString(4)));
// doubles
assertTrue("Array double",
new Double(23.45e-4).equals(jsonArray.getDouble(5)));
Double.valueOf(23.45e-4).equals(jsonArray.getDouble(5)));
assertTrue("Array string double",
new Double(23.45).equals(jsonArray.getDouble(6)));
Double.valueOf(23.45).equals(jsonArray.getDouble(6)));
assertTrue("Array double can be float",
new Float(23.45e-4f).equals(jsonArray.getFloat(5)));
Float.valueOf(23.45e-4f).equals(jsonArray.getFloat(5)));
// ints
assertTrue("Array value int",
new Integer(42).equals(jsonArray.getInt(7)));
Integer.valueOf(42).equals(jsonArray.getInt(7)));
assertTrue("Array value string int",
new Integer(43).equals(jsonArray.getInt(8)));
Integer.valueOf(43).equals(jsonArray.getInt(8)));
// nested objects
JSONArray nestedJsonArray = jsonArray.getJSONArray(9);
assertTrue("Array value JSONArray", nestedJsonArray != null);
JSONObject nestedJsonObject = jsonArray.getJSONObject(10);
assertTrue("Array value JSONObject", nestedJsonObject != null);
// longs
assertTrue("Array value long",
new Long(0).equals(jsonArray.getLong(11)));
Long.valueOf(0).equals(jsonArray.getLong(11)));
assertTrue("Array value string long",
new Long(-1).equals(jsonArray.getLong(12)));
Long.valueOf(-1).equals(jsonArray.getLong(12)));

assertTrue("Array value null", jsonArray.isNull(-1));
Util.checkJSONArrayMaps(jsonArray);
Expand Down Expand Up @@ -545,11 +545,11 @@ public void opt() {
Boolean.FALSE.equals(jsonArray.optBooleanObject(-1)));

assertTrue("Array opt double",
new Double(23.45e-4).equals(jsonArray.optDouble(5)));
Double.valueOf(23.45e-4).equals(jsonArray.optDouble(5)));
assertTrue("Array opt double default",
new Double(1).equals(jsonArray.optDouble(0, 1)));
Double.valueOf(1).equals(jsonArray.optDouble(0, 1)));
assertTrue("Array opt double default implicit",
new Double(jsonArray.optDouble(99)).isNaN());
Double.valueOf(jsonArray.optDouble(99)).isNaN());

assertTrue("Array opt double object",
Double.valueOf(23.45e-4).equals(jsonArray.optDoubleObject(5)));
Expand All @@ -559,11 +559,11 @@ public void opt() {
jsonArray.optDoubleObject(99).isNaN());

assertTrue("Array opt float",
new Float(23.45e-4).equals(jsonArray.optFloat(5)));
Float.valueOf(Double.valueOf(23.45e-4).floatValue()).equals(jsonArray.optFloat(5)));
assertTrue("Array opt float default",
new Float(1).equals(jsonArray.optFloat(0, 1)));
Float.valueOf(1).equals(jsonArray.optFloat(0, 1)));
assertTrue("Array opt float default implicit",
new Float(jsonArray.optFloat(99)).isNaN());
Float.valueOf(jsonArray.optFloat(99)).isNaN());

assertTrue("Array opt float object",
Float.valueOf(23.45e-4F).equals(jsonArray.optFloatObject(5)));
Expand All @@ -575,14 +575,14 @@ public void opt() {
assertTrue("Array opt Number",
BigDecimal.valueOf(23.45e-4).equals(jsonArray.optNumber(5)));
assertTrue("Array opt Number default",
new Double(1).equals(jsonArray.optNumber(0, 1d)));
Double.valueOf(1).equals(jsonArray.optNumber(0, 1d)));
assertTrue("Array opt Number default implicit",
new Double(jsonArray.optNumber(99,Double.NaN).doubleValue()).isNaN());
Double.valueOf(jsonArray.optNumber(99,Double.NaN).doubleValue()).isNaN());

assertTrue("Array opt int",
new Integer(42).equals(jsonArray.optInt(7)));
Integer.valueOf(42).equals(jsonArray.optInt(7)));
assertTrue("Array opt int default",
new Integer(-1).equals(jsonArray.optInt(0, -1)));
Integer.valueOf(-1).equals(jsonArray.optInt(0, -1)));
assertTrue("Array opt int default implicit",
0 == jsonArray.optInt(0));

Expand Down Expand Up @@ -1011,12 +1011,12 @@ public void iteratorTest() {
assertTrue("Array double [23.45e-4]",
new BigDecimal("0.002345").equals(it.next()));
assertTrue("Array string double",
new Double(23.45).equals(Double.parseDouble((String)it.next())));
Double.valueOf(23.45).equals(Double.parseDouble((String)it.next())));

assertTrue("Array value int",
new Integer(42).equals(it.next()));
Integer.valueOf(42).equals(it.next()));
assertTrue("Array value string int",
new Integer(43).equals(Integer.parseInt((String)it.next())));
Integer.valueOf(43).equals(Integer.parseInt((String)it.next())));

JSONArray nestedJsonArray = (JSONArray)it.next();
assertTrue("Array value JSONArray", nestedJsonArray != null);
Expand All @@ -1025,9 +1025,9 @@ public void iteratorTest() {
assertTrue("Array value JSONObject", nestedJsonObject != null);

assertTrue("Array value long",
new Long(0).equals(((Number) it.next()).longValue()));
Long.valueOf(0).equals(((Number) it.next()).longValue()));
assertTrue("Array value string long",
new Long(-1).equals(Long.parseLong((String) it.next())));
Long.valueOf(-1).equals(Long.parseLong((String) it.next())));
assertTrue("should be at end of array", !it.hasNext());
Util.checkJSONArraysMaps(new ArrayList<JSONArray>(Arrays.asList(
jsonArray, nestedJsonArray
Expand Down
57 changes: 28 additions & 29 deletions src/test/java/org/json/junit/JSONObjectTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@
import org.json.junit.data.SingletonEnum;
import org.json.junit.data.WeirdList;
import org.junit.Test;
import org.json.junit.Util;

import com.jayway.jsonpath.Configuration;
import com.jayway.jsonpath.JsonPath;
Expand Down Expand Up @@ -304,12 +303,12 @@ public void jsonObjectByNullMap() {
@Test
public void jsonObjectByMap() {
Map<String, Object> map = new HashMap<String, Object>();
map.put("trueKey", new Boolean(true));
map.put("falseKey", new Boolean(false));
map.put("trueKey", Boolean.valueOf(true));
map.put("falseKey", Boolean.valueOf(false));
map.put("stringKey", "hello world!");
map.put("escapeStringKey", "h\be\tllo w\u1234orld!");
map.put("intKey", new Long(42));
map.put("doubleKey", new Double(-23.45e67));
map.put("intKey", Long.valueOf(42));
map.put("doubleKey", Double.valueOf(-23.45e67));
JSONObject jsonObject = new JSONObject(map);

// validate JSON
Expand Down Expand Up @@ -570,13 +569,13 @@ public void jsonObjectByMapWithUnsupportedValues() {
@Test
public void jsonObjectByMapWithNullValue() {
Map<String, Object> map = new HashMap<String, Object>();
map.put("trueKey", new Boolean(true));
map.put("falseKey", new Boolean(false));
map.put("trueKey", Boolean.valueOf(true));
map.put("falseKey", Boolean.valueOf(false));
map.put("stringKey", "hello world!");
map.put("nullKey", null);
map.put("escapeStringKey", "h\be\tllo w\u1234orld!");
map.put("intKey", new Long(42));
map.put("doubleKey", new Double(-23.45e67));
map.put("intKey", Long.valueOf(42));
map.put("doubleKey", Double.valueOf(-23.45e67));
JSONObject jsonObject = new JSONObject(map);

// validate JSON
Expand Down Expand Up @@ -996,7 +995,7 @@ public void stringToValueNumbersTest() {
assertTrue( "0.2 should be a BigDecimal!",
JSONObject.stringToValue( "0.2" ) instanceof BigDecimal );
assertTrue( "Doubles should be BigDecimal, even when incorrectly converting floats!",
JSONObject.stringToValue( new Double( "0.2f" ).toString() ) instanceof BigDecimal );
JSONObject.stringToValue( Double.valueOf( "0.2f" ).toString() ) instanceof BigDecimal );
/**
* This test documents a need for BigDecimal conversion.
*/
Expand All @@ -1006,13 +1005,13 @@ public void stringToValueNumbersTest() {
assertTrue( "1 should be an Integer!",
JSONObject.stringToValue( "1" ) instanceof Integer );
assertTrue( "Integer.MAX_VALUE should still be an Integer!",
JSONObject.stringToValue( new Integer( Integer.MAX_VALUE ).toString() ) instanceof Integer );
JSONObject.stringToValue( Integer.valueOf( Integer.MAX_VALUE ).toString() ) instanceof Integer );
assertTrue( "Large integers should be a Long!",
JSONObject.stringToValue( Long.valueOf(((long)Integer.MAX_VALUE) + 1 ) .toString() ) instanceof Long );
assertTrue( "Long.MAX_VALUE should still be an Integer!",
JSONObject.stringToValue( new Long( Long.MAX_VALUE ).toString() ) instanceof Long );
JSONObject.stringToValue( Long.valueOf( Long.MAX_VALUE ).toString() ) instanceof Long );

String str = new BigInteger( new Long( Long.MAX_VALUE ).toString() ).add( BigInteger.ONE ).toString();
String str = new BigInteger( Long.valueOf( Long.MAX_VALUE ).toString() ).add( BigInteger.ONE ).toString();
assertTrue( "Really large integers currently evaluate to BigInteger",
JSONObject.stringToValue(str).equals(new BigInteger("9223372036854775808")));
}
Expand Down Expand Up @@ -1259,8 +1258,8 @@ public void unexpectedDoubleToIntConversion() {
String key30 = "key30";
String key31 = "key31";
JSONObject jsonObject = new JSONObject();
jsonObject.put(key30, new Double(3.0));
jsonObject.put(key31, new Double(3.1));
jsonObject.put(key30, Double.valueOf(3.0));
jsonObject.put(key31, Double.valueOf(3.1));

assertTrue("3.0 should remain a double",
jsonObject.getDouble(key30) == 3);
Expand Down Expand Up @@ -1713,19 +1712,19 @@ public void jsonObjectIncrement() {
*/
assertFalse("Document unexpected behaviour with explicit type-casting float as double!", (double)0.2f == 0.2d );
assertFalse("Document unexpected behaviour with implicit type-cast!", 0.2f == 0.2d );
Double d1 = new Double( 1.1f );
Double d2 = new Double( "1.1f" );
Double d1 = Double.valueOf( 1.1f );
Double d2 = Double.valueOf( "1.1f" );
assertFalse( "Document implicit type cast from float to double before calling Double(double d) constructor", d1.equals( d2 ) );

assertTrue( "Correctly converting float to double via base10 (string) representation!", new Double( 3.1d ).equals( new Double( new Float( 3.1f ).toString() ) ) );
assertTrue( "Correctly converting float to double via base10 (string) representation!", Double.valueOf( 3.1d ).equals( Double.valueOf( Float.valueOf( 3.1f ).toString() ) ) );

// Pinpointing the not so obvious "buggy" conversion from float to double in JSONObject
JSONObject jo = new JSONObject();
jo.put( "bug", 3.1f ); // will call put( String key, double value ) with implicit and "buggy" type-cast from float to double
assertFalse( "The java-compiler did add some zero bits for you to the mantissa (unexpected, but well documented)", jo.get( "bug" ).equals( new Double( 3.1d ) ) );
assertFalse( "The java-compiler did add some zero bits for you to the mantissa (unexpected, but well documented)", jo.get( "bug" ).equals( Double.valueOf( 3.1d ) ) );

JSONObject inc = new JSONObject();
inc.put( "bug", new Float( 3.1f ) ); // This will put in instance of Float into JSONObject, i.e. call put( String key, Object value )
inc.put( "bug", Float.valueOf( 3.1f ) ); // This will put in instance of Float into JSONObject, i.e. call put( String key, Object value )
assertTrue( "Everything is ok here!", inc.get( "bug" ) instanceof Float );
inc.increment( "bug" ); // after adding 1, increment will call put( String key, double value ) with implicit and "buggy" type-cast from float to double!
// this.put(key, (Float) value + 1);
Expand Down Expand Up @@ -2040,14 +2039,14 @@ public void valueToString() {
assertTrue("map valueToString() incorrect",
jsonObject.toString().equals(JSONObject.valueToString(map)));
Collection<Integer> collection = new ArrayList<Integer>();
collection.add(new Integer(1));
collection.add(new Integer(2));
collection.add(new Integer(3));
collection.add(Integer.valueOf(1));
collection.add(Integer.valueOf(2));
collection.add(Integer.valueOf(3));
assertTrue("collection valueToString() expected: "+
jsonArray.toString()+ " actual: "+
JSONObject.valueToString(collection),
jsonArray.toString().equals(JSONObject.valueToString(collection)));
Integer[] array = { new Integer(1), new Integer(2), new Integer(3) };
Integer[] array = { Integer.valueOf(1), Integer.valueOf(2), Integer.valueOf(3) };
assertTrue("array valueToString() incorrect",
jsonArray.toString().equals(JSONObject.valueToString(array)));
Util.checkJSONObjectMaps(jsonObject);
Expand Down Expand Up @@ -2085,7 +2084,7 @@ public void wrapObject() {
JSONObject.NULL == JSONObject.wrap(null));

// wrap(Integer) returns Integer
Integer in = new Integer(1);
Integer in = Integer.valueOf(1);
assertTrue("Integer wrap() incorrect",
in == JSONObject.wrap(in));

Expand All @@ -2112,9 +2111,9 @@ public void wrapObject() {

// wrap collection returns JSONArray
Collection<Integer> collection = new ArrayList<Integer>();
collection.add(new Integer(1));
collection.add(new Integer(2));
collection.add(new Integer(3));
collection.add(Integer.valueOf(1));
collection.add(Integer.valueOf(2));
collection.add(Integer.valueOf(3));
JSONArray jsonArray = (JSONArray) (JSONObject.wrap(collection));

// validate JSON
Expand All @@ -2125,7 +2124,7 @@ public void wrapObject() {
assertTrue("expected 3", Integer.valueOf(3).equals(jsonArray.query("/2")));

// wrap Array returns JSONArray
Integer[] array = { new Integer(1), new Integer(2), new Integer(3) };
Integer[] array = { Integer.valueOf(1), Integer.valueOf(2), Integer.valueOf(3) };
JSONArray integerArrayJsonArray = (JSONArray)(JSONObject.wrap(array));

// validate JSON
Expand Down