下面列出了怎么用org.springframework.boot.configurationmetadata.ValueHint的API类实例代码及写法,或者点击链接到github查看源代码。
void addValueHintsProposals(final String dsl, AppRegistration appRegistration, final List<CompletionProposal> collector, final String propertyName, final ValueHintProvider[] valueHintProviders){
final Resource metadataResource = this.appRegistry.getAppMetadataResource(appRegistration);
if (metadataResource != null) {
final URLClassLoader classLoader = metadataResolver.createAppClassLoader(metadataResource);
this.doWithClassLoader(classLoader, () -> {
CompletionProposal.Factory proposals = CompletionProposal.expanding(dsl);
List<ConfigurationMetadataProperty> whiteList = metadataResolver.listProperties(metadataResource);
for (ConfigurationMetadataProperty property : metadataResolver.listProperties(metadataResource, true)) {
if (CompletionUtils.isMatchingProperty(propertyName, property, whiteList)) {
for (ValueHintProvider valueHintProvider : valueHintProviders) {
for (ValueHint valueHint : valueHintProvider.generateValueHints(property, classLoader)) {
collector.add(proposals.withSuffix(String.valueOf(valueHint.getValue()),
valueHint.getShortDescription()));
}
}
}
}
return null;
});
}
}
@Override
public List<ValueHint> generateValueHints(ConfigurationMetadataProperty property, ClassLoader classLoader) {
List<ValueHint> result = new ArrayList<>();
if (ClassUtils.isPresent(property.getType(), classLoader)) {
Class<?> clazz = ClassUtils.resolveClassName(property.getType(), classLoader);
if (clazz.isEnum()) {
for (Object o : clazz.getEnumConstants()) {
ValueHint hint = new ValueHint();
hint.setValue(o);
result.add(hint);
}
}
}
return result;
}
public static void completeBoolean(String filter, Consumer<ValueHint> consumer) {
if ("true".contains(filter)) {
consumer.accept(Utils.createHint("true"));
}
if ("false".contains(filter)) {
consumer.accept(Utils.createHint("false"));
}
}
public static void completeCharset(String filter, Consumer<ValueHint> consumer) {
HintSupport.getAllCharsets().stream()
.filter(chrsName -> chrsName.toLowerCase().contains(filter.toLowerCase()))
.forEachOrdered(chrsName -> {
consumer.accept(Utils.createHint(chrsName));
});
}
public static void completeLocale(String filter, Consumer<ValueHint> consumer) {
HintSupport.getAllLocales().stream()
.filter(lclName -> lclName.toLowerCase().contains(filter.toLowerCase()))
.forEachOrdered(lclName -> {
consumer.accept(Utils.createHint(lclName));
});
}
public static void completeMimetype(String filter, Consumer<ValueHint> consumer) {
HintSupport.MIMETYPES.stream()
.filter(mime -> mime.toLowerCase().contains(filter.toLowerCase()))
.forEachOrdered(mime -> {
consumer.accept(Utils.createHint(mime));
});
}
public static void completeEnum(ClassPath cp, String dataType, String filter, Consumer<ValueHint> consumer) {
try {
Object[] enumvals = cp.getClassLoader(true).loadClass(dataType).getEnumConstants();
if (enumvals != null) {
for (Object val : enumvals) {
final String valName = val.toString().toLowerCase();
if (filter == null || valName.contains(filter)) {
consumer.accept(createEnumHint(valName));
}
}
}
} catch (ClassNotFoundException ex) {
// enum not available in project classpath, no completion possible
}
}
boolean addAlreadyTypedValueHintsProposals(final String text, AppRegistration appRegistration, final List<CompletionProposal> collector, final String propertyName, final ValueHintProvider[] valueHintProviders, final String alreadyTyped){
final Resource metadataResource = this.appRegistry.getAppMetadataResource(appRegistration);
boolean result = false;
if (metadataResource == null) {
result = false;
} else {
final URLClassLoader classLoader = metadataResolver.createAppClassLoader(metadataResource);
result = this.doWithClassLoader(classLoader, () -> {
CompletionProposal.Factory proposals = CompletionProposal.expanding(text);
List<ConfigurationMetadataProperty> allProps = metadataResolver.listProperties(metadataResource, true);
List<ConfigurationMetadataProperty> whiteListedProps = metadataResolver.listProperties(metadataResource);
for (ConfigurationMetadataProperty property : allProps) {
if (CompletionUtils.isMatchingProperty(propertyName, property, whiteListedProps)) {
for (ValueHintProvider valueHintProvider : valueHintProviders) {
List<ValueHint> valueHints = valueHintProvider.generateValueHints(property, classLoader);
if (!valueHints.isEmpty() && valueHintProvider.isExclusive(property)) {
collector.clear();
}
for (ValueHint valueHint : valueHints) {
String candidate = String.valueOf(valueHint.getValue());
if (!candidate.equals(alreadyTyped) && candidate.startsWith(alreadyTyped)) {
collector.add(proposals.withSuffix(candidate.substring(alreadyTyped.length()),
valueHint.getShortDescription()));
}
}
if (!valueHints.isEmpty() && valueHintProvider.isExclusive(property)) {
return true;
}
}
}
}
return false;
});
}
return result;
}
@Override
public List<ValueHint> generateValueHints(ConfigurationMetadataProperty property, ClassLoader classLoader) {
List<ValueHint> result = new ArrayList<>();
if (ClassUtils.isPresent(property.getType(), classLoader)) {
Class<?> clazz = ClassUtils.resolveClassName(property.getType(), classLoader);
if (clazz.isEnum()) {
for (Object o : clazz.getEnumConstants()) {
ValueHint hint = new ValueHint();
hint.setValue(o);
result.add(hint);
}
}
}
return result;
}
@Override
public List<ValueHint> generateValueHints(ConfigurationMetadataProperty property, ClassLoader classLoader) {
return property.getValueHints();
}
@Override
public List<ValueHint> generateValueHints(ConfigurationMetadataProperty property, ClassLoader classLoader) {
return "java.lang.Boolean".equals(property.getType())
? BOOLEANS
: Collections.<ValueHint>emptyList();
}
private void completePropName(CompletionResultSet completionResultSet, String filter, int startOffset, int caretOffset) {
final Preferences prefs = NbPreferences.forModule(PrefConstants.class);
final boolean bDeprLast = prefs.getBoolean(PREF_DEPR_SORT_LAST, true);
final boolean bErrorShow = prefs.getBoolean(PREF_DEPR_ERROR_SHOW, true);
long mark = System.currentTimeMillis();
// check if completing a property map key
if (filter != null) {
ClassPath cpExec = Utils.execClasspathForProj(proj);
for (String mapProp : sbs.getMapPropertyNames()) {
if (filter.length() > mapProp.length() && filter.contains(mapProp)) {
String key = filter.substring(mapProp.length() + 1);
logger.log(FINER, "Completing key for map property {0} from: ''{1}''", new Object[]{mapProp, key});
final ConfigurationMetadataProperty propMetadata = sbs.getPropertyMetadata(mapProp);
// if key data type is an enum complete with enum values
final String keyDataType = extractMapKeyType(propMetadata);
if (!keyDataType.contains("<")) {
Utils.completeEnum(cpExec, keyDataType, key, hint -> {
completionResultSet.addItem(new KeyCompletionItem(hint, startOffset + mapProp.length() + 1,
caretOffset));
});
}
// check if key data type is boolean
if (keyDataType.equals("java.lang.Boolean")) {
Utils.completeBoolean(key, hint -> {
completionResultSet.addItem(new KeyCompletionItem(hint, startOffset + mapProp.length() + 1,
caretOffset));
});
}
// check if key data type is Charset
if (keyDataType.equals("java.nio.charset.Charset")) {
Utils.completeCharset(key, hint -> {
completionResultSet.addItem(new KeyCompletionItem(hint, startOffset + mapProp.length() + 1,
caretOffset));
});
}
// add metadata defined key hints to completion list
final Hints hints = propMetadata.getHints();
if (!hints.getKeyHints().isEmpty()) {
String keyLowcase = key.toLowerCase();
for (ValueHint keyHint : hints.getKeyHints()) {
if (keyHint.getValue().toString().toLowerCase().contains(keyLowcase)) {
completionResultSet.addItem(new KeyCompletionItem(keyHint, startOffset + mapProp.length() + 1,
caretOffset));
}
}
}
// invoke key providers
if (!hints.getKeyProviders().isEmpty()) {
logger.log(FINER, "Key providers for {0}:", mapProp);
for (ValueProvider vp : hints.getKeyProviders()) {
logger.log(FINER, " {0} - params: {1}", new Object[]{vp.getName(), vp.getParameters()});
sbs.getHintProvider(vp.getName()).provide(vp.getParameters(), propMetadata, key, true,
completionResultSet, startOffset + mapProp.length() + 1, caretOffset);
}
}
}
}
}
for (ConfigurationMetadataProperty propMeta : sbs.queryPropertyMetadata(filter)) {
if (Utils.isErrorDeprecated(propMeta)) {
// show error level deprecated props based on pref
if (bErrorShow) {
completionResultSet.addItem(new CfgPropCompletionItem(propMeta, startOffset, caretOffset, bDeprLast));
}
} else {
completionResultSet.addItem(new CfgPropCompletionItem(propMeta, startOffset, caretOffset, bDeprLast));
}
}
final long elapsed = System.currentTimeMillis() - mark;
logger.log(FINE, "Name completion of ''{0}'' took: {1} msecs", new Object[]{filter, elapsed});
}
public ValueCompletionItem(ValueHint hint, int dotOffset, int caretOffset) {
this(hint, dotOffset, caretOffset, false);
}
public ValueCompletionItem(ValueHint hint, int dotOffset, int caretOffset, boolean continueCompletion) {
this.hint = hint;
this.dotOffset = dotOffset;
this.caretOffset = caretOffset;
this.continueCompletion = continueCompletion;
}
public ValueHint getHint() {
return hint;
}
public KeyCompletionItem(ValueHint hint, int dotOffset, int caretOffset) {
this.hint = hint;
this.dotOffset = dotOffset;
this.caretOffset = caretOffset;
}
public ValueHint getHint() {
return hint;
}
public CfgPropValueCompletionDocumentation(ValueHint valueHint) {
this.valueHint = valueHint;
}
@Override
public String getText() {
StringBuilder sb = new StringBuilder();
// name
sb.append("<b>").append(configurationMeta.getId()).append("</b>");
// type (may be null for deprecated props)
final String type = configurationMeta.getType();
if (type != null) {
sb.append("<br/>").append(simpleHtmlEscape(type));
}
// deprecation (optional)
Deprecation deprecation = configurationMeta.getDeprecation();
if (deprecation != null) {
sb.append("<br/><br/><b><i>");
if (Utils.isErrorDeprecated(configurationMeta)) {
sb.append("REMOVED");
} else {
sb.append("Deprecated");
}
// deprecation reason if present
String reason = deprecation.getReason();
if (reason != null) {
sb.append(":</i></b> ").append(simpleHtmlEscape(reason));
} else {
sb.append("</i></b>");
}
String replacement = deprecation.getReplacement();
if (replacement != null) {
sb.append("<br/><i>Replaced by:</i> <code>").append(replacement).append("</code>");
}
}
// default value (optional)
final Object defaultValue = configurationMeta.getDefaultValue();
if (null != defaultValue) {
sb.append("<br/><br/><i>Default:</i> ");
if (defaultValue instanceof Object[]) {
sb.append(Arrays.toString((Object[]) defaultValue));
} else {
sb.append(String.valueOf(defaultValue));
}
}
// description (optional)
final String description = configurationMeta.getDescription();
if (description != null) {
sb.append("<br/><br/>").append(description);
}
// list of values (optional)
Hints hints = configurationMeta.getHints();
List<ValueHint> valueHints = hints.getValueHints();
if (valueHints != null && !valueHints.isEmpty()) {
sb.append("<br/><br/><table><tr><td><i>Value</i></td><td><i>Description</i></td></tr>");
for (ValueHint vHint : valueHints) {
sb.append("<tr><td>").append(vHint.getValue()).append("</td><td>");
final String vDesc = vHint.getDescription();
if (vDesc != null) {
sb.append(simpleHtmlEscape(vDesc)).append("</th></tr>");
}
}
sb.append("</table>");
}
return sb.toString();
}
@Override
public List<ValueHint> generateValueHints(ConfigurationMetadataProperty property, ClassLoader classLoader) {
return property.getHints().getValueHints();
}
@Override
public List<ValueHint> generateValueHints(ConfigurationMetadataProperty property, ClassLoader classLoader) {
return "java.lang.Boolean".equals(property.getType()) ? BOOLEANS : Collections.<ValueHint>emptyList();
}
/**
* Create a {@code ValueHint} object from the given value and description.
*
* @param value the value to use
* @param description the description to use
* @return a ValueHint object
*/
public static ValueHint createHint(String value, String description) {
ValueHint vh = new ValueHint();
vh.setValue(value);
vh.setDescription(description);
return vh;
}
/**
* For a given property, return a list of {@link ValueHint} that may apply.
*
* @param property property for which to generate value hints
* @param classLoader class loader for the artifact/module that this
* property applies to; this may be used to load other classes/resources
* for generating value hints
* @return list of value hints for the provided property
*/
List<ValueHint> generateValueHints(ConfigurationMetadataProperty property, ClassLoader classLoader);
/**
* Create a {@code ValueHint} object from the given value.
* <p>
* Created hint has no description.
*
* @param value the value to use
* @return a ValueHint object
*/
public static ValueHint createHint(String value) {
ValueHint vh = new ValueHint();
vh.setValue(value);
return vh;
}
/**
* Create a {@code ValueHint} object from the given java enumeration value.
* <p>
* Created hint has no description and has a Spring Boot property name canonical format.
*
* @param value the value to use
* @return a ValueHint object
*/
public static ValueHint createEnumHint(String value) {
ValueHint vh = new ValueHint();
vh.setValue(value.replaceAll("_", "-"));
return vh;
}
/**
* For a given property, return a list of {@link ValueHint} that may apply.
*
* @param property property for which to generate value hints
* @param classLoader class loader for the artifact/module that this property applies
* to; this may be used to load other classes/resources for generating value hints
* @return list of value hints for the provided property
*/
List<ValueHint> generateValueHints(ConfigurationMetadataProperty property, ClassLoader classLoader);