OLD | NEW |
1 import 'dart:mirrors'; | 1 import 'dart:mirrors'; |
2 | 2 |
3 askUserForNameOfFunction() => 'foo'; | 3 askUserForNameOfFunction() => 'foo'; |
4 | 4 |
5 class Person { | 5 class Person { |
6 String firstName; | 6 String firstName; |
7 String lastName; | 7 String lastName; |
8 int age; | 8 int age; |
9 | 9 |
10 Person(this.firstName, this.lastName, this.age); | 10 Person(this.firstName, this.lastName, this.age); |
11 Person.noFirstName(this.lastName, this.age); | |
12 | 11 |
13 String get fullName => '$firstName $lastName'; | 12 String get fullName => '$firstName $lastName'; |
14 | 13 |
15 void greet(String other) { | 14 void greet(String other) { |
16 print('Hello there, $other!'); | 15 print('Hello there, $other!'); |
17 } | 16 } |
18 } | 17 } |
19 | 18 |
20 main() { | 19 main() { |
21 // If the symbol name is known at compile time. | 20 // If the symbol name is known at compile time. |
(...skipping 12 matching lines...) Expand all Loading... |
34 | 33 |
35 reflectFromInstance(); | 34 reflectFromInstance(); |
36 showConstructors(mirror); | 35 showConstructors(mirror); |
37 showFields(mirror); | 36 showFields(mirror); |
38 reflectOnInstance(); | 37 reflectOnInstance(); |
39 } | 38 } |
40 | 39 |
41 reflectFromInstance() { | 40 reflectFromInstance() { |
42 var person = new Person('Bob', 'Smith', 33); | 41 var person = new Person('Bob', 'Smith', 33); |
43 ClassMirror mirror = reflectClass(person.runtimeType); | 42 ClassMirror mirror = reflectClass(person.runtimeType); |
| 43 assert('Person' == MirrorSystem.getName(mirror.simpleName)); |
44 } | 44 } |
45 | 45 |
46 showConstructors(ClassMirror mirror) { | 46 showConstructors(ClassMirror mirror) { |
47 var methods = mirror.declarations.values.where((m) => m is MethodMirror); | 47 var methods = mirror.declarations.values.where((m) => m is MethodMirror); |
48 var constructors = methods.where((m) => m.isConstructor); | 48 var constructors = methods.where((m) => m.isConstructor); |
49 | 49 |
50 constructors.forEach((m) { | 50 constructors.forEach((m) { |
51 print('The constructor ${m.simpleName} has ${m.parameters.length} parameters
.'); | 51 print('The constructor ${m.simpleName} has ${m.parameters.length} parameters
.'); |
52 }); | 52 }); |
53 } | 53 } |
54 | 54 |
55 showFields(ClassMirror mirror) { | 55 showFields(ClassMirror mirror) { |
56 var fields = mirror.declarations.values.where((m) => m is VariableMirror); | 56 var fields = mirror.declarations.values.where((m) => m is VariableMirror); |
57 | 57 |
58 fields.forEach((VariableMirror m) { | 58 fields.forEach((VariableMirror m) { |
59 var finalStatus = m.isFinal ? 'final' : 'not final'; | 59 var finalStatus = m.isFinal ? 'final' : 'not final'; |
60 var privateStatus = m.isPrivate ? 'private' : 'not private'; | 60 var privateStatus = m.isPrivate ? 'private' : 'not private'; |
61 var typeAnnotation = m.type.simpleName; | 61 var typeAnnotation = m.type.simpleName; |
62 | 62 |
63 print('The field ${m.simpleName} is $privateStatus and $finalStatus and is a
nnotated ' | 63 print('The field ${m.simpleName} is $privateStatus and $finalStatus and is a
nnotated ' |
64 'as $typeAnnotation'); | 64 'as $typeAnnotation'); |
65 }); | 65 }); |
66 } | 66 } |
67 | 67 |
68 reflectOnInstance() { | 68 reflectOnInstance() { |
69 var p = new Person('Bob', 'Smith', 42); | 69 var p = new Person('Bob', 'Smith', 42); |
70 InstanceMirror mirror = reflect(p); | 70 InstanceMirror mirror = reflect(p); |
71 | 71 |
| 72 // Get the object that the mirror reflects. |
72 var person = mirror.reflectee; | 73 var person = mirror.reflectee; |
73 assert(identical(p, person)); | 74 assert(identical(p, person)); |
| 75 |
| 76 // Invoke a method on the object. |
| 77 mirror.invoke(#greet, ['Shailen']); |
| 78 |
| 79 // Get the value of a property. |
| 80 var fullName = mirror.getField(#fullName).reflectee; |
| 81 assert(fullName == 'Bob Smith'); |
| 82 |
| 83 // Set the value of a property. |
| 84 mirror.setField(#firstName, 'Mary'); |
| 85 assert(p.firstName == 'Mary'); |
| 86 assert(p.fullName == 'Mary Smith'); |
74 } | 87 } |
OLD | NEW |