AnnotationArgument.fromExpression constructor
AnnotationArgument.fromExpression(})
Implementation
factory AnnotationArgument.fromExpression(
Expression expression, {
String? annotationContext,
String? knownNamedParameter,
bool? knownIsRequired,
}) {
final source = expression.toSource();
final type = expression.staticType;
final element = type?.element;
if (element == null || type == null) {
throw ArgumentError(
'The argument expression has not been resolved yet '
'(${_describeExpression(expression)}'
'${_annotationSuffix(annotationContext)})',
);
}
var isInjectable = false;
final parents = switch (element) {
ClassElement(:final allSupertypes) => [...allSupertypes],
_ => <InterfaceType>[],
};
while (parents.isNotEmpty) {
final parent = parents.removeLast();
if (parent is ClassElement) continue;
if (parent.getDisplayString() == (Inject).name) {
isInjectable = true;
break;
}
}
final resolved = _resolveParameterName(
expression,
knownNamedParameter: knownNamedParameter,
knownIsRequired: knownIsRequired,
);
if (resolved == null) {
throw ArgumentError(
'Could not determine the parameter name for an annotation argument '
'(${_describeExpression(expression)}'
'${_annotationSuffix(annotationContext)}). '
'Ancestor chain: ${_ancestorChain(expression)}',
);
}
final (name: parameterName, isRequired: isRequired) = resolved;
return AnnotationArgument(
parameterName: parameterName,
type: switch (expression.correspondingParameter?.type ??
expression.staticType) {
final e? => ServerType.fromType(e),
_ => throw Exception('Expression has not been resolved yet...'),
},
isRequired: isRequired,
source: source,
element: element,
isInjectable: isInjectable,
);
}