diff --git a/angel_container/CHANGELOG.md b/angel_container/CHANGELOG.md index 3de4653f..2c886944 100644 --- a/angel_container/CHANGELOG.md +++ b/angel_container/CHANGELOG.md @@ -1,3 +1,7 @@ +# 1.1.0 +* `pedantic` lints. +* Add `ThrowingReflector`, which throws on all operations. + # 1.0.4 * Slight patch to prevent annoying segfault. diff --git a/angel_container/example/throwing.dart b/angel_container/example/throwing.dart new file mode 100644 index 00000000..f6032417 --- /dev/null +++ b/angel_container/example/throwing.dart @@ -0,0 +1,6 @@ +import 'package:angel_container/angel_container.dart'; + +void main() { + var reflector = const ThrowingReflector(); + reflector.reflectClass(StringBuffer); +} diff --git a/angel_container/lib/angel_container.dart b/angel_container/lib/angel_container.dart index 8667e0ad..e47500f6 100644 --- a/angel_container/lib/angel_container.dart +++ b/angel_container/lib/angel_container.dart @@ -5,3 +5,4 @@ export 'src/empty/empty.dart'; export 'src/static/static.dart'; export 'src/exception.dart'; export 'src/reflector.dart'; +export 'src/throwing.dart'; diff --git a/angel_container/lib/src/throwing.dart b/angel_container/lib/src/throwing.dart new file mode 100644 index 00000000..0055e657 --- /dev/null +++ b/angel_container/lib/src/throwing.dart @@ -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(); +}