OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2012, 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 // Test basic mirrors functionality. |
| 6 |
| 7 import 'dart:mirrors'; |
| 8 import 'dart:isolate'; |
| 9 |
| 10 import 'async_helper.dart'; |
| 11 |
| 12 var simpleVar = 123; |
| 13 final finalVar = 456; |
| 14 |
| 15 dynamic functionDynamic() { |
| 16 return 123; |
| 17 } |
| 18 int functionInt() { |
| 19 return 456; |
| 20 } |
| 21 void functionVoid() { |
| 22 } |
| 23 |
| 24 class C_Used { |
| 25 int xx; |
| 26 set x(int _xx) { xx = _xx; } |
| 27 |
| 28 int yy; |
| 29 int ss; |
| 30 int vv; |
| 31 C_Used(this.xx, [this.ss = 128, this.vv = 256]) { |
| 32 yy = 111; |
| 33 } |
| 34 C_Used.withOpts(this.xx, {var s : 1024, var v : 2048}) { |
| 35 yy = 222; ss = s; vv = v; |
| 36 } |
| 37 |
| 38 String toString() => 'C_Used(xx=$xx, yy=$yy, ss=$ss, vv=$vv)'; |
| 39 } |
| 40 class C1_NotUsed {} |
| 41 |
| 42 void test() { |
| 43 MirrorSystem mirrorSystem = currentMirrorSystem(); |
| 44 |
| 45 for (LibraryMirror lm in mirrorSystem.libraries.values) { |
| 46 print("${lm.simpleName}"); |
| 47 if (lm.classes != null) { |
| 48 lm.classes.forEach( |
| 49 (key, value) { |
| 50 print("\tClass: $key\t${value == null?'':value}"); |
| 51 if (value.methods != null) { |
| 52 value.methods.forEach( |
| 53 (key, value) => print("\t\tMethod: $key\t$value")); |
| 54 } |
| 55 if (value.getters != null) { |
| 56 value.getters.forEach( |
| 57 (key, value) => print("\t\tGetter: $key\t$value")); |
| 58 } |
| 59 if (value.setters != null) { |
| 60 value.setters.forEach( |
| 61 (key, value) => print("\t\tSetter: $key\t$value")); |
| 62 } |
| 63 if (value.variables != null) { |
| 64 value.variables.forEach( |
| 65 (key, value) => print("\t\tVariable: $key\t$value")); |
| 66 } |
| 67 if (value.constructors != null) { |
| 68 value.constructors.forEach( |
| 69 (key, value) => print("\t\tConstuctor: $key\t$value")); |
| 70 } |
| 71 if (value.typeVariables != null) { |
| 72 value.typeVariables.forEach( |
| 73 (key, value) => print("\t\tTypeVariable: $key\t$value")); |
| 74 } |
| 75 if (value.typeArguments != null) { |
| 76 value.typeArguments.forEach( |
| 77 (key, value) => print("\t\tTypeArgument: $key\t$value")); |
| 78 } |
| 79 }); |
| 80 } |
| 81 if (lm.functions != null) { |
| 82 lm.functions.forEach( |
| 83 (key, value) => print("\tFunction: $key\t${value == null?'':value}")); |
| 84 } |
| 85 if (lm.variables != null) { |
| 86 lm.variables.forEach( |
| 87 (key, value) => print("\tVariable: $key\t${value == null?'':value}")); |
| 88 } |
| 89 } |
| 90 |
| 91 Collection<LibraryMirror> libraries = mirrorSystem.libraries.values; |
| 92 Expect.equals(1, libraries.filter( |
| 93 (library) => library.simpleName == "dart.mirrors").length); |
| 94 Collection<LibraryMirror> thisLibraryMirrors = |
| 95 libraries.filter( |
| 96 (library) => library.simpleName.endsWith('mirror_libraries_test.dart')); |
| 97 Expect.equals(1, thisLibraryMirrors.length); |
| 98 LibraryMirror thisLibraryMirror = thisLibraryMirrors[0]; |
| 99 Expect.isTrue(thisLibraryMirror.classes.containsKey("C_Used")); |
| 100 ClassMirror classMirror = thisLibraryMirror.classes["C_Used"]; |
| 101 Expect.equals(2, classMirror.constructors.length); |
| 102 classMirror.constructors.forEach((name, constructorMirror) { |
| 103 Expect.isNotNull(name); |
| 104 Expect.isNotNull(constructorMirror.constructorName); |
| 105 Expect.isTrue(constructorMirror.isConstructor); |
| 106 |
| 107 asyncTest((onDone) { |
| 108 classMirror.newInstance( |
| 109 constructorMirror.constructorName, |
| 110 [218], |
| 111 null).then((value) { |
| 112 Expect.equals(218, value.reflectee.xx); |
| 113 }); |
| 114 onDone(true); |
| 115 }); |
| 116 }); |
| 117 |
| 118 Expect.equals(4, classMirror.variables.length); |
| 119 Expect.equals(1, |
| 120 classMirror.variables.values.filter((v) => v.simpleName == "xx").length); |
| 121 Expect.equals(1, |
| 122 classMirror.variables.values.filter((v) => v.simpleName == "yy").length); |
| 123 Expect.equals(1, |
| 124 classMirror.variables.values.filter((v) => v.simpleName == "ss").length); |
| 125 Expect.equals(1, |
| 126 classMirror.variables.values.filter((v) => v.simpleName == "vv").length); |
| 127 |
| 128 asyncTest((onDone) { |
| 129 classMirror.newInstance("", [], null).then((value) { |
| 130 Expect.isTrue(value.hasReflectee); |
| 131 }); |
| 132 onDone(true); |
| 133 }); |
| 134 asyncTest((onDone) { |
| 135 classMirror.newInstance("", [], null).then((value) { |
| 136 Expect.isNull(value.reflectee.xx); |
| 137 }); |
| 138 onDone(true); |
| 139 }); |
| 140 asyncTest((onDone) { |
| 141 classMirror.newInstance("", [314], null).then((value) { |
| 142 Expect.equals(314, value.reflectee.xx); |
| 143 }); |
| 144 onDone(true); |
| 145 }); |
| 146 asyncTest((onDone) { |
| 147 classMirror.newInstance("", [314], null).then((value) { |
| 148 Expect.equals(111, value.reflectee.yy); |
| 149 }); |
| 150 onDone(true); |
| 151 }); |
| 152 asyncTest((onDone) { |
| 153 classMirror.newInstance("withOpts", [159], null).then((value) { |
| 154 Expect.equals(159, value.reflectee.xx); |
| 155 }); |
| 156 onDone(true); |
| 157 }); |
| 158 asyncTest((onDone) { |
| 159 classMirror.newInstance("withOpts", [26], null).then((value) { |
| 160 Expect.equals(222, value.reflectee.yy); |
| 161 }); |
| 162 onDone(true); |
| 163 }); |
| 164 |
| 165 asyncTest((onDone) { |
| 166 classMirror.newInstance("", [314, 159, 26], null).then((instance) { |
| 167 asyncTest((onDone) { |
| 168 instance.invoke("toString", []).then((value) { |
| 169 Expect.equals( |
| 170 "C_Used(xx=314, yy=111, ss=159, vv=26)", |
| 171 value.reflectee); |
| 172 onDone(true); |
| 173 }); |
| 174 }); |
| 175 }); |
| 176 onDone(true); |
| 177 }); |
| 178 |
| 179 Expect.equals(1, |
| 180 thisLibraryMirror.variables.values.filter( |
| 181 (v) => v.simpleName == "simpleVar").length); |
| 182 VariableMirror variableMirror = |
| 183 thisLibraryMirror.variables.values.filter( |
| 184 (v) => v.simpleName == "simpleVar")[0]; |
| 185 Expect.isFalse(variableMirror.isStatic); |
| 186 Expect.isFalse(variableMirror.isFinal); |
| 187 Expect.equals(1, |
| 188 thisLibraryMirror.variables.values.filter( |
| 189 (v) => v.simpleName == "finalVar").length); |
| 190 variableMirror = |
| 191 thisLibraryMirror.variables.values.filter( |
| 192 (v) => v.simpleName == "finalVar")[0]; |
| 193 Expect.isFalse(variableMirror.isStatic); |
| 194 Expect.isTrue(variableMirror.isFinal); |
| 195 |
| 196 Expect.isTrue(thisLibraryMirror.classes.containsKey("C1_NotUsed")); |
| 197 classMirror = thisLibraryMirror.classes["C1_NotUsed"]; |
| 198 asyncTest((onDone) { |
| 199 classMirror.newInstance("", [], null).then((value) { |
| 200 Expect.isFalse(value.hasReflectee); |
| 201 }); |
| 202 onDone(true); |
| 203 }); |
| 204 asyncTest((onDone) { |
| 205 classMirror.newInstance("", [], null).then((value) { |
| 206 Expect.isNull(value.reflectee); |
| 207 }); |
| 208 onDone(true); |
| 209 }); |
| 210 |
| 211 Expect.isTrue(thisLibraryMirror.functions.containsKey("functionVoid")); |
| 212 MethodMirror functionMirror = thisLibraryMirror.functions["functionVoid"]; |
| 213 Expect.equals(functionMirror.returnType, mirrorSystem.voidType); |
| 214 |
| 215 Expect.isTrue(thisLibraryMirror.functions.containsKey("functionDynamic")); |
| 216 functionMirror = thisLibraryMirror.functions["functionDynamic"]; |
| 217 Expect.equals(functionMirror.returnType, mirrorSystem.dynamicType); |
| 218 |
| 219 Expect.isTrue(thisLibraryMirror.functions.containsKey("functionInt")); |
| 220 functionMirror = thisLibraryMirror.functions["functionInt"]; |
| 221 Expect.isNotNull(functionMirror.returnType); |
| 222 } |
| 223 |
| 224 void main() { |
| 225 // Dummy use to get class constructor generated. |
| 226 new C_Used(123); |
| 227 new C_Used.withOpts(456); |
| 228 |
| 229 var result; |
| 230 for (var i=0; i<123; i++) { |
| 231 functionVoid(); |
| 232 result = functionDynamic(); |
| 233 } |
| 234 print("$result"); |
| 235 for (var i=0; i<123; i++) { |
| 236 result = functionInt(); |
| 237 } |
| 238 |
| 239 print(""" |
| 240 |
| 241 This program is using an experimental feature called \"mirrors\". As |
| 242 currently implemented, mirrors do not work with minification, and will |
| 243 cause spurious errors depending on how code was optimized. |
| 244 |
| 245 The authors of this program are aware of these problems and have |
| 246 decided the thrill of using an experimental feature is outweighing the |
| 247 risks. Furthermore, the authors of this program understand that |
| 248 long-term, to fix the problems mentioned above, mirrors may have |
| 249 negative impact on size and performance of Dart programs compiled to |
| 250 JavaScript. |
| 251 """); |
| 252 |
| 253 test(); |
| 254 } |
OLD | NEW |