Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | |
|
ngeoffray
2013/08/16 12:52:45
Why in dart2js_extra?
ahe
2013/08/30 13:43:47
Because I import dart:html.
| |
| 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 // Prints all information about all mirrors. | |
|
karlklose
2013/08/16 12:53:09
'Tests that it is possible to enumerate all metada
ahe
2013/08/30 13:43:47
Done.
| |
| 6 | |
| 7 library test.mirror_printer_test; | |
| 8 | |
| 9 @MirrorsUsed(targets: '*') | |
| 10 import 'dart:mirrors'; | |
| 11 | |
| 12 // Importing dart:html to make things interesting. | |
| 13 import 'dart:html'; | |
| 14 | |
| 15 class MirrorPrinter { | |
| 16 final StringBuffer buffer; | |
| 17 final TypeMirror dynamicType = currentMirrorSystem().dynamicType; | |
| 18 | |
| 19 int indentationLevel = 0; | |
| 20 | |
| 21 MirrorPrinter(this.buffer); | |
| 22 | |
| 23 void w(object) { | |
| 24 buffer.write(object); | |
| 25 } | |
| 26 | |
| 27 n(Symbol symbol) => MirrorSystem.getName(symbol); | |
| 28 | |
| 29 void indented(action) { | |
| 30 indentationLevel++; | |
| 31 action(); | |
| 32 indentationLevel--; | |
| 33 } | |
| 34 | |
| 35 get indent { | |
| 36 for (int i = 0; i < indentationLevel; i++) { | |
| 37 w(' '); | |
| 38 } | |
| 39 } | |
| 40 | |
| 41 String stringifyInstance(InstanceMirror mirror) { | |
| 42 var reflectee = mirror.reflectee; | |
| 43 if (reflectee is String) return '"${reflectee}"'; | |
| 44 if (reflectee is Null || reflectee is bool || reflectee is num || | |
| 45 reflectee is List || reflectee is Map) { | |
| 46 return '$reflectee'; | |
| 47 } | |
| 48 StringBuffer buffer = new StringBuffer(); | |
| 49 var variables = mirror.type.variables; | |
|
karlklose
2013/08/16 12:53:09
Could you add a type annotation here?
ahe
2013/08/30 13:43:47
Done.
| |
| 50 buffer | |
| 51 ..write(n(mirror.type.simpleName)) | |
| 52 ..write('('); | |
| 53 bool first = true; | |
| 54 variables.forEach((Symbol name, VariableMirror variable) { | |
| 55 if (variable.isStatic) return; | |
| 56 // TODO(ahe): Include superclasses. | |
| 57 if (first) { | |
| 58 first = false; | |
| 59 } else { | |
| 60 buffer.write(', '); | |
| 61 } | |
| 62 buffer | |
| 63 ..write(n(name)) | |
| 64 ..write(': ') | |
| 65 ..write(stringifyInstance(mirror.getField(name))); | |
| 66 }); | |
| 67 return buffer | |
| 68 ..write(')') | |
| 69 ..toString(); | |
| 70 } | |
| 71 | |
| 72 String stringifyMetadata(InstanceMirror mirror) { | |
| 73 return '@${stringifyInstance(mirror)}'; | |
| 74 } | |
| 75 | |
| 76 bool writeType(TypeMirror mirror) { | |
| 77 if (mirror == null || mirror == dynamicType) return false; | |
| 78 w('${n(mirror.simpleName)} '); | |
| 79 return true; | |
| 80 } | |
| 81 | |
| 82 writeVariable(VariableMirror mirror) { | |
| 83 bool needsVar = true; | |
| 84 if (mirror.isStatic) w('static '); | |
| 85 // TODO(ahe): What about const? | |
| 86 if (mirror.isFinal) { | |
| 87 w('final '); | |
| 88 needsVar = false; | |
| 89 } | |
| 90 | |
| 91 if (writeType(mirror.type)) needsVar = false; | |
| 92 | |
| 93 if (needsVar) { | |
| 94 w('var '); | |
| 95 } | |
| 96 w('${n(mirror.simpleName)};'); | |
| 97 } | |
| 98 | |
| 99 writeMethod(MethodMirror mirror) { | |
| 100 writeType(mirror.returnType); | |
| 101 if (mirror.isOperator) { | |
| 102 w('operator '); | |
| 103 } | |
| 104 if (mirror.isGetter) { | |
| 105 w('get '); | |
| 106 } | |
| 107 if (mirror.isSetter) { | |
| 108 w('set '); | |
| 109 } | |
| 110 w('${n(mirror.simpleName)}'); | |
| 111 if (!mirror.isGetter) { | |
| 112 w('()'); | |
| 113 } | |
| 114 w(';'); | |
| 115 } | |
| 116 | |
| 117 dumpClass(ClassMirror mirror) { | |
|
karlklose
2013/08/16 12:53:09
'dumpClass' -> 'writeClass'.
ahe
2013/08/30 13:43:47
Done.
| |
| 118 w('class ${n(mirror.simpleName)}'); | |
| 119 w(' {'); | |
| 120 bool first = true; | |
| 121 indented(() { | |
| 122 for (DeclarationMirror declaration in mirror.members.values) { | |
| 123 if (first) { | |
| 124 first = false; | |
| 125 } else { | |
| 126 w('\n'); | |
| 127 } | |
| 128 writeDeclaration(declaration); | |
| 129 } | |
| 130 }); | |
| 131 w('\n}\n'); | |
| 132 } | |
| 133 | |
| 134 writeDeclaration(DeclarationMirror declaration) { | |
| 135 w('\n'); | |
| 136 var metadata = declaration.metadata; | |
| 137 if (!metadata.isEmpty) { | |
| 138 indent; | |
| 139 buffer.writeAll(metadata.map(stringifyMetadata), ' '); | |
| 140 w('\n'); | |
| 141 } | |
| 142 indent; | |
| 143 if (declaration is ClassMirror) { | |
| 144 dumpClass(declaration); | |
| 145 } else if (declaration is VariableMirror) { | |
| 146 writeVariable(declaration); | |
| 147 } else if (declaration is MethodMirror) { | |
| 148 writeMethod(declaration); | |
| 149 } else { | |
| 150 w('$declaration'); | |
|
karlklose
2013/08/16 12:53:09
Add a TODO what is missing here.
ahe
2013/08/30 13:43:47
Done.
| |
| 151 } | |
| 152 } | |
| 153 | |
| 154 writeLibrary(LibraryMirror library) { | |
| 155 w('library ${n(library.simpleName)};\n\n'); | |
| 156 library.members.values.forEach(writeDeclaration); | |
| 157 w('\n'); | |
| 158 } | |
| 159 | |
| 160 static StringBuffer stringify(Map<Uri, LibraryMirror> libraries) { | |
| 161 StringBuffer buffer = new StringBuffer(); | |
| 162 libraries.values.forEach(new MirrorPrinter(buffer).writeLibrary); | |
| 163 return buffer; | |
| 164 } | |
| 165 } | |
| 166 | |
| 167 main() { | |
| 168 print(MirrorPrinter.stringify(currentMirrorSystem().libraries)); | |
| 169 } | |
| OLD | NEW |