ThrowingReflector

This commit is contained in:
Tobe O 2019-10-12 09:39:34 -04:00
parent 5386722df3
commit 7cdeec36ad
4 changed files with 45 additions and 0 deletions

View file

@ -1,3 +1,7 @@
# 1.1.0
* `pedantic` lints.
* Add `ThrowingReflector`, which throws on all operations.
# 1.0.4 # 1.0.4
* Slight patch to prevent annoying segfault. * Slight patch to prevent annoying segfault.

View file

@ -0,0 +1,6 @@
import 'package:angel_container/angel_container.dart';
void main() {
var reflector = const ThrowingReflector();
reflector.reflectClass(StringBuffer);
}

View file

@ -5,3 +5,4 @@ export 'src/empty/empty.dart';
export 'src/static/static.dart'; export 'src/static/static.dart';
export 'src/exception.dart'; export 'src/exception.dart';
export 'src/reflector.dart'; export 'src/reflector.dart';
export 'src/throwing.dart';

View file

@ -0,0 +1,34 @@
import 'package:angel_container/angel_container.dart';
/// A [Reflector] implementation that throws exceptions on all attempts
/// to perform reflection.
///
/// Use this in contexts where you know you won't need any reflective capabilities.
class ThrowingReflector extends Reflector {
/// The error message to give the end user when an [UnsupportedError] is thrown.
final String errorMessage;
static const String defaultErrorMessage =
'You attempted to perform a reflective action, but you are using `ThrowingReflector`, '
'a class which disables reflection. Consider using the `MirrorsReflector` '
'class if you need reflection.';
const ThrowingReflector({this.errorMessage = defaultErrorMessage});
@override
String getName(Symbol symbol) => const EmptyReflector().getName(symbol);
UnsupportedError _error() => UnsupportedError(errorMessage);
@override
ReflectedClass reflectClass(Type clazz) => throw _error();
@override
ReflectedInstance reflectInstance(Object object) => throw _error();
@override
ReflectedType reflectType(Type type) => throw _error();
@override
ReflectedFunction reflectFunction(Function function) => throw _error();
}