Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(166)

Side by Side Diff: reflectable/lib/src/mirrors_unimpl.dart

Issue 1289933004: Implements support for reflection on parameters. (Closed) Base URL: https://github.com/dart-lang/reflectable.git@master
Patch Set: Created 5 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2015, the Dart Team. All rights reserved. Use of this 1 // Copyright (c) 2015, the Dart Team. All rights reserved. Use of this
2 // source code is governed by a BSD-style license that can be found in 2 // source code is governed by a BSD-style license that can be found in
3 // the LICENSE file. 3 // the LICENSE file.
4 4
5 library reflectable.src.mirrors_unimpl; 5 library reflectable.src.mirrors_unimpl;
6 6
7 import 'dart:collection' show UnmodifiableMapView, UnmodifiableListView; 7 import 'dart:collection' show UnmodifiableMapView, UnmodifiableListView;
8 8
9 import '../capability.dart'; 9 import '../capability.dart';
10 import '../mirrors.dart'; 10 import '../mirrors.dart';
(...skipping 11 matching lines...) Expand all
22 // in ways that are not covered by the specified capabilities. 22 // in ways that are not covered by the specified capabilities.
23 // 23 //
24 // Each of these classes implements the corresponding class in 24 // Each of these classes implements the corresponding class in
25 // `package:reflectable/mirrors.dart`, and they replicate the internal 25 // `package:reflectable/mirrors.dart`, and they replicate the internal
26 // implements `structure in package:reflectable/mirrors.dart` using `extends` 26 // implements `structure in package:reflectable/mirrors.dart` using `extends`
27 // and `with` clauses, such that the (un)implementation is inherited rather than 27 // and `with` clauses, such that the (un)implementation is inherited rather than
28 // replicated wherever possible. 28 // replicated wherever possible.
29 29
30 _unsupported() => throw new UnimplementedError(); 30 _unsupported() => throw new UnimplementedError();
31 31
32 abstract class DeclarationMirrorUnimpl implements DeclarationMirror {
33 String get simpleName => _unsupported();
34 String get qualifiedName => _unsupported();
35 DeclarationMirror get owner => _unsupported();
36 bool get isPrivate => _unsupported();
37 bool get isTopLevel => _unsupported();
38 SourceLocation get location => _unsupported();
39 List<Object> get metadata => _unsupported();
40 }
41
42 abstract class ObjectMirrorUnimpl implements ObjectMirror {
43 Object invoke(String memberName, List positionalArguments,
44 [Map<Symbol, dynamic> namedArguments]) => _unsupported();
45 Object invokeGetter(String getterName) => _unsupported();
46 Object invokeSetter(String setterName, Object value) => _unsupported();
47
48 Object getField(String name) {
49 throw new UnsupportedError("Use invokeGetter instead of getField");
50 }
51
52 void setField(String name, Object value) {
53 throw new UnsupportedError("Use invokeSetter instead of setField");
54 }
55 }
56
57 abstract class InstanceMirrorUnimpl extends ObjectMirrorUnimpl
58 implements InstanceMirror {
59 ClassMirror get type => _unsupported();
60 bool get hasReflectee => _unsupported();
61 get reflectee => _unsupported();
62 bool operator ==(other) => _unsupported();
63 int get hashCode => _unsupported();
64 delegate(Invocation invocation) => _unsupported();
65 }
66
67 /// Invokes a getter on an object. 32 /// Invokes a getter on an object.
68 typedef Object InvokerOfGetter(Object instance); 33 typedef Object _InvokerOfGetter(Object instance);
69 34
70 /// Invokes a setter on an object. 35 /// Invokes a setter on an object.
71 typedef Object InvokerOfSetter(Object instance, Object value); 36 typedef Object _InvokerOfSetter(Object instance, Object value);
72 37
73 /// Invokes a static getter. 38 /// Invokes a static getter.
74 typedef Object StaticGetter(); 39 typedef Object _StaticGetter();
75 40
76 /// Invokes a setter on an object. 41 /// Invokes a setter on an object.
77 typedef Object StaticSetter(Object value); 42 typedef Object _StaticSetter(Object value);
78 43
79 /// The data backing a reflector. 44 /// The data backing a reflector.
80 class ReflectorData { 45 class ReflectorData {
81 /// List of class mirrors, one class mirror for each class that is 46 /// List of class mirrors, one class mirror for each class that is
82 /// supported by reflection as specified by the reflector backed up 47 /// supported by reflection as specified by the reflector backed up
83 /// by this [ReflectorData]. 48 /// by this [ReflectorData].
84 final List<ClassMirror> classMirrors; 49 final List<ClassMirror> classMirrors;
85 50
86 /// Repository of method mirrors used (via indices into this list) by 51 /// Repository of method mirrors used (via indices into this list) by
87 /// the above class mirrors to describe the behavior of the mirrored 52 /// the [classMirrors] to describe the behavior of the mirrored
88 /// class and its instances, and of variable mirrors used to describe 53 /// class and its instances, and of variable mirrors used to describe
89 /// their state. From the behavioral point of view there are no fields, 54 /// their state. From the behavioral point of view there are no fields,
90 /// but each of them gives rise to a getter and possibly a setter (whose 55 /// but each of them gives rise to a getter and possibly a setter (whose
91 /// `isSynthetic` is true). The implicit getters/setters are not stored 56 /// `isSynthetic` is true). The implicit getters/setters are not stored
92 /// in this list, but they can be reconstructed based on the variable 57 /// in this list, but they can be reconstructed based on the variable
93 /// mirrors describing the fields. From the program structure point of 58 /// mirrors describing the fields. From the program structure point of
94 /// view (used for `declarations`), the method mirrors and the field 59 /// view (used for `declarations`), the method mirrors and the field
95 /// mirrors must both be included directly. Hence, both points of view 60 /// mirrors must both be included directly. Hence, both points of view
96 /// require a little bit of processing, but in return we avoid some 61 /// require a little bit of processing, but in return we avoid some
97 /// redundancy in the stored information. 62 /// redundancy in the stored information.
98 final List<DeclarationMirror> memberMirrors; 63 final List<DeclarationMirror> memberMirrors;
99 64
65 /// Repository of parameter mirrors used (via indices into this list) by
66 /// the [memberMirrors] to describe the parameters of methods
67 final List<ParameterMirror> parameterMirrors;
68
100 /// List of [Type]s used to select class mirrors: If M is a class 69 /// List of [Type]s used to select class mirrors: If M is a class
101 /// mirror in [classMirrors] at index `i`, the mirrored class (and 70 /// mirror in [classMirrors] at index `i`, the mirrored class (and
102 /// hence the runtime type of the mirrored instances) is found in 71 /// hence the runtime type of the mirrored instances) is found in
103 /// `types[i]`. 72 /// `types[i]`.
104 final List<Type> types; 73 final List<Type> types;
105 74
106 /// Map from getter names to closures accepting an instance and returning 75 /// Map from getter names to closures accepting an instance and returning
107 /// the result of invoking the getter with that name on that instance. 76 /// the result of invoking the getter with that name on that instance.
108 final Map<String, InvokerOfGetter> getters; 77 final Map<String, _InvokerOfGetter> getters;
109 78
110 /// Map from setter names to closures accepting an instance and a new value, 79 /// Map from setter names to closures accepting an instance and a new value,
111 /// invoking the setter of that name on that instance, and returning its 80 /// invoking the setter of that name on that instance, and returning its
112 /// return value. 81 /// return value.
113 final Map<String, InvokerOfSetter> setters; 82 final Map<String, _InvokerOfSetter> setters;
114 83
115 Map<Type, ClassMirror> _typeToClassMirrorCache; 84 Map<Type, ClassMirror> _typeToClassMirrorCache;
116 85
117 ReflectorData(this.classMirrors, this.memberMirrors, this.types, this.getters, 86 ReflectorData(this.classMirrors, this.memberMirrors, this.parameterMirrors,
118 this.setters); 87 this.types, this.getters, this.setters);
119 88
120 /// Returns a class-mirror for the given [type]. 89 /// Returns a class-mirror for the given [type].
121 /// 90 ///
122 /// Returns `null` if the given class is not marked for reflection. 91 /// Returns `null` if the given class is not marked for reflection.
123 ClassMirror classMirrorForType(Type type) { 92 ClassMirror classMirrorForType(Type type) {
124 if (_typeToClassMirrorCache == null) { 93 if (_typeToClassMirrorCache == null) {
125 _typeToClassMirrorCache = new Map.fromIterables(types, classMirrors); 94 _typeToClassMirrorCache = new Map.fromIterables(types, classMirrors);
126 } 95 }
127 return _typeToClassMirrorCache[type]; 96 return _typeToClassMirrorCache[type];
128 } 97 }
129 } 98 }
130 99
131 /// This mapping contains the mirror-data for each reflector. 100 /// This mapping contains the mirror-data for each reflector.
132 /// It will be initialized in the generated code. 101 /// It will be initialized in the generated code.
133 Map<Reflectable, ReflectorData> data = throw new StateError( 102 Map<Reflectable, ReflectorData> data = throw new StateError(
134 "Reflectable has not been initialized. " 103 "Reflectable has not been initialized. "
135 "Did you forget to add the main file to the " 104 "Did you forget to add the main file to the "
136 "reflectable transformer's entry_points in pubspec.yaml?"); 105 "reflectable transformer's entry_points in pubspec.yaml?");
137 106
138 class InstanceMirrorImpl implements InstanceMirror { 107
108 abstract class _DataCaching {
139 ReflectorData _dataCache; 109 ReflectorData _dataCache;
110 ReflectableImpl get _reflector;
111
140 ReflectorData get _data { 112 ReflectorData get _data {
141 if (_dataCache == null) { 113 if (_dataCache == null) {
142 _dataCache = data[_reflectable]; 114 _dataCache = data[_reflector];
floitsch 2015/08/13 17:33:36 Why is this lazy? It's just a lookup into a map. T
eernst 2015/08/14 12:05:34 We have done that for a while, I just created the
floitsch 2015/08/17 14:32:23 Why is it not filled at construction?
eernst 2015/08/19 13:41:56 Could do that, check https://codereview.chromium.o
143 } 115 }
144 return _dataCache; 116 return _dataCache;
145 } 117 }
118 }
146 119
147 final ReflectableImpl _reflectable; 120 class _InstanceMirrorImpl extends _DataCaching implements InstanceMirror {
148 121 final ReflectableImpl _reflector;
149 final Object reflectee; 122 final Object reflectee;
150 123
151 InstanceMirrorImpl(this.reflectee, this._reflectable) { 124 _InstanceMirrorImpl(this.reflectee, this._reflector) {
152 _type = _data.classMirrorForType(reflectee.runtimeType); 125 _type = _data.classMirrorForType(reflectee.runtimeType);
153 if (_type == null) { 126 if (_type == null) {
154 throw new NoSuchCapabilityError( 127 throw new NoSuchCapabilityError(
155 "Reflecting on un-marked type ${reflectee.runtimeType}"); 128 "Reflecting on un-marked type ${reflectee.runtimeType}");
156 } 129 }
157 } 130 }
158 131
159 ClassMirror _type; 132 ClassMirror _type;
160 133
161 ClassMirror get type => _type; 134 ClassMirror get type => _type;
162 135
163 Object invoke(String methodName, List<Object> positionalArguments, 136 Object invoke(String methodName, List<Object> positionalArguments,
164 [Map<Symbol, Object> namedArguments]) { 137 [Map<Symbol, Object> namedArguments]) {
165 Function methodTearer = _data.getters[methodName]; 138 Function methodTearer = _data.getters[methodName];
166 if (methodTearer != null) { 139 if (methodTearer != null) {
167 return Function.apply( 140 return Function.apply(
168 methodTearer(reflectee), positionalArguments, namedArguments); 141 methodTearer(reflectee), positionalArguments, namedArguments);
169 } 142 }
170 throw new NoSuchInvokeCapabilityError( 143 throw new NoSuchInvokeCapabilityError(
171 reflectee, methodName, positionalArguments, namedArguments); 144 reflectee, methodName, positionalArguments, namedArguments);
172 } 145 }
173 146
174 bool get hasReflectee => true; 147 bool get hasReflectee => true;
175 148
176 bool operator ==(other) { 149 bool operator ==(other) {
177 return other is InstanceMirrorImpl && 150 return other is _InstanceMirrorImpl &&
178 other._reflectable == _reflectable && 151 other._reflector == _reflector &&
179 other.reflectee == reflectee; 152 other.reflectee == reflectee;
180 } 153 }
181 154
182 int get hashCode => reflectee.hashCode ^ _reflectable.hashCode; 155 int get hashCode => reflectee.hashCode ^ _reflector.hashCode;
183 156
184 delegate(Invocation invocation) => _unsupported(); 157 delegate(Invocation invocation) => _unsupported();
185 158
186 @override 159 @override
187 Object invokeGetter(String getterName) { 160 Object invokeGetter(String getterName) {
188 Function getter = _data.getters[getterName]; 161 Function getter = _data.getters[getterName];
189 if (getter != null) { 162 if (getter != null) {
190 return getter(reflectee); 163 return getter(reflectee);
191 } 164 }
192 throw new NoSuchInvokeCapabilityError(reflectee, getterName, [], {}); 165 throw new NoSuchInvokeCapabilityError(reflectee, getterName, [], {});
193 } 166 }
194 167
195 @override 168 @override
196 Object invokeSetter(String setterName, Object value) { 169 Object invokeSetter(String setterName, Object value) {
197 if (setterName.substring(setterName.length - 1) != "=") { 170 if (setterName.substring(setterName.length - 1) != "=") {
198 setterName += "="; 171 setterName += "=";
199 } 172 }
200 Function setter = _data.setters[setterName]; 173 Function setter = _data.setters[setterName];
201 if (setter != null) { 174 if (setter != null) {
202 return setter(reflectee, value); 175 return setter(reflectee, value);
203 } 176 }
204 throw new NoSuchInvokeCapabilityError(reflectee, setterName, [value], {}); 177 throw new NoSuchInvokeCapabilityError(reflectee, setterName, [value], {});
205 } 178 }
206 } 179 }
207 180
208 abstract class ClosureMirrorUnimpl extends InstanceMirrorUnimpl
209 implements ClosureMirror {
210 MethodMirror get function => _unsupported();
211 InstanceMirror apply(List positionalArguments,
212 [Map<Symbol, dynamic> namedArguments]) => _unsupported();
213 }
214
215 abstract class LibraryMirrorUnimpl extends DeclarationMirrorUnimpl
216 with ObjectMirrorUnimpl implements LibraryMirror {
217 Uri get uri => _unsupported();
218 Map<String, DeclarationMirror> get declarations => _unsupported();
219 bool operator ==(other) => _unsupported();
220 int get hashCode => _unsupported();
221 List<LibraryDependencyMirror> get libraryDependencies => _unsupported();
222 }
223
224 class LibraryDependencyMirrorUnimpl implements LibraryDependencyMirror {
225 bool get isImport => _unsupported();
226 bool get isExport => _unsupported();
227 bool get isDeferred => _unsupported();
228 LibraryMirror get sourceLibrary => _unsupported();
229 LibraryMirror get targetLibrary => _unsupported();
230 String get prefix => _unsupported();
231 List<CombinatorMirror> get combinators => _unsupported();
232 SourceLocation get location => _unsupported();
233 List<Object> get metadata => _unsupported();
234 }
235
236 abstract class CombinatorMirrorUnimpl implements CombinatorMirror {
237 List<String> get identifiers => _unsupported();
238 bool get isShow => _unsupported();
239 bool get isHide => _unsupported();
240 }
241
242 abstract class TypeMirrorUnimpl extends DeclarationMirrorUnimpl
243 implements TypeMirror {
244 bool get hasReflectedType => _unsupported();
245 Type get reflectedType => _unsupported();
246 List<TypeVariableMirror> get typeVariables => _unsupported();
247 List<TypeMirror> get typeArguments => _unsupported();
248 bool get isOriginalDeclaration => _unsupported();
249 TypeMirror get originalDeclaration => _unsupported();
250 bool isSubtypeOf(TypeMirror other) => _unsupported();
251 bool isAssignableTo(TypeMirror other) => _unsupported();
252 }
253
254 int _variableToImplicitAccessorAttributes(VariableMirror variableMirror) { 181 int _variableToImplicitAccessorAttributes(VariableMirror variableMirror) {
255 int attributes = 0; 182 int attributes = 0;
256 if (variableMirror.isPrivate) attributes |= constants.privateAttribute; 183 if (variableMirror.isPrivate) attributes |= constants.privateAttribute;
257 if (variableMirror.isStatic) attributes |= constants.staticAttribute; 184 if (variableMirror.isStatic) attributes |= constants.staticAttribute;
258 attributes |= constants.syntheticAttribute; 185 attributes |= constants.syntheticAttribute;
259 return attributes; 186 return attributes;
260 } 187 }
261 188
262 MethodMirror _variableToGetterMirror(VariableMirrorImpl variableMirror) { 189 MethodMirror _variableToGetterMirror(VariableMirrorImpl variableMirror) {
263 int descriptor = constants.getter; 190 int descriptor = constants.getter;
264 descriptor |= _variableToImplicitAccessorAttributes(variableMirror); 191 descriptor |= _variableToImplicitAccessorAttributes(variableMirror);
192 // TODO(eernst) clarify: Make sure it is the right `ownerIndex`: Write a test
193 // that compares owners in pre/post-transform code.
265 return new MethodMirrorImpl(variableMirror.simpleName, descriptor, 194 return new MethodMirrorImpl(variableMirror.simpleName, descriptor,
266 variableMirror.ownerIndex, variableMirror.reflectable, []); 195 variableMirror._ownerIndex, [], variableMirror._reflector, []);
267 } 196 }
268 197
198 // TODO(eernst) implement: Make this method take an `index` parameter which
199 // will be the index of the returned `MethodMirror`, such that the enclosed
200 // parameter mirror can get the right ownerIndex.
269 MethodMirror _variableToSetterMirror(VariableMirrorImpl variableMirror) { 201 MethodMirror _variableToSetterMirror(VariableMirrorImpl variableMirror) {
270 int descriptor = constants.setter; 202 int descriptor = constants.setter | constants.syntheticAttribute;
271 descriptor |= _variableToImplicitAccessorAttributes(variableMirror); 203 descriptor |= _variableToImplicitAccessorAttributes(variableMirror);
272 return new MethodMirrorImpl(variableMirror.simpleName + "=", descriptor, 204 int parameterDescriptor = constants.parameter | constants.syntheticAttribute;
273 variableMirror.ownerIndex, variableMirror.reflectable, []); 205 String name = variableMirror.simpleName + "=";
206 return new MethodMirrorImpl(name, descriptor, variableMirror._ownerIndex,
207 [ // TODO(eernst) clarify: Make sure it is the right `ownerIndex`: Write a
208 // test that compares owners in pre/post-transform code.
floitsch 2015/08/13 17:33:36 What's missing to write the test?
floitsch 2015/08/17 14:32:23 ?
eernst 2015/08/19 13:41:56 Oops, forgot that. Turned out to require more refa
209 new ParameterMirrorImpl(name, parameterDescriptor,
210 variableMirror._ownerIndex, variableMirror._reflector, [], null, -1) ],
floitsch 2015/08/13 17:33:36 long line.
eernst 2015/08/14 12:05:34 Fixed. Btw, doing the usual 'Reformat with Dart st
211 variableMirror._reflector, []);
274 } 212 }
275 213
276 class ClassMirrorImpl implements ClassMirror { 214 class ClassMirrorImpl extends _DataCaching implements ClassMirror {
277 ReflectorData _dataCache;
278 ReflectorData get _data {
279 if (_dataCache == null) {
280 _dataCache = data[_reflectable];
281 }
282 return _dataCache;
283 }
284
285 /// The reflector which represents the mirror system that this 215 /// The reflector which represents the mirror system that this
286 /// mirror belongs to. 216 /// mirror belongs to.
287 final ReflectableImpl _reflectable; 217 final ReflectableImpl _reflector;
288 218
289 /// The index of this mirror in the [ReflectorData.classMirrors] table. 219 /// The index of this mirror in the [ReflectorData.classMirrors] table.
290 /// Also this is the index of the Type of the reflected class in 220 /// Also this is the index of the Type of the reflected class in
291 /// [ReflectorData.types]. 221 /// [ReflectorData.types].
292 final int _classIndex; 222 final int _classIndex;
293 223
294 /// A list of the indices in [ReflectorData.memberMirrors] of the 224 /// A list of the indices in [ReflectorData.memberMirrors] of the
295 /// declarations of the reflected class. This includes method mirrors 225 /// declarations of the reflected class. This includes method mirrors
296 /// and variable mirrors and it directly corresponds to `declarations`. 226 /// and variable mirrors and it directly corresponds to `declarations`.
297 final List<int> _declarationIndices; 227 final List<int> _declarationIndices;
298 228
299 /// A list of the indices in [ReflectorData.memberMirrors] of the 229 /// A list of the indices in [ReflectorData.memberMirrors] of the
300 /// instance members of the reflected class, except that it includes 230 /// instance members of the reflected class, except that it includes
301 /// variable mirrors describing fields which must be converted to 231 /// variable mirrors describing fields which must be converted to
302 /// implicit getters and possibly implicit setters in order to 232 /// implicit getters and possibly implicit setters in order to
303 /// obtain the correct result for `instanceMembers`. 233 /// obtain the correct result for `instanceMembers`.
304 final List<int> _instanceMemberIndices; 234 final List<int> _instanceMemberIndices;
305 235
306 /// The index of the mirror of the superclass in the 236 /// The index of the mirror of the superclass in the
307 /// [ReflectorData.classMirrors] table. 237 /// [ReflectorData.classMirrors] table.
308 final int _superclassIndex; 238 final int _superclassIndex;
309 239
310 final String simpleName; 240 final String simpleName;
311 final String qualifiedName; 241 final String qualifiedName;
312 final List<Object> _metadata; 242 final List<Object> _metadata;
313 final Map<String, StaticGetter> getters; 243 final Map<String, _StaticGetter> getters;
314 final Map<String, StaticSetter> setters; 244 final Map<String, _StaticSetter> setters;
315 final Map<String, Function> constructors; 245 final Map<String, Function> constructors;
316 246
317 ClassMirrorImpl(this.simpleName, this.qualifiedName, this._classIndex, 247 ClassMirrorImpl(this.simpleName, this.qualifiedName, this._classIndex,
318 this._reflectable, this._declarationIndices, this._instanceMemberIndices, 248 this._reflector, this._declarationIndices, this._instanceMemberIndices,
319 this._superclassIndex, this.getters, this.setters, this.constructors, 249 this._superclassIndex, this.getters, this.setters, this.constructors,
320 metadata) 250 metadata)
321 : _metadata = (metadata == null) 251 : _metadata = (metadata == null)
322 ? null 252 ? null
323 : new UnmodifiableListView(metadata); 253 : new UnmodifiableListView(metadata);
324 254
325 ClassMirror get superclass { 255 ClassMirror get superclass {
326 if (_superclassIndex == null) return null; 256 if (_superclassIndex == null) return null;
327 if (_superclassIndex == -1) { 257 if (_superclassIndex == -1) {
328 throw new NoSuchCapabilityError( 258 throw new NoSuchCapabilityError(
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after
401 return superclass.isSubclassOf(other); 331 return superclass.isSubclassOf(other);
402 } 332 }
403 } 333 }
404 334
405 @override 335 @override
406 bool get hasReflectedType => true; 336 bool get hasReflectedType => true;
407 337
408 @override 338 @override
409 Object invoke(String memberName, List positionalArguments, 339 Object invoke(String memberName, List positionalArguments,
410 [Map<Symbol, dynamic> namedArguments]) { 340 [Map<Symbol, dynamic> namedArguments]) {
411 StaticGetter getter = getters[memberName]; 341 _StaticGetter getter = getters[memberName];
412 if (getter == null) { 342 if (getter == null) {
413 throw new NoSuchInvokeCapabilityError( 343 throw new NoSuchInvokeCapabilityError(
414 reflectedType, memberName, positionalArguments, namedArguments); 344 reflectedType, memberName, positionalArguments, namedArguments);
415 } 345 }
416 return Function.apply( 346 return Function.apply(
417 getters[memberName](), positionalArguments, namedArguments); 347 getters[memberName](), positionalArguments, namedArguments);
418 } 348 }
419 349
420 @override 350 @override
421 Object invokeGetter(String getterName) { 351 Object invokeGetter(String getterName) {
422 StaticGetter getter = getters[getterName]; 352 _StaticGetter getter = getters[getterName];
423 if (getter == null) { 353 if (getter == null) {
424 throw new NoSuchInvokeCapabilityError(reflectedType, getterName, [], {}); 354 throw new NoSuchInvokeCapabilityError(reflectedType, getterName, [], {});
425 } 355 }
426 return getter(); 356 return getter();
427 } 357 }
428 358
429 @override 359 @override
430 Object invokeSetter(String setterName, Object value) { 360 Object invokeSetter(String setterName, Object value) {
431 StaticSetter setter = setters[setterName]; 361 _StaticSetter setter = setters[setterName];
432 if (setter == null) { 362 if (setter == null) {
433 throw new NoSuchInvokeCapabilityError( 363 throw new NoSuchInvokeCapabilityError(
434 reflectedType, setterName, [value], {}); 364 reflectedType, setterName, [value], {});
435 } 365 }
436 return setter(value); 366 return setter(value);
437 } 367 }
438 368
439 // TODO(eernst) feature: Implement `isAssignableTo`. 369 // TODO(eernst) feature: Implement `isAssignableTo`.
440 @override 370 @override
441 bool isAssignableTo(TypeMirror other) => _unsupported(); 371 bool isAssignableTo(TypeMirror other) => _unsupported();
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
493 return getter; 423 return getter;
494 } 424 }
495 425
496 String toString() => "ClassMirrorImpl($qualifiedName)"; 426 String toString() => "ClassMirrorImpl($qualifiedName)";
497 427
498 // Because we take care to only ever create one instance for each 428 // Because we take care to only ever create one instance for each
499 // type/reflector-combination we can rely on the default `hashCode` and `==` 429 // type/reflector-combination we can rely on the default `hashCode` and `==`
500 // operations. 430 // operations.
501 } 431 }
502 432
503 abstract class TypeVariableMirrorUnimpl extends TypeMirrorUnimpl 433 class MethodMirrorImpl extends _DataCaching implements MethodMirror {
504 implements TypeVariableMirror {
505 TypeMirror get upperBound => _unsupported();
506 bool get isStatic => _unsupported();
507 bool operator ==(other) => _unsupported();
508 int get hashCode => _unsupported();
509 }
510
511 abstract class TypedefMirrorUnimpl extends TypeMirrorUnimpl
512 implements TypedefMirror {
513 FunctionTypeMirror get referent => _unsupported();
514 }
515
516 abstract class MethodMirrorUnimpl extends DeclarationMirrorUnimpl
517 implements MethodMirror {
518 TypeMirror get returnType => _unsupported();
519 String get source => _unsupported();
520 List<ParameterMirror> get parameters => _unsupported();
521 bool get isStatic => _unsupported();
522 bool get isAbstract => _unsupported();
523 bool get isSynthetic => _unsupported();
524 bool get isRegularMethod => _unsupported();
525 bool get isOperator => _unsupported();
526 bool get isGetter => _unsupported();
527 bool get isSetter => _unsupported();
528 bool get isConstructor => _unsupported();
529 String get constructorName => _unsupported();
530 bool get isConstConstructor => _unsupported();
531 bool get isGenerativeConstructor => _unsupported();
532 bool get isRedirectingConstructor => _unsupported();
533 bool get isFactoryConstructor => _unsupported();
534 bool operator ==(other) => _unsupported();
535 int get hashCode => _unsupported();
536 }
537
538 class MethodMirrorImpl implements MethodMirror {
539 /// An encoding of the attributes and kind of this mirror. 434 /// An encoding of the attributes and kind of this mirror.
540 final int _descriptor; 435 final int _descriptor;
541 436
542 /// The name of this method. Setters names will end in '='. 437 /// The name of this method. Setters names will end in '='.
543 final String _name; 438 final String _name;
544 439
545 /// The index of the [ClassMirror] of the owner of this method, 440 /// The index of the [ClassMirror] of the owner of this method,
546 final int _ownerIndex; 441 final int _ownerIndex;
547 442
443 /// The indices of the [ParameterMirror]s describing the formal parameters
444 /// of this method.
445 final List<int> _parameterIndices;
446
548 /// The [Reflectable] associated with this mirror. 447 /// The [Reflectable] associated with this mirror.
549 final ReflectableImpl _reflector; 448 final ReflectableImpl _reflector;
550 449
551 /// A cache of the metadata of the mirrored method. The empty list means 450 /// A cache of the metadata of the mirrored method. The empty list means
552 /// no metadata, null means that [_reflector] does not have 451 /// no metadata, null means that [_reflector] does not have
553 /// [metadataCapability]. 452 /// [metadataCapability].
554 final List<Object> _metadata; 453 final List<Object> _metadata;
555 454
556 MethodMirrorImpl(this._name, this._descriptor, this._ownerIndex, 455 MethodMirrorImpl(this._name, this._descriptor, this._ownerIndex,
557 this._reflector, List<Object> metadata) 456 this._parameterIndices, this._reflector, List<Object> metadata)
558 : _metadata = (metadata == null) 457 : _metadata = (metadata == null)
559 ? null 458 ? null
560 : new UnmodifiableListView(metadata); 459 : new UnmodifiableListView(metadata);
561 460
562 int get kind => constants.kindFromEncoding(_descriptor); 461 int get kind => constants.kindFromEncoding(_descriptor);
563 462
564 ClassMirror get owner => data[_reflector].classMirrors[_ownerIndex]; 463 ClassMirror get owner => _data.classMirrors[_ownerIndex];
565 464
566 @override 465 @override
567 String get constructorName => _name; 466 String get constructorName => _name;
568 467
569 @override 468 @override
570 bool get isAbstract => 0 != _descriptor & constants.abstractAttribute; 469 bool get isAbstract => 0 != _descriptor & constants.abstractAttribute;
571 470
572 @override 471 @override
573 bool get isConstConstructor => 0 != _descriptor & constants.constAttribute; 472 bool get isConstConstructor => 0 != _descriptor & constants.constAttribute;
574 473
(...skipping 28 matching lines...) Expand all
603 502
604 @override 503 @override
605 bool get isStatic => 0 != _descriptor & constants.staticAttribute; 504 bool get isStatic => 0 != _descriptor & constants.staticAttribute;
606 505
607 @override 506 @override
608 bool get isSynthetic => 0 != _descriptor & constants.syntheticAttribute; 507 bool get isSynthetic => 0 != _descriptor & constants.syntheticAttribute;
609 508
610 @override 509 @override
611 bool get isTopLevel => owner is LibraryMirror; 510 bool get isTopLevel => owner is LibraryMirror;
612 511
512 // It is allowed to return null.
613 @override 513 @override
614 SourceLocation get location => 514 SourceLocation get location => null;
615 throw new UnsupportedError("Location not supported");
616 515
617 @override 516 @override
618 List<Object> get metadata { 517 List<Object> get metadata {
619 if (_metadata == null) { 518 if (_metadata == null) {
620 throw new NoSuchCapabilityError( 519 throw new NoSuchCapabilityError(
621 "Requesting metadata of method $simpleName without capability"); 520 "Requesting metadata of method $simpleName without capability");
622 } 521 }
623 return _metadata; 522 return _metadata;
624 } 523 }
625 524
626 // TODO(sigurdm) feature: support `parameters`.
627 @override 525 @override
628 List<ParameterMirror> get parameters => throw new UnimplementedError(); 526 List<ParameterMirror> get parameters {
527 return _parameterIndices
528 .map((int parameterIndex) => _data.parameterMirrors[parameterIndex])
529 .toList();
530 }
629 531
630 @override 532 @override
631 String get qualifiedName => "${owner.qualifiedName}.$_name"; 533 String get qualifiedName => "${owner.qualifiedName}.$_name";
632 534
633 // TODO(sigurdm) feature: suport `returnType`. 535 // TODO(sigurdm) feature: suport `returnType`.
634 @override 536 @override
635 TypeMirror get returnType => throw new UnimplementedError(); 537 TypeMirror get returnType => throw new UnimplementedError();
636 538
637 @override 539 @override
638 String get simpleName => isConstructor 540 String get simpleName => isConstructor
639 ? (_name == '' ? "${owner.simpleName}" : "${owner.simpleName}.$_name") 541 ? (_name == '' ? "${owner.simpleName}" : "${owner.simpleName}.$_name")
640 : _name; 542 : _name;
641 543
642 @override 544 @override
643 String get source => null; 545 String get source => null;
644 546
645 @override 547 @override
646 String toString() => "MethodMirror($_name)"; 548 String toString() => "MethodMirror($_name)";
647 } 549 }
648 550
649 abstract class VariableMirrorUnimpl extends DeclarationMirrorUnimpl 551 abstract class VariableMirrorBase extends _DataCaching
650 implements VariableMirror { 552 implements VariableMirror {
651 TypeMirror get type => _unsupported(); 553 final String _name;
652 bool get isStatic => _unsupported(); 554 final int _descriptor;
653 bool get isFinal => _unsupported(); 555 final int _ownerIndex;
654 bool get isConst => _unsupported(); 556 final ReflectableImpl _reflector;
655 bool operator ==(other) => _unsupported();
656 int get hashCode => _unsupported();
657 }
658
659 class VariableMirrorImpl implements VariableMirror {
660 final int descriptor;
661 final String name;
662 final int ownerIndex;
663 final ReflectableImpl reflectable;
664 final List<Object> _metadata; 557 final List<Object> _metadata;
665 558
666 VariableMirrorImpl(this.name, this.descriptor, this.ownerIndex, 559 VariableMirrorBase(this._name, this._descriptor, this._ownerIndex,
667 this.reflectable, List<Object> metadata) 560 this._reflector, List<Object> metadata)
668 : _metadata = (metadata == null) 561 : _metadata = (metadata == null)
669 ? null 562 ? null : new UnmodifiableListView(metadata);
670 : new UnmodifiableListView(metadata);
671 563
672 int get kind => constants.kindFromEncoding(descriptor); 564 int get kind => constants.kindFromEncoding(_descriptor);
673
674 ClassMirror get owner => data[reflectable].classMirrors[ownerIndex];
675 565
676 @override 566 @override
677 String get qualifiedName => "${owner.qualifiedName}.$name"; 567 bool get isPrivate => 0 != _descriptor & constants.privateAttribute;
floitsch 2015/08/13 17:33:36 parenthesis and put 0 to the right.
eernst 2015/08/14 12:05:34 Done. Btw, that's a very old C-ish habit: `if (NU
678
679 @override
680 bool get isPrivate => 0 != descriptor & constants.privateAttribute;
681 568
682 @override 569 @override
683 bool get isTopLevel => owner is LibraryMirror; 570 bool get isTopLevel => owner is LibraryMirror;
684 571
685 @override 572 @override
686 SourceLocation get location => 573 bool get isFinal => 0 != _descriptor & constants.finalAttribute;
floitsch 2015/08/13 17:33:36 ditto. (and probably other locations).
eernst 2015/08/14 12:05:34 Done.
687 throw new UnsupportedError("Location not supported"); 574
575 // It is allowed to return null.
576 @override
577 SourceLocation get location => null;
688 578
689 @override 579 @override
690 List<Object> get metadata { 580 List<Object> get metadata {
691 if (_metadata == null) { 581 if (_metadata == null) {
692 throw new NoSuchCapabilityError( 582 throw new NoSuchCapabilityError(
693 "Requesting metadata of field $simpleName without capability"); 583 "Requesting metadata of field $simpleName without capability");
694 } 584 }
695 return _metadata; 585 return _metadata;
696 } 586 }
697 587
698 @override 588 @override
699 TypeMirror get type => _unsupported();
700
701 @override
702 bool get isStatic => 0 != descriptor & constants.staticAttribute;
703
704 @override
705 bool get isFinal => 0 != descriptor & constants.finalAttribute;
706
707 @override
708 bool get isConst => 0 != descriptor & constants.constAttribute;
709
710 @override
711 bool operator ==(other) => _unsupported(); 589 bool operator ==(other) => _unsupported();
712 590
713 @override 591 @override
714 int get hashCode => _unsupported(); 592 int get hashCode => _unsupported();
715 593
716 @override 594 @override
717 String get simpleName => name; 595 String get simpleName => _name;
718 } 596 }
719 597
720 abstract class ParameterMirrorUnimpl extends VariableMirrorUnimpl 598 class VariableMirrorImpl extends VariableMirrorBase {
721 implements ParameterMirror { 599 @override
600 ClassMirror get owner => _data.classMirrors[_ownerIndex];
601
602 @override
603 String get qualifiedName => "${owner.qualifiedName}.$_name";
604
605 @override
722 TypeMirror get type => _unsupported(); 606 TypeMirror get type => _unsupported();
723 bool get isOptional => _unsupported(); 607
724 bool get isNamed => _unsupported(); 608 @override
725 bool get hasDefaultValue => _unsupported(); 609 bool get isStatic => 0 != _descriptor & constants.staticAttribute;
726 Object get defaultValue => _unsupported(); 610
611 @override
612 bool get isConst => 0 != _descriptor & constants.constAttribute;
613
614 VariableMirrorImpl(String name, int descriptor, int ownerIndex,
615 ReflectableImpl reflectable, List<Object> metadata)
616 : super(name, descriptor, ownerIndex, reflectable, metadata);
727 } 617 }
728 618
729 abstract class SourceLocationUnimpl { 619 class ParameterMirrorImpl extends VariableMirrorBase
730 int get line => _unsupported(); 620 implements ParameterMirror {
731 int get column => _unsupported(); 621 final _classMirrorIndex;
732 Uri get sourceUri => _unsupported(); 622
623 @override
624 final defaultValue;
625
626 @override
627 bool get isStatic => 0 != _descriptor & constants.staticAttribute;
628
629 @override
630 bool get isConst => 0 != _descriptor & constants.constAttribute;
631
632 @override
633 bool get hasDefaultValue =>
634 0 != _descriptor & constants.hasDefaultValueAttribute;
635
636 @override
637 bool get isOptional => 0 != _descriptor & constants.optionalAttribute;
638
639 @override
640 bool get isNamed => 0 != _descriptor & constants.namedAttribute;
641
642 bool get _isDynamic => 0 != _descriptor & constants.dynamicAttribute;
643
644 bool get _isClassType => 0 != _descriptor & constants.classTypeAttribute;
645
646 // TODO(eernst) clarify: A parameter cannot be accessed using dot
647 // notation, and hence it has no qualified name. So is the following
648 // behavior correct?
649 @override
650 String get qualifiedName {
651 throw new NoSuchCapabilityError(
652 "Attempting to get the `qualifiedName` of the parameter $_name.");
653 }
654
655 @override
656 MethodMirror get owner => _data.memberMirrors[_ownerIndex];
657
658 @override
659 TypeMirror get type {
660 if (_isDynamic) return new DynamicMirrorImpl();
661 if (_isClassType) {
662 if (_classMirrorIndex == -1) {
663 throw new NoSuchCapabilityError(
664 "Attempting to get a class mirror for an un-marked class (type of $_ name)");
floitsch 2015/08/13 17:33:36 long line.
eernst 2015/08/14 12:05:34 Funny, 'Reformat with Dart style' does not output
665 }
666 return _data.classMirrors[_classMirrorIndex];
667 }
668 return _unsupported();
669 }
670
671 ParameterMirrorImpl(String name, int descriptor, int ownerIndex,
672 ReflectableImpl reflectable, List<Object> metadata, this.defaultValue,
673 this._classMirrorIndex)
674 : super(name, descriptor, ownerIndex, reflectable, metadata);
675 }
676
677 class DynamicMirrorImpl implements TypeMirror {
678
679 @override
680 bool get isPrivate => false;
681
682 @override
683 bool get isTopLevel => true;
684
685 // TODO(eernst) implement: test what 'dart:mirrors' does, then do the same.
686 @override
687 bool get isOriginalDeclaration => true;
688
689 @override
690 bool get hasReflectedType => true;
691
692 @override
693 Type get reflectedType => dynamic;
694
695 @override
696 String get simpleName => "dynamic";
697
698 // TODO(eernst) implement: do as in 'dart:mirrors'.
699 @override
700 List<TypeVariableMirror> get typeVariables => <TypeVariableMirror>[];
701
702 @override
703 List<TypeMirror> get typeArguments => <TypeMirror>[];
704
705 // TODO(eernst) implement: do as in 'dart:mirrors'.
706 @override
707 TypeMirror get originalDeclaration => null;
708
709 // It is allowed to return null.
710 @override
711 SourceLocation get location => null;
712
713 @override
714 bool isSubtypeOf(TypeMirror other) => true;
715
716 @override
717 bool isAssignableTo(TypeMirror other) => true;
718
719 // TODO(eernst) implement: do as 'dart:mirrors' does.
720 @override
721 DeclarationMirror get owner => null;
722
723 @override
724 String get qualifiedName => simpleName;
725
726 @override
727 List<Object> get metadata => <Object>[];
733 } 728 }
734 729
735 abstract class ReflectableImpl extends ReflectableBase 730 abstract class ReflectableImpl extends ReflectableBase
736 implements ReflectableInterface { 731 implements ReflectableInterface {
737 732
738 /// Const constructor, to enable usage as metadata, allowing for varargs 733 /// Const constructor, to enable usage as metadata, allowing for varargs
739 /// style invocation with up to ten arguments. 734 /// style invocation with up to ten arguments.
740 const ReflectableImpl([ReflectCapability cap0 = null, 735 const ReflectableImpl([ReflectCapability cap0 = null,
741 ReflectCapability cap1 = null, ReflectCapability cap2 = null, 736 ReflectCapability cap1 = null, ReflectCapability cap2 = null,
742 ReflectCapability cap3 = null, ReflectCapability cap4 = null, 737 ReflectCapability cap3 = null, ReflectCapability cap4 = null,
743 ReflectCapability cap5 = null, ReflectCapability cap6 = null, 738 ReflectCapability cap5 = null, ReflectCapability cap6 = null,
744 ReflectCapability cap7 = null, ReflectCapability cap8 = null, 739 ReflectCapability cap7 = null, ReflectCapability cap8 = null,
745 ReflectCapability cap9 = null]) 740 ReflectCapability cap9 = null])
746 : super(cap0, cap1, cap2, cap3, cap4, cap5, cap6, cap7, cap8, cap9); 741 : super(cap0, cap1, cap2, cap3, cap4, cap5, cap6, cap7, cap8, cap9);
747 742
748 const ReflectableImpl.fromList(List<ReflectCapability> capabilities) 743 const ReflectableImpl.fromList(List<ReflectCapability> capabilities)
749 : super.fromList(capabilities); 744 : super.fromList(capabilities);
750 745
751 @override 746 @override
752 InstanceMirror reflect(Object reflectee) { 747 InstanceMirror reflect(Object reflectee) {
753 return new InstanceMirrorImpl(reflectee, this); 748 return new _InstanceMirrorImpl(reflectee, this);
754 } 749 }
755 750
756 @override 751 @override
757 bool canReflect(Object reflectee) { 752 bool canReflect(Object reflectee) {
758 return data[this].classMirrorForType(reflectee.runtimeType) != null; 753 return data[this].classMirrorForType(reflectee.runtimeType) != null;
759 } 754 }
760 755
761 @override 756 @override
762 ClassMirror reflectType(Type type) { 757 ClassMirror reflectType(Type type) {
763 ClassMirror result = data[this].classMirrorForType(type); 758 ClassMirror result = data[this].classMirrorForType(type);
(...skipping 13 matching lines...) Expand all
777 LibraryMirror findLibrary(String library) => _unsupported(); 772 LibraryMirror findLibrary(String library) => _unsupported();
778 773
779 @override 774 @override
780 Map<Uri, LibraryMirror> get libraries => _unsupported(); 775 Map<Uri, LibraryMirror> get libraries => _unsupported();
781 776
782 @override 777 @override
783 Iterable<ClassMirror> get annotatedClasses { 778 Iterable<ClassMirror> get annotatedClasses {
784 return new UnmodifiableListView<ClassMirror>(data[this].classMirrors); 779 return new UnmodifiableListView<ClassMirror>(data[this].classMirrors);
785 } 780 }
786 } 781 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698