OLD | NEW |
(Empty) | |
| 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 |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 |
| 5 /** |
| 6 * Provides an implementation of 'noSuchMethod' that may be used as a |
| 7 * substitute for fixed getter and setter methods. |
| 8 */ |
| 9 class DynamicBuilder extends Builder { |
| 10 |
| 11 /** |
| 12 * Implement getXxx, setXxx, and hasXxx methods dynamically. |
| 13 */ |
| 14 Dynamic noSuchMethod(String name, List args) { |
| 15 if (name.startsWith("get:")) { |
| 16 int tagNumber = info_.tagNumber(name.substring(4)); |
| 17 if (tagNumber !== null) { |
| 18 return getField(tagNumber); |
| 19 } |
| 20 } else if (name.startsWith("set:")) { |
| 21 int tagNumber = info_.tagNumber(name.substring(4)); |
| 22 if (tagNumber !== null) { |
| 23 setField(tagNumber, args[0]); |
| 24 return; |
| 25 } |
| 26 } else if (name.startsWith("has") && name.length > 3) { |
| 27 String fieldName = "${name[3].toLowerCase()}${name.substring(4)}"; |
| 28 int tagNumber = info_.tagNumber(fieldName); |
| 29 if (tagNumber !== null) { |
| 30 return hasField(tagNumber); |
| 31 } |
| 32 } |
| 33 // Throw an exception like what would have occurred if not intercepted here |
| 34 throw new NoSuchMethodException(this, name, args); |
| 35 } |
| 36 } |
OLD | NEW |