OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2013, 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 /// Example illustrating how to use checked_mirrors, and how it works when you |
| 6 /// declare a meta-target with [MirrorsUsed]. |
| 7 library checked_mirrors.example.meta_target; |
| 8 |
| 9 import 'package:checked_mirrors/checked_mirrors.dart'; |
| 10 import 'package:checked_mirrors/control.dart' as control; |
| 11 import 'package:logging/logging.dart'; |
| 12 |
| 13 // Typically this annotation goes in an import to 'dart:mirrors', we would like |
| 14 // to put it in the import of checked_mirrors, but we moved it here until |
| 15 // dartbug.com/10360 is fixed. |
| 16 @MirrorsUsed(metaTargets: const[Reflected]) |
| 17 const checked_mirrors_workaround_for_issue_10360 = 0; |
| 18 |
| 19 class Reflected { const Reflected(); } |
| 20 const reflected = const Reflected(); |
| 21 |
| 22 class A { |
| 23 int x = 1; // not annotated - reading this should give warnings. |
| 24 } |
| 25 |
| 26 @Reflected() // all symbols in this class are covered |
| 27 class B { |
| 28 int y = 4; |
| 29 int z = 5; |
| 30 } |
| 31 |
| 32 class C { |
| 33 @reflected int y = 6; |
| 34 int z = 7; // not covered |
| 35 } |
| 36 |
| 37 var a = new A(); |
| 38 var b = new B(); |
| 39 var c = new C(); |
| 40 |
| 41 main() { |
| 42 // This loads up the rules declared with @MirrorsUsed. |
| 43 control.initialize(log: true); |
| 44 |
| 45 // Print the warnings to the console. |
| 46 Logger.root.onRecord.listen((r) => print(r)); |
| 47 |
| 48 var x = reflect(a).getField(#x).reflectee; |
| 49 reflect(a).setField(#x, x + 1); |
| 50 |
| 51 var by = reflect(b).getField(#y).reflectee; |
| 52 reflect(b).setField(#y, by + 1); |
| 53 var bz = reflect(b).getField(#z).reflectee; |
| 54 reflect(b).setField(#y, bz + 1); |
| 55 |
| 56 var cy = reflect(c).getField(#y).reflectee; |
| 57 reflect(c).setField(#y, by + 1); |
| 58 var cz = reflect(c).getField(#z).reflectee; |
| 59 reflect(c).setField(#y, bz + 1); |
| 60 } |
OLD | NEW |