| OLD | NEW |
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 // VM-specific implementation of the dart:mirrors library. | 5 // VM-specific implementation of the dart:mirrors library. |
| 6 | 6 |
| 7 import "dart:collection"; | 7 import "dart:collection"; |
| 8 | 8 |
| 9 final emptyList = new UnmodifiableListView([]); | 9 final emptyList = new UnmodifiableListView([]); |
| 10 final emptyMap = new _UnmodifiableMapView({}); | 10 final emptyMap = new UnmodifiableMapView({}); |
| 11 | |
| 12 // TODO(14314): Move to dart:collection. | |
| 13 class _UnmodifiableMapView<K, V> implements Map<K, V> { | |
| 14 final Map<K, V> _source; | |
| 15 | |
| 16 _UnmodifiableMapView(Map<K, V> source) : _source = source; | |
| 17 | |
| 18 static void _throw() { | |
| 19 throw new UnsupportedError("Cannot modify an unmodifiable Map"); | |
| 20 } | |
| 21 | |
| 22 int get length => _source.length; | |
| 23 bool get isEmpty => _source.isEmpty; | |
| 24 bool get isNotEmpty => _source.isNotEmpty; | |
| 25 V operator [](K key) => _source[key]; | |
| 26 bool containsKey(K key) => _source.containsKey(key); | |
| 27 bool containsValue(V value) => _source.containsValue(value); | |
| 28 void forEach(void f(K key, V value)) => _source.forEach(f); | |
| 29 Iterable<K> get keys => _source.keys; | |
| 30 Iterable<V> get values => _source.values; | |
| 31 void operator []=(K key, V value) => _throw(); | |
| 32 V putIfAbsent(K key, V ifAbsent()) { _throw(); } | |
| 33 void addAll(Map<K, V> other) => _throw(); | |
| 34 V remove(K key) { _throw(); } | |
| 35 void clear() => _throw(); | |
| 36 } | |
| 37 | 11 |
| 38 class _InternalMirrorError { | 12 class _InternalMirrorError { |
| 39 final String _msg; | 13 final String _msg; |
| 40 const _InternalMirrorError(String this._msg); | 14 const _InternalMirrorError(String this._msg); |
| 41 String toString() => _msg; | 15 String toString() => _msg; |
| 42 } | 16 } |
| 43 | 17 |
| 44 Map _filterMap(Map<Symbol, dynamic> old_map, bool filter(Symbol key, value)) { | 18 Map _filterMap(Map<Symbol, dynamic> old_map, bool filter(Symbol key, value)) { |
| 45 Map new_map = new Map<Symbol, dynamic>(); | 19 Map new_map = new Map<Symbol, dynamic>(); |
| 46 old_map.forEach((key, value) { | 20 old_map.forEach((key, value) { |
| 47 if (filter(key, value)) { | 21 if (filter(key, value)) { |
| 48 new_map[key] = value; | 22 new_map[key] = value; |
| 49 } | 23 } |
| 50 }); | 24 }); |
| 51 return new _UnmodifiableMapView(new_map); | 25 return new UnmodifiableMapView(new_map); |
| 52 } | 26 } |
| 53 | 27 |
| 54 Map _makeMemberMap(List mirrors) { | 28 Map _makeMemberMap(List mirrors) { |
| 55 return new _UnmodifiableMapView<Symbol, DeclarationMirror>( | 29 return new UnmodifiableMapView<Symbol, DeclarationMirror>( |
| 56 new Map<Symbol, DeclarationMirror>.fromIterable( | 30 new Map<Symbol, DeclarationMirror>.fromIterable( |
| 57 mirrors, key: (e) => e.simpleName)); | 31 mirrors, key: (e) => e.simpleName)); |
| 58 } | 32 } |
| 59 | 33 |
| 60 String _n(Symbol symbol) => _symbol_dev.Symbol.getName(symbol); | 34 String _n(Symbol symbol) => _symbol_dev.Symbol.getName(symbol); |
| 61 | 35 |
| 62 Symbol _s(String name) { | 36 Symbol _s(String name) { |
| 63 if (name == null) return null; | 37 if (name == null) return null; |
| 64 return new _symbol_dev.Symbol.unvalidated(name); | 38 return new _symbol_dev.Symbol.unvalidated(name); |
| 65 } | 39 } |
| (...skipping 567 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 633 result[getterName] = | 607 result[getterName] = |
| 634 new _SyntheticAccessor(this, getterName, true, true, false, decl); | 608 new _SyntheticAccessor(this, getterName, true, true, false, decl); |
| 635 if (!decl.isFinal) { | 609 if (!decl.isFinal) { |
| 636 var setterName = _asSetter(decl.simpleName, this.owner); | 610 var setterName = _asSetter(decl.simpleName, this.owner); |
| 637 result[setterName] = new _SyntheticAccessor( | 611 result[setterName] = new _SyntheticAccessor( |
| 638 this, setterName, false, true, false, decl); | 612 this, setterName, false, true, false, decl); |
| 639 } | 613 } |
| 640 } | 614 } |
| 641 }); | 615 }); |
| 642 _cachedStaticMembers = | 616 _cachedStaticMembers = |
| 643 new _UnmodifiableMapView<Symbol, MethodMirror>(result); | 617 new UnmodifiableMapView<Symbol, MethodMirror>(result); |
| 644 } | 618 } |
| 645 return _cachedStaticMembers; | 619 return _cachedStaticMembers; |
| 646 } | 620 } |
| 647 | 621 |
| 648 var _cachedInstanceMembers; | 622 var _cachedInstanceMembers; |
| 649 Map<Symbol, MethodMirror> get instanceMembers { | 623 Map<Symbol, MethodMirror> get instanceMembers { |
| 650 if (_cachedInstanceMembers == null) { | 624 if (_cachedInstanceMembers == null) { |
| 651 var result = new Map<Symbol, MethodMirror>(); | 625 var result = new Map<Symbol, MethodMirror>(); |
| 652 if (superclass != null) { | 626 if (superclass != null) { |
| 653 result.addAll(superclass.instanceMembers); | 627 result.addAll(superclass.instanceMembers); |
| 654 } | 628 } |
| 655 declarations.values.forEach((decl) { | 629 declarations.values.forEach((decl) { |
| 656 if (decl is MethodMirror && !decl.isStatic && | 630 if (decl is MethodMirror && !decl.isStatic && |
| 657 !decl.isConstructor && !decl.isAbstract) { | 631 !decl.isConstructor && !decl.isAbstract) { |
| 658 result[decl.simpleName] = decl; | 632 result[decl.simpleName] = decl; |
| 659 } | 633 } |
| 660 if (decl is VariableMirror && !decl.isStatic) { | 634 if (decl is VariableMirror && !decl.isStatic) { |
| 661 var getterName = decl.simpleName; | 635 var getterName = decl.simpleName; |
| 662 result[getterName] = | 636 result[getterName] = |
| 663 new _SyntheticAccessor(this, getterName, true, false, false, decl)
; | 637 new _SyntheticAccessor(this, getterName, true, false, false, decl)
; |
| 664 if (!decl.isFinal) { | 638 if (!decl.isFinal) { |
| 665 var setterName = _asSetter(decl.simpleName, this.owner); | 639 var setterName = _asSetter(decl.simpleName, this.owner); |
| 666 result[setterName] = new _SyntheticAccessor( | 640 result[setterName] = new _SyntheticAccessor( |
| 667 this, setterName, false, false, false, decl); | 641 this, setterName, false, false, false, decl); |
| 668 } | 642 } |
| 669 } | 643 } |
| 670 }); | 644 }); |
| 671 _cachedInstanceMembers = | 645 _cachedInstanceMembers = |
| 672 new _UnmodifiableMapView<Symbol, MethodMirror>(result); | 646 new UnmodifiableMapView<Symbol, MethodMirror>(result); |
| 673 } | 647 } |
| 674 return _cachedInstanceMembers; | 648 return _cachedInstanceMembers; |
| 675 } | 649 } |
| 676 | 650 |
| 677 Map<Symbol, DeclarationMirror> _declarations; | 651 Map<Symbol, DeclarationMirror> _declarations; |
| 678 Map<Symbol, DeclarationMirror> get declarations { | 652 Map<Symbol, DeclarationMirror> get declarations { |
| 679 if (_declarations != null) return _declarations; | 653 if (_declarations != null) return _declarations; |
| 680 var decls = new Map<Symbol, DeclarationMirror>(); | 654 var decls = new Map<Symbol, DeclarationMirror>(); |
| 681 decls.addAll(_members); | 655 decls.addAll(_members); |
| 682 decls.addAll(_constructors); | 656 decls.addAll(_constructors); |
| 683 typeVariables.forEach((tv) => decls[tv.simpleName] = tv); | 657 typeVariables.forEach((tv) => decls[tv.simpleName] = tv); |
| 684 return _declarations = | 658 return _declarations = |
| 685 new _UnmodifiableMapView<Symbol, DeclarationMirror>(decls); | 659 new UnmodifiableMapView<Symbol, DeclarationMirror>(decls); |
| 686 } | 660 } |
| 687 | 661 |
| 688 Map<Symbol, Mirror> _cachedMembers; | 662 Map<Symbol, Mirror> _cachedMembers; |
| 689 Map<Symbol, Mirror> get _members { | 663 Map<Symbol, Mirror> get _members { |
| 690 if (_cachedMembers == null) { | 664 if (_cachedMembers == null) { |
| 691 var whoseMembers = _isMixinAlias ? _trueSuperclass : this; | 665 var whoseMembers = _isMixinAlias ? _trueSuperclass : this; |
| 692 _cachedMembers = _makeMemberMap(mixin._computeMembers(whoseMembers._reflec
tee)); | 666 _cachedMembers = _makeMemberMap(mixin._computeMembers(whoseMembers._reflec
tee)); |
| 693 } | 667 } |
| 694 return _cachedMembers; | 668 return _cachedMembers; |
| 695 } | 669 } |
| (...skipping 316 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1012 | 986 |
| 1013 List<TypeVariableMirror> get typeVariables => emptyList; | 987 List<TypeVariableMirror> get typeVariables => emptyList; |
| 1014 List<TypeMirror> get typeArguments => emptyList; | 988 List<TypeMirror> get typeArguments => emptyList; |
| 1015 | 989 |
| 1016 bool get isOriginalDeclaration => true; | 990 bool get isOriginalDeclaration => true; |
| 1017 TypeMirror get originalDeclaration => this; | 991 TypeMirror get originalDeclaration => this; |
| 1018 | 992 |
| 1019 String toString() => "TypeVariableMirror on '${_n(simpleName)}'"; | 993 String toString() => "TypeVariableMirror on '${_n(simpleName)}'"; |
| 1020 | 994 |
| 1021 operator ==(other) { | 995 operator ==(other) { |
| 1022 return other is TypeVariableMirror | 996 return other is TypeVariableMirror |
| 1023 && simpleName == other.simpleName | 997 && simpleName == other.simpleName |
| 1024 && owner == other.owner; | 998 && owner == other.owner; |
| 1025 } | 999 } |
| 1026 int get hashCode => simpleName.hashCode; | 1000 int get hashCode => simpleName.hashCode; |
| 1027 | 1001 |
| 1028 bool isSubtypeOf(TypeMirror other) { | 1002 bool isSubtypeOf(TypeMirror other) { |
| 1029 if (other == currentMirrorSystem().dynamicType) return true; | 1003 if (other == currentMirrorSystem().dynamicType) return true; |
| 1030 if (other == currentMirrorSystem().voidType) return false; | 1004 if (other == currentMirrorSystem().voidType) return false; |
| 1031 return _subtypeTest(_reflectedType, other._reflectedType); | 1005 return _subtypeTest(_reflectedType, other._reflectedType); |
| 1032 } | 1006 } |
| (...skipping 148 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1181 Type get _instantiator => null; | 1155 Type get _instantiator => null; |
| 1182 | 1156 |
| 1183 SourceLocation get location { | 1157 SourceLocation get location { |
| 1184 throw new UnimplementedError('LibraryMirror.location is not implemented'); | 1158 throw new UnimplementedError('LibraryMirror.location is not implemented'); |
| 1185 } | 1159 } |
| 1186 | 1160 |
| 1187 Map<Symbol, DeclarationMirror> _declarations; | 1161 Map<Symbol, DeclarationMirror> _declarations; |
| 1188 Map<Symbol, DeclarationMirror> get declarations { | 1162 Map<Symbol, DeclarationMirror> get declarations { |
| 1189 if (_declarations != null) return _declarations; | 1163 if (_declarations != null) return _declarations; |
| 1190 return _declarations = | 1164 return _declarations = |
| 1191 new _UnmodifiableMapView<Symbol, DeclarationMirror>(_members); | 1165 new UnmodifiableMapView<Symbol, DeclarationMirror>(_members); |
| 1192 } | 1166 } |
| 1193 | 1167 |
| 1194 Map<Symbol, Mirror> _cachedMembers; | 1168 Map<Symbol, Mirror> _cachedMembers; |
| 1195 Map<Symbol, Mirror> get _members { | 1169 Map<Symbol, Mirror> get _members { |
| 1196 if (_cachedMembers == null) { | 1170 if (_cachedMembers == null) { |
| 1197 _cachedMembers = _makeMemberMap(_computeMembers(_reflectee)); | 1171 _cachedMembers = _makeMemberMap(_computeMembers(_reflectee)); |
| 1198 } | 1172 } |
| 1199 return _cachedMembers; | 1173 return _cachedMembers; |
| 1200 } | 1174 } |
| 1201 | 1175 |
| (...skipping 416 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1618 if (typeMirror == null) { | 1592 if (typeMirror == null) { |
| 1619 typeMirror = makeLocalTypeMirror(key); | 1593 typeMirror = makeLocalTypeMirror(key); |
| 1620 _instanitationCache[key] = typeMirror; | 1594 _instanitationCache[key] = typeMirror; |
| 1621 if (typeMirror is ClassMirror && !typeMirror._isGeneric) { | 1595 if (typeMirror is ClassMirror && !typeMirror._isGeneric) { |
| 1622 _declarationCache[key] = typeMirror; | 1596 _declarationCache[key] = typeMirror; |
| 1623 } | 1597 } |
| 1624 } | 1598 } |
| 1625 return typeMirror; | 1599 return typeMirror; |
| 1626 } | 1600 } |
| 1627 } | 1601 } |
| OLD | NEW |