From f116e9b90e440b320529d2ccb2f3b3dd89773d7e Mon Sep 17 00:00:00 2001 From: Duarte Meneses Date: Fri, 1 Jul 2016 16:04:13 +0200 Subject: [PATCH] SONAR-7680 Fallback to class name when Sensor name is not provided --- .../main/java/org/sonar/batch/sensor/SensorWrapper.java | 6 +++++- .../src/main/java/org/sonar/batch/util/BatchUtils.java | 5 ++++- .../test/java/org/sonar/batch/util/BatchUtilsTest.java | 8 +++++++- 3 files changed, 16 insertions(+), 3 deletions(-) diff --git a/sonar-scanner-engine/src/main/java/org/sonar/batch/sensor/SensorWrapper.java b/sonar-scanner-engine/src/main/java/org/sonar/batch/sensor/SensorWrapper.java index 3e82a222546..2fb6d98ecdb 100644 --- a/sonar-scanner-engine/src/main/java/org/sonar/batch/sensor/SensorWrapper.java +++ b/sonar-scanner-engine/src/main/java/org/sonar/batch/sensor/SensorWrapper.java @@ -59,6 +59,10 @@ public class SensorWrapper implements org.sonar.api.batch.Sensor { @Override public String toString() { - return descriptor.name() + (LOG.isDebugEnabled() ? " (wrapped)" : ""); + if (descriptor.name() != null) { + return descriptor.name(); + } else { + return wrappedSensor.getClass().getName(); + } } } diff --git a/sonar-scanner-engine/src/main/java/org/sonar/batch/util/BatchUtils.java b/sonar-scanner-engine/src/main/java/org/sonar/batch/util/BatchUtils.java index 81eed874378..5b0a8f85437 100644 --- a/sonar-scanner-engine/src/main/java/org/sonar/batch/util/BatchUtils.java +++ b/sonar-scanner-engine/src/main/java/org/sonar/batch/util/BatchUtils.java @@ -61,7 +61,10 @@ public class BatchUtils { public static String describe(Object o) { try { if (o.getClass().getMethod("toString").getDeclaringClass() != Object.class) { - return o.toString(); + String str = o.toString(); + if (str != null) { + return str; + } } } catch (Exception e) { // fallback diff --git a/sonar-scanner-engine/src/test/java/org/sonar/batch/util/BatchUtilsTest.java b/sonar-scanner-engine/src/test/java/org/sonar/batch/util/BatchUtilsTest.java index 8de6036b5e3..eeab4f402a1 100644 --- a/sonar-scanner-engine/src/test/java/org/sonar/batch/util/BatchUtilsTest.java +++ b/sonar-scanner-engine/src/test/java/org/sonar/batch/util/BatchUtilsTest.java @@ -33,8 +33,13 @@ public class BatchUtilsTest { assertThat(BatchUtils.encodeForUrl("foo&bar")).isEqualTo("foo%26bar"); } + private class MyClass { + @Override + public String toString() { + return null; + } + } @Test - public void testDescribe() { Object withToString = new Object() { @Override @@ -47,5 +52,6 @@ public class BatchUtilsTest { assertThat(BatchUtils.describe(withToString)).isEqualTo(("desc")); assertThat(BatchUtils.describe(withoutToString)).isEqualTo("java.lang.Object"); + assertThat(BatchUtils.describe(new MyClass())).endsWith("MyClass"); } } -- 2.39.5