aboutsummaryrefslogtreecommitdiffstats
path: root/buildSrc/src/main/java/tools/refinery/gradle/utils/SonarPropertiesUtils.java
diff options
context:
space:
mode:
Diffstat (limited to 'buildSrc/src/main/java/tools/refinery/gradle/utils/SonarPropertiesUtils.java')
-rw-r--r--buildSrc/src/main/java/tools/refinery/gradle/utils/SonarPropertiesUtils.java44
1 files changed, 44 insertions, 0 deletions
diff --git a/buildSrc/src/main/java/tools/refinery/gradle/utils/SonarPropertiesUtils.java b/buildSrc/src/main/java/tools/refinery/gradle/utils/SonarPropertiesUtils.java
new file mode 100644
index 00000000..3810fccf
--- /dev/null
+++ b/buildSrc/src/main/java/tools/refinery/gradle/utils/SonarPropertiesUtils.java
@@ -0,0 +1,44 @@
1package tools.refinery.gradle.utils;
2
3import java.util.ArrayList;
4import java.util.Collections;
5import java.util.Map;
6
7public final class SonarPropertiesUtils {
8 private SonarPropertiesUtils() {
9 throw new IllegalStateException("This is a static utility class and should not be instantiated directly");
10 }
11
12 /**
13 * Adds the entries to a Sonar property of list type.
14 * <p>
15 * According to the Sonar Gradle documentation for {@link org.sonarqube.gradle.SonarProperties}, property values
16 * are converted to Strings as follows:
17 * <ul>
18 * <li>{@code Iterable}s are recursively converted and joined into a comma-separated String.</li>
19 * <li>All other values are converted to Strings by calling their {@code toString()} method.</li>
20 * </ul>
21 * Therefore, we use {@link ArrayList} to retain lists entries, which will be recursively converted later.
22 *
23 * @param properties The sonar properties map returned by
24 * {@link org.sonarqube.gradle.SonarProperties#getProperties()}.
25 * @param propertyName The name of the property to append to.
26 * @param entries The entries to append.
27 */
28 public static void addToList(Map<String, Object> properties, String propertyName, String... entries) {
29 ArrayList<Object> newValue;
30 var currentValue = properties.get(propertyName);
31 if (currentValue instanceof ArrayList<?> currentList) {
32 @SuppressWarnings("unchecked")
33 var objectList = (ArrayList<Object>) currentList;
34 newValue = objectList;
35 } else if (currentValue == null) {
36 newValue = new ArrayList<>(entries.length);
37 } else {
38 newValue = new ArrayList<>(entries.length + 1);
39 newValue.add(currentValue);
40 }
41 Collections.addAll(newValue, entries);
42 properties.put(propertyName, newValue);
43 }
44}