|
| 1 | +// Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file |
| 2 | +// for details. All rights reserved. Use of this source code is governed by a |
| 3 | +// BSD-style license that can be found in the LICENSE file. |
| 4 | + |
| 5 | +import 'package:analyzer/dart/constant/value.dart'; |
| 6 | + |
| 7 | +/// Returns whether or not [object] is or represents a `null` value. |
| 8 | +bool _isNull(DartObject object) => object?.isNull != false; |
| 9 | + |
| 10 | +/// Similar to [DartObject.getField], but traverses super classes. |
| 11 | +/// |
| 12 | +/// Returns `null` if ultimately [field] is never found. |
| 13 | +DartObject _getFieldRecursive(DartObject object, String field) { |
| 14 | + if (_isNull(object)) { |
| 15 | + return null; |
| 16 | + } |
| 17 | + final result = object.getField(field); |
| 18 | + if (_isNull(result)) { |
| 19 | + return _getFieldRecursive(object.getField('(super)'), field); |
| 20 | + } |
| 21 | + return result; |
| 22 | +} |
| 23 | + |
| 24 | +/// A wrapper for analyzer's [DartObject] with a predictable high-level API. |
| 25 | +/// |
| 26 | +/// Unlike [DartObject.getField], all `readX` methods attempt to access super |
| 27 | +/// classes for the field value if not found. |
| 28 | +abstract class ConstantReader { |
| 29 | + factory ConstantReader(DartObject object) => |
| 30 | + _isNull(object) ? const _NullConstant() : new _Constant(object); |
| 31 | + |
| 32 | + /// Returns whether this constant represents a `bool` literal. |
| 33 | + bool get isBool; |
| 34 | + |
| 35 | + /// Returns this constant as a `bool` value. |
| 36 | + bool get boolValue; |
| 37 | + |
| 38 | + /// Returns whether this constant represents an `int` literal. |
| 39 | + bool get isInt; |
| 40 | + |
| 41 | + /// Returns this constant as an `int` value. |
| 42 | + /// |
| 43 | + /// Throws [FormatException] if [isInt] is `false`. |
| 44 | + int get intValue; |
| 45 | + |
| 46 | + /// Returns whether this constant represents a `String` literal. |
| 47 | + /// |
| 48 | + /// If `true`, [stringValue] will return a `String` (not throw). |
| 49 | + bool get isString; |
| 50 | + |
| 51 | + /// Returns this constant as an `String` value. |
| 52 | + /// |
| 53 | + /// Throws [FormatException] if [isString] is `false`. |
| 54 | + String get stringValue; |
| 55 | + |
| 56 | + /// Returns whether this constant represents `null`. |
| 57 | + bool get isNull; |
| 58 | + |
| 59 | + /// Reads[ field] from the constant as another constant value. |
| 60 | + ConstantReader read(String field); |
| 61 | +} |
| 62 | + |
| 63 | +/// Implements a [ConstantReader] representing a `null` value. |
| 64 | +class _NullConstant implements ConstantReader { |
| 65 | + const _NullConstant(); |
| 66 | + |
| 67 | + @override |
| 68 | + bool get boolValue => throw new FormatException('Not a bool', 'null'); |
| 69 | + |
| 70 | + @override |
| 71 | + int get intValue => throw new FormatException('Not an int', 'null'); |
| 72 | + |
| 73 | + @override |
| 74 | + String get stringValue => throw new FormatException('Not a String', 'null'); |
| 75 | + |
| 76 | + @override |
| 77 | + bool get isBool => false; |
| 78 | + |
| 79 | + @override |
| 80 | + bool get isInt => false; |
| 81 | + |
| 82 | + @override |
| 83 | + bool get isNull => true; |
| 84 | + |
| 85 | + @override |
| 86 | + bool get isString => false; |
| 87 | + |
| 88 | + @override |
| 89 | + ConstantReader read(_) => this; |
| 90 | +} |
| 91 | + |
| 92 | +/// Default implementation of [ConstantReader]. |
| 93 | +class _Constant implements ConstantReader { |
| 94 | + final DartObject _object; |
| 95 | + |
| 96 | + const _Constant(this._object); |
| 97 | + |
| 98 | + @override |
| 99 | + bool get boolValue => isBool |
| 100 | + ? _object.toBoolValue() |
| 101 | + : throw new FormatException('Not a bool', _object); |
| 102 | + |
| 103 | + @override |
| 104 | + int get intValue => isInt |
| 105 | + ? _object.toIntValue() |
| 106 | + : throw new FormatException('Not an int', _object); |
| 107 | + |
| 108 | + @override |
| 109 | + String get stringValue => isString |
| 110 | + ? _object.toStringValue() |
| 111 | + : throw new FormatException('Not a String', _object); |
| 112 | + |
| 113 | + @override |
| 114 | + bool get isBool => _object.toBoolValue() != null; |
| 115 | + |
| 116 | + @override |
| 117 | + bool get isInt => _object.toIntValue() != null; |
| 118 | + |
| 119 | + @override |
| 120 | + bool get isNull => _isNull(_object); |
| 121 | + |
| 122 | + @override |
| 123 | + bool get isString => _object.toStringValue() != null; |
| 124 | + |
| 125 | + @override |
| 126 | + ConstantReader read(String field) => |
| 127 | + new ConstantReader(_getFieldRecursive(_object, field)); |
| 128 | +} |
0 commit comments