Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(17)

Side by Side Diff: reflectable/lib/src/mirrors_unimpl.dart

Issue 1284423004: Initializes _data in constructor (Closed) Base URL: https://github.com/dart-lang/reflectable.git@master
Patch Set: Created 5 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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';
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after
97 } 97 }
98 } 98 }
99 99
100 /// This mapping contains the mirror-data for each reflector. 100 /// This mapping contains the mirror-data for each reflector.
101 /// It will be initialized in the generated code. 101 /// It will be initialized in the generated code.
102 Map<Reflectable, ReflectorData> data = 102 Map<Reflectable, ReflectorData> data =
103 throw new StateError("Reflectable has not been initialized. " 103 throw new StateError("Reflectable has not been initialized. "
104 "Did you forget to add the main file to the " 104 "Did you forget to add the main file to the "
105 "reflectable transformer's entry_points in pubspec.yaml?"); 105 "reflectable transformer's entry_points in pubspec.yaml?");
106 106
107 abstract class _DataCaching { 107 abstract class _DataCachingMirror {
108 // TODO(eernst) clarify: When we have some substantial pieces of code using 108 /// The reflector which represents the mirror system that this
109 // reflectable, perform some experiments to detect how useful it is to have 109 /// mirror belongs to.
110 // this kind of caching. 110 final ReflectableImpl _reflector;
111 111
112 ReflectorData _dataCache; 112 /// Reflector data associated with this mirror.
113 ReflectableImpl get _reflector; 113 final ReflectorData _data;
114 114
115 ReflectorData get _data { 115 _DataCachingMirror(ReflectableImpl reflector)
116 if (_dataCache == null) { 116 : _reflector = reflector,
117 _dataCache = data[_reflector]; 117 _data = data[reflector];
118 }
119 return _dataCache;
120 }
121 } 118 }
122 119
123 class _InstanceMirrorImpl extends _DataCaching implements InstanceMirror { 120 class _InstanceMirrorImpl extends _DataCachingMirror implements InstanceMirror {
124 final ReflectableImpl _reflector;
125 final Object reflectee; 121 final Object reflectee;
126 122
127 _InstanceMirrorImpl(this.reflectee, this._reflector) { 123 _InstanceMirrorImpl(this.reflectee, ReflectableImpl reflector)
124 : super(reflector) {
128 _type = _data.classMirrorForType(reflectee.runtimeType); 125 _type = _data.classMirrorForType(reflectee.runtimeType);
129 if (_type == null) { 126 if (_type == null) {
130 throw new NoSuchCapabilityError( 127 throw new NoSuchCapabilityError(
131 "Reflecting on un-marked type ${reflectee.runtimeType}"); 128 "Reflecting on un-marked type ${reflectee.runtimeType}");
132 } 129 }
133 } 130 }
134 131
135 ClassMirror _type; 132 ClassMirror _type;
136 133
137 ClassMirror get type => _type; 134 ClassMirror get type => _type;
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after
219 variableMirror._ownerIndex, 216 variableMirror._ownerIndex,
220 variableMirror._reflector, 217 variableMirror._reflector,
221 variableMirror.metadata, 218 variableMirror.metadata,
222 null, 219 null,
223 -1) 220 -1)
224 ], 221 ],
225 variableMirror._reflector, 222 variableMirror._reflector,
226 []); 223 []);
227 } 224 }
228 225
229 class ClassMirrorImpl extends _DataCaching implements ClassMirror { 226 class ClassMirrorImpl extends _DataCachingMirror implements ClassMirror {
230 /// The reflector which represents the mirror system that this
231 /// mirror belongs to.
232 final ReflectableImpl _reflector;
233
234 /// The index of this mirror in the [ReflectorData.classMirrors] table. 227 /// The index of this mirror in the [ReflectorData.classMirrors] table.
235 /// Also this is the index of the Type of the reflected class in 228 /// Also this is the index of the Type of the reflected class in
236 /// [ReflectorData.types]. 229 /// [ReflectorData.types].
237 final int _classIndex; 230 final int _classIndex;
238 231
239 /// A list of the indices in [ReflectorData.memberMirrors] of the 232 /// A list of the indices in [ReflectorData.memberMirrors] of the
240 /// declarations of the reflected class. This includes method mirrors 233 /// declarations of the reflected class. This includes method mirrors
241 /// and variable mirrors and it directly corresponds to `declarations`. 234 /// and variable mirrors and it directly corresponds to `declarations`.
242 final List<int> _declarationIndices; 235 final List<int> _declarationIndices;
243 236
(...skipping 12 matching lines...) Expand all
256 final String qualifiedName; 249 final String qualifiedName;
257 final List<Object> _metadata; 250 final List<Object> _metadata;
258 final Map<String, _StaticGetter> getters; 251 final Map<String, _StaticGetter> getters;
259 final Map<String, _StaticSetter> setters; 252 final Map<String, _StaticSetter> setters;
260 final Map<String, Function> constructors; 253 final Map<String, Function> constructors;
261 254
262 ClassMirrorImpl( 255 ClassMirrorImpl(
263 this.simpleName, 256 this.simpleName,
264 this.qualifiedName, 257 this.qualifiedName,
265 this._classIndex, 258 this._classIndex,
266 this._reflector, 259 ReflectableImpl reflector,
267 this._declarationIndices, 260 this._declarationIndices,
268 this._instanceMemberIndices, 261 this._instanceMemberIndices,
269 this._superclassIndex, 262 this._superclassIndex,
270 this.getters, 263 this.getters,
271 this.setters, 264 this.setters,
272 this.constructors, 265 this.constructors,
273 metadata) 266 metadata)
274 : _metadata = 267 : _metadata =
275 (metadata == null) ? null : new UnmodifiableListView(metadata); 268 (metadata == null) ? null : new UnmodifiableListView(metadata),
269 super(reflector);
276 270
277 ClassMirror get superclass { 271 ClassMirror get superclass {
278 if (_superclassIndex == null) return null; 272 if (_superclassIndex == null) return null;
279 if (_superclassIndex == -1) { 273 if (_superclassIndex == -1) {
280 throw new NoSuchCapabilityError( 274 throw new NoSuchCapabilityError(
281 "Requesting mirror on un-marked class, superclass of $simpleName"); 275 "Requesting mirror on un-marked class, superclass of $simpleName");
282 } 276 }
283 return _data.classMirrors[_superclassIndex]; 277 return _data.classMirrors[_superclassIndex];
284 } 278 }
285 279
(...skipping 159 matching lines...) Expand 10 before | Expand all | Expand 10 after
445 return getter; 439 return getter;
446 } 440 }
447 441
448 String toString() => "ClassMirrorImpl($qualifiedName)"; 442 String toString() => "ClassMirrorImpl($qualifiedName)";
449 443
450 // Because we take care to only ever create one instance for each 444 // Because we take care to only ever create one instance for each
451 // type/reflector-combination we can rely on the default `hashCode` and `==` 445 // type/reflector-combination we can rely on the default `hashCode` and `==`
452 // operations. 446 // operations.
453 } 447 }
454 448
455 class MethodMirrorImpl extends _DataCaching implements MethodMirror { 449 class MethodMirrorImpl extends _DataCachingMirror implements MethodMirror {
456 /// An encoding of the attributes and kind of this mirror. 450 /// An encoding of the attributes and kind of this mirror.
457 final int _descriptor; 451 final int _descriptor;
458 452
459 /// The name of this method. Setters names will end in '='. 453 /// The name of this method. Setters names will end in '='.
460 final String _name; 454 final String _name;
461 455
462 /// The index of the [ClassMirror] of the owner of this method, 456 /// The index of the [ClassMirror] of the owner of this method,
463 final int _ownerIndex; 457 final int _ownerIndex;
464 458
465 /// The indices of the [ParameterMirror]s describing the formal parameters 459 /// The indices of the [ParameterMirror]s describing the formal parameters
466 /// of this method. 460 /// of this method.
467 final List<int> _parameterIndices; 461 final List<int> _parameterIndices;
468 462
469 /// The [Reflectable] associated with this mirror.
470 final ReflectableImpl _reflector;
471
472 /// A cache of the metadata of the mirrored method. The empty list means 463 /// A cache of the metadata of the mirrored method. The empty list means
473 /// no metadata, null means that [_reflector] does not have 464 /// no metadata, null means that [_reflector] does not have
474 /// [metadataCapability]. 465 /// [metadataCapability].
475 final List<Object> _metadata; 466 final List<Object> _metadata;
476 467
477 MethodMirrorImpl(this._name, this._descriptor, this._ownerIndex, 468 MethodMirrorImpl(this._name, this._descriptor, this._ownerIndex,
478 this._parameterIndices, this._reflector, List<Object> metadata) 469 this._parameterIndices, ReflectableImpl reflector, List<Object> metadata)
479 : _metadata = 470 : _metadata =
480 (metadata == null) ? null : new UnmodifiableListView(metadata); 471 (metadata == null) ? null : new UnmodifiableListView(metadata),
472 super(reflector);
481 473
482 int get kind => constants.kindFromEncoding(_descriptor); 474 int get kind => constants.kindFromEncoding(_descriptor);
483 475
484 ClassMirror get owner => _data.classMirrors[_ownerIndex]; 476 ClassMirror get owner => _data.classMirrors[_ownerIndex];
485 477
486 @override 478 @override
487 String get constructorName => _name; 479 String get constructorName => _name;
488 480
489 @override 481 @override
490 bool get isAbstract => (_descriptor & constants.abstractAttribute != 0); 482 bool get isAbstract => (_descriptor & constants.abstractAttribute != 0);
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after
562 ? (_name == '' ? "${owner.simpleName}" : "${owner.simpleName}.$_name") 554 ? (_name == '' ? "${owner.simpleName}" : "${owner.simpleName}.$_name")
563 : _name; 555 : _name;
564 556
565 @override 557 @override
566 String get source => null; 558 String get source => null;
567 559
568 @override 560 @override
569 String toString() => "MethodMirror($_name)"; 561 String toString() => "MethodMirror($_name)";
570 } 562 }
571 563
572 abstract class VariableMirrorBase extends _DataCaching 564 abstract class VariableMirrorBase extends _DataCachingMirror
573 implements VariableMirror { 565 implements VariableMirror {
574 final String _name; 566 final String _name;
575 final int _descriptor; 567 final int _descriptor;
576 final int _ownerIndex; 568 final int _ownerIndex;
577 final ReflectableImpl _reflector;
578 final List<Object> _metadata; 569 final List<Object> _metadata;
579 570
580 VariableMirrorBase(this._name, this._descriptor, this._ownerIndex, 571 VariableMirrorBase(this._name, this._descriptor, this._ownerIndex,
581 this._reflector, List<Object> metadata) 572 ReflectableImpl reflector, List<Object> metadata)
582 : _metadata = 573 : _metadata =
583 (metadata == null) ? null : new UnmodifiableListView(metadata); 574 (metadata == null) ? null : new UnmodifiableListView(metadata),
575 super(reflector);
584 576
585 int get kind => constants.kindFromEncoding(_descriptor); 577 int get kind => constants.kindFromEncoding(_descriptor);
586 578
587 @override 579 @override
588 bool get isPrivate => (_descriptor & constants.privateAttribute != 0); 580 bool get isPrivate => (_descriptor & constants.privateAttribute != 0);
589 581
590 @override 582 @override
591 bool get isTopLevel => owner is LibraryMirror; 583 bool get isTopLevel => owner is LibraryMirror;
592 584
593 @override 585 @override
(...skipping 207 matching lines...) Expand 10 before | Expand all | Expand 10 after
801 LibraryMirror findLibrary(String library) => _unsupported(); 793 LibraryMirror findLibrary(String library) => _unsupported();
802 794
803 @override 795 @override
804 Map<Uri, LibraryMirror> get libraries => _unsupported(); 796 Map<Uri, LibraryMirror> get libraries => _unsupported();
805 797
806 @override 798 @override
807 Iterable<ClassMirror> get annotatedClasses { 799 Iterable<ClassMirror> get annotatedClasses {
808 return new UnmodifiableListView<ClassMirror>(data[this].classMirrors); 800 return new UnmodifiableListView<ClassMirror>(data[this].classMirrors);
809 } 801 }
810 } 802 }
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698