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

Side by Side Diff: pkg/docgen/lib/src/models/annotation.dart

Issue 268583002: Access parameter data in Annotations, as well as fixed export visibility (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 6 years, 7 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 unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a 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. 3 // BSD-style license that can be found in the LICENSE file.
4 4
5 library docgen.models.annotation; 5 library docgen.models.annotation;
6 6
7 import '../exports/mirrors_util.dart' as dart2js_util;
8 import '../exports/source_mirrors.dart'; 7 import '../exports/source_mirrors.dart';
9 8
9 import '../exports/dart2js_mirrors.dart' show ResolvedNode;
10
10 import '../library_helpers.dart'; 11 import '../library_helpers.dart';
11 12
12 import 'library.dart'; 13 import 'library.dart';
13 import 'mirror_based.dart'; 14 import 'mirror_based.dart';
14 15
16 import 'dart:mirrors';
17 import '../../../../../sdk/lib/_internal/compiler/implementation/tree/tree.dart' ;
18
15 /// Holds the name of the annotation, and its parameters. 19 /// Holds the name of the annotation, and its parameters.
16 class Annotation extends MirrorBased<ClassMirror> { 20 class Annotation extends MirrorBased<ClassMirror> {
17 /// The class of this annotation. 21 /// The class of this annotation.
18 final ClassMirror mirror; 22 Mirror mirror;
19 final Library owningLibrary; 23 final Library owningLibrary;
20 final List<String> parameters; 24 List<String> parameters;
21 25
22 Annotation(InstanceMirror originalMirror, this.owningLibrary) 26 Annotation(ResolvedNode resolvedNode, this.owningLibrary) {
23 : mirror = originalMirror.type, 27 parameters = [];
24 parameters = _createParamaters(originalMirror); 28 getMirrorForResolvedNode(resolvedNode, (m) => mirror = m,
29 (String param) => parameters.add(param));
30 }
31
32 String getMirrorForResolvedNode(ResolvedNode node, callbackFunc,
33 paramCallbackFunc) {
34 ResolvedNodeMirrorFinder finder = new ResolvedNodeMirrorFinder(node,
35 callbackFunc, paramCallbackFunc);
36 finder.unparse(node.node);
37 return finder.result;
38 }
25 39
26 Map toMap() => { 40 Map toMap() => {
27 'name': getDocgenObject(mirror, owningLibrary).docName, 41 'name': getDocgenObject(mirror, owningLibrary).docName,
28 'parameters': parameters 42 'parameters': parameters
29 }; 43 };
30 } 44 }
31 45
32 List<String> _createParamaters(InstanceMirror originalMirror) { 46 class ResolvedNodeMirrorFinder extends Unparser {
33 var curMirror = originalMirror.type; 47 final ResolvedNode resolvedNode;
34 Map<Symbol, DeclarationMirror> allDeclarations = 48 final Function annotationMirrorCallback;
35 new Map.from(curMirror.declarations); 49 final Function parameterValueCallback;
36 // This method assumes that our users aren't creating deep inheritance 50 int recursionLevel;
37 // chains of custom annotation inheritance. If this is not the case, 51
38 // re-write this section for performance. 52 ResolvedNodeMirrorFinder(this.resolvedNode, this.annotationMirrorCallback,
39 while (curMirror.superclass != null && 53 this.parameterValueCallback) : recursionLevel = 0;
40 curMirror.superclass.simpleName.toString() != 'Object') { 54
41 allDeclarations.addAll(curMirror.superclass.declarations); 55 visitSend(Send node) {
42 curMirror = curMirror.superclass; 56 var m = resolvedNode.resolvedMirror(node);
57 annotationMirrorCallback(m);
58 recursionLevel++;
59 visit(node.argumentsNode);
60 recursionLevel--;
43 } 61 }
44 62
45 // TODO(efortuna): Some originalMirrors, such as the 63 unparseNodeListFrom(NodeList node, var from) {
46 // Dart2JsMapConstantMirror and Dart2JsListConstantMirror don't have a 64 if (from.isEmpty) return;
47 // reflectee field, but we want the value of the parameter from them. 65
48 // Gross workaround is to assemble the object manually. 66 visit(from.head);
49 // See issue 18346. 67
50 return dart2js_util.variablesOf(allDeclarations) 68 for (var link = from.tail; !link.isEmpty; link = link.tail) {
51 .where((e) => e.isFinal && 69 if (recursionLevel == 2) {
52 originalMirror.getField(e.simpleName).hasReflectee) 70 parameterValueCallback(sb.toString());
53 .map((e) => originalMirror.getField(e.simpleName).reflectee) 71 sb.clear();
54 .where((e) => e != null) 72 }
55 .toList(); 73 visit(link.head);
74 }
75 if (recursionLevel == 2) {
76 parameterValueCallback(sb.toString());
77 sb.clear();
78 }
79 }
80
81 visitNodeList(NodeList node) {
82 addToken(node.beginToken);
83 if (recursionLevel == 1) sb.clear();
84 if (node.nodes != null) {
85 recursionLevel++;
86 unparseNodeListFrom(node, node.nodes);
87 recursionLevel--;
88 }
89 if (node.endToken != null) add(node.endToken.value);
90 }
56 } 91 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698