OLD | NEW |
| (Empty) |
1 // Copyright 2013 Google Inc. All Rights Reserved. | |
2 // | |
3 // Licensed under the Apache License, Version 2.0 (the "License"); | |
4 // you may not use this file except in compliance with the License. | |
5 // You may obtain a copy of the License at | |
6 // | |
7 // http://www.apache.org/licenses/LICENSE-2.0 | |
8 // | |
9 // Unless required by applicable law or agreed to in writing, software | |
10 // distributed under the License is distributed on an "AS IS" BASIS, | |
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
12 // See the License for the specific language governing permissions and | |
13 // limitations under the License. | |
14 | |
15 library quiver.mirrors; | |
16 | |
17 import 'dart:mirrors'; | |
18 | |
19 /** | |
20 * Returns the qualified name of [t]. | |
21 */ | |
22 Symbol getTypeName(Type t) => reflectClass(t).qualifiedName; | |
23 | |
24 /** | |
25 * Returns true if [o] implements [type]. | |
26 */ | |
27 bool implements(Object o, Type type) => | |
28 classImplements(reflect(o).type, reflectClass(type)); | |
29 | |
30 /** | |
31 * Returns true if the class represented by [classMirror] implements the class | |
32 * represented by [interfaceMirror]. | |
33 */ | |
34 bool classImplements(ClassMirror classMirror, ClassMirror interfaceMirror) { | |
35 if (classMirror == null) return false; | |
36 // TODO: change to comparing mirrors when dartbug.com/19781 is fixed | |
37 if (classMirror.qualifiedName == interfaceMirror.qualifiedName) return true; | |
38 if (classImplements(classMirror.superclass, interfaceMirror)) return true; | |
39 if (classMirror.superinterfaces | |
40 .any((i) => classImplements(i, interfaceMirror))) return true; | |
41 return false; | |
42 } | |
43 | |
44 /** | |
45 * Walks up the class hierarchy to find a method declaration with the given | |
46 * [name]. | |
47 * | |
48 * Note that it's not possible to tell if there's an implementation via | |
49 * noSuchMethod(). | |
50 */ | |
51 DeclarationMirror getDeclaration(ClassMirror classMirror, Symbol name) { | |
52 if (classMirror.declarations.containsKey(name)) { | |
53 return classMirror.declarations[name]; | |
54 } | |
55 if (classMirror.superclass != null) { | |
56 var mirror = getDeclaration(classMirror.superclass, name); | |
57 if (mirror != null) { | |
58 return mirror; | |
59 } | |
60 } | |
61 for (ClassMirror supe in classMirror.superinterfaces) { | |
62 var mirror = getDeclaration(supe, name); | |
63 if (mirror != null) { | |
64 return mirror; | |
65 } | |
66 } | |
67 return null; | |
68 } | |
69 | |
70 /** | |
71 * Closurzes a method reflectively. | |
72 */ | |
73 class Method /* implements Function */ { | |
74 final InstanceMirror mirror; | |
75 final Symbol symbol; | |
76 | |
77 Method(this.mirror, this.symbol); | |
78 | |
79 dynamic noSuchMethod(Invocation i) { | |
80 if (i.isMethod && i.memberName == const Symbol('call')) { | |
81 if (i.namedArguments != null && i.namedArguments.isNotEmpty) { | |
82 // this will fail until named argument support is implemented | |
83 return mirror.invoke( | |
84 symbol, i.positionalArguments, i.namedArguments).reflectee; | |
85 } | |
86 return mirror.invoke(symbol, i.positionalArguments).reflectee; | |
87 } | |
88 return super.noSuchMethod(i); | |
89 } | |
90 } | |
OLD | NEW |