Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(665)

Unified Diff: pkg/checked_mirrors/lib/control.dart

Issue 111643015: Preliminary checked mirrors (not ready for review yet) (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 7 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « pkg/checked_mirrors/lib/checked_mirrors.dart ('k') | pkg/checked_mirrors/lib/src/checker.dart » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: pkg/checked_mirrors/lib/control.dart
diff --git a/pkg/checked_mirrors/lib/control.dart b/pkg/checked_mirrors/lib/control.dart
new file mode 100644
index 0000000000000000000000000000000000000000..730d2010cd905641c1efa1d80010125d69af9d85
--- /dev/null
+++ b/pkg/checked_mirrors/lib/control.dart
@@ -0,0 +1,75 @@
+// Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file
+// for details. All rights reserved. Use of this source code is governed by a
+// BSD-style license that can be found in the LICENSE file.
+
+/// This part of checked_mirrors contains definitions for controlling the
+/// checked_mirrors library. In particular, to initialize it and to listen
+/// for warnings.
+library checked_mirrors.control;
+
+import 'dart:mirrors';
+
+import 'package:logging/logging.dart';
+
+import 'src/checker.dart';
+import 'src/utils.dart';
+
+export 'src/checker.dart' show onWarning, MirrorsUsedWarning,
+ MirrorsUsedWarningKind;
+
+/// Initializes checked_mirrors.
+///
+/// **Note**: this function must be invoked before any uses of the mirror system
+/// in the program. If possible, it should be the first thing in `main`.
+///
+/// This loads up any declarations of MirrorsUsed and sets up the necessary
+/// checks on each mirror access.
+///
+/// By default, warnings are available through the [onWarning] stream.
+/// Alternatively, setting [log] to true will automatically log a formatted
+/// warning message in a logger named `checked_mirrors`. The first logged
+/// message will include a longer explanation of where these warnings come from,
+/// and, if provided, any additional [hints] for how to fix things up. By
+/// default there are no hints, but frameworks that have special annotations
+/// that indirectly declare a [MirrorUsed] constaint might want to include some
+/// extra hints here.
+///
+/// If [throwOnwarning] is true, the system will throw as soon a soon as an
+/// it detects a warning.
+void initialize({bool throwOnWarning, bool log, String hints}) {
+ checker.init(throwOnWarning: throwOnWarning);
+ if (log) {
+ var logger = new Logger('checked_mirrors');
+ bool first = true;
+ hints = hints == null ? '' : hints;
+ onWarning.listen((warning) {
+ if (first) {
+ first = false;
+ logger.warning('$FIRST_ERROR_MESSAGE$hints');
+ }
+ var message;
+ if (warning.kind == MirrorsUsedWarningKind.UNDECLARED_SYMBOL) {
+ var name = MirrorSystem.getName(warning.symbol);
+ message = 'Symbol "$name" was used, but it is missing a '
+ '@MirrorsUsed annotation.';
+ } else {
+ var declaration = getDeclarationOf(warning.object);
+ var exp = declaration != null ?
+ MirrorSystem.getName(declaration.simpleName) : warning.object;
+ var name = MirrorSystem.getName(warning.symbol);
+ message = 'Tried to access "$exp.$name" via mirrors, '
+ 'but it is missing a @MirrorsUsed annotation.';
+ }
+ logger.warning(message);
+ });
+ }
+}
+
+/// Default error message shown when the first error is found. This constant is
+/// visible mainly for testing purposes.
+const String FIRST_ERROR_MESSAGE =
+ 'Parts of your program uses mirrors to access objects. Some '
+ 'of these mirror accesses were not declared with @MirrorsUsed annotations. '
+ 'This could lead to unexpected behavior when deploying your app '
+ 'with dart2js. Below, you will find additional warnings where we '
+ 'detected that the @MirrorsUsed annotation is missing.';
« no previous file with comments | « pkg/checked_mirrors/lib/checked_mirrors.dart ('k') | pkg/checked_mirrors/lib/src/checker.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698