Skip to content

Commit e4be6b5

Browse files
PLUGINAPI-33 Revert removal of 'org.sonar.api.config.Settings'
1 parent eafa66f commit e4be6b5

File tree

4 files changed

+261
-1
lines changed

4 files changed

+261
-1
lines changed

CHANGELOG.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@
4141
* Remove `org.sonar.api.issue.IssueComment` and `org.sonar.api.issue.Issue.comments()`. No replacement
4242
* Remove `org.sonar.api.batch.rule.Rules`. No replacement
4343
* Remove `org.sonar.api.notifications.NotificationChannel` and `org.sonar.api.notifications.Notification`. No replacement
44-
* Remove `org.sonar.api.config.Settings` and `org.sonar.api.batch.sensor.SensorContext.settings()`. Use `org.sonar.api.config.Configuration` instead
4544
* Remove `org.sonar.api.scan.filesystem.PathResolver.RelativePath` and `org.sonar.api.scan.filesystem.PathResolver.relativePath(java.util.Collection<java.io.File>, java.io.File)`. No replacement
4645
* Remove `org.sonar.api.platform.Server.getPermanentServerId()`. Use `org.sonar.api.platform.Server.getId()` instead
4746
* Remove `org.sonar.api.rules.RulePriority.valueOfString(String)`. Use `org.sonar.api.rules.RulePriority.valueOf(String)` instead

plugin-api/src/main/java/org/sonar/api/batch/sensor/SensorContext.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
import org.sonar.api.batch.sensor.rule.NewAdHocRule;
4444
import org.sonar.api.batch.sensor.symbol.NewSymbolTable;
4545
import org.sonar.api.config.Configuration;
46+
import org.sonar.api.config.Settings;
4647
import org.sonar.api.scanner.fs.InputProject;
4748
import org.sonar.api.scanner.sensor.ProjectSensor;
4849
import org.sonar.api.utils.Version;
@@ -53,6 +54,13 @@
5354
* @since 5.1
5455
*/
5556
public interface SensorContext {
57+
58+
/**
59+
* @deprecated since 6.5 use {@link #config()}
60+
*/
61+
@Deprecated(since = "6.5")
62+
Settings settings();
63+
5664
/**
5765
* Get settings of the project.
5866
* @since 6.5
Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
/*
2+
* Sonar Plugin API
3+
* Copyright (C) 2009-2023 SonarSource SA
4+
* mailto:info AT sonarsource DOT com
5+
*
6+
* This program is free software; you can redistribute it and/or
7+
* modify it under the terms of the GNU Lesser General Public
8+
* License as published by the Free Software Foundation; either
9+
* version 3 of the License, or (at your option) any later version.
10+
*
11+
* This program is distributed in the hope that it will be useful,
12+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14+
* Lesser General Public License for more details.
15+
*
16+
* You should have received a copy of the GNU Lesser General Public License
17+
* along with this program; if not, write to the Free Software Foundation,
18+
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19+
*/
20+
package org.sonar.api.config;
21+
22+
import java.util.Date;
23+
import java.util.List;
24+
import javax.annotation.CheckForNull;
25+
import org.sonar.api.ce.ComputeEngineSide;
26+
import org.sonar.api.scanner.ScannerSide;
27+
import org.sonar.api.server.ServerSide;
28+
import org.sonar.api.utils.DateUtils;
29+
import org.sonarsource.api.sonarlint.SonarLintSide;
30+
31+
/**
32+
* @deprecated since 6.5 use {@link Configuration}. Implementation moved out of the API in 8.3. Only remains minimal interface to make some outdated plugins happy.
33+
*/
34+
@ServerSide
35+
@ComputeEngineSide
36+
@ScannerSide
37+
@SonarLintSide
38+
@Deprecated(since = "6.5")
39+
public abstract class Settings {
40+
41+
/**
42+
* @return {@code true} if the property has a non-default value, else {@code false}.
43+
*/
44+
public abstract boolean hasKey(String key);
45+
46+
/**
47+
* The effective value of the specified property. Can return
48+
* {@code null} if the property is not set and has no
49+
* defined default value.
50+
* <p>
51+
* If the property is encrypted with a secret key,
52+
* then the returned value is decrypted.
53+
* </p>
54+
*
55+
* @throws IllegalStateException if value is encrypted but fails to be decrypted.
56+
*/
57+
@CheckForNull
58+
public abstract String getString(String key);
59+
60+
/**
61+
* Effective value as boolean. It is {@code false} if {@link #getString(String)}
62+
* does not return {@code "true"}, even if it's not a boolean representation.
63+
*
64+
* @return {@code true} if the effective value is {@code "true"}, else {@code false}.
65+
*/
66+
public abstract boolean getBoolean(String key);
67+
68+
/**
69+
* Effective value as {@code int}.
70+
*
71+
* @return the value as {@code int}. If the property does not have value nor default value, then {@code 0} is returned.
72+
* @throws NumberFormatException if value is not empty and is not a parsable integer
73+
*/
74+
public abstract int getInt(String key);
75+
76+
/**
77+
* Effective value as {@code long}.
78+
*
79+
* @return the value as {@code long}. If the property does not have value nor default value, then {@code 0L} is returned.
80+
* @throws NumberFormatException if value is not empty and is not a parsable {@code long}
81+
*/
82+
public abstract long getLong(String key);
83+
84+
/**
85+
* Effective value as {@link Date}, without time fields. Format is {@link DateUtils#DATE_FORMAT}.
86+
*
87+
* @return the value as a {@link Date}. If the property does not have value nor default value, then {@code null} is returned.
88+
* @throws RuntimeException if value is not empty and is not in accordance with {@link DateUtils#DATE_FORMAT}.
89+
*/
90+
@CheckForNull
91+
public abstract Date getDate(String key);
92+
93+
/**
94+
* Effective value as {@link Date}, with time fields. Format is {@link DateUtils#DATETIME_FORMAT}.
95+
*
96+
* @return the value as a {@link Date}. If the property does not have value nor default value, then {@code null} is returned.
97+
* @throws RuntimeException if value is not empty and is not in accordance with {@link DateUtils#DATETIME_FORMAT}.
98+
*/
99+
@CheckForNull
100+
public abstract Date getDateTime(String key);
101+
102+
/**
103+
* Effective value as {@code Float}.
104+
*
105+
* @return the value as {@code Float}. If the property does not have value nor default value, then {@code null} is returned.
106+
* @throws NumberFormatException if value is not empty and is not a parsable number
107+
*/
108+
@CheckForNull
109+
public abstract Float getFloat(String key);
110+
111+
/**
112+
* Effective value as {@code Double}.
113+
*
114+
* @return the value as {@code Double}. If the property does not have value nor default value, then {@code null} is returned.
115+
* @throws NumberFormatException if value is not empty and is not a parsable number
116+
*/
117+
@CheckForNull
118+
public abstract Double getDouble(String key);
119+
120+
/**
121+
* Value is split by comma and trimmed. Never returns null.
122+
* <br>
123+
* Examples :
124+
* <ul>
125+
* <li>"one,two,three " -&gt; ["one", "two", "three"]</li>
126+
* <li>" one, two, three " -&gt; ["one", "two", "three"]</li>
127+
* <li>"one, , three" -&gt; ["one", "", "three"]</li>
128+
* </ul>
129+
*/
130+
public abstract String[] getStringArray(String key);
131+
132+
/**
133+
* Value is split by carriage returns.
134+
*
135+
* @return non-null array of lines. The line termination characters are excluded.
136+
* @since 3.2
137+
*/
138+
public abstract String[] getStringLines(String key);
139+
140+
/**
141+
* Value is split and trimmed.
142+
*/
143+
public abstract String[] getStringArrayBySeparator(String key, String separator);
144+
145+
public abstract List<String> getKeysStartingWith(String prefix);
146+
147+
}
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
/*
2+
* Sonar Plugin API
3+
* Copyright (C) 2009-2023 SonarSource SA
4+
* mailto:info AT sonarsource DOT com
5+
*
6+
* This program is free software; you can redistribute it and/or
7+
* modify it under the terms of the GNU Lesser General Public
8+
* License as published by the Free Software Foundation; either
9+
* version 3 of the License, or (at your option) any later version.
10+
*
11+
* This program is distributed in the hope that it will be useful,
12+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14+
* Lesser General Public License for more details.
15+
*
16+
* You should have received a copy of the GNU Lesser General Public License
17+
* along with this program; if not, write to the Free Software Foundation,
18+
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19+
*/
20+
package org.sonar.api.config;
21+
22+
import java.util.Date;
23+
import java.util.List;
24+
import javax.annotation.CheckForNull;
25+
import org.junit.jupiter.api.Test;
26+
27+
import static org.assertj.core.api.Assertions.assertThat;
28+
29+
class SettingsTest {
30+
@Test
31+
void settings_can_be_subclassed() {
32+
var settings = new Settings() {
33+
@Override
34+
public boolean hasKey(String key) {
35+
return false;
36+
}
37+
38+
@CheckForNull
39+
@Override
40+
public String getString(String key) {
41+
return "key";
42+
}
43+
44+
@Override
45+
public boolean getBoolean(String key) {
46+
return false;
47+
}
48+
49+
@Override
50+
public int getInt(String key) {
51+
return 0;
52+
}
53+
54+
@Override
55+
public long getLong(String key) {
56+
return 0;
57+
}
58+
59+
@CheckForNull
60+
@Override
61+
public Date getDate(String key) {
62+
return null;
63+
}
64+
65+
@CheckForNull
66+
@Override
67+
public Date getDateTime(String key) {
68+
return null;
69+
}
70+
71+
@CheckForNull
72+
@Override
73+
public Float getFloat(String key) {
74+
return null;
75+
}
76+
77+
@CheckForNull
78+
@Override
79+
public Double getDouble(String key) {
80+
return null;
81+
}
82+
83+
@Override
84+
public String[] getStringArray(String key) {
85+
return new String[]{key};
86+
}
87+
88+
@Override
89+
public String[] getStringLines(String key) {
90+
return new String[0];
91+
}
92+
93+
@Override
94+
public String[] getStringArrayBySeparator(String key, String separator) {
95+
return new String[0];
96+
}
97+
98+
@Override
99+
public List<String> getKeysStartingWith(String prefix) {
100+
return null;
101+
}
102+
};
103+
104+
assertThat(settings.getString("key")).isEqualTo("key");
105+
}
106+
}

0 commit comments

Comments
 (0)