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