Skip to content

Commit

Permalink
Autocomplete configuration values of properties with java enum data t…
Browse files Browse the repository at this point in the history
…ype.

Special case treatment of enumerations when calculating the completion
list of properties values.
  • Loading branch information
AlexFalappa committed Aug 2, 2019
1 parent 0f28ec8 commit d5ded0d
Showing 1 changed file with 29 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,10 @@
import org.springframework.boot.configurationmetadata.ConfigurationMetadataProperty;
import org.springframework.boot.configurationmetadata.ConfigurationMetadataRepository;
import org.springframework.boot.configurationmetadata.ConfigurationMetadataRepositoryJsonBuilder;
import org.springframework.boot.configurationmetadata.Hints;
import org.springframework.boot.configurationmetadata.SimpleConfigurationMetadataRepository;
import org.springframework.boot.configurationmetadata.ValueHint;
import org.springframework.boot.configurationmetadata.ValueProvider;

import com.github.alexfalappa.nbspringboot.projects.service.api.SpringBootService;

Expand Down Expand Up @@ -198,7 +200,33 @@ public List<ValueHint> queryHintMetadata(String propertyName, String filter) {
List<ValueHint> ret = new LinkedList<>();
ConfigurationMetadataProperty cfgMeta = getPropertyMetadata(propertyName);
if (cfgMeta != null) {
for (ValueHint valueHint : cfgMeta.getHints().getValueHints()) {
// special case: chack if data type is an enum
try {
Object[] enumvals = cpExec.getClassLoader(true).loadClass(cfgMeta.getType()).getEnumConstants();
if (enumvals != null) {
for (Object val : enumvals) {
final String valName = val.toString().toLowerCase();
if (filter == null || valName.contains(filter)) {
ValueHint valueHint = new ValueHint();
valueHint.setValue(valName);
valueHint.setDescription(String.format("Enumeration value %s", valName.toUpperCase()));
ret.add(valueHint);
}
}
}
} catch (ClassNotFoundException ex) {
// enum not available in project classpath, no completion possible
}
// log value providers
final Hints hints = cfgMeta.getHints();
if (!hints.getValueProviders().isEmpty()) {
logger.info(String.format("Value providers for %s:", propertyName));
for (ValueProvider vp : hints.getValueProviders()) {
logger.info(vp.getName());
}
}
// add defined value hints to completion lists
for (ValueHint valueHint : hints.getValueHints()) {
if (filter == null || valueHint.getValue().toString().contains(filter)) {
ret.add(valueHint);
}
Expand Down

0 comments on commit d5ded0d

Please sign in to comment.