use of org.mule.runtime.extension.api.annotation.metadata.MetadataKeyId in project mule by mulesoft.
the class MetadataKeyIdObjectResolver method resolve.
/**
* Returns the populated key in the Type that the component parameter requires by looking for default values, if no
* {@link MetadataKeyId} is present an empty value is returned since is a key less component.
* <p>
* If a key should be built and there is at least one default value missing an {@link IllegalArgumentException} is thrown.
*
* @return a new instance of the {@link MetadataKeyId} parameter {@code type}.
* @throws MetadataResolvingException if the Parameter type is not instantiable.
* @throws IllegalArgumentException if cannot found the required default values for an specified key.
*/
public Object resolve() throws MetadataResolvingException {
if (isKeyLess()) {
return NullMetadataKey.ID;
}
if (!keyParts.stream().allMatch(p -> p.getDefaultValue() != null)) {
throw new IllegalArgumentException("Could not build metadata key from an object that does" + " not have a default value for all it's components.");
}
String id = keyParts.get(0).getDefaultValue().toString();
final MetadataKeyIdModelProperty keyIdModelProperty = findMetadataKeyIdModelProperty(component);
MetadataType type = keyIdModelProperty.getType();
KeyMetadataTypeVisitor visitor = new KeyMetadataTypeVisitor(id, getType(type)) {
@Override
protected Map<Field, String> getFieldValuesMap() {
return keyParts.stream().filter(p -> p.getModelProperty(DeclaringMemberModelProperty.class).isPresent()).collect(toMap(p -> p.getModelProperty(DeclaringMemberModelProperty.class).get().getDeclaringField(), p -> p.getDefaultValue().toString()));
}
};
type.accept(visitor);
return visitor.getResultId();
}
Aggregations