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

Unified Diff: modules/angular2/src/transform/annotation_processor.dart

Issue 927373004: Initial commit of Dart transformer to generate constructor stubs, see https://github.com/angular/an… (Closed) Base URL: https://github.com/kegluneq/angular.git@master
Patch Set: Created 5 years, 10 months 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
Index: modules/angular2/src/transform/annotation_processor.dart
diff --git a/modules/angular2/src/transform/annotation_processor.dart b/modules/angular2/src/transform/annotation_processor.dart
new file mode 100644
index 0000000000000000000000000000000000000000..323aa23df82250524a8aad5aea68e98be9d4fad6
--- /dev/null
+++ b/modules/angular2/src/transform/annotation_processor.dart
@@ -0,0 +1,58 @@
+import 'dart:collection' show Queue;
jakemac 2015/02/17 23:46:45 Should probably add a library/license (ditto other
tjblasi 2015/02/18 21:18:25 None of the other files seem to have licenses righ
+import 'package:analyzer/src/generated/element.dart';
+
+/// Provides a mechanism for checking an element for the provided
+/// [_annotationClass] and reporting the resulting (element, annotation) pairs.
+class AnnotationMatcher {
+ /// Queue for annotations.
+ final initQueue = new Queue<AnnotationMatch>();
jakemac 2015/02/17 23:46:45 nit: may want to rename this
tjblasi 2015/02/18 21:18:25 Done.
+ /// All the annotations we have seen for each element
+ final _seenAnnotations = new Map<Element, Set<ElementAnnotation>>();
+
+ /// The class we are searching for to populate [initQueue].
+ final ClassElement _annotationClass;
+
+ AnnotationMatcher(this._annotationClass);
+
+ /// Records all [_annotationClass] annotations and the [element]s they apply to.
+ /// Returns [true] if 1) [element] is annotated with [_annotationClass] and
+ /// 2) ([element], [_annotationClass]) has been seen previously.
jakemac 2015/02/17 23:46:45 has -> has not?
tjblasi 2015/02/18 21:18:25 Done.
+ bool processAnnotations(ClassElement element) {
+ var found = false;
+ element.metadata.where((ElementAnnotation meta) {
+ // Only process [_annotationClass]s.
+ // TODO(tjblasi): Make this recognize non-ConstructorElement annotations.
+ return meta.element is ConstructorElement &&
+ _isAnnotationMatch(meta.element.returnType);
+ }).where((ElementAnnotation meta) {
+ // Only process ([element], [meta]) combinations we haven't seen previously.
+ return !_seenAnnotations
+ .putIfAbsent(element, () => new Set<ElementAnnotation>())
+ .contains(meta);
+ }).forEach((ElementAnnotation meta) {
+ _seenAnnotations[element].add(meta);
+ initQueue.addLast(new AnnotationMatch(element, meta));
+ found = true;
+ });
+ return found;
+ }
+
+ /// Whether [type], its superclass, or one of its interfaces matches [_annotationClass].
+ bool _isAnnotationMatch(InterfaceType type) {
+ if (type == null || type.element == null) return false;
+ if (type.element.type == _annotationClass.type) return true;
+ if (_isAnnotationMatch(type.superclass)) return true;
+ for (var interface in type.interfaces) {
+ if (_isAnnotationMatch(interface)) return true;
+ }
+ return false;
+ }
+}
+
+// Element/ElementAnnotation pair.
+class AnnotationMatch {
+ final Element element;
+ final ElementAnnotation annotation;
+
+ AnnotationMatch(this.element, this.annotation);
+}

Powered by Google App Engine
This is Rietveld 408576698