visitProvideClass method
void
visitProvideClass(
- int index,
- TokenElement token,
- Expression tokenExpression,
- Reference type,
- String? constructor,
- List<
Expression> dependencies, - bool isMulti,
override
Implement providing a new instance of type
, calling constructor
.
Any dependencies
are expected to invoke local methods as appropriate:
refer('inject').call([refer('Dep1')])
Implementation
@override
void visitProvideClass(
int index,
TokenElement token,
Expression tokenExpression,
Reference type,
String? constructor,
List<Expression> dependencies,
bool isMulti,
) {
final fieldName = '_field$index';
final types = type is TypeReference ? type.types : <Reference>[];
_fieldCache.add(
Field((b) => b
..name = fieldName
..type = TypeReference((b) => b
..symbol = type.symbol
..url = type.url
..types.addAll(types)
..isNullable = true)),
);
final methodName = '_get${type.symbol}\$$index';
final instance = constructor == null
? type.newInstance(dependencies)
: type.newInstanceNamed(constructor, dependencies);
_methodCache.add(Method((b) => b
..name = methodName
..returns = type
..body = refer(fieldName).assignNullAware(instance).code));
if (isMulti) {
_addToMulti(token, tokenExpression, methodName);
} else {
_addToBody(tokenExpression, methodName);
}
}