ServerLifecycleComponent.fromClassElement constructor
ServerLifecycleComponent.fromClassElement(
- ClassElement element,
- AnnotationArguments arguments, {
- ConstructorElement? constructor,
- DartObject? instanceFields,
Implementation
factory ServerLifecycleComponent.fromClassElement(
ClassElement element,
AnnotationArguments arguments, {
ConstructorElement? constructor,
DartObject? instanceFields,
}) {
final methods = element.methods
.map(ServerLifecycleComponentMethod.fromElement)
.whereType<ServerLifecycleComponentMethod>()
.toList();
final guards = <ServerLifecycleComponentMethod>[];
final middlewares = <ServerLifecycleComponentMethod>[];
final interceptors = (
pre: <ServerLifecycleComponentMethod>[],
post: <ServerLifecycleComponentMethod>[],
);
final exceptionCatchers = <ServerLifecycleComponentMethod>[];
for (final method in methods) {
final _ = switch (true) {
_ when method.isGuard => guards.add(method),
_ when method.isMiddleware => middlewares.add(method),
_ when method.isInterceptorPre => interceptors.pre.add(method),
_ when method.isInterceptorPost => interceptors.post.add(method),
_ when method.isExceptionCatcher => exceptionCatchers.add(method),
_ => null,
};
}
final ctor =
constructor ?? element.constructors.firstWhereOrNull((e) => e.isPublic);
if (ctor == null) {
throw ArgumentError.value(
LifecycleComponent,
'type',
'Expected a class element with a public constructor',
);
}
final paramNamesFromConstructor = {
for (final p in ctor.formalParameters) p.name3 ?? '',
};
final params = <ServerParam>[
...ctor.formalParameters.map((FormalParameterElement e) {
final param = ServerParam.fromElement(e);
if (arguments.all[param.name] case final arg?) {
param.argument = arg;
}
return param;
}),
if (instanceFields != null)
for (final field in element.fields) ...[
if (_getInitializerListFieldValue(
field,
paramNamesFromConstructor,
instanceFields,
)
case final value?)
ServerParam(
name: field.name3!,
type: ServerType.fromType(field.type),
isRequired: true,
isNamed: true,
argument: AnnotationArgument.fromFieldValue(
field.name3!,
value,
field,
),
),
],
];
final name = element.name3;
if (name == null) {
throw Exception('Class name is null');
}
return ServerLifecycleComponent(
name: name,
guards: guards,
middlewares: middlewares,
interceptors: interceptors,
exceptionCatchers: exceptionCatchers,
params: params,
import: ServerImports.fromElement(ctor.returnType.element),
arguments: arguments,
genericTypes: element.typeParameters
.map(ServerGenericType.fromElement)
.toList(),
);
}