@ScannerSide @ServerSide @ComputeEngineSide public abstract class Settings extends Object
public class MyExtension {
private final Settings settings;
public MyExtension(Settings settings) {
this.settings = settings;
}
public void doSomething() {
String fooValue = settings.getString("sonar.foo");
// ..
}
}
public class MySensor implements Sensor {
@Override
public void execute(SensorContext context) {
String fooValue = context.settings().getString("sonar.foo");
// ..
}
}
For testing, and only for testing, the in-memory implementation MapSettings can be used.
@Test
public void my_test() {
Settings settings = new MapSettings();
settings.setProperty("foo", "bar");
MyExtension underTest = new MyExtension(settings);
// ...
}
History - this class is abstract since 6.1.MapSettings,
PropertyDefinition| Modifier | Constructor and Description |
|---|---|
protected |
Settings(PropertyDefinitions definitions,
Encryption encryption) |
| Modifier and Type | Method and Description |
|---|---|
Settings |
addProperties(Map<String,String> props) |
Settings |
addProperties(Properties props) |
Settings |
appendProperty(String key,
String value) |
protected abstract Optional<String> |
get(String key) |
boolean |
getBoolean(String key)
Effective value as boolean.
|
Date |
getDate(String key)
Effective value as
Date, without time fields. |
Date |
getDateTime(String key)
Effective value as
Date, with time fields. |
String |
getDefaultValue(String key) |
Optional<PropertyDefinition> |
getDefinition(String key)
The definition related to the specified property.
|
PropertyDefinitions |
getDefinitions()
All the property definitions declared by core and plugins.
|
Double |
getDouble(String key)
Effective value as
Double. |
Encryption |
getEncryption() |
Float |
getFloat(String key)
Effective value as
Float. |
int |
getInt(String key)
Effective value as
int. |
List<String> |
getKeysStartingWith(String prefix) |
long |
getLong(String key)
Effective value as
long. |
abstract Map<String,String> |
getProperties()
Immutable map of the properties that have non-default values.
|
Optional<String> |
getRawString(String key)
The value that overrides the default value.
|
String |
getString(String key)
The effective value of the specified property.
|
String[] |
getStringArray(String key)
Value is split by comma and trimmed.
|
String[] |
getStringArrayBySeparator(String key,
String separator)
Value is split and trimmed.
|
String[] |
getStringLines(String key)
Value is split by carriage returns.
|
boolean |
hasDefaultValue(String key) |
boolean |
hasKey(String key) |
protected abstract void |
remove(String key) |
Settings |
removeProperty(String key) |
protected abstract void |
set(String key,
String value) |
Settings |
setProperty(String key,
Boolean value) |
Settings |
setProperty(String key,
Date date) |
Settings |
setProperty(String key,
Date date,
boolean includeTime) |
Settings |
setProperty(String key,
Double value) |
Settings |
setProperty(String key,
Float value) |
Settings |
setProperty(String key,
Integer value) |
Settings |
setProperty(String key,
Long value) |
Settings |
setProperty(String key,
String value)
Change a property value in a restricted scope only, depending on execution context.
|
Settings |
setProperty(String key,
String[] values) |
protected Settings(PropertyDefinitions definitions, Encryption encryption)
public abstract Map<String,String> getProperties()
PropertyDefinitions are ignored,
so the returned values are not the effective values. Basically only
the non-empty results of getRawString(String) are returned.
Values are not decrypted if they are encrypted with a secret key.
public Encryption getEncryption()
public Optional<String> getRawString(String key)
getString(String) to get
the effective and decrypted value.public PropertyDefinitions getDefinitions()
public Optional<PropertyDefinition> getDefinition(String key)
public boolean hasKey(String key)
true if the property has a non-default value, else false.@CheckForNull public String getDefaultValue(String key)
public boolean hasDefaultValue(String key)
@CheckForNull public String getString(String key)
null 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.public boolean getBoolean(String key)
false if getString(String)
does not return "true", even if it's not a boolean representation.true if the effective value is "true", else false.public int getInt(String key)
int.int. If the property does not have value nor default value, then 0 is returned.NumberFormatException - if value is not empty and is not a parsable integerpublic long getLong(String key)
long.long. If the property does not have value nor default value, then 0L is returned.NumberFormatException - if value is not empty and is not a parsable long@CheckForNull public Date getDate(String key)
Date, without time fields. Format is DateUtils.DATE_FORMAT.Date. If the property does not have value nor default value, then null is returned.RuntimeException - if value is not empty and is not in accordance with DateUtils.DATE_FORMAT.@CheckForNull public Date getDateTime(String key)
Date, with time fields. Format is DateUtils.DATETIME_FORMAT.Date. If the property does not have value nor default value, then null is returned.RuntimeException - if value is not empty and is not in accordance with DateUtils.DATETIME_FORMAT.@CheckForNull public Float getFloat(String key)
Float.Float. If the property does not have value nor default value, then null is returned.NumberFormatException - if value is not empty and is not a parsable number@CheckForNull public Double getDouble(String key)
Double.Double. If the property does not have value nor default value, then null is returned.NumberFormatException - if value is not empty and is not a parsable numberpublic String[] getStringArray(String key)
public String[] getStringLines(String key)
public String[] getStringArrayBySeparator(String key, String separator)
public Settings appendProperty(String key, @Nullable String value)
public Settings setProperty(String key, @Nullable String[] values)
public Settings setProperty(String key, @Nullable String value)
value is nullpublic Settings setProperty(String key, @Nullable Boolean value)
setProperty(String, String)public Settings setProperty(String key, @Nullable Integer value)
setProperty(String, String)public Settings setProperty(String key, @Nullable Long value)
setProperty(String, String)public Settings setProperty(String key, @Nullable Double value)
setProperty(String, String)public Settings setProperty(String key, @Nullable Float value)
setProperty(String, String)public Settings setProperty(String key, @Nullable Date date)
setProperty(String, String)public Settings addProperties(Map<String,String> props)
public Settings addProperties(Properties props)
public Settings setProperty(String key, @Nullable Date date, boolean includeTime)
setProperty(String, String)public Settings removeProperty(String key)
public List<String> getKeysStartingWith(String prefix)
Copyright © 2009–2017 SonarSource. All rights reserved.