From: Duarte Meneses Date: Fri, 1 Jul 2016 14:04:13 +0000 (+0200) Subject: SONAR-7680 Fallback to class name when Sensor name is not provided X-Git-Tag: 6.0-RC1~198 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=refs%2Fpull%2F1076%2Fhead;p=sonarqube.git SONAR-7680 Fallback to class name when Sensor name is not provided --- 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"); } }