use of org.mule.runtime.extension.api.loader.ProblemsReporter 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.runtime.extension.api.loader.ProblemsReporter in project mule by mulesoft.
the class InputParametersTypeModelValidator method validate.
@Override
public void validate(ExtensionModel extensionModel, ProblemsReporter problems) {
final Set<Class<?>> validatedTypes = new HashSet<>();
new IdempotentExtensionWalker() {
@Override
public void onOperation(OperationModel model) {
model.getAllParameterModels().forEach(parameterModel -> validateJavaType(model, parameterModel.getType(), problems, validatedTypes));
}
@Override
public void onSource(SourceModel model) {
validateCallback(model, model.getSuccessCallback());
validateCallback(model, model.getErrorCallback());
validateCallback(model, model.getTerminateCallback());
}
private void validateCallback(SourceModel model, Optional<SourceCallbackModel> callback) {
callback.ifPresent(cb -> cb.getAllParameterModels().forEach(parameterModel -> validateJavaType(model, parameterModel.getType(), problems, validatedTypes)));
}
}.walk(extensionModel);
extensionModel.getSubTypes().forEach(subTypesModel -> getClassForValidation(subTypesModel.getBaseType()).filter(validatedTypes::contains).ifPresent(type -> {
subTypesModel.getSubTypes().forEach(subtype -> validateSubtypesHaveGetters(extensionModel, subtype, problems, validatedTypes));
}));
}
use of org.mule.runtime.extension.api.loader.ProblemsReporter in project mule by mulesoft.
the class MetadataComponentModelValidator method validateResolversName.
private void validateResolversName(ComponentModel model, MetadataResolverFactory resolverFactory, Table<String, String, Class<?>> names, ProblemsReporter problemsReporter) {
List<NamedTypeResolver> resolvers = new LinkedList<>();
resolvers.addAll(getAllInputResolvers(model, resolverFactory));
resolvers.add(resolverFactory.getOutputResolver());
resolvers.stream().filter(r -> !r.getClass().equals(NullMetadataResolver.class)).forEach(r -> {
if (isBlank(r.getResolverName())) {
problemsReporter.addError(new Problem(model, format(EMPTY_RESOLVER_NAME, getComponentModelTypeName(model), model.getName(), r.getClass().getSimpleName(), "resolver")));
} else {
if (names.get(r.getCategoryName(), r.getResolverName()) != null && names.get(r.getCategoryName(), r.getResolverName()) != r.getClass()) {
problemsReporter.addError(new Problem(model, format("%s [%s] specifies metadata resolvers with repeated name [%s] for the same category [%s]. Resolver names should be unique for a given category. Affected resolvers are '%s' and '%s'", getComponentModelTypeName(model), model.getName(), r.getResolverName(), r.getCategoryName(), names.get(r.getCategoryName(), r.getResolverName()).getSimpleName(), r.getClass().getSimpleName())));
}
names.put(r.getCategoryName(), r.getResolverName(), r.getClass());
}
});
}
use of org.mule.runtime.extension.api.loader.ProblemsReporter in project mule by mulesoft.
the class SourceCallbacksModelValidatorTestCase method setUp.
@Before
public void setUp() throws Exception {
problemsReporter = new ProblemsReporter(extensionModel);
validator = new SourceCallbacksModelValidator();
when(extensionModel.getSourceModels()).thenReturn(singletonList(sourceModel));
when(sourceModel.getTerminateCallback()).thenReturn(of(onTerminateCallback));
when(sourceModel.getSuccessCallback()).thenReturn(Optional.empty());
when(sourceModel.getErrorCallback()).thenReturn(Optional.empty());
}
use of org.mule.runtime.extension.api.loader.ProblemsReporter in project mule by mulesoft.
the class ExportedPackagesValidatorTestCase method validExportedPackages.
@Test
public void validExportedPackages() {
setUpValidExtension();
ExportedPackagesValidator exportedPackagesValidator = new ExportedPackagesValidator();
ProblemsReporter problemsReporter = new ProblemsReporter(extensionModel);
exportedPackagesValidator.validate(extensionModel, problemsReporter);
assertThat(problemsReporter.getErrors(), is(IsEmptyCollection.empty()));
assertThat(problemsReporter.getWarnings(), is(IsEmptyCollection.empty()));
}
Aggregations