Chromium Code Reviews| 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 424 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 435 * All of our fields, indexed by name. Note that the names are | 435 * All of our fields, indexed by name. Note that the names are |
| 436 * typically Symbols, but can also be arbitrary constants. | 436 * typically Symbols, but can also be arbitrary constants. |
| 437 */ | 437 */ |
| 438 Map<dynamic, _Field> allFields = new Map<dynamic, _Field>(); | 438 Map<dynamic, _Field> allFields = new Map<dynamic, _Field>(); |
| 439 | 439 |
| 440 /** | 440 /** |
| 441 * The fields which are used in the constructor. The fields themselves also | 441 * The fields which are used in the constructor. The fields themselves also |
| 442 * know if they are constructor fields or not, but we need to keep this | 442 * know if they are constructor fields or not, but we need to keep this |
| 443 * information here because the order matters. | 443 * information here because the order matters. |
| 444 */ | 444 */ |
| 445 List _constructorFields = const []; | 445 final List<_Field> _constructorFields = <_Field>[]; |
| 446 | 446 |
| 447 /** The list of fields to exclude if we are computing the list ourselves. */ | 447 /** The list of fields to exclude if we are computing the list ourselves. */ |
| 448 List<Symbol> _excludedFieldNames = const []; | 448 final List<Symbol> _excludedFieldNames = <Symbol>[]; |
| 449 | 449 |
| 450 /** The mirror we will use to compute the fields. */ | 450 /** The mirror we will use to compute the fields. */ |
| 451 final ClassMirror mirror; | 451 final ClassMirror mirror; |
| 452 | 452 |
| 453 /** Cached, sorted list of fields. */ | 453 /** Cached, sorted list of fields. */ |
| 454 List<_Field> _contents; | 454 List<_Field> _contents; |
| 455 | 455 |
| 456 /** Should we compute the fields or just use whatever we were given. */ | 456 /** Should we compute the fields or just use whatever we were given. */ |
| 457 bool _shouldFigureOutFields = true; | 457 bool _shouldFigureOutFields = true; |
| 458 | 458 |
| 459 _FieldList(this.mirror); | 459 _FieldList(this.mirror); |
| 460 | 460 |
| 461 /** Look up a field by [name]. */ | 461 /** Look up a field by [name]. */ |
| 462 _Field named(name) => allFields[name]; | 462 _Field named(name) => allFields[name]; |
| 463 | 463 |
| 464 /** Set the fields to be used in the constructor. */ | 464 /** Set the fields to be used in the constructor. */ |
| 465 set constructorFields(List fieldNames) { | 465 set constructorFields(List fieldNames) { |
| 466 if (fieldNames == null || fieldNames.isEmpty) return; | 466 if (fieldNames == null || fieldNames.isEmpty) return; |
| 467 _constructorFields = []; | 467 _constructorFields.clear(); |
| 468 for (var each in fieldNames) { | 468 for (var each in fieldNames) { |
| 469 var symbol = _asSymbol(each); | 469 var symbol = _asSymbol(each); |
| 470 var name = _Field._isReallyAField(symbol, this) ? symbol : each; | 470 var name = _Field._isReallyAField(symbol, this) ? symbol : each; |
| 471 var field = new _Field(name, this)..usedInConstructor = true; | 471 var field = new _Field(name, this)..usedInConstructor = true; |
| 472 allFields[name] = field; | 472 allFields[name] = field; |
| 473 _constructorFields.add(field); | 473 _constructorFields.add(field); |
| 474 } | 474 } |
| 475 invalidate(); | 475 invalidate(); |
| 476 } | 476 } |
| 477 | 477 |
| 478 /** Set the fields that aren't used in the constructor. */ | 478 /** Set the fields that aren't used in the constructor. */ |
| 479 set regular(List<String> fields) { | 479 void set regular(List<String> fields) { |
| 480 if (fields == null) return; | 480 if (fields == null) { |
|
Alan Knight
2013/06/26 19:26:29
As above for a guard clause.
| |
| 481 return; | |
| 482 } | |
| 481 _shouldFigureOutFields = false; | 483 _shouldFigureOutFields = false; |
| 482 addAllByName(fields); | 484 addAllByName(fields); |
| 483 } | 485 } |
| 484 | 486 |
| 485 /** Set the fields to be excluded. This is mutually exclusive with setting | 487 /** Set the fields to be excluded. This is mutually exclusive with setting |
| 486 * the regular fields. | 488 * the regular fields. |
| 487 */ | 489 */ |
| 488 set exclude(List<String> fields) { | 490 void set exclude(List<String> fields) { |
| 489 // TODO(alanknight): This isn't well tested. | 491 // TODO(alanknight): This isn't well tested. |
| 490 if (fields == null || fields.isEmpty) return; | 492 if (fields == null || fields.isEmpty) { |
| 493 return; | |
| 494 } | |
| 491 if (allFields.length > _constructorFields.length) { | 495 if (allFields.length > _constructorFields.length) { |
| 492 throw "You can't specify both excludeFields and regular fields"; | 496 throw "You can't specify both excludeFields and regular fields"; |
| 493 } | 497 } |
| 494 _excludedFieldNames = fields.map((x) => new Symbol(x)).toList(); | 498 _excludedFieldNames.clear(); |
| 499 _excludedFieldNames.addAll(fields.map((x) => new Symbol(x))); | |
| 495 } | 500 } |
| 496 | 501 |
| 497 int get length => allFields.length; | 502 int get length => allFields.length; |
| 498 | 503 |
| 499 /** Add all the fields which aren't on the exclude list. */ | 504 /** Add all the fields which aren't on the exclude list. */ |
| 500 void addAllNotExplicitlyExcluded(Iterable<String> aCollection) { | 505 void addAllNotExplicitlyExcluded(Iterable<String> aCollection) { |
| 501 if (aCollection == null) return; | 506 if (aCollection == null) { |
| 502 var names = aCollection; | 507 return; |
| 503 names = names.where((x) => !_excludedFieldNames.contains(x)); | 508 } |
| 509 var names = aCollection.where((x) => !_excludedFieldNames.contains(x)); | |
| 504 addAllByName(names); | 510 addAllByName(names); |
| 505 } | 511 } |
| 506 | 512 |
| 507 /** Add all the fields with the given names without any special properties. */ | 513 /** Add all the fields with the given names without any special properties. */ |
| 508 void addAllByName(Iterable<String> names) { | 514 void addAllByName(Iterable<String> names) { |
| 509 for (var each in names) { | 515 for (var each in names) { |
| 510 var symbol = _asSymbol(each); | 516 var symbol = _asSymbol(each); |
| 511 var field = new _Field(symbol, this); | 517 var field = new _Field(symbol, this); |
| 512 allFields.putIfAbsent(symbol, () => new _Field(symbol, this)); | 518 allFields.putIfAbsent(symbol, () => new _Field(symbol, this)); |
| 513 } | 519 } |
| (...skipping 159 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 673 if (value is String) { | 679 if (value is String) { |
| 674 try { | 680 try { |
| 675 return new Symbol(value); | 681 return new Symbol(value); |
| 676 } on ArgumentError { | 682 } on ArgumentError { |
| 677 return null; | 683 return null; |
| 678 }; | 684 }; |
| 679 } else { | 685 } else { |
| 680 return null; | 686 return null; |
| 681 } | 687 } |
| 682 } | 688 } |
| OLD | NEW |