use of org.mule.metadata.api.model.ArrayType in project mule by mulesoft.
the class ParameterModelsLoaderDelegate method parseNullSafe.
private void parseNullSafe(ExtensionParameter extensionParameter, ParameterDeclarer parameter) {
if (extensionParameter.isAnnotatedWith(NullSafe.class)) {
if (extensionParameter.isAnnotatedWith(ConfigOverride.class)) {
throw new IllegalParameterModelDefinitionException(format("Parameter '%s' is annotated with '@%s' and also marked as a config override, which is redundant. " + "The default value for this parameter will come from the configuration parameter", extensionParameter.getName(), NullSafe.class.getSimpleName()));
}
if (extensionParameter.isRequired() && !extensionParameter.isAnnotatedWith(ParameterGroup.class)) {
throw new IllegalParameterModelDefinitionException(format("Parameter '%s' is required but annotated with '@%s', which is redundant", extensionParameter.getName(), NullSafe.class.getSimpleName()));
}
Type nullSafeAnnotationType = extensionParameter.getValueFromAnnotation(NullSafe.class).get().getClassValue(NullSafe::defaultImplementingType);
final boolean hasDefaultOverride = !nullSafeAnnotationType.isSameType(Object.class);
MetadataType nullSafeType = hasDefaultOverride ? nullSafeAnnotationType.asMetadataType() : parameter.getDeclaration().getType();
boolean isInstantiable = hasDefaultOverride ? nullSafeAnnotationType.isInstantiable() : extensionParameter.getType().isInstantiable();
parameter.getDeclaration().getType().accept(new BasicTypeMetadataVisitor() {
@Override
protected void visitBasicType(MetadataType metadataType) {
throw new IllegalParameterModelDefinitionException(format("Parameter '%s' is annotated with '@%s' but is of type '%s'. That annotation can only be " + "used with complex types (Pojos, Lists, Maps)", extensionParameter.getName(), NullSafe.class.getSimpleName(), extensionParameter.getType().getName()));
}
@Override
public void visitArrayType(ArrayType arrayType) {
if (hasDefaultOverride) {
throw new IllegalParameterModelDefinitionException(format("Parameter '%s' is annotated with '@%s' is of type '%s'" + " but a 'defaultImplementingType' was provided." + " Type override is not allowed for Collections", extensionParameter.getName(), NullSafe.class.getSimpleName(), extensionParameter.getType().getName()));
}
}
@Override
public void visitObject(ObjectType objectType) {
if (hasDefaultOverride && isMap(objectType)) {
throw new IllegalParameterModelDefinitionException(format("Parameter '%s' is annotated with '@%s' is of type '%s'" + " but a 'defaultImplementingType' was provided." + " Type override is not allowed for Maps", extensionParameter.getName(), NullSafe.class.getSimpleName(), extensionParameter.getType().getName()));
}
if (hasDefaultOverride && extensionParameter.getType().isInstantiable()) {
throw new IllegalParameterModelDefinitionException(format("Parameter '%s' is annotated with '@%s' is of concrete type '%s'," + " but a 'defaultImplementingType' was provided." + " Type override is not allowed for concrete types", extensionParameter.getName(), NullSafe.class.getSimpleName(), extensionParameter.getType().getName()));
}
if (!isInstantiable && !isMap(nullSafeType)) {
throw new IllegalParameterModelDefinitionException(format("Parameter '%s' is annotated with '@%s' but is of type '%s'. That annotation can only be " + "used with complex instantiable types (Pojos, Lists, Maps)", extensionParameter.getName(), NullSafe.class.getSimpleName(), extensionParameter.getType().getName()));
}
if (hasDefaultOverride && !extensionParameter.getType().isAssignableFrom(nullSafeAnnotationType)) {
throw new IllegalParameterModelDefinitionException(format("Parameter '%s' is annotated with '@%s' of type '%s', but provided type '%s" + " is not a subtype of the parameter's type", extensionParameter.getName(), NullSafe.class.getSimpleName(), extensionParameter.getType().getName(), getType(nullSafeType).getName()));
}
}
});
parameter.withModelProperty(new NullSafeModelProperty(nullSafeType));
if (hasDefaultOverride) {
parameter.withModelProperty(new DefaultImplementingTypeModelProperty(nullSafeType));
}
}
}
use of org.mule.metadata.api.model.ArrayType in project mule by mulesoft.
the class MetadataOutputDelegate method adaptToListIfNecessary.
private MetadataType adaptToListIfNecessary(MetadataType resolvedType, Object key, MetadataContext metadataContext) throws MetadataResolvingException {
MetadataType componentOutputType = ((HasOutputModel) component).getOutput().getType();
if (!isCollection(componentOutputType) || isVoid(resolvedType) || isNullType(resolvedType)) {
return resolvedType;
}
MetadataType collectionValueType = ((ArrayType) componentOutputType).getType();
Class<?> collectionType = getCollectionType(collectionValueType);
if (Message.class.equals(collectionType)) {
MessageMetadataType message = (MessageMetadataType) collectionValueType;
resolvedType = wrapInMessageType(resolvedType, key, metadataContext, message.getAttributesType());
}
return metadataContext.getTypeBuilder().arrayType().with(new ClassInformationAnnotation(getCollectionType(componentOutputType))).of(resolvedType).build();
}
use of org.mule.metadata.api.model.ArrayType in project mule by mulesoft.
the class IntrospectionUtils method getReturnType.
private static MetadataType getReturnType(Type returnType) {
Type type = returnType;
if (returnType.isAssignableTo(Result.class)) {
List<TypeGeneric> generics = returnType.getGenerics();
if (generics.isEmpty()) {
return typeBuilder().anyType().build();
}
Type payloadType = generics.get(0).getConcreteType();
if (!payloadType.isAnyType()) {
type = payloadType;
} else {
type = null;
}
}
if (isPagingProvider(returnType)) {
Type itemType = getPagingProviderTypes(returnType).getSecond();
if (itemType.isSameType(Result.class)) {
return returnListOfMessagesType(returnType, itemType);
} else {
return typeBuilder().arrayType().of(itemType.asMetadataType()).with(returnType.getClassInformation()).build();
}
}
if (returnType.isAssignableTo(ParameterResolver.class) || returnType.isAssignableTo(TypedValue.class) || returnType.isAssignableTo(Literal.class)) {
type = returnType.getGenerics().get(0).getConcreteType();
}
if (isCollection(returnType) && !returnType.getGenerics().isEmpty()) {
Type itemType = returnType.getGenerics().get(0).getConcreteType();
if (itemType.isAssignableTo(Result.class)) {
return returnListOfMessagesType(returnType, itemType);
}
}
return type != null ? type.asMetadataType() : typeBuilder().anyType().build();
}
use of org.mule.metadata.api.model.ArrayType in project mule by mulesoft.
the class IntrospectionUtils method toDataType.
/**
* Transforms a {@link MetadataType} and generates the correspondent {@link DataType}
*
* @param metadataType to introspect a create a {@link DataType} from it.
* @return a {@link DataType} based on the given {@link MetadataType}
*/
public static DataType toDataType(MetadataType metadataType) {
Class<?> type = getType(metadataType).orElse(null);
if (type == null) {
return DataType.fromType(Object.class);
}
Reference<DataType> dataType = new Reference<>();
metadataType.accept(new MetadataTypeVisitor() {
@Override
protected void defaultVisit(MetadataType metadataType) {
dataType.set(DataType.fromType(type));
}
@Override
public void visitArrayType(ArrayType arrayType) {
Class itemClass = getType(arrayType.getType()).get();
if (Collection.class.isAssignableFrom(type)) {
dataType.set(DataType.builder().collectionType((Class<? extends Collection>) type).itemType(itemClass).build());
} else if (Iterator.class.isAssignableFrom(type)) {
dataType.set(DataType.builder().streamType((Class<? extends Iterator>) type).itemType(itemClass).build());
} else {
defaultVisit(arrayType);
}
}
@Override
public void visitObject(ObjectType objectType) {
if (Map.class.isAssignableFrom(type)) {
dataType.set(DataType.builder().mapType((Class<? extends Map>) type).keyType(String.class).valueType(objectType.getOpenRestriction().map(restriction -> {
if (restriction.getAnnotation(TypedValueTypeAnnotation.class).isPresent()) {
return TypedValue.class;
}
return getType(restriction).get();
}).orElse(Object.class)).build());
} else {
defaultVisit(objectType);
}
}
});
return dataType.get();
}
use of org.mule.metadata.api.model.ArrayType in project mule by mulesoft.
the class IntrospectionUtils method collectRelativeClassesAsString.
/**
* Given a {@link MetadataType} it adds all the {@link Class} that are related from that type. This includes generics of an
* {@link ArrayType}, open restriction of an {@link ObjectType} as well as its fields.
*
* @param type {@link MetadataType} to inspect
* @return {@link Set<Class>>} with the classes reachable from the {@code type}
*/
public static Set<String> collectRelativeClassesAsString(MetadataType type) {
Set<String> relativeClasses = new HashSet<>();
type.accept(new MetadataTypeVisitor() {
@Override
public void visitArrayType(ArrayType arrayType) {
arrayType.getType().accept(this);
}
@Override
public void visitObjectField(ObjectFieldType objectFieldType) {
objectFieldType.getValue().accept(this);
}
@Override
public void visitObject(ObjectType objectType) {
if (objectType.getMetadataFormat() != JAVA) {
return;
}
final String clazz = getId(objectType).orElse(null);
if (clazz == null || relativeClasses.contains(clazz)) {
return;
}
Optional<ClassInformationAnnotation> classInformation = objectType.getAnnotation(ClassInformationAnnotation.class);
classInformation.ifPresent(classInformationAnnotation -> relativeClasses.addAll(classInformationAnnotation.getGenericTypes()));
relativeClasses.add(clazz);
objectType.getFields().forEach(objectFieldType -> objectFieldType.accept(this));
objectType.getOpenRestriction().ifPresent(t -> t.accept(this));
}
@Override
public void visitString(StringType stringType) {
if (stringType.getMetadataFormat() == JAVA && isEnum(stringType)) {
getId(stringType).ifPresent(relativeClasses::add);
}
}
});
return relativeClasses;
}
Aggregations