use of org.mule.runtime.module.extension.api.loader.java.type.MethodElement in project mule by mulesoft.
the class SourceModelLoaderDelegate method declareSourceCallback.
private void declareSourceCallback(SourceElement sourceType, SourceDeclarer source) {
final Optional<MethodElement> onResponseMethod = sourceType.getOnResponseMethod();
final Optional<MethodElement> onErrorMethod = sourceType.getOnErrorMethod();
final Optional<MethodElement> onTerminateMethod = sourceType.getOnTerminateMethod();
final Optional<MethodElement> onBackPressureMethod = sourceType.getOnBackPressureMethod();
// TODO: MULE-9220 add syntax validator to check that none of these use @UseConfig or @Connection
declareSourceCallbackParameters(source, onResponseMethod, source::onSuccess);
declareSourceCallbackParameters(source, onErrorMethod, source::onError);
declareSourceCallbackParameters(source, onTerminateMethod, source::onTerminate);
declareSourceCallbackParameters(source, onBackPressureMethod, source::onBackPressure);
source.withModelProperty(new SourceCallbackModelProperty(getMethod(onResponseMethod), getMethod(onErrorMethod), getMethod(onTerminateMethod), getMethod(onBackPressureMethod)));
}
use of org.mule.runtime.module.extension.api.loader.java.type.MethodElement in project mule by mulesoft.
the class NotificationsDeclarationEnricher method enrich.
@Override
public void enrich(ExtensionLoadingContext extensionLoadingContext) {
ExtensionDeclaration declaration = extensionLoadingContext.getExtensionDeclarer().getDeclaration();
Optional<ExtensionTypeDescriptorModelProperty> extensionType = declaration.getModelProperty(ExtensionTypeDescriptorModelProperty.class);
String extensionNamespace = getExtensionsNamespace(declaration);
ClassTypeLoader typeLoader = ExtensionsTypeLoaderFactory.getDefault().createTypeLoader();
if (extensionType.isPresent() && extensionType.get().getType().getDeclaringClass().isPresent()) {
Type extensionElement = extensionType.get().getType();
Optional<NotificationActions> annotation = extensionElement.getAnnotation(NotificationActions.class);
annotation.ifPresent(actionsAnnotation -> {
NotificationActionDefinition<?>[] actions = (NotificationActionDefinition<?>[]) actionsAnnotation.value().getEnumConstants();
Map<NotificationActionDefinition, NotificationModel> notificationModels = new HashMap<>();
stream(actions).forEach(action -> {
NotificationModel model = new ImmutableNotificationModel(extensionNamespace, ((Enum) action).name(), typeLoader.load(action.getDataType().getType()));
declaration.addNotificationModel(model);
notificationModels.put(action, model);
});
new IdempotentDeclarationWalker() {
@Override
public void onOperation(WithOperationsDeclaration owner, OperationDeclaration declaration) {
Optional<ExtensionOperationDescriptorModelProperty> modelProperty = declaration.getModelProperty(ExtensionOperationDescriptorModelProperty.class);
if (modelProperty.isPresent()) {
MethodElement method = modelProperty.get().getOperationMethod();
Optional<Fires> emitsNotifications = getOperationNotificationDeclaration(method, extensionElement);
includeNotificationDeclarationIfNeeded(declaration, emitsNotifications);
}
}
@Override
public void onSource(SourceDeclaration declaration) {
Optional<ExtensionTypeDescriptorModelProperty> modelProperty = declaration.getModelProperty(ExtensionTypeDescriptorModelProperty.class);
if (modelProperty.isPresent()) {
Type sourceContainer = modelProperty.get().getType();
Optional<Fires> emitsNotifications = getNotificationDeclaration(sourceContainer, extensionElement);
includeNotificationDeclarationIfNeeded(declaration, emitsNotifications);
}
}
private Optional<Fires> getOperationNotificationDeclaration(MethodElement operationMethod, Type extensionElement) {
Type operationContainer = operationMethod.getEnclosingType();
return ofNullable(operationMethod.getAnnotation(Fires.class)).orElse(getNotificationDeclaration(operationContainer, extensionElement));
}
private Optional<Fires> getNotificationDeclaration(Type container, Type extensionElement) {
return ofNullable(container.getAnnotation(Fires.class).orElseGet(() -> extensionElement.getAnnotation(Fires.class).orElse(null)));
}
private void includeNotificationDeclarationIfNeeded(ExecutableComponentDeclaration declaration, Optional<Fires> emitsNotifications) {
emitsNotifications.ifPresent(emits -> {
Class<? extends NotificationActionProvider>[] providers = emits.value();
stream(providers).forEach(provider -> {
try {
NotificationActionProvider notificationActionProvider = provider.newInstance();
notificationActionProvider.getNotificationActions().stream().map(action -> validateEmits(actions, action)).forEach(action -> declaration.addNotificationModel(notificationModels.get(action)));
} catch (InstantiationException | IllegalAccessException e) {
throw new MuleRuntimeException(createStaticMessage("Could not create NotificationActionProvider of type " + provider.getName()), e);
}
});
});
}
private NotificationActionDefinition validateEmits(NotificationActionDefinition[] actions, NotificationActionDefinition action) {
Class<?> extensionAction = actions.getClass().getComponentType();
if (!action.getClass().equals(extensionAction) && !action.getClass().getSuperclass().equals(extensionAction)) {
throw new IllegalModelDefinitionException(format("Invalid EmitsNotification detected, the extension declared" + " firing notifications of %s type, but a notification of %s type has been detected", extensionAction, action.getClass()));
} else {
return action;
}
}
}.walk(declaration);
});
}
}
use of org.mule.runtime.module.extension.api.loader.java.type.MethodElement 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.module.extension.api.loader.java.type.MethodElement in project mule by mulesoft.
the class OperationModelLoaderDelegate method processNonBlockingOperation.
static void processNonBlockingOperation(OperationDeclarer operation, MethodElement operationMethod, boolean allowStreaming) {
List<ExtensionParameter> callbackParameters = operationMethod.getParameters().stream().filter(p -> p.getType().isSameType(CompletionCallback.class)).collect(toList());
checkDefinition(!callbackParameters.isEmpty(), format("Operation '%s' does not declare a '%s' parameter. One is required for a non-blocking operation", operationMethod.getAlias(), CompletionCallback.class.getSimpleName()));
checkDefinition(callbackParameters.size() <= 1, format("Operation '%s' defines more than one %s parameters. Only one is allowed", operationMethod.getAlias(), CompletionCallback.class.getSimpleName()));
checkDefinition(isVoid(operationMethod), format("Operation '%s' has a parameter of type %s but is not void. " + "Non-blocking operations have to be declared as void and the " + "return type provided through the callback", operationMethod.getAlias(), CompletionCallback.class.getSimpleName()));
ExtensionParameter callbackParameter = callbackParameters.get(0);
List<MetadataType> genericTypes = callbackParameter.getType().getGenerics().stream().map(generic -> generic.getConcreteType().asMetadataType()).collect(toList());
if (genericTypes.isEmpty()) {
// This is an invalid state, but is better to fail when executing the Extension Model Validators
genericTypes.add(ANY_TYPE);
genericTypes.add(ANY_TYPE);
}
operation.withOutput().ofType(genericTypes.get(0));
operation.withOutputAttributes().ofType(genericTypes.get(1));
operation.blocking(false);
if (allowStreaming) {
handleByteStreaming(operationMethod, operation, genericTypes.get(0));
} else {
operation.supportsStreaming(false);
}
}
use of org.mule.runtime.module.extension.api.loader.java.type.MethodElement in project mule by mulesoft.
the class ErrorsDeclarationEnricher method collectErrorOperations.
private List<Pair<ComponentDeclaration, MethodElement>> collectErrorOperations(ExtensionDeclaration declaration) {
List<Pair<ComponentDeclaration, MethodElement>> operations = new LinkedList<>();
new IdempotentDeclarationWalker() {
@Override
public void onOperation(WithOperationsDeclaration owner, OperationDeclaration declaration) {
addComponent(declaration);
}
@Override
protected void onConstruct(ConstructDeclaration declaration) {
addComponent(declaration);
}
private void addComponent(ComponentDeclaration<?> declaration) {
declaration.getModelProperty(ExtensionOperationDescriptorModelProperty.class).ifPresent(implementingMethodModelProperty -> operations.add(new Pair<>(declaration, implementingMethodModelProperty.getOperationMethod())));
}
}.walk(declaration);
return operations;
}
Aggregations