use of org.mule.metadata.api.model.MetadataType in project mule by mulesoft.
the class SourceModelLoaderDelegate method resolveOutputTypes.
private void resolveOutputTypes(SourceDeclarer source, SourceElement sourceType) {
MetadataType returnMetadataType = sourceType.getReturnMetadataType();
source.withOutput().ofType(returnMetadataType);
source.withOutputAttributes().ofType(sourceType.getAttributesMetadataType());
source.supportsStreaming(isInputStream(returnMetadataType) || sourceType.getAnnotation(Streaming.class).isPresent());
}
use of org.mule.metadata.api.model.MetadataType in project mule by mulesoft.
the class NullSafeModelValidator method validate.
@Override
public void validate(ExtensionModel extensionModel, ProblemsReporter problemsReporter) {
ReflectionCache reflectionCache = new ReflectionCache();
TypeLoader typeLoader = ExtensionsTypeLoaderFactory.getDefault().createTypeLoader();
new ExtensionWalker() {
@Override
public void onParameter(ParameterizedModel owner, ParameterGroupModel groupModel, ParameterModel model) {
model.getType().accept(new MetadataTypeVisitor() {
@Override
public void visitObject(ObjectType objectType) {
if (objectType.getMetadataFormat().equals(JAVA) && !isMap(objectType)) {
objectType.getAnnotation(TypeIdAnnotation.class).map(TypeIdAnnotation::getValue).ifPresent(typeId -> typeLoader.load(typeId).ifPresent(fieldMetadataType -> objectType.getFields().stream().filter(f -> f.getAnnotation(NullSafeTypeAnnotation.class).isPresent()).forEach(f -> validateField(getLocalPart(f), f, getType(fieldMetadataType), f.getAnnotation(NullSafeTypeAnnotation.class).get()))));
}
}
private void validateField(String fieldName, ObjectFieldType field, Class<?> declaringClass, NullSafeTypeAnnotation nullSafeTypeAnnotation) {
Class<?> nullSafeType = nullSafeTypeAnnotation.getType();
Class<?> fieldType = getType(field.getValue());
boolean hasDefaultOverride = nullSafeTypeAnnotation.hasDefaultOverride();
field.getValue().accept(new BasicTypeMetadataVisitor() {
@Override
protected void visitBasicType(MetadataType metadataType) {
problemsReporter.addError(new Problem(extensionModel, format("Field '%s' in class '%s' is annotated with '@%s' but is of type '%s'. That annotation can only be " + "used with complex types (Pojos, Lists, Maps)", fieldName, declaringClass.getName(), NullSafe.class.getSimpleName(), fieldType.getName())));
}
@Override
public void visitArrayType(ArrayType arrayType) {
if (hasDefaultOverride) {
problemsReporter.addError(new Problem(extensionModel, format("Field '%s' in class '%s' is annotated with '@%s' is of type '%s'" + " but a 'defaultImplementingType' was provided." + " Type override is not allowed for Collections", fieldName, declaringClass.getName(), NullSafe.class.getSimpleName(), fieldType.getName())));
}
}
@Override
public void visitObject(ObjectType objectType) {
String requiredFields = objectType.getFields().stream().filter(f -> f.isRequired() && !isFlattenedParameterGroup(f)).map(MetadataTypeUtils::getLocalPart).collect(joining(", "));
if (!isBlank(requiredFields) && isCompiletime(extensionModel)) {
problemsReporter.addError(new Problem(model, format("Class '%s' cannot be used with '@%s' parameter since it contains non optional fields: [%s]", getId(objectType).orElse(""), NullSafe.class.getSimpleName(), requiredFields)));
}
if (objectType.isOpen()) {
if (hasDefaultOverride) {
problemsReporter.addError(new Problem(model, format("Field '%s' in class '%s' is annotated with '@%s' is of type '%s'" + " but a 'defaultImplementingType' was provided." + " Type override is not allowed for Maps", fieldName, declaringClass.getName(), NullSafe.class.getSimpleName(), fieldType.getName())));
}
return;
}
if (hasDefaultOverride && isInstantiable(fieldType, reflectionCache)) {
problemsReporter.addError(new Problem(model, format("Field '%s' in class '%s' is annotated with '@%s' is of concrete type '%s'," + " but a 'defaultImplementingType' was provided." + " Type override is not allowed for concrete types", fieldName, declaringClass.getName(), NullSafe.class.getSimpleName(), fieldType.getName())));
}
if (!isInstantiable(nullSafeType, reflectionCache)) {
problemsReporter.addError(new Problem(model, format("Field '%s' in class '%s' is annotated with '@%s' but is of type '%s'. That annotation can only be " + "used with complex instantiable types (Pojos, Lists, Maps)", fieldName, declaringClass.getName(), NullSafe.class.getSimpleName(), nullSafeType.getName())));
}
if (hasDefaultOverride && !fieldType.isAssignableFrom(nullSafeType)) {
problemsReporter.addError(new Problem(model, format("Field '%s' in class '%s' is annotated with '@%s' of type '%s', but provided type '%s" + " is not a subtype of the parameter's type", fieldName, declaringClass.getName(), NullSafe.class.getSimpleName(), fieldType.getName(), nullSafeType.getName())));
}
}
});
}
});
}
}.walk(extensionModel);
}
use of org.mule.metadata.api.model.MetadataType in project mule by mulesoft.
the class ImplicitObjectUtils method buildImplicitResolverSet.
/**
* Creates a {@link ResolverSet} based on the default values for the {@link ParameterModel}s in the {@code parametrizedModel}.
* <p>
* If a {@link ParameterModel} returns {@code null} for {@link ParameterModel#getDefaultValue()} then it's ignored
*
* @param model a model holding the {@link ParameterModel}s to consider
* @param muleContext the Mule node.
* @return a {@link ResolverSet}
*/
public static ResolverSet buildImplicitResolverSet(ParameterizedModel model, ReflectionCache reflectionCache, MuleContext muleContext) {
ResolverSet resolverSet = new HashedResolverSet(muleContext);
ParametersResolver parametersResolver = ParametersResolver.fromDefaultValues(model, muleContext, reflectionCache);
for (ParameterGroupModel groupModel : model.getParameterGroupModels()) {
Optional<ParameterGroupDescriptor> descriptor = groupModel.getModelProperty(ParameterGroupModelProperty.class).map(ParameterGroupModelProperty::getDescriptor);
if (descriptor.isPresent() && groupModel.getParameterModels().stream().noneMatch(ParameterModel::isRequired)) {
String groupKey = getContainerName(descriptor.get().getContainer());
resolverSet.add(groupKey, NullSafeValueResolverWrapper.of(new StaticValueResolver<>(null), descriptor.get().getMetadataType(), reflectionCache, muleContext, parametersResolver));
} else {
groupModel.getParameterModels().forEach(parameterModel -> {
Object defaultValue = parameterModel.getDefaultValue();
ValueResolver<?> resolver;
if (defaultValue instanceof String) {
resolver = getExpressionBasedValueResolver((String) defaultValue, parameterModel.getType(), muleContext);
} else {
resolver = new StaticValueResolver<>(null);
}
if (parameterModel.getModelProperty(NullSafeModelProperty.class).isPresent()) {
MetadataType metadataType = parameterModel.getModelProperty(NullSafeModelProperty.class).get().defaultType();
resolver = NullSafeValueResolverWrapper.of(resolver, metadataType, reflectionCache, muleContext, parametersResolver);
}
resolverSet.add(parameterModel.getName(), resolver);
});
}
}
return resolverSet;
}
use of org.mule.metadata.api.model.MetadataType in project mule by mulesoft.
the class ParametersResolver method resolveParameters.
/**
* {@inheritDoc}
*/
@Override
public void resolveParameters(ObjectType objectType, DefaultObjectBuilder builder) {
final Class<?> objectClass = getType(objectType);
final boolean isParameterGroup = isFlattenedParameterGroup(objectType);
objectType.getFields().forEach(field -> {
final String key = getLocalPart(field);
ValueResolver<?> valueResolver = null;
Field objectField = getField(objectClass, key);
if (parameters.containsKey(key)) {
valueResolver = toValueResolver(parameters.get(key));
} else if (!isParameterGroup) {
valueResolver = getDefaultValue(field).isPresent() ? getFieldDefaultValueValueResolver(field, muleContext) : null;
}
Optional<NullSafeTypeAnnotation> nullSafe = field.getAnnotation(NullSafeTypeAnnotation.class);
if (nullSafe.isPresent()) {
ValueResolver<?> delegate = valueResolver != null ? valueResolver : new StaticValueResolver<>(null);
MetadataType type = getMetadataType(nullSafe.get().getType(), ExtensionsTypeLoaderFactory.getDefault().createTypeLoader());
valueResolver = NullSafeValueResolverWrapper.of(delegate, type, reflectionCache, muleContext, this);
}
if (field.getAnnotation(ConfigOverrideTypeAnnotation.class).isPresent()) {
valueResolver = ConfigOverrideValueResolverWrapper.of(valueResolver != null ? valueResolver : new StaticValueResolver<>(null), key, reflectionCache, muleContext);
}
if (valueResolver != null) {
try {
initialiseIfNeeded(valueResolver, true, muleContext);
builder.addPropertyResolver(objectField.getName(), valueResolver);
} catch (InitialisationException e) {
throw new MuleRuntimeException(e);
}
} else if (field.isRequired() && !isFlattenedParameterGroup(field) && !lazyInitEnabled) {
throw new RequiredParameterNotSetException(objectField.getName());
}
});
}
use of org.mule.metadata.api.model.MetadataType in project mule by mulesoft.
the class IntrospectionUtilsTestCase method getPagingProviderImplementationTypesReturnType.
@Test
public void getPagingProviderImplementationTypesReturnType() throws Exception {
OperationElement getPagingProvider = getMethod("getPagingProvider");
MetadataType methodReturnType = IntrospectionUtils.getMethodReturnType(getPagingProvider);
assertThat(methodReturnType, instanceOf(ArrayType.class));
MetadataType valueType = ((ArrayType) methodReturnType).getType();
assertThat(valueType, instanceOf(MessageMetadataType.class));
MessageMetadataType messageType = (MessageMetadataType) valueType;
MetadataType payloadType = messageType.getPayloadType().get();
MetadataType attributesTypes = messageType.getAttributesType().get();
// These assertions are too simple, but AST Loader doesn't enrich with the same annotations as the Java does
// making impossible to do an equals assertion.
assertThat(payloadType.getAnnotation(TypeIdAnnotation.class).get().getValue(), is(Banana.class.getName()));
assertThat(attributesTypes.getAnnotation(TypeIdAnnotation.class).get().getValue(), is(Apple.class.getName()));
}
Aggregations