aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-plugin-api
diff options
context:
space:
mode:
authorSimon Brandhof <simon.brandhof@sonarsource.com>2016-11-11 15:54:18 +0100
committerSimon Brandhof <simon.brandhof@sonarsource.com>2016-11-11 15:54:18 +0100
commit15e9f443eed5c74be9abe4738b07d62fe335c8e6 (patch)
treef8a3f00e91c202b615cd391ddc84470db8ca09c0 /sonar-plugin-api
parent129d630f52bd531f070969d806166f8c70593751 (diff)
downloadsonarqube-15e9f443eed5c74be9abe4738b07d62fe335c8e6.tar.gz
sonarqube-15e9f443eed5c74be9abe4738b07d62fe335c8e6.zip
Fix Quality flaws about complexity
Diffstat (limited to 'sonar-plugin-api')
-rw-r--r--sonar-plugin-api/src/main/java/org/sonar/api/measures/Measure.java21
-rw-r--r--sonar-plugin-api/src/main/java/org/sonar/api/utils/text/JsonWriter.java62
2 files changed, 44 insertions, 39 deletions
diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/measures/Measure.java b/sonar-plugin-api/src/main/java/org/sonar/api/measures/Measure.java
index 04732b9fae5..eb1cae02267 100644
--- a/sonar-plugin-api/src/main/java/org/sonar/api/measures/Measure.java
+++ b/sonar-plugin-api/src/main/java/org/sonar/api/measures/Measure.java
@@ -246,31 +246,30 @@ public class Measure<G extends Serializable> implements Serializable {
* For internal use.
*/
public G value() {
+ if (value == null) {
+ return null;
+ }
switch (getMetric().getType()) {
case BOOL:
- return value == null ? null : (G) Boolean.valueOf(Double.doubleToRawLongBits(value) != 0L);
+ return (G) Boolean.valueOf(Double.doubleToRawLongBits(value) != 0L);
case INT:
case MILLISEC:
case RATING:
- return value == null ? null : (G) Integer.valueOf(value.intValue());
+ return (G) Integer.valueOf(value.intValue());
case FLOAT:
case PERCENT:
- return value == null ? null : (G) value;
case STRING:
case LEVEL:
case DATA:
case DISTRIB:
- return data == null ? null : (G) data;
+ return (G) data;
case WORK_DUR:
- return value == null ? null : (G) Long.valueOf(value.longValue());
+ return (G) Long.valueOf(value.longValue());
default:
- if (getMetric().isNumericType()) {
- return value == null ? null : (G) value;
- } else if (getMetric().isDataType()) {
- return data == null ? null : (G) data;
- } else {
- throw new UnsupportedOperationException("Unsupported type :" + getMetric().getType());
+ if (getMetric().isNumericType() || getMetric().isDataType()) {
+ return (G) value;
}
+ throw new UnsupportedOperationException("Unsupported type :" + getMetric().getType());
}
}
diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/utils/text/JsonWriter.java b/sonar-plugin-api/src/main/java/org/sonar/api/utils/text/JsonWriter.java
index de3b3cd988d..b40e8e1dfb0 100644
--- a/sonar-plugin-api/src/main/java/org/sonar/api/utils/text/JsonWriter.java
+++ b/sonar-plugin-api/src/main/java/org/sonar/api/utils/text/JsonWriter.java
@@ -19,6 +19,7 @@
*/
package org.sonar.api.utils.text;
+import java.io.IOException;
import java.io.Writer;
import java.util.Date;
import java.util.Map;
@@ -214,37 +215,13 @@ public class JsonWriter {
* @throws org.sonar.api.utils.text.WriterException on any failure
*/
public JsonWriter valueObject(@Nullable Object value) {
+
try {
if (value == null) {
stream.nullValue();
- } else {
- if (value instanceof String) {
- stream.value(serializeEmptyStrings ? (String) value : emptyToNull((String) value));
- } else if (value instanceof Number) {
- stream.value((Number) value);
- } else if (value instanceof Boolean) {
- stream.value((Boolean) value);
- } else if (value instanceof Date) {
- valueDateTime((Date) value);
- } else if (value instanceof Enum) {
- stream.value(((Enum) value).name());
- } else if (value instanceof Map) {
- stream.beginObject();
- for (Map.Entry<Object, Object> entry : ((Map<Object, Object>) value).entrySet()) {
- stream.name(entry.getKey().toString());
- valueObject(entry.getValue());
- }
- stream.endObject();
- } else if (value instanceof Iterable) {
- stream.beginArray();
- for (Object o : (Iterable<Object>) value) {
- valueObject(o);
- }
- stream.endArray();
- } else {
- throw new IllegalArgumentException(getClass() + " does not support encoding of type: " + value.getClass());
- }
+ return this;
}
+ valueNonNullObject(value);
return this;
} catch (IllegalArgumentException e) {
throw e;
@@ -253,6 +230,35 @@ public class JsonWriter {
}
}
+ private void valueNonNullObject(Object value) throws IOException {
+ if (value instanceof String) {
+ stream.value(serializeEmptyStrings ? (String) value : emptyToNull((String) value));
+ } else if (value instanceof Number) {
+ stream.value((Number) value);
+ } else if (value instanceof Boolean) {
+ stream.value((Boolean) value);
+ } else if (value instanceof Date) {
+ valueDateTime((Date) value);
+ } else if (value instanceof Enum) {
+ stream.value(((Enum) value).name());
+ } else if (value instanceof Map) {
+ stream.beginObject();
+ for (Map.Entry<Object, Object> entry : ((Map<Object, Object>) value).entrySet()) {
+ stream.name(entry.getKey().toString());
+ valueObject(entry.getValue());
+ }
+ stream.endObject();
+ } else if (value instanceof Iterable) {
+ stream.beginArray();
+ for (Object o : (Iterable<Object>) value) {
+ valueObject(o);
+ }
+ stream.endArray();
+ } else {
+ throw new IllegalArgumentException(getClass() + " does not support encoding of type: " + value.getClass());
+ }
+ }
+
/**
* Write a list of values in an array, for example:
* <pre>
@@ -382,7 +388,7 @@ public class JsonWriter {
}
private static IllegalStateException rethrow(Exception e) {
- throw new WriterException("Fail to write JSON", e);
+ throw new WriterException("Fail to write JSON", e);
}
@Nullable