OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2014, 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 // Test to make sure that all members of reflectable classes are reflectable, |
| 6 // including ones inherited from super classes and the overriding members |
| 7 // of subclasses. |
| 8 |
| 9 @MirrorsUsed(metaTargets: "Meta") |
| 10 import 'dart:mirrors'; |
| 11 import 'package:expect/expect.dart'; |
| 12 |
| 13 class Meta { |
| 14 const Meta(); |
| 15 } |
| 16 |
| 17 class Super { |
| 18 var inheritedField = 1; |
| 19 var overriddenField = 1; |
| 20 |
| 21 inheritedMethod(x) => x; |
| 22 overriddenMethod(x) => x; |
| 23 } |
| 24 |
| 25 @Meta() |
| 26 class Reflected extends Super { |
| 27 var overriddenField = 2; |
| 28 var subclassedField = 2; |
| 29 |
| 30 overriddenMethod(x) => 2 * x; |
| 31 subclassedMethod(x) => 2 * x; |
| 32 } |
| 33 |
| 34 class Subclass extends Reflected { |
| 35 var subclassedField = 4; |
| 36 var subclassField = 4; |
| 37 |
| 38 subclassedMethod(x) => 4 * x; |
| 39 subclassMethod(x) => 4 * x; |
| 40 } |
| 41 |
| 42 tryCall(object, symbol, value, expected) { |
| 43 var mirror = reflect(object); |
| 44 var result = mirror.invoke(symbol, [value]).reflectee; |
| 45 Expect.equals(result, expected); |
| 46 } |
| 47 |
| 48 tryField(object, symbol, expected) { |
| 49 var mirror = reflect(object); |
| 50 var result = mirror.getField(symbol).reflectee; |
| 51 Expect.equals(result, expected); |
| 52 } |
| 53 |
| 54 main () { |
| 55 var objects = [new Reflected(), new Subclass()]; |
| 56 |
| 57 // Make sure the subclass methods are alive. |
| 58 objects[1].subclassField = 9; |
| 59 print(objects[1].subclassMethod(9)); |
| 60 |
| 61 var index = 1; |
| 62 if (new DateTime.now().year == 1984) { |
| 63 index = 0; |
| 64 } |
| 65 |
| 66 // Reflect an instance of [Subclass], which should only expose the interface |
| 67 // of [Reflected]. |
| 68 var subclass = objects[index]; |
| 69 tryCall(subclass, #inheritedMethod, 11, 11); |
| 70 tryCall(subclass, #overriddenMethod, 11, 22); |
| 71 tryCall(subclass, #subclassedMethod, 11, 44); |
| 72 tryField(subclass, #inheritedField, 1); |
| 73 tryField(subclass, #overriddenField, 2); |
| 74 tryField(subclass, #subclassedField, 4); |
| 75 Expect.throws(() => |
| 76 reflect(subclass).invoke(#subclassMethod, [11])); |
| 77 Expect.throws(() => reflect(subclass).getField(#subclassField)); |
| 78 } |
OLD | NEW |