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 /// This part of checked_mirrors contains definitions for controlling the |
| 6 /// checked_mirrors library. In particular, to initialize it and to listen |
| 7 /// for warnings. |
| 8 library checked_mirrors.control; |
| 9 |
| 10 import 'dart:mirrors'; |
| 11 |
| 12 import 'package:logging/logging.dart'; |
| 13 |
| 14 import 'src/checker.dart'; |
| 15 import 'src/utils.dart'; |
| 16 |
| 17 export 'src/checker.dart' show onWarning, MirrorsUsedWarning, |
| 18 MirrorsUsedWarningKind; |
| 19 |
| 20 /// Initializes checked_mirrors. |
| 21 /// |
| 22 /// **Note**: this function must be invoked before any uses of the mirror system |
| 23 /// in the program. If possible, it should be the first thing in `main`. |
| 24 /// |
| 25 /// This loads up any declarations of MirrorsUsed and sets up the necessary |
| 26 /// checks on each mirror access. |
| 27 /// |
| 28 /// By default, warnings are available through the [onWarning] stream. |
| 29 /// Alternatively, setting [log] to true will automatically log a formatted |
| 30 /// warning message in a logger named `checked_mirrors`. The first logged |
| 31 /// message will include a longer explanation of where these warnings come from, |
| 32 /// and, if provided, any additional [hints] for how to fix things up. By |
| 33 /// default there are no hints, but frameworks that have special annotations |
| 34 /// that indirectly declare a [MirrorUsed] constaint might want to include some |
| 35 /// extra hints here. |
| 36 /// |
| 37 /// If [throwOnwarning] is true, the system will throw as soon a soon as an |
| 38 /// it detects a warning. |
| 39 void initialize({bool throwOnWarning, bool log, String hints}) { |
| 40 checker.init(throwOnWarning: throwOnWarning); |
| 41 if (log) { |
| 42 var logger = new Logger('checked_mirrors'); |
| 43 bool first = true; |
| 44 hints = hints == null ? '' : hints; |
| 45 onWarning.listen((warning) { |
| 46 if (first) { |
| 47 first = false; |
| 48 logger.warning('$FIRST_ERROR_MESSAGE$hints'); |
| 49 } |
| 50 var message; |
| 51 if (warning.kind == MirrorsUsedWarningKind.UNDECLARED_SYMBOL) { |
| 52 var name = MirrorSystem.getName(warning.symbol); |
| 53 message = 'Symbol "$name" was used, but it is missing a ' |
| 54 '@MirrorsUsed annotation.'; |
| 55 } else { |
| 56 var declaration = getDeclarationOf(warning.object); |
| 57 var exp = declaration != null ? |
| 58 MirrorSystem.getName(declaration.simpleName) : warning.object; |
| 59 var name = MirrorSystem.getName(warning.symbol); |
| 60 message = 'Tried to access "$exp.$name" via mirrors, ' |
| 61 'but it is missing a @MirrorsUsed annotation.'; |
| 62 } |
| 63 logger.warning(message); |
| 64 }); |
| 65 } |
| 66 } |
| 67 |
| 68 /// Default error message shown when the first error is found. This constant is |
| 69 /// visible mainly for testing purposes. |
| 70 const String FIRST_ERROR_MESSAGE = |
| 71 'Parts of your program uses mirrors to access objects. Some ' |
| 72 'of these mirror accesses were not declared with @MirrorsUsed annotations. ' |
| 73 'This could lead to unexpected behavior when deploying your app ' |
| 74 'with dart2js. Below, you will find additional warnings where we ' |
| 75 'detected that the @MirrorsUsed annotation is missing.'; |
OLD | NEW |