From 15e9f443eed5c74be9abe4738b07d62fe335c8e6 Mon Sep 17 00:00:00 2001 From: Simon Brandhof Date: Fri, 11 Nov 2016 15:54:18 +0100 Subject: [PATCH] Fix Quality flaws about complexity --- .../java/org/sonar/core/issue/FieldDiffs.java | 37 ++++++----- .../java/org/sonar/api/measures/Measure.java | 21 +++---- .../org/sonar/api/utils/text/JsonWriter.java | 62 ++++++++++--------- .../org/sonar/scanner/index/DefaultIndex.java | 59 +++++++++--------- 4 files changed, 94 insertions(+), 85 deletions(-) diff --git a/sonar-core/src/main/java/org/sonar/core/issue/FieldDiffs.java b/sonar-core/src/main/java/org/sonar/core/issue/FieldDiffs.java index 8bc22fa3554..8722f76fb27 100644 --- a/sonar-core/src/main/java/org/sonar/core/issue/FieldDiffs.java +++ b/sonar-core/src/main/java/org/sonar/core/issue/FieldDiffs.java @@ -28,6 +28,8 @@ import java.util.Map; import javax.annotation.CheckForNull; import javax.annotation.Nullable; +import static com.google.common.base.Strings.isNullOrEmpty; + /** * PLUGINS MUST NOT USE THIS CLASS, EXCEPT FOR UNIT TESTING. * @@ -110,24 +112,25 @@ public class FieldDiffs implements Serializable { public static FieldDiffs parse(@Nullable String s) { FieldDiffs diffs = new FieldDiffs(); - if (!Strings.isNullOrEmpty(s)) { - Iterable fields = FIELDS_SPLITTER.split(s); - for (String field : fields) { - String[] keyValues = field.split("="); - if (keyValues.length == 2) { - String[] values = keyValues[1].split("\\|"); - String oldValue = ""; - String newValue = ""; - if (values.length == 1) { - newValue = Strings.nullToEmpty(values[0]); - } else if (values.length == 2) { - oldValue = Strings.nullToEmpty(values[0]); - newValue = Strings.nullToEmpty(values[1]); - } - diffs.setDiff(keyValues[0], oldValue, newValue); - } else { - diffs.setDiff(keyValues[0], "", ""); + if (isNullOrEmpty(s)) { + return diffs; + } + Iterable fields = FIELDS_SPLITTER.split(s); + for (String field : fields) { + String[] keyValues = field.split("="); + if (keyValues.length == 2) { + String[] values = keyValues[1].split("\\|"); + String oldValue = ""; + String newValue = ""; + if (values.length == 1) { + newValue = Strings.nullToEmpty(values[0]); + } else if (values.length == 2) { + oldValue = Strings.nullToEmpty(values[0]); + newValue = Strings.nullToEmpty(values[1]); } + diffs.setDiff(keyValues[0], oldValue, newValue); + } else { + diffs.setDiff(keyValues[0], "", ""); } } return diffs; 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 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 entry : ((Map) value).entrySet()) { - stream.name(entry.getKey().toString()); - valueObject(entry.getValue()); - } - stream.endObject(); - } else if (value instanceof Iterable) { - stream.beginArray(); - for (Object o : (Iterable) 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 entry : ((Map) value).entrySet()) { + stream.name(entry.getKey().toString()); + valueObject(entry.getValue()); + } + stream.endObject(); + } else if (value instanceof Iterable) { + stream.beginArray(); + for (Object o : (Iterable) 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: *
@@ -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
diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/index/DefaultIndex.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/index/DefaultIndex.java
index b3e43598e91..851df664962 100644
--- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/index/DefaultIndex.java
+++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/index/DefaultIndex.java
@@ -198,36 +198,37 @@ public class DefaultIndex {
 
   public Measure addMeasure(Resource resource, Measure measure) {
     Bucket bucket = getBucket(resource);
-    if (bucket != null) {
-      if (sensorStorage.isDeprecatedMetric(measure.getMetricKey())) {
-        // Ignore deprecated metrics
-        return measure;
-      }
-      org.sonar.api.batch.measure.Metric metric = metricFinder.findByKey(measure.getMetricKey());
-      if (metric == null) {
-        throw new UnsupportedOperationException("Unknown metric: " + measure.getMetricKey());
-      }
-      DefaultMeasure newMeasure;
-      if (Boolean.class.equals(metric.valueType())) {
-        newMeasure = new DefaultMeasure().forMetric((Metric) metric)
-          .withValue(measure.getValue() != 0.0);
-      } else if (Integer.class.equals(metric.valueType())) {
-        newMeasure = new DefaultMeasure().forMetric((Metric) metric)
-          .withValue(measure.getValue().intValue());
-      } else if (Double.class.equals(metric.valueType())) {
-        newMeasure = new DefaultMeasure().forMetric((Metric) metric)
-          .withValue(measure.getValue());
-      } else if (String.class.equals(metric.valueType())) {
-        newMeasure = new DefaultMeasure().forMetric((Metric) metric)
-          .withValue(measure.getData());
-      } else if (Long.class.equals(metric.valueType())) {
-        newMeasure = new DefaultMeasure().forMetric((Metric) metric)
-          .withValue(measure.getValue().longValue());
-      } else {
-        throw new UnsupportedOperationException("Unsupported type :" + metric.valueType());
-      }
-      sensorStorage.saveMeasure(componentCache.get(resource).inputComponent(), newMeasure);
+    if (bucket == null) {
+      return measure;
+    }
+    if (sensorStorage.isDeprecatedMetric(measure.getMetricKey())) {
+      // Ignore deprecated metrics
+      return measure;
+    }
+    org.sonar.api.batch.measure.Metric metric = metricFinder.findByKey(measure.getMetricKey());
+    if (metric == null) {
+      throw new UnsupportedOperationException("Unknown metric: " + measure.getMetricKey());
+    }
+    DefaultMeasure newMeasure;
+    if (Boolean.class.equals(metric.valueType())) {
+      newMeasure = new DefaultMeasure().forMetric((Metric) metric)
+        .withValue(measure.getValue() != 0.0);
+    } else if (Integer.class.equals(metric.valueType())) {
+      newMeasure = new DefaultMeasure().forMetric((Metric) metric)
+        .withValue(measure.getValue().intValue());
+    } else if (Double.class.equals(metric.valueType())) {
+      newMeasure = new DefaultMeasure().forMetric((Metric) metric)
+        .withValue(measure.getValue());
+    } else if (String.class.equals(metric.valueType())) {
+      newMeasure = new DefaultMeasure().forMetric((Metric) metric)
+        .withValue(measure.getData());
+    } else if (Long.class.equals(metric.valueType())) {
+      newMeasure = new DefaultMeasure().forMetric((Metric) metric)
+        .withValue(measure.getValue().longValue());
+    } else {
+      throw new UnsupportedOperationException("Unsupported type :" + metric.valueType());
     }
+    sensorStorage.saveMeasure(componentCache.get(resource).inputComponent(), newMeasure);
     return measure;
   }
 
-- 
2.39.5