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 target with [MirrorsUsed]. |
| 7 library checked_mirrors.example.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(targets: const [A]) |
| 17 const checked_mirrors_workaround_for_issue_10360 = 0; |
| 18 |
| 19 class A { // this class is declared in @MirrorsUsed, so using 'x' is OK. |
| 20 int x = 1; |
| 21 } |
| 22 |
| 23 class B { // this class is not declared, so reading 'x' will issue warnings. |
| 24 int x = 1; |
| 25 } |
| 26 |
| 27 var a = new A(); |
| 28 var b = new B(); |
| 29 |
| 30 main() { |
| 31 // This loads up the rules declared with @MirrorsUsed. |
| 32 control.initialize(log: true); |
| 33 |
| 34 // Print the warnings to the console. |
| 35 Logger.root.onRecord.listen((r) => print(r)); |
| 36 |
| 37 var ax = reflect(a).getField(#x).reflectee; |
| 38 var bx = reflect(b).getField(#x).reflectee; |
| 39 |
| 40 reflect(a).setField(#x, ax + 1); |
| 41 reflect(b).setField(#x, bx + 2); |
| 42 } |
OLD | NEW |