@@ -53,6 +53,10 @@ abstract class ConstantReader {
5353 _isNull (object) ? const _NullConstant () : new _Constant (object);
5454
5555 /// Constant as any supported literal value.
56+ ///
57+ /// Throws [FormatException] if a valid literal value cannot be returned. This
58+ /// is the case if the constant is not a literal or if the literal value
59+ /// is represented at least partially with [DartObject] instances.
5660 dynamic get anyValue;
5761
5862 /// Returns whether this constant represents a `bool` literal.
@@ -76,6 +80,9 @@ abstract class ConstantReader {
7680
7781 /// Returns this constant as a `List` value.
7882 ///
83+ /// Note: the list values are instances of [DartObject] which represent the
84+ /// original values.
85+ ///
7986 /// Throws [FormatException] if [isList] is `false` .
8087 List <DartObject > get listValue;
8188
@@ -86,6 +93,9 @@ abstract class ConstantReader {
8693
8794 /// Returns this constant as a `Map` value.
8895 ///
96+ /// Note: the map keys and values are instances of [DartObject] which
97+ /// represent the original values.
98+ ///
8999 /// Throws [FormatException] if [isMap] is `false` .
90100 Map <DartObject , DartObject > get mapValue;
91101
@@ -146,15 +156,12 @@ abstract class ConstantReader {
146156 Revivable revive ();
147157}
148158
149- dynamic _throw (String expected, [dynamic object]) => throw new FormatException (
150- 'Not an instance of $expected .' , object == null ? null : '$object ' );
151-
152159/// Implements a [ConstantReader] representing a `null` value.
153160class _NullConstant implements ConstantReader {
154161 const _NullConstant ();
155162
156163 @override
157- dynamic get anyValue => _throw ( 'dynamic' ) ;
164+ dynamic get anyValue => null ;
158165
159166 @override
160167 bool get boolValue => _throw ('bool' );
@@ -215,6 +222,9 @@ class _NullConstant implements ConstantReader {
215222
216223 @override
217224 Revivable revive () => throw new UnsupportedError ('Null' );
225+
226+ dynamic _throw (String expected) =>
227+ throw new FormatException ('Not an instance of $expected .' );
218228}
219229
220230/// Default implementation of [ConstantReader] .
@@ -230,28 +240,25 @@ class _Constant implements ConstantReader {
230240 _object.toStringValue () ??
231241 _object.toDoubleValue () ??
232242 (isSymbol ? this .symbolValue : null ) ??
233- _object.toTypeValue () ??
234- _object.toListValue () ??
235- _object.toMapValue ();
243+ _throw ('bool, int, double, String or Symbol' );
236244
237245 @override
238- bool get boolValue =>
239- isBool ? _object.toBoolValue () : _throw ('bool' , _object);
246+ bool get boolValue => isBool ? _object.toBoolValue () : _throw ('bool' );
240247
241248 @override
242- int get intValue => isInt ? _object.toIntValue () : _throw ('int' , _object );
249+ int get intValue => isInt ? _object.toIntValue () : _throw ('int' );
243250
244251 @override
245252 String get stringValue =>
246- isString ? _object.toStringValue () : _throw ('String' , _object );
253+ isString ? _object.toStringValue () : _throw ('String' );
247254
248255 @override
249256 List <DartObject > get listValue =>
250- isList ? _object.toListValue () : _throw ('List' , _object );
257+ isList ? _object.toListValue () : _throw ('List' );
251258
252259 @override
253260 Map <DartObject , DartObject > get mapValue =>
254- isMap ? _object.toMapValue () : _throw ('Map' , _object );
261+ isMap ? _object.toMapValue () : _throw ('Map' );
255262
256263 @override
257264 bool get isBool => _object.toBoolValue () != null ;
@@ -276,22 +283,20 @@ class _Constant implements ConstantReader {
276283
277284 @override
278285 double get doubleValue =>
279- isDouble ? _object.toDoubleValue () : _throw ('double' , _object );
286+ isDouble ? _object.toDoubleValue () : _throw ('double' );
280287
281288 @override
282289 bool get isSymbol => _object.toSymbolValue () != null ;
283290
284291 @override
285- Symbol get symbolValue => isSymbol
286- ? new Symbol (_object.toSymbolValue ())
287- : _throw ('Symbol' , _object);
292+ Symbol get symbolValue =>
293+ isSymbol ? new Symbol (_object.toSymbolValue ()) : _throw ('Symbol' );
288294
289295 @override
290296 bool get isType => _object.toTypeValue () != null ;
291297
292298 @override
293- DartType get typeValue =>
294- isType ? _object.toTypeValue () : _throw ("Type" , _object);
299+ DartType get typeValue => isType ? _object.toTypeValue () : _throw ("Type" );
295300
296301 @override
297302 bool instanceOf (TypeChecker checker) =>
@@ -311,4 +316,7 @@ class _Constant implements ConstantReader {
311316
312317 @override
313318 String toString () => 'ConstantReader ${_object }' ;
319+
320+ dynamic _throw (String expected) =>
321+ throw new FormatException ('Not an instance of $expected .' , _object);
314322}
0 commit comments