use of org.mule.metadata.api.model.MetadataFormat.JAVA in project mule by mulesoft.
the class InjectedFieldsModelValidator method validate.
@Override
public void validate(ExtensionModel extensionModel, ProblemsReporter problemsReporter) {
final Set<Class<?>> validatedTypes = new HashSet<>();
// TODO - MULE-14401 - Make InjectedFieldsModelValidator work in AST Mode
Boolean isASTMode = !extensionModel.getModelProperty(ExtensionTypeDescriptorModelProperty.class).map(mp -> mp.getType().getDeclaringClass().isPresent()).orElse(false);
if (!isASTMode) {
extensionModel.getModelProperty(ClassLoaderModelProperty.class).ifPresent(classLoaderModelProperty -> {
new ExtensionWalker() {
@Override
protected void onSource(HasSourceModels owner, SourceModel model) {
validateFields(model, model.getModelProperty(ImplementingTypeModelProperty.class), DefaultEncoding.class);
}
@Override
protected void onConfiguration(ConfigurationModel model) {
validateFields(model, model.getModelProperty(ImplementingTypeModelProperty.class), DefaultEncoding.class);
validateFields(model, model.getModelProperty(ImplementingTypeModelProperty.class), RefName.class);
}
@Override
protected void onOperation(HasOperationModels owner, OperationModel model) {
validateArguments(model, model.getModelProperty(ExtensionOperationDescriptorModelProperty.class), DefaultEncoding.class);
}
@Override
protected void onConnectionProvider(HasConnectionProviderModels owner, ConnectionProviderModel model) {
validateFields(model, model.getModelProperty(ImplementingTypeModelProperty.class), DefaultEncoding.class);
validateFields(model, model.getModelProperty(ImplementingTypeModelProperty.class), RefName.class);
}
@Override
protected void onParameter(ParameterizedModel owner, ParameterGroupModel groupModel, ParameterModel model) {
if (model.getType().getMetadataFormat().equals(JAVA)) {
model.getType().accept(new MetadataTypeVisitor() {
@Override
public void visitObject(ObjectType objectType) {
if (!objectType.getAnnotation(InfrastructureTypeAnnotation.class).isPresent()) {
try {
Class<?> type = getType(objectType, classLoaderModelProperty.getClassLoader());
if (validatedTypes.add(type)) {
validateType(model, type, DefaultEncoding.class);
}
} catch (Exception e) {
problemsReporter.addWarning(new Problem(model, "Could not validate Class: " + e.getMessage()));
}
}
}
});
}
}
private void validateArguments(NamedObject model, Optional<ExtensionOperationDescriptorModelProperty> modelProperty, Class<? extends Annotation> annotationClass) {
modelProperty.ifPresent(operationDescriptorModelProperty -> {
MethodElement operation = operationDescriptorModelProperty.getOperationMethod();
int size = operation.getParametersAnnotatedWith(annotationClass).size();
if (size == 0) {
return;
} else if (size > 1) {
problemsReporter.addError(new Problem(model, format("Operation method '%s' has %d arguments annotated with @%s. Only one argument may carry that annotation", operation.getName(), size, annotationClass.getSimpleName())));
}
ExtensionParameter argument = operation.getParametersAnnotatedWith(annotationClass).get(0);
if (!argument.getType().isSameType(String.class)) {
problemsReporter.addError(new Problem(model, format("Operation method '%s' declares an argument '%s' which is annotated with @%s and is of type '%s'. Only " + "arguments of type String are allowed to carry such annotation", operation.getName(), argument.getName(), annotationClass.getSimpleName(), argument.getType().getName())));
}
});
}
private void validateFields(NamedObject model, Optional<ImplementingTypeModelProperty> modelProperty, Class<? extends Annotation> annotationClass) {
modelProperty.ifPresent(implementingTypeModelProperty -> {
validateType(model, implementingTypeModelProperty.getType(), annotationClass);
});
}
private void validateType(NamedObject model, Class<?> type, Class<? extends Annotation> annotationClass) {
List<Field> fields = getAnnotatedFields(type, annotationClass);
if (fields.isEmpty()) {
return;
} else if (fields.size() > 1) {
problemsReporter.addError(new Problem(model, format("Class '%s' has %d fields annotated with @%s. Only one field may carry that annotation", type.getName(), fields.size(), annotationClass.getSimpleName())));
}
Field field = fields.get(0);
if (!String.class.equals(field.getType())) {
problemsReporter.addError(new Problem(model, format("Class '%s' declares the field '%s' which is annotated with @%s and is of type '%s'. Only " + "fields of type String are allowed to carry such annotation", type.getName(), field.getName(), annotationClass.getSimpleName(), field.getType().getName())));
}
}
}.walk(extensionModel);
});
}
}
use of org.mule.metadata.api.model.MetadataFormat.JAVA 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.MetadataFormat.JAVA in project mule by mulesoft.
the class NullSafeValueResolverWrapper method of.
/**
* Creates a new instance
*
* @param delegate the {@link ValueResolver} to wrap
* @param type the type of the value this resolver returns
* @param reflectionCache the cache for expensive reflection lookups
* @param muleContext the current {@link MuleContext}
* @param <T> the generic type of the produced values
* @return a new null safe {@link ValueResolver}
* @throws IllegalParameterModelDefinitionException if used on parameters of not supported types
*/
public static <T> ValueResolver<T> of(ValueResolver<T> delegate, MetadataType type, ReflectionCache reflectionCache, MuleContext muleContext, ObjectTypeParametersResolver parametersResolver) {
checkArgument(delegate != null, "delegate cannot be null");
Reference<ValueResolver> wrappedResolver = new Reference<>();
type.accept(new MetadataTypeVisitor() {
@Override
public void visitObject(ObjectType objectType) {
Class clazz = getType(objectType);
if (isMap(objectType)) {
ValueResolver<?> fallback = MapValueResolver.of(clazz, emptyList(), emptyList(), reflectionCache, muleContext);
wrappedResolver.set(new NullSafeValueResolverWrapper(delegate, fallback, muleContext));
return;
}
String requiredFields = objectType.getFields().stream().filter(f -> f.isRequired() && !isFlattenedParameterGroup(f)).map(MetadataTypeUtils::getLocalPart).collect(joining(", "));
if (!isBlank(requiredFields)) {
if (LOGGER.isDebugEnabled()) {
LOGGER.debug(format("Class '%s' cannot be used with NullSafe Wrapper since it contains non optional fields: [%s]", clazz.getName(), requiredFields));
}
wrappedResolver.set(delegate);
return;
}
ResolverSet resolverSet = new ResolverSet(muleContext);
for (Field field : getFields(clazz)) {
ValueResolver<?> fieldResolver = null;
ObjectFieldType objectField = objectType.getFieldByName(getAlias(field)).orElse(null);
if (objectField == null) {
continue;
}
Optional<String> defaultValue = getDefaultValue(objectField);
// TODO MULE-13066 Extract ParameterResolver logic into a centralized resolver
if (defaultValue.isPresent()) {
fieldResolver = getFieldDefaultValueValueResolver(objectField, muleContext);
} else if (isFlattenedParameterGroup(objectField)) {
DefaultObjectBuilder groupBuilder = new DefaultObjectBuilder<>(getType(objectField.getValue()));
resolverSet.add(field.getName(), new ObjectBuilderValueResolver<T>(groupBuilder, muleContext));
ObjectType childGroup = (ObjectType) objectField.getValue();
parametersResolver.resolveParameters(childGroup, groupBuilder);
parametersResolver.resolveParameterGroups(childGroup, groupBuilder);
} else {
NullSafe nullSafe = field.getAnnotation(NullSafe.class);
if (nullSafe != null) {
MetadataType nullSafeType;
if (Object.class.equals(nullSafe.defaultImplementingType())) {
nullSafeType = objectField.getValue();
} else {
nullSafeType = new BaseTypeBuilder(JAVA).objectType().with(new TypeIdAnnotation(nullSafe.defaultImplementingType().getName())).build();
}
fieldResolver = NullSafeValueResolverWrapper.of(new StaticValueResolver<>(null), nullSafeType, reflectionCache, muleContext, parametersResolver);
}
if (field.getAnnotation(ConfigOverride.class) != null) {
ValueResolver<?> fieldDelegate = fieldResolver != null ? fieldResolver : new StaticValueResolver<>(null);
fieldResolver = ConfigOverrideValueResolverWrapper.of(fieldDelegate, field.getName(), reflectionCache, muleContext);
}
}
if (fieldResolver != null) {
resolverSet.add(field.getName(), fieldResolver);
}
}
ObjectBuilder<T> objectBuilder = new DefaultResolverSetBasedObjectBuilder<T>(clazz, resolverSet);
wrappedResolver.set(new NullSafeValueResolverWrapper(delegate, new ObjectBuilderValueResolver(objectBuilder, muleContext), muleContext));
}
@Override
public void visitArrayType(ArrayType arrayType) {
Class collectionClass = getType(arrayType);
ValueResolver<?> fallback = CollectionValueResolver.of(collectionClass, emptyList());
wrappedResolver.set(new NullSafeValueResolverWrapper(delegate, fallback, muleContext));
}
@Override
protected void defaultVisit(MetadataType metadataType) {
if (LOGGER.isDebugEnabled()) {
LOGGER.debug(format("Class '%s' cannot be used with NullSafe Wrapper since it is of a simple type", getType(metadataType).getName()));
}
wrappedResolver.set(delegate);
}
});
return wrappedResolver.get();
}
use of org.mule.metadata.api.model.MetadataFormat.JAVA 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.MetadataFormat.JAVA in project mule by mulesoft.
the class IntrospectionUtils method collectRelativeClasses.
/**
* 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
* @param extensionClassLoader extension class loader
* @return {@link Set<Class<?>>} with the classes reachable from the {@code type}
*/
public static Set<Class<?>> collectRelativeClasses(MetadataType type, ClassLoader extensionClassLoader) {
Set<Class<?>> 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 Class<Object> clazz = getType(objectType).orElse(null);
if (clazz == null || relativeClasses.contains(clazz)) {
return;
}
Optional<ClassInformationAnnotation> classInformation = objectType.getAnnotation(ClassInformationAnnotation.class);
if (classInformation.isPresent()) {
classInformation.get().getGenericTypes().forEach(generic -> relativeClasses.add(loadClass(generic, extensionClassLoader)));
}
relativeClasses.add(clazz);
objectType.getFields().stream().forEach(objectFieldType -> objectFieldType.accept(this));
objectType.getOpenRestriction().ifPresent(t -> t.accept(this));
}
@Override
public void visitString(StringType stringType) {
if (stringType.getMetadataFormat() == JAVA && isEnum(stringType)) {
getType(stringType).ifPresent(relativeClasses::add);
}
}
});
return relativeClasses;
}
Aggregations