| OLD | NEW |
| 1 // Copyright (c) 2015, the Dart Team. All rights reserved. Use of this | 1 // Copyright (c) 2015, the Dart Team. All rights reserved. Use of this |
| 2 // source code is governed by a BSD-style license that can be found in | 2 // source code is governed by a BSD-style license that can be found in |
| 3 // the LICENSE file. | 3 // the LICENSE file. |
| 4 | 4 |
| 5 library reflectable.src.mirrors_unimpl; | 5 library reflectable.src.mirrors_unimpl; |
| 6 | 6 |
| 7 import 'dart:collection' show UnmodifiableMapView, UnmodifiableListView; | 7 import 'dart:collection' show UnmodifiableMapView, UnmodifiableListView; |
| 8 | 8 |
| 9 import '../capability.dart'; | 9 import '../capability.dart'; |
| 10 import '../mirrors.dart'; | 10 import '../mirrors.dart'; |
| 11 import '../reflectable.dart'; | 11 import '../reflectable.dart'; |
| 12 import 'encoding_constants.dart' as constants; | 12 import 'encoding_constants.dart' as constants; |
| 13 import 'encoding_constants.dart' show NO_CAPABILITY_INDEX; | 13 import 'encoding_constants.dart' show NO_CAPABILITY_INDEX; |
| 14 import 'reflectable_base.dart'; | 14 import 'reflectable_base.dart'; |
| 15 | 15 |
| 16 bool get isTransformed => true; | 16 bool get isTransformed => true; |
| 17 | 17 |
| 18 /// Returns the set of reflectors in the current program. |
| 19 Set<Reflectable> get reflectors => data.keys.toSet(); |
| 20 |
| 18 // Mirror classes with default implementations of all methods, to be used as | 21 // Mirror classes with default implementations of all methods, to be used as |
| 19 // superclasses of transformer generated static mirror classes. They serve to | 22 // superclasses of transformer generated static mirror classes. They serve to |
| 20 // ensure that the static mirror classes always implement all methods, such that | 23 // ensure that the static mirror classes always implement all methods, such that |
| 21 // they throw an exception at runtime, rather than causing an error at compile | 24 // they throw an exception at runtime, rather than causing an error at compile |
| 22 // time, which is the required behavior for static mirrors when they are used | 25 // time, which is the required behavior for static mirrors when they are used |
| 23 // in ways that are not covered by the specified capabilities. | 26 // in ways that are not covered by the specified capabilities. |
| 24 // | 27 // |
| 25 // Each of these classes implements the corresponding class in | 28 // Each of these classes implements the corresponding class in |
| 26 // `package:reflectable/mirrors.dart`, and they replicate the internal | 29 // `package:reflectable/mirrors.dart`, and they replicate the internal |
| 27 // implements `structure in package:reflectable/mirrors.dart` using `extends` | 30 // implements `structure in package:reflectable/mirrors.dart` using `extends` |
| (...skipping 173 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 201 } | 204 } |
| 202 throw new NoSuchInvokeCapabilityError(reflectee, setterName, [value], {}); | 205 throw new NoSuchInvokeCapabilityError(reflectee, setterName, [value], {}); |
| 203 } | 206 } |
| 204 } | 207 } |
| 205 | 208 |
| 206 class ClassMirrorImpl extends _DataCaching implements ClassMirror { | 209 class ClassMirrorImpl extends _DataCaching implements ClassMirror { |
| 207 /// The reflector which represents the mirror system that this | 210 /// The reflector which represents the mirror system that this |
| 208 /// mirror belongs to. | 211 /// mirror belongs to. |
| 209 final ReflectableImpl _reflector; | 212 final ReflectableImpl _reflector; |
| 210 | 213 |
| 214 /// An encoding of the attributes and kind of this class mirror. |
| 215 final int _descriptor; |
| 216 |
| 211 /// The index of this mirror in the [ReflectorData.classMirrors] table. | 217 /// The index of this mirror in the [ReflectorData.classMirrors] table. |
| 212 /// Also this is the index of the Type of the reflected class in | 218 /// Also this is the index of the Type of the reflected class in |
| 213 /// [ReflectorData.types]. | 219 /// [ReflectorData.types]. |
| 214 final int _classIndex; | 220 final int _classIndex; |
| 215 | 221 |
| 216 /// The index of the owner library in the [ReflectorData.libraryMirrors] | 222 /// The index of the owner library in the [ReflectorData.libraryMirrors] |
| 217 /// table. | 223 /// table. |
| 218 final int _ownerIndex; | 224 final int _ownerIndex; |
| 219 | 225 |
| 220 /// The index of the mirror of the superclass in the | 226 /// The index of the mirror of the superclass in the |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 274 final String simpleName; | 280 final String simpleName; |
| 275 final String qualifiedName; | 281 final String qualifiedName; |
| 276 final List<Object> _metadata; | 282 final List<Object> _metadata; |
| 277 final Map<String, _StaticGetter> _getters; | 283 final Map<String, _StaticGetter> _getters; |
| 278 final Map<String, _StaticSetter> _setters; | 284 final Map<String, _StaticSetter> _setters; |
| 279 final Map<String, Function> _constructors; | 285 final Map<String, Function> _constructors; |
| 280 | 286 |
| 281 ClassMirrorImpl( | 287 ClassMirrorImpl( |
| 282 this.simpleName, | 288 this.simpleName, |
| 283 this.qualifiedName, | 289 this.qualifiedName, |
| 290 this._descriptor, |
| 284 this._classIndex, | 291 this._classIndex, |
| 285 this._reflector, | 292 this._reflector, |
| 286 this._declarationIndices, | 293 this._declarationIndices, |
| 287 this._instanceMemberIndices, | 294 this._instanceMemberIndices, |
| 288 this._staticMemberIndices, | 295 this._staticMemberIndices, |
| 289 this._superclassIndex, | 296 this._superclassIndex, |
| 290 this._getters, | 297 this._getters, |
| 291 this._setters, | 298 this._setters, |
| 292 this._constructors, | 299 this._constructors, |
| 293 this._ownerIndex, | 300 this._ownerIndex, |
| 294 this._mixinIndex, | 301 this._mixinIndex, |
| 295 this._superinterfaceIndices, | 302 this._superinterfaceIndices, |
| 296 List<Object> metadata) | 303 List<Object> metadata) |
| 297 : _metadata = | 304 : _metadata = |
| 298 (metadata == null) ? null : new UnmodifiableListView(metadata); | 305 (metadata == null) ? null : new UnmodifiableListView(metadata); |
| 299 | 306 |
| 300 bool get isAbstract => _unsupported(); | 307 bool get isAbstract => (_descriptor & constants.abstractAttribute != 0); |
| 301 | 308 |
| 302 Map<String, DeclarationMirror> _declarations; | 309 Map<String, DeclarationMirror> _declarations; |
| 303 | 310 |
| 304 Map<String, DeclarationMirror> get declarations { | 311 Map<String, DeclarationMirror> get declarations { |
| 305 if (_declarations == null) { | 312 if (_declarations == null) { |
| 306 Map<String, DeclarationMirror> result = | 313 Map<String, DeclarationMirror> result = |
| 307 new Map<String, DeclarationMirror>(); | 314 new Map<String, DeclarationMirror>(); |
| 308 for (int declarationIndex in _declarationIndices) { | 315 for (int declarationIndex in _declarationIndices) { |
| 309 // We encode a missing `declarations` capability as an index with | 316 // We encode a missing `declarations` capability as an index with |
| 310 // the value NO_CAPABILITY_INDEX. Note that `_declarations` will not be | 317 // the value NO_CAPABILITY_INDEX. Note that `_declarations` will not be |
| (...skipping 175 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 486 return _data.classMirrors[_superclassIndex]; | 493 return _data.classMirrors[_superclassIndex]; |
| 487 } | 494 } |
| 488 | 495 |
| 489 String toString() => "ClassMirrorImpl($qualifiedName)"; | 496 String toString() => "ClassMirrorImpl($qualifiedName)"; |
| 490 | 497 |
| 491 // Because we take care to only ever create one instance for each | 498 // Because we take care to only ever create one instance for each |
| 492 // type/reflector-combination we can rely on the default `hashCode` and `==` | 499 // type/reflector-combination we can rely on the default `hashCode` and `==` |
| 493 // operations. | 500 // operations. |
| 494 } | 501 } |
| 495 | 502 |
| 496 class LibraryMirrorImpl implements LibraryMirror { | 503 class LibraryMirrorImpl extends _DataCaching implements LibraryMirror { |
| 497 LibraryMirrorImpl(this.simpleName, this.uri, this.getters, this.setters, | 504 LibraryMirrorImpl( |
| 505 this.simpleName, |
| 506 this.uri, |
| 507 this._reflector, |
| 508 this._declarationIndices, |
| 509 this.getters, |
| 510 this.setters, |
| 498 List<Object> metadata) | 511 List<Object> metadata) |
| 499 : _metadata = | 512 : _metadata = |
| 500 metadata == null ? null : new UnmodifiableListView(metadata); | 513 metadata == null ? null : new UnmodifiableListView(metadata); |
| 501 | 514 |
| 515 final ReflectableImpl _reflector; |
| 516 |
| 517 /// A list of the indices in [ReflectorData.memberMirrors] of the |
| 518 /// declarations of the reflected class. This includes method mirrors for |
| 519 /// top level functions of this library and it directly corresponds to |
| 520 /// `declarations`. Exception: When the given `_reflector.capabilities` do |
| 521 /// not support the operation `declarations`, this will be |
| 522 /// `<int>[NO_CAPABILITY_INDEX]`. It is enough to check that the list is |
| 523 /// non-empty and first element is NO_CAPABILITY_INDEX to detect this |
| 524 /// situation, because NO_CAPABILITY_INDEX` will otherwise never occur. |
| 525 final List<int> _declarationIndices; |
| 526 |
| 527 @override |
| 502 final Uri uri; | 528 final Uri uri; |
| 503 | 529 |
| 504 // TODO(eernst) clarify: `_unsupported()` means not yet implemented here? | |
| 505 Map<String, DeclarationMirror> get declarations => _unsupported(); | |
| 506 | |
| 507 @override | 530 @override |
| 508 final String simpleName; | 531 final String simpleName; |
| 509 | 532 |
| 510 final Map<String, _StaticGetter> getters; | 533 final Map<String, _StaticGetter> getters; |
| 511 final Map<String, _StaticSetter> setters; | 534 final Map<String, _StaticSetter> setters; |
| 512 | 535 |
| 536 Map<String, DeclarationMirror> _declarations; |
| 537 |
| 538 @override |
| 539 Map<String, DeclarationMirror> get declarations { |
| 540 if (_declarations == null) { |
| 541 Map<String, DeclarationMirror> result = |
| 542 new Map<String, DeclarationMirror>(); |
| 543 for (int declarationIndex in _declarationIndices) { |
| 544 // We encode a missing `declarations` capability as an index with |
| 545 // the value NO_CAPABILITY_INDEX. Note that `_declarations` will not be |
| 546 // initialized and hence we will come here repeatedly if that is the |
| 547 // case; however, performing operations for which there is no capability |
| 548 // need not have stellar performance, it is almost always a bug to do |
| 549 // that. |
| 550 if (declarationIndex == NO_CAPABILITY_INDEX) { |
| 551 throw new NoSuchCapabilityError( |
| 552 "Requesting declarations of '$qualifiedName' without capability"); |
| 553 } |
| 554 DeclarationMirror declarationMirror = |
| 555 _data.memberMirrors[declarationIndex]; |
| 556 result[declarationMirror.simpleName] = declarationMirror; |
| 557 } |
| 558 _declarations = |
| 559 new UnmodifiableMapView<String, DeclarationMirror>(result); |
| 560 } |
| 561 return _declarations; |
| 562 } |
| 563 |
| 513 @override | 564 @override |
| 514 Object invoke(String memberName, List positionalArguments, | 565 Object invoke(String memberName, List positionalArguments, |
| 515 [Map<Symbol, dynamic> namedArguments]) { | 566 [Map<Symbol, dynamic> namedArguments]) { |
| 516 _StaticGetter getter = getters[memberName]; | 567 _StaticGetter getter = getters[memberName]; |
| 517 if (getter == null) { | 568 if (getter == null) { |
| 518 throw new NoSuchInvokeCapabilityError( | 569 throw new NoSuchInvokeCapabilityError( |
| 519 null, memberName, positionalArguments, namedArguments); | 570 null, memberName, positionalArguments, namedArguments); |
| 520 } | 571 } |
| 521 return Function.apply( | 572 return Function.apply( |
| 522 getters[memberName](), positionalArguments, namedArguments); | 573 getter(), positionalArguments, namedArguments); |
| 523 } | 574 } |
| 524 | 575 |
| 525 @override | 576 @override |
| 526 Object invokeGetter(String getterName) { | 577 Object invokeGetter(String getterName) { |
| 527 _StaticGetter getter = getters[getterName]; | 578 _StaticGetter getter = getters[getterName]; |
| 528 if (getter == null) { | 579 if (getter == null) { |
| 529 throw new NoSuchInvokeCapabilityError(null, getterName, [], {}); | 580 throw new NoSuchInvokeCapabilityError(null, getterName, [], {}); |
| 530 } | 581 } |
| 531 return getter(); | 582 return getter(); |
| 532 } | 583 } |
| (...skipping 659 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1192 LibraryMirror findLibrary(String libraryName) { | 1243 LibraryMirror findLibrary(String libraryName) { |
| 1193 if (data[this].libraryMirrors == null) { | 1244 if (data[this].libraryMirrors == null) { |
| 1194 throw new NoSuchCapabilityError("Using 'findLibrary' without capability. " | 1245 throw new NoSuchCapabilityError("Using 'findLibrary' without capability. " |
| 1195 "Try adding 'libraryCapability'."); | 1246 "Try adding 'libraryCapability'."); |
| 1196 } | 1247 } |
| 1197 return data[this].libraryMirrors.singleWhere( | 1248 return data[this].libraryMirrors.singleWhere( |
| 1198 (LibraryMirror mirror) => mirror.qualifiedName == libraryName); | 1249 (LibraryMirror mirror) => mirror.qualifiedName == libraryName); |
| 1199 } | 1250 } |
| 1200 | 1251 |
| 1201 @override | 1252 @override |
| 1202 Map<Uri, LibraryMirror> get libraries => _unsupported(); | 1253 Map<Uri, LibraryMirror> get libraries { |
| 1254 Map<Uri, LibraryMirror> result = <Uri, LibraryMirror>{}; |
| 1255 for (LibraryMirror library in data[this].libraryMirrors) { |
| 1256 result[library.uri] = library; |
| 1257 } |
| 1258 return new UnmodifiableMapView(result); |
| 1259 } |
| 1203 | 1260 |
| 1204 @override | 1261 @override |
| 1205 Iterable<ClassMirror> get annotatedClasses { | 1262 Iterable<ClassMirror> get annotatedClasses { |
| 1206 return new UnmodifiableListView<ClassMirror>(data[this].classMirrors); | 1263 return new UnmodifiableListView<ClassMirror>(data[this].classMirrors); |
| 1207 } | 1264 } |
| 1208 } | 1265 } |
| 1209 | 1266 |
| 1210 // For mixin-applications we need to construct objects that represents their | 1267 // For mixin-applications we need to construct objects that represents their |
| 1211 // type. | 1268 // type. |
| 1212 class FakeType implements Type { | 1269 class FakeType implements Type { |
| 1213 const FakeType(this.description); | 1270 const FakeType(this.description); |
| 1214 | 1271 |
| 1215 final String description; | 1272 final String description; |
| 1216 | 1273 |
| 1217 String toString() => "Type($description)"; | 1274 String toString() => "Type($description)"; |
| 1218 } | 1275 } |
| OLD | NEW |