2021-02-14 05:22:25 +00:00
|
|
|
import '../../angel_container.dart';
|
2018-08-21 14:18:11 +00:00
|
|
|
|
|
|
|
final Map<Symbol, String> _symbolNames = <Symbol, String>{};
|
|
|
|
|
|
|
|
/// A [Reflector] implementation that performs no actual reflection,
|
|
|
|
/// instead returning empty objects on every invocation.
|
|
|
|
///
|
|
|
|
/// Use this in contexts where you know you won't need any reflective capabilities.
|
2019-04-17 13:24:13 +00:00
|
|
|
class EmptyReflector extends Reflector {
|
2018-08-21 14:18:11 +00:00
|
|
|
/// A [RegExp] that can be used to extract the name of a symbol without reflection.
|
2019-10-12 13:34:15 +00:00
|
|
|
static final RegExp symbolRegex = RegExp(r'Symbol\("([^"]+)"\)');
|
2018-08-21 14:18:11 +00:00
|
|
|
|
|
|
|
const EmptyReflector();
|
|
|
|
|
|
|
|
@override
|
|
|
|
String getName(Symbol symbol) {
|
|
|
|
return _symbolNames.putIfAbsent(
|
|
|
|
symbol, () => symbolRegex.firstMatch(symbol.toString()).group(1));
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
ReflectedClass reflectClass(Type clazz) {
|
|
|
|
return const _EmptyReflectedClass();
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
ReflectedInstance reflectInstance(Object object) {
|
|
|
|
return const _EmptyReflectedInstance();
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
ReflectedType reflectType(Type type) {
|
|
|
|
return const _EmptyReflectedType();
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
ReflectedFunction reflectFunction(Function function) {
|
|
|
|
return const _EmptyReflectedFunction();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class _EmptyReflectedClass extends ReflectedClass {
|
|
|
|
const _EmptyReflectedClass()
|
|
|
|
: super(
|
|
|
|
'(empty)',
|
|
|
|
const <ReflectedTypeParameter>[],
|
|
|
|
const <ReflectedInstance>[],
|
|
|
|
const <ReflectedFunction>[],
|
|
|
|
const <ReflectedDeclaration>[],
|
2019-10-12 13:34:15 +00:00
|
|
|
Object);
|
2018-08-21 14:18:11 +00:00
|
|
|
|
|
|
|
@override
|
|
|
|
ReflectedInstance newInstance(
|
|
|
|
String constructorName, List positionalArguments,
|
|
|
|
[Map<String, dynamic> namedArguments, List<Type> typeArguments]) {
|
2019-10-12 13:34:15 +00:00
|
|
|
throw UnsupportedError(
|
2018-08-21 14:18:11 +00:00
|
|
|
'Classes reflected via an EmptyReflector cannot be instantiated.');
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
bool isAssignableTo(ReflectedType other) {
|
|
|
|
return other == this;
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
bool operator ==(other) {
|
|
|
|
return other is ReflectedClass && other.hashCode == hashCode;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class _EmptyReflectedType extends ReflectedType {
|
|
|
|
const _EmptyReflectedType()
|
2019-10-12 13:34:15 +00:00
|
|
|
: super('(empty)', const <ReflectedTypeParameter>[], Object);
|
2018-08-21 14:18:11 +00:00
|
|
|
|
|
|
|
@override
|
|
|
|
ReflectedInstance newInstance(
|
|
|
|
String constructorName, List positionalArguments,
|
|
|
|
[Map<String, dynamic> namedArguments, List<Type> typeArguments]) {
|
2019-10-12 13:34:15 +00:00
|
|
|
throw UnsupportedError(
|
2018-08-21 14:18:11 +00:00
|
|
|
'Types reflected via an EmptyReflector cannot be instantiated.');
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
bool isAssignableTo(ReflectedType other) {
|
|
|
|
return other == this;
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
bool operator ==(other) {
|
|
|
|
return other is ReflectedType && other.hashCode == hashCode;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class _EmptyReflectedInstance extends ReflectedInstance {
|
|
|
|
const _EmptyReflectedInstance()
|
|
|
|
: super(const _EmptyReflectedType(), const _EmptyReflectedClass(), null);
|
|
|
|
|
|
|
|
@override
|
|
|
|
bool operator ==(other) {
|
|
|
|
return other is ReflectedInstance && other.hashCode == hashCode;
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
ReflectedInstance getField(String name) {
|
2019-10-12 13:34:15 +00:00
|
|
|
throw UnsupportedError(
|
2018-08-21 14:18:11 +00:00
|
|
|
'Instances reflected via an EmptyReflector cannot call getField().');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class _EmptyReflectedFunction extends ReflectedFunction {
|
|
|
|
const _EmptyReflectedFunction()
|
|
|
|
: super(
|
|
|
|
'(empty)',
|
|
|
|
const <ReflectedTypeParameter>[],
|
|
|
|
const <ReflectedInstance>[],
|
|
|
|
const _EmptyReflectedType(),
|
|
|
|
const <ReflectedParameter>[],
|
|
|
|
false,
|
|
|
|
false);
|
|
|
|
|
|
|
|
@override
|
|
|
|
ReflectedInstance invoke(Invocation invocation) {
|
2019-10-12 13:34:15 +00:00
|
|
|
throw UnsupportedError(
|
2018-08-21 14:18:11 +00:00
|
|
|
'Instances reflected via an EmptyReflector cannot call invoke().');
|
|
|
|
}
|
|
|
|
}
|