use of org.mule.metadata.api.model.MetadataType 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.MetadataType in project mule by mulesoft.
the class IntrospectionUtils method getMethodArgumentTypes.
/**
* Returns an array of {@link MetadataType} representing each of the given {@link Method}'s argument types.
*
* @param method a not {@code null} {@link Method}
* @param typeLoader a {@link ClassTypeLoader} to be used to create the returned {@link MetadataType}s
* @return an array of {@link MetadataType} matching the method's arguments. If the method doesn't take any, then the array will
* be empty
* @throws IllegalArgumentException is method is {@code null}
*/
public static MetadataType[] getMethodArgumentTypes(Method method, ClassTypeLoader typeLoader) {
checkArgument(method != null, "Can't introspect a null method");
Class<?>[] parameters = method.getParameterTypes();
if (isEmpty(parameters)) {
return new MetadataType[] {};
}
MetadataType[] types = new MetadataType[parameters.length];
for (int i = 0; i < parameters.length; i++) {
ResolvableType type = ResolvableType.forMethodParameter(method, i);
types[i] = typeLoader.load(type.getType());
}
return types;
}
use of org.mule.metadata.api.model.MetadataType in project mule by mulesoft.
the class IntrospectionUtils method getMethodReturnAttributesType.
/**
* Returns a {@link MetadataType} representing the {@link Result#getAttributes()} that will be set after executing the given
* {@code method}.
* <p>
* If the {@code method} returns a {@link Result}, then it returns the type of the {@code Attributes} generic. In any other case
* (including raw uses of {@link Result}) it will return a {@link VoidType}
* <p>
* If the {@code method} returns a collection or a {@link PagingProvider} of {@link Result}, then this will return
* {@link VoidType} since the messages in the main output already contain an attributes for each item.
*
* @param method the {@link Method} being introspected
* @return a {@link MetadataType}
* @throws IllegalArgumentException is method is {@code null}
*/
public static MetadataType getMethodReturnAttributesType(MethodElement method) {
Type returnType = method.getReturnType();
Type attributesType = null;
if (returnType.isAssignableTo(Result.class)) {
List<TypeGeneric> generics = returnType.getGenerics();
if (generics.size() == 2) {
Type genericType = generics.get(1).getConcreteType();
if (genericType != null) {
if (genericType.isAnyType()) {
return typeBuilder().anyType().build();
} else {
attributesType = genericType;
}
} else {
return typeBuilder().anyType().build();
}
} else {
return typeBuilder().anyType().build();
}
}
if (isPagingProvider(returnType)) {
Type second = getPagingProviderTypes(returnType).getSecond();
if (second.isSameType(Result.class)) {
attributesType = null;
}
}
if (isCollection(returnType)) {
List<TypeGeneric> generics = returnType.getGenerics();
if (generics.size() > 0) {
Type genericType = generics.get(0).getConcreteType();
if (genericType.isAssignableTo(Result.class)) {
attributesType = null;
}
}
}
return attributesType != null ? attributesType.asMetadataType() : typeBuilder().voidType().build();
}
use of org.mule.metadata.api.model.MetadataType 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;
}
use of org.mule.metadata.api.model.MetadataType in project mule by mulesoft.
the class IntrospectionUtils method returnListOfMessagesType.
private static MetadataType returnListOfMessagesType(Type returnType, Type resultType) {
if (resultType.getGenerics().isEmpty()) {
AnyType anyType = typeBuilder().anyType().build();
return getListOfMessageType(returnType, anyType, anyType);
} else {
TypeGeneric genericType = resultType.getGenerics().get(0);
Type payloadType = genericType.getConcreteType();
MetadataType outputType;
if (payloadType.isAnyType()) {
outputType = typeBuilder().anyType().build();
} else {
if (payloadType.isAssignableTo(TypedValue.class)) {
payloadType = payloadType.getGenerics().get(0).getConcreteType();
}
outputType = payloadType.asMetadataType();
}
Type attributesType = resultType.getGenerics().get(1).getConcreteType();
MetadataType attributesOutputType = attributesType.isAnyType() ? typeBuilder().anyType().build() : attributesType.asMetadataType();
return getListOfMessageType(returnType, outputType, attributesOutputType);
}
}
Aggregations