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

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

Issue 1297503006: Clarifies the status of implicit accessors; fixed several related bugs. (Closed) Base URL: https://github.com/dart-lang/reflectable.git@master
Patch Set: Review response. 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 | reflectable/lib/src/transformer_implementation.dart » ('j') | 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 163 matching lines...) Expand 10 before | Expand all | Expand 10 after
174 setterName += "="; 174 setterName += "=";
175 } 175 }
176 Function setter = _data.setters[setterName]; 176 Function setter = _data.setters[setterName];
177 if (setter != null) { 177 if (setter != null) {
178 return setter(reflectee, value); 178 return setter(reflectee, value);
179 } 179 }
180 throw new NoSuchInvokeCapabilityError(reflectee, setterName, [value], {}); 180 throw new NoSuchInvokeCapabilityError(reflectee, setterName, [value], {});
181 } 181 }
182 } 182 }
183 183
184 int _variableToImplicitAccessorAttributes(VariableMirror variableMirror) {
185 int attributes = 0;
186 if (variableMirror.isPrivate) attributes |= constants.privateAttribute;
187 if (variableMirror.isStatic) attributes |= constants.staticAttribute;
188 attributes |= constants.syntheticAttribute;
189 return attributes;
190 }
191
192 MethodMirror _variableToGetterMirror(VariableMirrorImpl variableMirror) {
193 int descriptor = constants.getter;
194 descriptor |= _variableToImplicitAccessorAttributes(variableMirror);
195 // TODO(eernst) clarify: Make sure it is the right `ownerIndex`: Write a test
196 // that compares owners in pre/post-transform code.
197 return new MethodMirrorImpl(variableMirror.simpleName, descriptor,
198 variableMirror._ownerIndex, [], variableMirror._reflector, []);
199 }
200
201 // TODO(eernst) implement: Make this method take an `index` parameter which
202 // will be the index of the returned `MethodMirror`, such that the enclosed
203 // parameter mirror can get the right ownerIndex.
204 MethodMirror _variableToSetterMirror(VariableMirrorImpl variableMirror) {
205 int descriptor = constants.setter | constants.syntheticAttribute;
206 descriptor |= _variableToImplicitAccessorAttributes(variableMirror);
207 int parameterDescriptor = constants.parameter | constants.syntheticAttribute;
208 String name = variableMirror.simpleName + "=";
209 return new MethodMirrorImpl(
210 name,
211 descriptor,
212 variableMirror._ownerIndex,
213 [
214 // TODO(eernst) clarify: Make sure it is the right `ownerIndex`: Write a
215 // test that compares owners in pre/post-transform code.
216 new ParameterMirrorImpl(
217 name,
218 parameterDescriptor,
219 variableMirror._ownerIndex,
220 variableMirror._reflector,
221 -1,
222 variableMirror.metadata,
223 null)
224 ],
225 variableMirror._reflector,
226 []);
227 }
228
229 class ClassMirrorImpl extends _DataCaching implements ClassMirror { 184 class ClassMirrorImpl extends _DataCaching implements ClassMirror {
230 /// The reflector which represents the mirror system that this 185 /// The reflector which represents the mirror system that this
231 /// mirror belongs to. 186 /// mirror belongs to.
232 final ReflectableImpl _reflector; 187 final ReflectableImpl _reflector;
233 188
234 /// The index of this mirror in the [ReflectorData.classMirrors] table. 189 /// The index of this mirror in the [ReflectorData.classMirrors] table.
235 /// Also this is the index of the Type of the reflected class in 190 /// Also this is the index of the Type of the reflected class in
236 /// [ReflectorData.types]. 191 /// [ReflectorData.types].
237 final int _classIndex; 192 final int _classIndex;
238 193
239 /// A list of the indices in [ReflectorData.memberMirrors] of the 194 /// A list of the indices in [ReflectorData.memberMirrors] of the
240 /// declarations of the reflected class. This includes method mirrors 195 /// declarations of the reflected class. This includes method mirrors
241 /// and variable mirrors and it directly corresponds to `declarations`. 196 /// and variable mirrors and it directly corresponds to `declarations`.
242 /// Exception: When the given `_reflector.capabilities` do not support 197 /// Exception: When the given `_reflector.capabilities` do not support
243 /// the operation `declarations`, this will be `<int>[-1]`. It is enough 198 /// the operation `declarations`, this will be `<int>[-1]`. It is enough
244 /// to check that the list is non-empty and first element is -1 to 199 /// to check that the list is non-empty and first element is -1 to
245 /// detect this situation, because -1 will otherwise never occur. 200 /// detect this situation, because -1 will otherwise never occur.
246 final List<int> _declarationIndices; 201 final List<int> _declarationIndices;
247 202
248 /// A list of the indices in [ReflectorData.memberMirrors] of the 203 /// A list of the indices in [ReflectorData.memberMirrors] of the
249 /// instance members of the reflected class, except that it includes 204 /// instance members of the reflected class, except that it includes
250 /// variable mirrors describing fields which must be converted to 205 /// variable mirrors describing fields which must be converted to
251 /// implicit getters and possibly implicit setters in order to 206 /// implicit getters and possibly implicit setters in order to
252 /// obtain the correct result for `instanceMembers`. 207 /// obtain the correct result for `instanceMembers`.
253 final List<int> _instanceMemberIndices; 208 final List<int> _instanceMemberIndices;
254 209
210 /// A list of the indices in [ReflectorData.memberMirrors] of the
211 /// static members of the reflected class, except that it includes
212 /// variable mirrors describing static fields nwhich must be converted
213 /// to implicit getters and possibly implicit setters in order to
214 /// obtain the correct result for `staticMembers`.
215 final List<int> _staticMemberIndices;
216
255 /// The index of the mirror of the superclass in the 217 /// The index of the mirror of the superclass in the
256 /// [ReflectorData.classMirrors] table. 218 /// [ReflectorData.classMirrors] table.
257 final int _superclassIndex; 219 final int _superclassIndex;
258 220
259 final String simpleName; 221 final String simpleName;
260 final String qualifiedName; 222 final String qualifiedName;
261 final List<Object> _metadata; 223 final List<Object> _metadata;
262 final Map<String, _StaticGetter> getters; 224 final Map<String, _StaticGetter> getters;
263 final Map<String, _StaticSetter> setters; 225 final Map<String, _StaticSetter> setters;
264 final Map<String, Function> constructors; 226 final Map<String, Function> constructors;
265 227
266 ClassMirrorImpl( 228 ClassMirrorImpl(
267 this.simpleName, 229 this.simpleName,
268 this.qualifiedName, 230 this.qualifiedName,
269 this._classIndex, 231 this._classIndex,
270 this._reflector, 232 this._reflector,
271 this._declarationIndices, 233 this._declarationIndices,
272 this._instanceMemberIndices, 234 this._instanceMemberIndices,
235 this._staticMemberIndices,
273 this._superclassIndex, 236 this._superclassIndex,
274 this.getters, 237 this.getters,
275 this.setters, 238 this.setters,
276 this.constructors, 239 this.constructors,
277 metadata) 240 metadata)
278 : _metadata = 241 : _metadata =
279 (metadata == null) ? null : new UnmodifiableListView(metadata); 242 (metadata == null) ? null : new UnmodifiableListView(metadata);
280 243
281 ClassMirror get superclass { 244 ClassMirror get superclass {
282 if (_superclassIndex == null) return null; 245 if (_superclassIndex == null) return null;
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
318 } 281 }
319 282
320 Map<String, MethodMirror> _instanceMembers; 283 Map<String, MethodMirror> _instanceMembers;
321 284
322 Map<String, MethodMirror> get instanceMembers { 285 Map<String, MethodMirror> get instanceMembers {
323 if (_instanceMembers == null) { 286 if (_instanceMembers == null) {
324 Map<String, MethodMirror> result = new Map<String, MethodMirror>(); 287 Map<String, MethodMirror> result = new Map<String, MethodMirror>();
325 for (int instanceMemberIndex in _instanceMemberIndices) { 288 for (int instanceMemberIndex in _instanceMemberIndices) {
326 DeclarationMirror declarationMirror = 289 DeclarationMirror declarationMirror =
327 _data.memberMirrors[instanceMemberIndex]; 290 _data.memberMirrors[instanceMemberIndex];
328 if (declarationMirror is MethodMirror) { 291 assert(declarationMirror is MethodMirror);
329 result[declarationMirror.simpleName] = declarationMirror; 292 result[declarationMirror.simpleName] = declarationMirror;
330 } else {
331 assert(declarationMirror is VariableMirror);
332 // Need declaration because `assert` provides no type propagation.
333 VariableMirror variableMirror = declarationMirror;
334 result[variableMirror.simpleName] =
335 _variableToGetterMirror(variableMirror);
336 if (!variableMirror.isFinal) {
337 result[variableMirror.simpleName + "="] =
338 _variableToSetterMirror(variableMirror);
339 }
340 }
341 } 293 }
342 _instanceMembers = new UnmodifiableMapView<String, MethodMirror>(result); 294 _instanceMembers = new UnmodifiableMapView<String, MethodMirror>(result);
343 } 295 }
344 return _instanceMembers; 296 return _instanceMembers;
345 } 297 }
346 298
347 Map<String, MethodMirror> get staticMembers => _unsupported(); 299 Map<String, MethodMirror> _staticMembers;
300
301 Map<String, MethodMirror> get staticMembers {
302 if (_staticMembers == null) {
303 Map<String, MethodMirror> result = new Map<String, MethodMirror>();
304 for (int staticMemberIndex in _staticMemberIndices) {
305 DeclarationMirror declarationMirror =
306 _data.memberMirrors[staticMemberIndex];
307 assert(declarationMirror is MethodMirror);
308 result[declarationMirror.simpleName] = declarationMirror;
309 }
310 _staticMembers = new UnmodifiableMapView<String, MethodMirror>(result);
311 }
312 return _staticMembers;
313 }
348 314
349 ClassMirror get mixin => _unsupported(); 315 ClassMirror get mixin => _unsupported();
350 316
351 Object newInstance(String constructorName, List positionalArguments, 317 Object newInstance(String constructorName, List positionalArguments,
352 [Map<Symbol, dynamic> namedArguments]) { 318 [Map<Symbol, dynamic> namedArguments]) {
353 return Function.apply( 319 return Function.apply(
354 constructors["$constructorName"], positionalArguments, namedArguments); 320 constructors["$constructorName"], positionalArguments, namedArguments);
355 } 321 }
356 322
357 bool isSubclassOf(ClassMirror other) { 323 bool isSubclassOf(ClassMirror other) {
(...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after
468 class MethodMirrorImpl extends _DataCaching implements MethodMirror { 434 class MethodMirrorImpl extends _DataCaching implements MethodMirror {
469 /// An encoding of the attributes and kind of this mirror. 435 /// An encoding of the attributes and kind of this mirror.
470 final int _descriptor; 436 final int _descriptor;
471 437
472 /// The name of this method. Setters names will end in '='. 438 /// The name of this method. Setters names will end in '='.
473 final String _name; 439 final String _name;
474 440
475 /// The index of the [ClassMirror] of the owner of this method, 441 /// The index of the [ClassMirror] of the owner of this method,
476 final int _ownerIndex; 442 final int _ownerIndex;
477 443
444 /// The index of the return type of this method.
445 final int _returnTypeIndex;
446
478 /// The indices of the [ParameterMirror]s describing the formal parameters 447 /// The indices of the [ParameterMirror]s describing the formal parameters
479 /// of this method. 448 /// of this method.
480 final List<int> _parameterIndices; 449 final List<int> _parameterIndices;
481 450
482 /// The [Reflectable] associated with this mirror. 451 /// The [Reflectable] associated with this mirror.
483 final ReflectableImpl _reflector; 452 final ReflectableImpl _reflector;
484 453
485 /// A cache of the metadata of the mirrored method. The empty list means 454 /// A cache of the metadata of the mirrored method. The empty list means
486 /// no metadata, null means that [_reflector] does not have 455 /// no metadata, null means that [_reflector] does not have
487 /// [metadataCapability]. 456 /// [metadataCapability].
488 final List<Object> _metadata; 457 final List<Object> _metadata;
489 458
490 MethodMirrorImpl(this._name, this._descriptor, this._ownerIndex, 459 MethodMirrorImpl(
491 this._parameterIndices, this._reflector, List<Object> metadata) 460 this._name,
461 this._descriptor,
462 this._ownerIndex,
463 this._returnTypeIndex,
464 this._parameterIndices,
465 this._reflector,
466 List<Object> metadata)
492 : _metadata = 467 : _metadata =
493 (metadata == null) ? null : new UnmodifiableListView(metadata); 468 (metadata == null) ? null : new UnmodifiableListView(metadata);
494 469
495 int get kind => constants.kindFromEncoding(_descriptor); 470 int get kind => constants.kindFromEncoding(_descriptor);
496 471
497 ClassMirror get owner => _data.classMirrors[_ownerIndex]; 472 ClassMirror get owner => _data.classMirrors[_ownerIndex];
498 473
499 @override 474 @override
500 String get constructorName => _name; 475 String get constructorName => isConstructor ? _name : "";
501 476
502 @override 477 @override
503 bool get isAbstract => (_descriptor & constants.abstractAttribute != 0); 478 bool get isAbstract => (_descriptor & constants.abstractAttribute != 0);
504 479
505 @override 480 @override
506 bool get isConstConstructor => (_descriptor & constants.constAttribute != 0); 481 bool get isConstConstructor => (_descriptor & constants.constAttribute != 0);
507 482
508 @override 483 @override
509 bool get isConstructor => isFactoryConstructor || isGenerativeConstructor; 484 bool get isConstructor => isFactoryConstructor || isGenerativeConstructor;
510 485
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
559 @override 534 @override
560 List<ParameterMirror> get parameters { 535 List<ParameterMirror> get parameters {
561 return _parameterIndices 536 return _parameterIndices
562 .map((int parameterIndex) => _data.parameterMirrors[parameterIndex]) 537 .map((int parameterIndex) => _data.parameterMirrors[parameterIndex])
563 .toList(); 538 .toList();
564 } 539 }
565 540
566 @override 541 @override
567 String get qualifiedName => "${owner.qualifiedName}.$_name"; 542 String get qualifiedName => "${owner.qualifiedName}.$_name";
568 543
569 // TODO(sigurdm) feature: suport `returnType`.
570 @override 544 @override
571 TypeMirror get returnType => throw new UnimplementedError(); 545 TypeMirror get returnType {
546 if (_returnTypeIndex == -1) {
547 _unsupported();
548 }
549 return _data.classMirrors[_returnTypeIndex];
550 }
572 551
573 @override 552 @override
574 String get simpleName => isConstructor 553 String get simpleName => isConstructor
575 ? (_name == '' ? "${owner.simpleName}" : "${owner.simpleName}.$_name") 554 ? (_name == '' ? "${owner.simpleName}" : "${owner.simpleName}.$_name")
576 : _name; 555 : _name;
577 556
578 @override 557 @override
579 String get source => null; 558 String get source => null;
580 559
581 @override 560 @override
582 String toString() => "MethodMirror($_name)"; 561 String toString() => "MethodMirrorImpl($qualifiedName)";
562 }
563
564 abstract class ImplicitAccessorMirrorImpl extends _DataCaching
565 implements MethodMirror {
566 final ReflectableImpl _reflector;
567 final int _variableMirrorIndex;
568
569 /// Index of this [ImplicitAccessorMirrorImpl] in `_data.memberMirrors`.
570 final int _selfIndex;
571
572 VariableMirrorImpl get _variableMirror =>
573 _data.memberMirrors[_variableMirrorIndex];
574
575 ImplicitAccessorMirrorImpl(
576 this._reflector, this._variableMirrorIndex, this._selfIndex);
577
578 int get kind => constants.kindFromEncoding(_variableMirror._descriptor);
579
580 ClassMirror get owner => _variableMirror.owner;
581
582 @override
583 String get constructorName => "";
584
585 @override
586 bool get isAbstract => false;
587
588 @override
589 bool get isConstConstructor => false;
590
591 @override
592 bool get isConstructor => false;
593
594 @override
595 bool get isFactoryConstructor => false;
596
597 @override
598 bool get isGenerativeConstructor => false;
599
600 @override
601 bool get isOperator => false;
602
603 @override
604 bool get isPrivate => _variableMirror.isPrivate;
605
606 @override
607 bool get isRedirectingConstructor => false;
608
609 @override
610 bool get isRegularMethod => false;
611
612 @override
613 bool get isStatic => _variableMirror.isStatic;
614
615 @override
616 bool get isSynthetic => true;
617
618 @override
619 bool get isTopLevel => false;
620
621 // It is allowed to return null.
622 @override
623 SourceLocation get location => null;
624
625 @override
626 List<Object> get metadata => <Object>[];
627
628 @override
629 TypeMirror get returnType => _variableMirror.type;
630
631 @override
632 String get source => null;
633 }
634
635 class ImplicitGetterMirrorImpl extends ImplicitAccessorMirrorImpl {
636 ImplicitGetterMirrorImpl(
637 ReflectableImpl reflector, int variableMirrorIndex, int selfIndex)
638 : super(reflector, variableMirrorIndex, selfIndex);
639
640 @override
641 bool get isGetter => true;
642
643 @override
644 bool get isSetter => false;
645
646 @override
647 List<ParameterMirror> get parameters => <ParameterMirror>[];
648
649 @override
650 String get qualifiedName => _variableMirror.qualifiedName;
651
652 @override
653 String get simpleName => _variableMirror.simpleName;
654
655 @override
656 String toString() => "ImplicitGetterMirrorImpl($qualifiedName)";
657 }
658
659 class ImplicitSetterMirrorImpl extends ImplicitAccessorMirrorImpl {
660 ImplicitSetterMirrorImpl(
661 ReflectableImpl reflector, int variableMirrorIndex, int selfIndex)
662 : super(reflector, variableMirrorIndex, selfIndex);
663
664 @override
665 bool get isGetter => false;
666
667 @override
668 bool get isSetter => true;
669
670 int get _parameterDescriptor {
671 int descriptor = constants.parameter;
672 if (_variableMirror.isStatic) descriptor |= constants.staticAttribute;
673 if (_variableMirror.isPrivate) descriptor |= constants.privateAttribute;
674 descriptor |= constants.syntheticAttribute;
675 if (_variableMirror._isDynamic) descriptor |= constants.dynamicAttribute;
676 if (_variableMirror._isClassType) descriptor |=
677 constants.classTypeAttribute;
678 return descriptor;
679 }
680
681 @override
682 List<ParameterMirror> get parameters {
683 return <ParameterMirror>[
684 new ParameterMirrorImpl(
685 _variableMirror.simpleName,
686 _parameterDescriptor,
687 _selfIndex,
688 _variableMirror._reflector,
689 _variableMirror._classMirrorIndex,
690 <Object>[],
691 null)
692 ];
693 }
694
695 @override
696 String get qualifiedName => "${_variableMirror.qualifiedName}=";
697
698 @override
699 String get simpleName => "${_variableMirror.simpleName}=";
700
701 @override
702 String toString() => "ImplicitSetterMirrorImpl($qualifiedName)";
583 } 703 }
584 704
585 abstract class VariableMirrorBase extends _DataCaching 705 abstract class VariableMirrorBase extends _DataCaching
586 implements VariableMirror { 706 implements VariableMirror {
587 final String _name; 707 final String _name;
588 final int _descriptor; 708 final int _descriptor;
589 final int _ownerIndex; 709 final int _ownerIndex;
590 final ReflectableImpl _reflector; 710 final ReflectableImpl _reflector;
591 final _classMirrorIndex; 711 final _classMirrorIndex;
592 final List<Object> _metadata; 712 final List<Object> _metadata;
(...skipping 219 matching lines...) Expand 10 before | Expand all | Expand 10 after
812 LibraryMirror findLibrary(String library) => _unsupported(); 932 LibraryMirror findLibrary(String library) => _unsupported();
813 933
814 @override 934 @override
815 Map<Uri, LibraryMirror> get libraries => _unsupported(); 935 Map<Uri, LibraryMirror> get libraries => _unsupported();
816 936
817 @override 937 @override
818 Iterable<ClassMirror> get annotatedClasses { 938 Iterable<ClassMirror> get annotatedClasses {
819 return new UnmodifiableListView<ClassMirror>(data[this].classMirrors); 939 return new UnmodifiableListView<ClassMirror>(data[this].classMirrors);
820 } 940 }
821 } 941 }
OLDNEW
« no previous file with comments | « no previous file | reflectable/lib/src/transformer_implementation.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698