@ScannerSide @ServerSide @ComputeEngineSide @SonarLintSide public interface Configuration
public class MyExtension { private final Configuration config; public MyExtension(Configuration config) { this.config = config; } public void doSomething() { String fooValue = config.get("sonar.foo").orElse(null); // .. } }
public class MySensor implements Sensor { @Override public void execute(SensorContext context) { String fooValue = context.config().get("sonar.foo").orElse(null); // .. } }
For testing, and only for testing, the in-memory implementation MapSettings
can be used.
@Test public void my_test() { MapSettings settings = new MapSettings(); settings.setProperty("foo", "bar"); MyExtension underTest = new MyExtension(settings.asConfig()); // ... }
MapSettings
,
PropertyDefinition
Modifier and Type | Method and Description |
---|---|
java.util.Optional<java.lang.String> |
get(java.lang.String key)
The effective value of the specified property.
|
default java.util.Optional<java.lang.Boolean> |
getBoolean(java.lang.String key)
Effective value as boolean.
|
default java.util.Optional<java.lang.Double> |
getDouble(java.lang.String key)
Effective value as
Double . |
default java.util.Optional<java.lang.Float> |
getFloat(java.lang.String key)
Effective value as
Float . |
default java.util.Optional<java.lang.Integer> |
getInt(java.lang.String key)
Effective value as
int . |
default java.util.Optional<java.lang.Long> |
getLong(java.lang.String key)
Effective value as
long . |
java.lang.String[] |
getStringArray(java.lang.String key)
Used to read multi-valued properties.
|
boolean |
hasKey(java.lang.String key) |
java.util.Optional<java.lang.String> get(java.lang.String key)
Optional#empty()
if the property is not set and has no defined default value.
If the property is encrypted with a secret key, then the returned value is decrypted.
java.lang.IllegalStateException
- if value is encrypted but fails to be decrypted.boolean hasKey(java.lang.String key)
true
if the property has a non-default value, else false
.java.lang.String[] getStringArray(java.lang.String key)
See PropertyDefinition.Builder.multiValues(boolean)
Multi-valued properties coming from scanner are parsed as CSV lines (ie comma separator and optional double quotes to escape values).
Non quoted values are trimmed and empty fields are ignored.
Examples :
default java.util.Optional<java.lang.Boolean> getBoolean(java.lang.String key)
empty
if get(String)
is empty or if it
does not return "true"
, even if it's not a boolean representation.true
if the effective value is "true"
, false
for any other non empty value.
If the property does not have value nor default value, then empty
is returned.default java.util.Optional<java.lang.Integer> getInt(java.lang.String key)
int
.int
. If the property does not have value nor default value, then empty
is returned.java.lang.NumberFormatException
- if value is not empty and is not a parsable integerdefault java.util.Optional<java.lang.Long> getLong(java.lang.String key)
long
.long
. If the property does not have value nor default value, then empty
is returned.java.lang.NumberFormatException
- if value is not empty and is not a parsable long
default java.util.Optional<java.lang.Float> getFloat(java.lang.String key)
Float
.Float
. If the property does not have value nor default value, then empty
is returned.java.lang.NumberFormatException
- if value is not empty and is not a parsable numberdefault java.util.Optional<java.lang.Double> getDouble(java.lang.String key)
Double
.Double
. If the property does not have value nor default value, then empty
is returned.java.lang.NumberFormatException
- if value is not empty and is not a parsable number