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 part of serialization; | 5 part of serialization; |
6 | 6 |
7 // TODO(alanknight): Figure out how to reasonably separate out the things | 7 // TODO(alanknight): Figure out how to reasonably separate out the things |
8 // that require reflection without making the API more awkward. Or if that is | 8 // that require reflection without making the API more awkward. Or if that is |
9 // in fact necessary. Maybe the tree-shaking will just remove it if unused. | 9 // in fact necessary. Maybe the tree-shaking will just remove it if unused. |
10 | 10 |
(...skipping 515 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
526 void forEachConstructorField(Function f) { | 526 void forEachConstructorField(Function f) { |
527 for (var each in contents) { | 527 for (var each in contents) { |
528 if (each.usedInConstructor) { | 528 if (each.usedInConstructor) { |
529 f(each); | 529 f(each); |
530 } | 530 } |
531 } | 531 } |
532 } | 532 } |
533 | 533 |
534 List get constructorFields => _constructorFields; | 534 List get constructorFields => _constructorFields; |
535 List constructorFieldNames() => | 535 List constructorFieldNames() => |
536 constructorFields.mappedBy((x) => x.name).toList(); | 536 constructorFields.map((x) => x.name).toList(); |
537 List constructorFieldIndices() => | 537 List constructorFieldIndices() => |
538 constructorFields.mappedBy((x) => x.index).toList(); | 538 constructorFields.map((x) => x.index).toList(); |
539 List regularFields() => contents.where((x) => !x.usedInConstructor).toList(); | 539 List regularFields() => contents.where((x) => !x.usedInConstructor).toList(); |
540 List regularFieldNames() => regularFields().mappedBy((x) => x.name).toList(); | 540 List regularFieldNames() => regularFields().map((x) => x.name).toList(); |
541 List regularFieldIndices() => | 541 List regularFieldIndices() => |
542 regularFields().mappedBy((x) => x.index).toList(); | 542 regularFields().map((x) => x.index).toList(); |
543 | 543 |
544 /** | 544 /** |
545 * If we weren't given any non-constructor fields to use, figure out what | 545 * If we weren't given any non-constructor fields to use, figure out what |
546 * we think they ought to be, based on the class definition. | 546 * we think they ought to be, based on the class definition. |
547 * We find public fields, getters that have corresponding setters, and getters | 547 * We find public fields, getters that have corresponding setters, and getters |
548 * that are listed in the constructor fields. | 548 * that are listed in the constructor fields. |
549 */ | 549 */ |
550 void figureOutFields() { | 550 void figureOutFields() { |
551 Iterable names(Iterable<DeclarationMirror> mirrors) => | 551 Iterable names(Iterable<DeclarationMirror> mirrors) => |
552 mirrors.mappedBy((each) => each.simpleName).toList(); | 552 mirrors.map((each) => each.simpleName).toList(); |
553 | 553 |
554 if (!_shouldFigureOutFields || !regularFields().isEmpty) return; | 554 if (!_shouldFigureOutFields || !regularFields().isEmpty) return; |
555 var fields = publicFields(mirror); | 555 var fields = publicFields(mirror); |
556 var getters = publicGetters(mirror); | 556 var getters = publicGetters(mirror); |
557 var gettersWithSetters = getters.where( (each) | 557 var gettersWithSetters = getters.where( (each) |
558 => mirror.setters["${each.simpleName}="] != null); | 558 => mirror.setters["${each.simpleName}="] != null); |
559 var gettersThatMatchConstructor = getters.where((each) | 559 var gettersThatMatchConstructor = getters.where((each) |
560 => (named(each.simpleName) != null) && | 560 => (named(each.simpleName) != null) && |
561 (named(each.simpleName).usedInConstructor)).toList(); | 561 (named(each.simpleName).usedInConstructor)).toList(); |
562 addAllNotExplicitlyExcluded(names(fields)); | 562 addAllNotExplicitlyExcluded(names(fields)); |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
599 if (name == null) name = ''; | 599 if (name == null) name = ''; |
600 if (fieldNumbers == null) fieldNumbers = const []; | 600 if (fieldNumbers == null) fieldNumbers = const []; |
601 } | 601 } |
602 | 602 |
603 /** | 603 /** |
604 * Find the field values in [state] and pass them to the constructor. | 604 * Find the field values in [state] and pass them to the constructor. |
605 * If any of [fieldNumbers] is not an int, then use it as a literal value. | 605 * If any of [fieldNumbers] is not an int, then use it as a literal value. |
606 */ | 606 */ |
607 constructFrom(state, Reader r) { | 607 constructFrom(state, Reader r) { |
608 // TODO(alanknight): Handle named parameters | 608 // TODO(alanknight): Handle named parameters |
609 Collection inflated = fieldNumbers.mappedBy( | 609 Iterable inflated = fieldNumbers.map( |
610 (x) => (x is int) ? reflect(r.inflateReference(state[x])) : reflect(x)); | 610 (x) => (x is int) ? reflect(r.inflateReference(state[x])) : reflect(x)); |
611 var result = type.newInstance(name, inflated.toList()); | 611 var result = type.newInstance(name, inflated.toList()); |
612 return deprecatedFutureValue(result); | 612 return deprecatedFutureValue(result); |
613 } | 613 } |
614 } | 614 } |
615 | 615 |
616 /** | 616 /** |
617 * This wraps a map to make it indexable by integer field numbers. It translates | 617 * This wraps a map to make it indexable by integer field numbers. It translates |
618 * from the index into a field name and then looks it up in the map. | 618 * from the index into a field name and then looks it up in the map. |
619 */ | 619 */ |
620 class _MapWrapper { | 620 class _MapWrapper { |
621 final _map; | 621 final _map; |
622 List fieldList; | 622 List fieldList; |
623 _MapWrapper(this.fieldList) : _map = new Map(); | 623 _MapWrapper(this.fieldList) : _map = new Map(); |
624 _MapWrapper.fromMap(this._map, this.fieldList); | 624 _MapWrapper.fromMap(this._map, this.fieldList); |
625 | 625 |
626 operator [](key) => _map[fieldList[key].name]; | 626 operator [](key) => _map[fieldList[key].name]; |
627 | 627 |
628 operator []=(key, value) { _map[fieldList[key].name] = value; } | 628 operator []=(key, value) { _map[fieldList[key].name] = value; } |
629 get length => _map.length; | 629 get length => _map.length; |
630 | 630 |
631 asMap() => _map; | 631 asMap() => _map; |
632 } | 632 } |
OLD | NEW |