| OLD | NEW |
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 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 | 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 mirrors_util; | 5 library mirrors_util; |
| 6 | 6 |
| 7 import 'dart:collection' show Queue, IterableBase; | 7 import 'dart:collection' show Queue, IterableBase; |
| 8 | 8 |
| 9 import 'source_mirrors.dart'; | 9 import 'source_mirrors.dart'; |
| 10 | 10 |
| (...skipping 162 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 173 } | 173 } |
| 174 | 174 |
| 175 Iterable<DeclarationMirror> membersOf( | 175 Iterable<DeclarationMirror> membersOf( |
| 176 Map<Symbol, DeclarationMirror> declarations) { | 176 Map<Symbol, DeclarationMirror> declarations) { |
| 177 return declarations.values.where( | 177 return declarations.values.where( |
| 178 (mirror) => mirror is MethodMirror || mirror is VariableMirror); | 178 (mirror) => mirror is MethodMirror || mirror is VariableMirror); |
| 179 } | 179 } |
| 180 | 180 |
| 181 Iterable<TypeMirror> classesOf( | 181 Iterable<TypeMirror> classesOf( |
| 182 Map<Symbol, DeclarationMirror> declarations) { | 182 Map<Symbol, DeclarationMirror> declarations) { |
| 183 return declarations.values.where((mirror) => mirror is ClassMirror); | 183 return new _TypeOfIterable<ClassMirror>(declarations.values); |
| 184 } | 184 } |
| 185 | 185 |
| 186 Iterable<TypeMirror> typesOf( | 186 Iterable<TypeMirror> typesOf( |
| 187 Map<Symbol, DeclarationMirror> declarations) { | 187 Map<Symbol, DeclarationMirror> declarations) { |
| 188 return declarations.values.where((mirror) => mirror is TypeMirror); | 188 return new _TypeOfIterable<TypeMirror>(declarations.values); |
| 189 } | 189 } |
| 190 | 190 |
| 191 Iterable<MethodMirror> methodsOf( | 191 Iterable<MethodMirror> methodsOf( |
| 192 Map<Symbol, DeclarationMirror> declarations) { | 192 Map<Symbol, DeclarationMirror> declarations) { |
| 193 return declarations.values.where( | 193 return anyMethodOf(declarations).where((mirror) => mirror.isRegularMethod); |
| 194 (mirror) => mirror is MethodMirror && mirror.isRegularMethod); | |
| 195 } | 194 } |
| 196 | 195 |
| 197 Iterable<MethodMirror> constructorsOf( | 196 Iterable<MethodMirror> constructorsOf( |
| 198 Map<Symbol, DeclarationMirror> declarations) { | 197 Map<Symbol, DeclarationMirror> declarations) { |
| 199 return declarations.values.where( | 198 return anyMethodOf(declarations).where((mirror) => mirror.isConstructor); |
| 200 (mirror) => mirror is MethodMirror && mirror.isConstructor); | |
| 201 } | 199 } |
| 202 | 200 |
| 203 Iterable<MethodMirror> settersOf( | 201 Iterable<MethodMirror> settersOf( |
| 204 Map<Symbol, DeclarationMirror> declarations) { | 202 Map<Symbol, DeclarationMirror> declarations) { |
| 205 return declarations.values.where( | 203 return anyMethodOf(declarations).where((mirror) => mirror.isSetter); |
| 206 (mirror) => mirror is MethodMirror && mirror.isSetter); | |
| 207 } | 204 } |
| 208 | 205 |
| 209 Iterable<MethodMirror> gettersOf( | 206 Iterable<MethodMirror> gettersOf( |
| 210 Map<Symbol, DeclarationMirror> declarations) { | 207 Map<Symbol, DeclarationMirror> declarations) { |
| 211 return declarations.values.where( | 208 return anyMethodOf(declarations).where((mirror) => mirror.isGetter); |
| 212 (mirror) => mirror is MethodMirror && mirror.isGetter); | 209 } |
| 210 |
| 211 Iterable<MethodMirror> anyMethodOf( |
| 212 Map<Symbol, DeclarationMirror> declarations) { |
| 213 return new _TypeOfIterable<MethodMirror>(declarations.values); |
| 213 } | 214 } |
| 214 | 215 |
| 215 Iterable<VariableMirror> variablesOf( | 216 Iterable<VariableMirror> variablesOf( |
| 216 Map<Symbol, DeclarationMirror> declarations) { | 217 Map<Symbol, DeclarationMirror> declarations) { |
| 217 return declarations.values.where((mirror) => mirror is VariableMirror); | 218 return new _TypeOfIterable<VariableMirror>(declarations.values); |
| 218 } | 219 } |
| 219 | 220 |
| 221 class _TypeOfIterable<T> extends IterableBase<T> { |
| 222 final Iterable _source; |
| 220 | 223 |
| 224 _TypeOfIterable(this._source); |
| 225 |
| 226 Iterator<T> get iterator => new _TypeOfIterator<T>(_source.iterator); |
| 227 } |
| 228 |
| 229 class _TypeOfIterator<T> implements Iterator<T> { |
| 230 final Iterator _source; |
| 231 |
| 232 T get current => _source.current; |
| 233 |
| 234 _TypeOfIterator(this._source); |
| 235 |
| 236 bool moveNext() { |
| 237 while(_source.moveNext()) { |
| 238 if (_source.current is T) { |
| 239 return true; |
| 240 } |
| 241 } |
| 242 return false; |
| 243 } |
| 244 } |
| 221 | 245 |
| 222 bool isObject(TypeMirror mirror) => | 246 bool isObject(TypeMirror mirror) => |
| 223 mirror is ClassMirror && mirror.superclass == null; | 247 mirror is ClassMirror && mirror.superclass == null; |
| 224 | 248 |
| 225 /// Returns `true` if [cls] is declared in a private dart library. | 249 /// Returns `true` if [cls] is declared in a private dart library. |
| 226 bool isFromPrivateDartLibrary(ClassMirror cls) { | 250 bool isFromPrivateDartLibrary(ClassMirror cls) { |
| 227 if (isMixinApplication(cls)) cls = cls.mixin; | 251 if (isMixinApplication(cls)) cls = cls.mixin; |
| 228 var uri = getLibrary(cls).uri; | 252 var uri = getLibrary(cls).uri; |
| 229 return uri.scheme == 'dart' && uri.path.startsWith('_'); | 253 return uri.scheme == 'dart' && uri.path.startsWith('_'); |
| 230 } | 254 } |
| (...skipping 165 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 396 // Try type variables. | 420 // Try type variables. |
| 397 result = mirror.typeVariables.firstWhere( | 421 result = mirror.typeVariables.firstWhere( |
| 398 (TypeVariableMirror v) => v.simpleName == id, orElse: () => null); | 422 (TypeVariableMirror v) => v.simpleName == id, orElse: () => null); |
| 399 } else if (mirror is MethodMirror) { | 423 } else if (mirror is MethodMirror) { |
| 400 result = mirror.parameters.firstWhere( | 424 result = mirror.parameters.firstWhere( |
| 401 (ParameterMirror p) => p.simpleName == id, orElse: () => null); | 425 (ParameterMirror p) => p.simpleName == id, orElse: () => null); |
| 402 } | 426 } |
| 403 return result; | 427 return result; |
| 404 | 428 |
| 405 } | 429 } |
| OLD | NEW |