use of org.mule.runtime.api.meta.model.parameter.ParameterModel in project mule by mulesoft.
the class SchemaBuilder method addInlineParameterGroup.
void addInlineParameterGroup(ParameterGroupModel group, ExplicitGroup parentSequence) {
DslElementSyntax groupDsl = dslResolver.resolveInline(group);
LocalComplexType complexType = objectTypeDelegate.createTypeExtension(MULE_ABSTRACT_EXTENSION_TYPE);
ExplicitGroup groupSequence = new ExplicitGroup();
List<ParameterModel> groupParameters = group.getParameterModels();
List<TopLevelElement> parameterElements = registerParameters(complexType.getComplexContent().getExtension(), groupParameters);
addParameterToSequence(parameterElements, groupSequence);
BigInteger minOccurs = ExtensionModelUtils.isRequired(group) ? ONE : ZERO;
TopLevelElement groupElement = createTopLevelElement(groupDsl.getElementName(), minOccurs, MAX_ONE);
groupElement.setComplexType(complexType);
complexType.getComplexContent().getExtension().setSequence(groupSequence);
parentSequence.getParticle().add(objectFactory.createElement(groupElement));
}
use of org.mule.runtime.api.meta.model.parameter.ParameterModel in project mule by mulesoft.
the class SchemaBuilder method registerParameters.
List<TopLevelElement> registerParameters(ExtensionType type, Collection<ParameterModel> parameterModels) {
List<TopLevelElement> all = new ArrayList<>(parameterModels.size());
getSortedParameterModels(parameterModels).stream().filter(p -> !p.getModelProperty(QNameModelProperty.class).isPresent()).forEach(parameterModel -> {
DslElementSyntax paramDsl = dslResolver.resolve(parameterModel);
declareAsParameter(parameterModel.getType(), type, parameterModel, paramDsl, all);
});
return all;
}
use of org.mule.runtime.api.meta.model.parameter.ParameterModel 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.api.meta.model.parameter.ParameterModel in project mule by mulesoft.
the class MetadataKeyIdObjectResolver method reconstructKeyFromType.
/**
* Given a {@link Object} representing the resolved value for a {@link MetadataKey}, generates the {@link MetadataKey} object.
*
* @param resolvedKey
* @return {@link MetadataKey} reconstructed from the resolved object key
* @throws MetadataResolvingException
*/
MetadataKey reconstructKeyFromType(Object resolvedKey, ReflectionCache reflectionCache) throws MetadataResolvingException {
if (isKeyLess() || resolvedKey == null) {
return new NullMetadataKey();
}
if (keyParts.size() == 1) {
return newKey(valueOf(resolvedKey)).build();
}
MetadataKeyBuilder rootBuilder = null;
MetadataKeyBuilder childBuilder = null;
for (ParameterModel p : keyParts) {
try {
if (p.getModelProperty(DeclaringMemberModelProperty.class).isPresent()) {
MetadataKeyBuilder fieldBuilder = getKeyFromField(resolvedKey, p.getModelProperty(DeclaringMemberModelProperty.class).get(), reflectionCache);
if (rootBuilder == null) {
rootBuilder = fieldBuilder;
childBuilder = rootBuilder;
} else {
childBuilder.withChild(fieldBuilder);
childBuilder = fieldBuilder;
}
}
} catch (Exception e) {
throw new MetadataResolvingException("Could not construct Metadata Key part for parameter " + p.getName(), FailureCode.INVALID_METADATA_KEY, e);
}
}
return rootBuilder != null ? rootBuilder.build() : new NullMetadataKey();
}
use of org.mule.runtime.api.meta.model.parameter.ParameterModel in project mule by mulesoft.
the class MetadataKeysDelegate method getMetadataKeys.
MetadataResult<MetadataKeysContainer> getMetadataKeys(MetadataContext context, Object partialKey, ReflectionCache reflectionCache) {
final TypeKeysResolver keyResolver = resolverFactory.getKeyResolver();
final String componentResolverName = keyResolver.getCategoryName();
final MetadataKeysContainerBuilder keysContainer = MetadataKeysContainerBuilder.getInstance();
if (keyParts.isEmpty()) {
return success(keysContainer.add(componentResolverName, ImmutableSet.of(new NullMetadataKey())).build());
}
try {
final Map<Integer, ParameterModel> partsByOrder = getPartOrderMapping(keyParts);
Set<MetadataKey> metadataKeys;
if (keyResolver instanceof PartialTypeKeysResolver && hasInitialLevel(partialKey, partsByOrder, reflectionCache)) {
metadataKeys = singleton(((PartialTypeKeysResolver) keyResolver).resolveChilds(context, partialKey));
} else {
metadataKeys = keyResolver.getKeys(context);
}
final Set<MetadataKey> enrichedMetadataKeys = metadataKeys.stream().map(metadataKey -> cloneAndEnrichMetadataKey(metadataKey, partsByOrder)).map(MetadataKeyBuilder::build).collect(toSet());
keysContainer.add(componentResolverName, enrichedMetadataKeys);
return success(keysContainer.build());
} catch (Exception e) {
return failure(newFailure(e).onKeys());
}
}
Aggregations