@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 |
---|---|
Optional<String> |
get(String key)
The effective value of the specified property.
|
default Optional<Boolean> |
getBoolean(String key)
Effective value as boolean.
|
default Optional<Double> |
getDouble(String key)
Effective value as
Double . |
default Optional<Float> |
getFloat(String key)
Effective value as
Float . |
default Optional<Integer> |
getInt(String key)
Effective value as
int . |
default Optional<Long> |
getLong(String key)
Effective value as
long . |
String[] |
getStringArray(String key)
Used to read multi-valued properties.
|
boolean |
hasKey(String key) |
Optional<String> get(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.
IllegalStateException
- if value is encrypted but fails to be decrypted.boolean hasKey(String key)
true
if the property has a non-default value, else false
.String[] getStringArray(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.
Examples :
default Optional<Boolean> getBoolean(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 Optional<Integer> getInt(String key)
int
.int
. If the property does not have value nor default value, then empty
is returned.NumberFormatException
- if value is not empty and is not a parsable integerdefault Optional<Long> getLong(String key)
long
.long
. If the property does not have value nor default value, then empty
is returned.NumberFormatException
- if value is not empty and is not a parsable long
default Optional<Float> getFloat(String key)
Float
.Float
. If the property does not have value nor default value, then empty
is returned.NumberFormatException
- if value is not empty and is not a parsable numberdefault Optional<Double> getDouble(String key)
Double
.Double
. If the property does not have value nor default value, then empty
is returned.NumberFormatException
- if value is not empty and is not a parsable numberCopyright © 2009–2017 SonarSource. All rights reserved.