Skip to content

Commit

Permalink
Use SuppressWarnings for unchecked conversion warnings (#1121)
Browse files Browse the repository at this point in the history
Switch to generic SuppressWarnings instead of IntelliJ-specific comment for unchecked warnings. This will minimize
errors shown in both IntellIJ and VSCode
  • Loading branch information
sleberknight authored Apr 25, 2024
1 parent f435fa8 commit 4b3cd77
Show file tree
Hide file tree
Showing 6 changed files with 9 additions and 8 deletions.
6 changes: 3 additions & 3 deletions src/main/java/org/kiwiproject/json/JsonHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,7 @@ public String toJsonIgnoringPaths(@Nullable Object object, String... ignoredPath
* @param <T> the object type
* @return a new instance of the given type, or {@code null} if the given input JSON is blank
*/
@SuppressWarnings("unchecked")
public <T> T toObject(@Nullable String json, Class<T> targetClass) {
if (isBlank(json)) {
return null;
Expand All @@ -350,7 +351,6 @@ public <T> T toObject(@Nullable String json, Class<T> targetClass) {
if (nonNull(e.getTargetType())
&& isNullOrEmpty(e.getPath())
&& e.getTargetType().isAssignableFrom(String.class)) {
//noinspection unchecked
return (T) json;
} else {
throw new RuntimeJsonException(e);
Expand Down Expand Up @@ -581,13 +581,13 @@ public <T, R> R copyIgnoringPaths(T object, Class<R> targetClass, String... igno
* @return a new instance of the target type
* @see ObjectMapper#convertValue(Object, Class)
*/
@SuppressWarnings("unchecked")
public <T> T convert(Object fromObject, Class<T> targetType) {
if (isNull(fromObject)) {
return null;
}

if (targetType.isAssignableFrom(String.class)) {
//noinspection unchecked
return (T) toJson(fromObject);
}

Expand Down Expand Up @@ -947,6 +947,7 @@ public enum MergeOption {
* @return a new instance of type T
* @see MergeOption
*/
@SuppressWarnings("unchecked")
public <T> T mergeObjects(T originalObject, Object updateObject, MergeOption... mergeOptions) {
var originalObjJson = toJson(originalObject);
var updateObjJson = toJson(updateObject);
Expand All @@ -956,7 +957,6 @@ public <T> T mergeObjects(T originalObject, Object updateObject, MergeOption...

JsonNode updatedNode = mergeNodes(originalNode, updateNode, mergeOptions);

//noinspection unchecked
return (T) toObject(updatedNode.toString(), originalObject.getClass());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ class InternalKiwiValidators {
* it becomes unreadable. Interestingly, neither IntelliJ not Sonar is complaining...maybe we don't have the
* appropriate rules enabled. Suggestions for improvement welcome!
*/
@SuppressWarnings("unchecked")
static <T> Comparable<T> toComparableOrNull(String compareValue, Comparable<T> value) {
if (isBlank(compareValue) || isNull(value)) {
return null;
Expand Down Expand Up @@ -88,7 +89,6 @@ static <T> Comparable<T> toComparableOrNull(String compareValue, Comparable<T> v
throw new IllegalArgumentException(message);
}

//noinspection unchecked
return (Comparable<T>) typedValue;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,14 @@ private static boolean hasMinOrMax(Range constraintAnnotation) {
return isNotBlank(constraintAnnotation.min()) || isNotBlank(constraintAnnotation.max());
}

@SuppressWarnings("unchecked")
@Override
public boolean isValid(Object value, ConstraintValidatorContext context) {
Validity validity;
try {
validity = checkNull(value, context);

if (validity == Validity.CONTINUE) {
// noinspection unchecked
validity = checkMinMax((Comparable<Object>) value, context);
}
} catch (Exception e) {
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/org/kiwiproject/jaxrs/JaxrsTestHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -105,11 +105,11 @@ public static void assertResponseEntityHasOneErrorMessage(Response response,
);
}

@SuppressWarnings("unchecked")
public static Map<String, Object> assertHasMapEntity(Response response) {
var entityObj = response.getEntity();
assertThat(entityObj).isInstanceOf(Map.class);

//noinspection unchecked
return (Map<String, Object>) entityObj;
}
}
1 change: 1 addition & 0 deletions src/test/java/org/kiwiproject/jaxrs/KiwiResourcesTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -448,6 +448,7 @@ private void assertEntity(Response response) throws IOException {
assertThat(mapEntity).isEqualTo(FromResponseTestResource.ENTITY);
}

@SuppressWarnings("resource")
@Test
void shouldThrowIllegalStateException_WhenEntityAlreadyConsumed() {
var originalResponse = RESOURCES.client().target("/from-response/with-entity").request().get();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ void shouldBuildResponse() {
var entity = assertHasMapEntity(response);
assertThat(entity).containsOnlyKeys("errors");

//noinspection unchecked
@SuppressWarnings("unchecked")
var errors = (List<ErrorMessage>) entity.get("errors");

assertThat(errors).containsExactlyInAnyOrder(
Expand Down Expand Up @@ -98,7 +98,7 @@ void shouldConvertToResponse() {
var entity = assertHasMapEntity(response);
assertThat(entity).containsOnlyKeys("errors");

//noinspection unchecked
@SuppressWarnings("unchecked")
var errors = (List<ErrorMessage>) entity.get("errors");
assertThat(errors).hasSameSizeAs(violations);
}
Expand Down

0 comments on commit 4b3cd77

Please sign in to comment.