From 0d3b95a4e1dc8059a6eb5b1f98c3f7f41f09b151 Mon Sep 17 00:00:00 2001 From: Jim Hahn Date: Tue, 12 Feb 2019 10:50:06 -0500 Subject: Fix sonar issues in gson superclasses Fixing some of the issues necessitated the use of generic/raw types. Removed version from assertj in pom. Removed trailing spaces. Change-Id: I76e0a05ca06c103ccc876675be04a4ab5e4de5ea Issue-ID: POLICY-1428 Signed-off-by: Jim Hahn --- .../policy/common/gson/GsonMessageBodyHandler.java | 9 ++++++--- .../org/onap/policy/common/gson/internal/Adapter.java | 19 ++++++++++++------- .../onap/policy/common/gson/internal/ClassWalker.java | 4 ++-- .../policy/common/gson/internal/Deserializer.java | 3 +-- .../onap/policy/common/gson/internal/Serializer.java | 1 + 5 files changed, 22 insertions(+), 14 deletions(-) (limited to 'gson/src') diff --git a/gson/src/main/java/org/onap/policy/common/gson/GsonMessageBodyHandler.java b/gson/src/main/java/org/onap/policy/common/gson/GsonMessageBodyHandler.java index 2112c97c..66475e3e 100644 --- a/gson/src/main/java/org/onap/policy/common/gson/GsonMessageBodyHandler.java +++ b/gson/src/main/java/org/onap/policy/common/gson/GsonMessageBodyHandler.java @@ -80,7 +80,7 @@ public class GsonMessageBodyHandler implements MessageBodyReader, Messag @Override public void writeTo(Object object, Class type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap httpHeaders, OutputStream entityStream) - throws IOException, WebApplicationException { + throws IOException { try (OutputStreamWriter writer = new OutputStreamWriter(entityStream, StandardCharsets.UTF_8)) { Type jsonType = (type.equals(genericType) ? type : genericType); @@ -107,8 +107,11 @@ public class GsonMessageBodyHandler implements MessageBodyReader, Messag String subtype = mediaType.getSubtype(); - return "json".equalsIgnoreCase(subtype) || subtype.endsWith("+json") || "javascript".equals(subtype) - || "x-javascript".equals(subtype) || "x-json".equals(subtype); + if ("json".equalsIgnoreCase(subtype) || "javascript".equals(subtype)) { + return true; + } + + return subtype.endsWith("+json") || "x-json".equals(subtype) || "x-javascript".equals(subtype); } @Override diff --git a/gson/src/main/java/org/onap/policy/common/gson/internal/Adapter.java b/gson/src/main/java/org/onap/policy/common/gson/internal/Adapter.java index b4ef53f7..94091c79 100644 --- a/gson/src/main/java/org/onap/policy/common/gson/internal/Adapter.java +++ b/gson/src/main/java/org/onap/policy/common/gson/internal/Adapter.java @@ -116,8 +116,9 @@ public class Adapter { } ConvInfo wtr = writer; - TypeAdapter conv = - (TypeAdapter) (wtr.clazz == clazz ? wtr.getConverter() : gson.getAdapter(clazz)); + + @SuppressWarnings("rawtypes") + TypeAdapter conv = (wtr.clazz == clazz ? wtr.getConverter() : gson.getAdapter(clazz)); return conv.toJsonTree(object); } @@ -305,29 +306,33 @@ public class Adapter { /** * Type on which the converter works. */ - private TypeToken type; + @SuppressWarnings("rawtypes") + private TypeToken type; /** * Class of object on which the converter works. */ - private Class clazz; + @SuppressWarnings("rawtypes") + private Class clazz; /** * Converter to use, initialized lazily. */ - private volatile TypeAdapter conv = null; + @SuppressWarnings("rawtypes") + private volatile TypeAdapter conv = null; /** * Constructs the object. * * @param type type of object to be converted */ - public ConvInfo(TypeToken type) { + public ConvInfo(@SuppressWarnings("rawtypes") TypeToken type) { this.type = type; this.clazz = type.getRawType(); } - public final TypeAdapter getConverter() { + @SuppressWarnings({"rawtypes", "unchecked"}) + public final TypeAdapter getConverter() { if (conv == null) { // race condition here, but it's ok to overwrite a previous value this.conv = gson.getAdapter(type); diff --git a/gson/src/main/java/org/onap/policy/common/gson/internal/ClassWalker.java b/gson/src/main/java/org/onap/policy/common/gson/internal/ClassWalker.java index e985d98a..89e4f89d 100644 --- a/gson/src/main/java/org/onap/policy/common/gson/internal/ClassWalker.java +++ b/gson/src/main/java/org/onap/policy/common/gson/internal/ClassWalker.java @@ -122,7 +122,7 @@ public class ClassWalker { * @return the property names having a non-null value */ private List getNonNull(Map props) { - List lst = new ArrayList(props.size()); + List lst = new ArrayList<>(props.size()); for (Entry ent : props.entrySet()) { if (ent.getValue() != null) { @@ -162,7 +162,7 @@ public class ClassWalker { */ @SuppressWarnings("unchecked") private List getProps(Class clazz, Collection values) { - List lst = new ArrayList(values.size()); + List lst = new ArrayList<>(values.size()); for (Object val : values) { if (val != null && val.getClass() == clazz) { diff --git a/gson/src/main/java/org/onap/policy/common/gson/internal/Deserializer.java b/gson/src/main/java/org/onap/policy/common/gson/internal/Deserializer.java index f2975860..4bf6e0c3 100644 --- a/gson/src/main/java/org/onap/policy/common/gson/internal/Deserializer.java +++ b/gson/src/main/java/org/onap/policy/common/gson/internal/Deserializer.java @@ -25,10 +25,9 @@ import com.google.gson.JsonObject; /** * Super class of all de-serializers. */ +@FunctionalInterface public interface Deserializer { - String INVOKE_ERR = "cannot invoke method to deserialize: "; - /** * Gets an value from a tree, converts it, and puts it into a target object. * diff --git a/gson/src/main/java/org/onap/policy/common/gson/internal/Serializer.java b/gson/src/main/java/org/onap/policy/common/gson/internal/Serializer.java index 2e2782ad..254a5fe7 100644 --- a/gson/src/main/java/org/onap/policy/common/gson/internal/Serializer.java +++ b/gson/src/main/java/org/onap/policy/common/gson/internal/Serializer.java @@ -25,6 +25,7 @@ import com.google.gson.JsonObject; /** * Super class of all serializers. */ +@FunctionalInterface public interface Serializer { /** -- cgit 1.2.3-korg