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

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: Merging with code from Sigurd, caused several adjustments 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 =
134 "Reflectable has not been initialized. " 103 throw new StateError("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 abstract class _DataCaching {
108 // TODO(eernst) clarify: When we have some substantial pieces of code using
109 // reflectable, perform some experiments to detect how useful it is to have
110 // this kind of caching.
111
139 ReflectorData _dataCache; 112 ReflectorData _dataCache;
113 ReflectableImpl get _reflector;
114
140 ReflectorData get _data { 115 ReflectorData get _data {
141 if (_dataCache == null) { 116 if (_dataCache == null) {
142 _dataCache = data[_reflectable]; 117 _dataCache = data[_reflector];
143 } 118 }
144 return _dataCache; 119 return _dataCache;
145 } 120 }
121 }
146 122
147 final ReflectableImpl _reflectable; 123 class _InstanceMirrorImpl extends _DataCaching implements InstanceMirror {
148 124 final ReflectableImpl _reflector;
149 final Object reflectee; 125 final Object reflectee;
150 126
151 InstanceMirrorImpl(this.reflectee, this._reflectable) { 127 _InstanceMirrorImpl(this.reflectee, this._reflector) {
152 _type = _data.classMirrorForType(reflectee.runtimeType); 128 _type = _data.classMirrorForType(reflectee.runtimeType);
153 if (_type == null) { 129 if (_type == null) {
154 throw new NoSuchCapabilityError( 130 throw new NoSuchCapabilityError(
155 "Reflecting on un-marked type ${reflectee.runtimeType}"); 131 "Reflecting on un-marked type ${reflectee.runtimeType}");
156 } 132 }
157 } 133 }
158 134
159 ClassMirror _type; 135 ClassMirror _type;
160 136
161 ClassMirror get type => _type; 137 ClassMirror get type => _type;
162 138
163 Object invoke(String methodName, List<Object> positionalArguments, 139 Object invoke(String methodName, List<Object> positionalArguments,
164 [Map<Symbol, Object> namedArguments]) { 140 [Map<Symbol, Object> namedArguments]) {
165 Function methodTearer = _data.getters[methodName]; 141 Function methodTearer = _data.getters[methodName];
166 if (methodTearer != null) { 142 if (methodTearer != null) {
167 return Function.apply( 143 return Function.apply(
168 methodTearer(reflectee), positionalArguments, namedArguments); 144 methodTearer(reflectee), positionalArguments, namedArguments);
169 } 145 }
170 throw new NoSuchInvokeCapabilityError( 146 throw new NoSuchInvokeCapabilityError(
171 reflectee, methodName, positionalArguments, namedArguments); 147 reflectee, methodName, positionalArguments, namedArguments);
172 } 148 }
173 149
174 bool get hasReflectee => true; 150 bool get hasReflectee => true;
175 151
176 bool operator ==(other) { 152 bool operator ==(other) {
177 return other is InstanceMirrorImpl && 153 return other is _InstanceMirrorImpl &&
178 other._reflectable == _reflectable && 154 other._reflector == _reflector &&
179 other.reflectee == reflectee; 155 other.reflectee == reflectee;
180 } 156 }
181 157
182 int get hashCode => reflectee.hashCode ^ _reflectable.hashCode; 158 int get hashCode => reflectee.hashCode ^ _reflector.hashCode;
183 159
184 delegate(Invocation invocation) => _unsupported(); 160 delegate(Invocation invocation) => _unsupported();
185 161
186 @override 162 @override
187 Object invokeGetter(String getterName) { 163 Object invokeGetter(String getterName) {
188 Function getter = _data.getters[getterName]; 164 Function getter = _data.getters[getterName];
189 if (getter != null) { 165 if (getter != null) {
190 return getter(reflectee); 166 return getter(reflectee);
191 } 167 }
192 throw new NoSuchInvokeCapabilityError(reflectee, getterName, [], {}); 168 throw new NoSuchInvokeCapabilityError(reflectee, getterName, [], {});
193 } 169 }
194 170
195 @override 171 @override
196 Object invokeSetter(String setterName, Object value) { 172 Object invokeSetter(String setterName, Object value) {
197 if (setterName.substring(setterName.length - 1) != "=") { 173 if (setterName.substring(setterName.length - 1) != "=") {
198 setterName += "="; 174 setterName += "=";
199 } 175 }
200 Function setter = _data.setters[setterName]; 176 Function setter = _data.setters[setterName];
201 if (setter != null) { 177 if (setter != null) {
202 return setter(reflectee, value); 178 return setter(reflectee, value);
203 } 179 }
204 throw new NoSuchInvokeCapabilityError(reflectee, setterName, [value], {}); 180 throw new NoSuchInvokeCapabilityError(reflectee, setterName, [value], {});
205 } 181 }
206 } 182 }
207 183
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) { 184 int _variableToImplicitAccessorAttributes(VariableMirror variableMirror) {
255 int attributes = 0; 185 int attributes = 0;
256 if (variableMirror.isPrivate) attributes |= constants.privateAttribute; 186 if (variableMirror.isPrivate) attributes |= constants.privateAttribute;
257 if (variableMirror.isStatic) attributes |= constants.staticAttribute; 187 if (variableMirror.isStatic) attributes |= constants.staticAttribute;
258 attributes |= constants.syntheticAttribute; 188 attributes |= constants.syntheticAttribute;
259 return attributes; 189 return attributes;
260 } 190 }
261 191
262 MethodMirror _variableToGetterMirror(VariableMirrorImpl variableMirror) { 192 MethodMirror _variableToGetterMirror(VariableMirrorImpl variableMirror) {
263 int descriptor = constants.getter; 193 int descriptor = constants.getter;
264 descriptor |= _variableToImplicitAccessorAttributes(variableMirror); 194 descriptor |= _variableToImplicitAccessorAttributes(variableMirror);
195 // TODO(eernst) clarify: Make sure it is the right `ownerIndex`: Write a test
196 // that compares owners in pre/post-transform code.
265 return new MethodMirrorImpl(variableMirror.simpleName, descriptor, 197 return new MethodMirrorImpl(variableMirror.simpleName, descriptor,
266 variableMirror.ownerIndex, variableMirror.reflectable, []); 198 variableMirror._ownerIndex, [], variableMirror._reflector, []);
267 } 199 }
268 200
201 // TODO(eernst) implement: Make this method take an `index` parameter which
202 // will be the index of the returned `MethodMirror`, such that the enclosed
203 // parameter mirror can get the right ownerIndex.
269 MethodMirror _variableToSetterMirror(VariableMirrorImpl variableMirror) { 204 MethodMirror _variableToSetterMirror(VariableMirrorImpl variableMirror) {
270 int descriptor = constants.setter; 205 int descriptor = constants.setter | constants.syntheticAttribute;
271 descriptor |= _variableToImplicitAccessorAttributes(variableMirror); 206 descriptor |= _variableToImplicitAccessorAttributes(variableMirror);
272 return new MethodMirrorImpl(variableMirror.simpleName + "=", descriptor, 207 int parameterDescriptor = constants.parameter | constants.syntheticAttribute;
273 variableMirror.ownerIndex, variableMirror.reflectable, []); 208 String name = variableMirror.simpleName + "=";
209 return new MethodMirrorImpl(
210 name,
211 descriptor,
212 variableMirror._ownerIndex,
213 [
214 // TODO(eernst) clarify: Make sure it is the right `ownerIndex`: Write a
215 // test that compares owners in pre/post-transform code.
216 new ParameterMirrorImpl(
217 name,
218 parameterDescriptor,
219 variableMirror._ownerIndex,
220 variableMirror._reflector,
221 variableMirror.metadata,
222 null,
223 -1)
224 ],
225 variableMirror._reflector,
226 []);
274 } 227 }
275 228
276 class ClassMirrorImpl implements ClassMirror { 229 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 230 /// The reflector which represents the mirror system that this
286 /// mirror belongs to. 231 /// mirror belongs to.
287 final ReflectableImpl _reflectable; 232 final ReflectableImpl _reflector;
288 233
289 /// The index of this mirror in the [ReflectorData.classMirrors] table. 234 /// The index of this mirror in the [ReflectorData.classMirrors] table.
290 /// Also this is the index of the Type of the reflected class in 235 /// Also this is the index of the Type of the reflected class in
291 /// [ReflectorData.types]. 236 /// [ReflectorData.types].
292 final int _classIndex; 237 final int _classIndex;
293 238
294 /// A list of the indices in [ReflectorData.memberMirrors] of the 239 /// A list of the indices in [ReflectorData.memberMirrors] of the
295 /// declarations of the reflected class. This includes method mirrors 240 /// declarations of the reflected class. This includes method mirrors
296 /// and variable mirrors and it directly corresponds to `declarations`. 241 /// and variable mirrors and it directly corresponds to `declarations`.
297 final List<int> _declarationIndices; 242 final List<int> _declarationIndices;
298 243
299 /// A list of the indices in [ReflectorData.memberMirrors] of the 244 /// A list of the indices in [ReflectorData.memberMirrors] of the
300 /// instance members of the reflected class, except that it includes 245 /// instance members of the reflected class, except that it includes
301 /// variable mirrors describing fields which must be converted to 246 /// variable mirrors describing fields which must be converted to
302 /// implicit getters and possibly implicit setters in order to 247 /// implicit getters and possibly implicit setters in order to
303 /// obtain the correct result for `instanceMembers`. 248 /// obtain the correct result for `instanceMembers`.
304 final List<int> _instanceMemberIndices; 249 final List<int> _instanceMemberIndices;
305 250
306 /// The index of the mirror of the superclass in the 251 /// The index of the mirror of the superclass in the
307 /// [ReflectorData.classMirrors] table. 252 /// [ReflectorData.classMirrors] table.
308 final int _superclassIndex; 253 final int _superclassIndex;
309 254
310 final String simpleName; 255 final String simpleName;
311 final String qualifiedName; 256 final String qualifiedName;
312 final List<Object> _metadata; 257 final List<Object> _metadata;
313 final Map<String, StaticGetter> getters; 258 final Map<String, _StaticGetter> getters;
314 final Map<String, StaticSetter> setters; 259 final Map<String, _StaticSetter> setters;
315 final Map<String, Function> constructors; 260 final Map<String, Function> constructors;
316 261
317 ClassMirrorImpl(this.simpleName, this.qualifiedName, this._classIndex, 262 ClassMirrorImpl(
318 this._reflectable, this._declarationIndices, this._instanceMemberIndices, 263 this.simpleName,
319 this._superclassIndex, this.getters, this.setters, this.constructors, 264 this.qualifiedName,
265 this._classIndex,
266 this._reflector,
267 this._declarationIndices,
268 this._instanceMemberIndices,
269 this._superclassIndex,
270 this.getters,
271 this.setters,
272 this.constructors,
320 metadata) 273 metadata)
321 : _metadata = (metadata == null) 274 : _metadata =
322 ? null 275 (metadata == null) ? null : new UnmodifiableListView(metadata);
323 : new UnmodifiableListView(metadata);
324 276
325 ClassMirror get superclass { 277 ClassMirror get superclass {
326 if (_superclassIndex == null) return null; 278 if (_superclassIndex == null) return null;
327 if (_superclassIndex == -1) { 279 if (_superclassIndex == -1) {
328 throw new NoSuchCapabilityError( 280 throw new NoSuchCapabilityError(
329 "Requesting mirror on un-marked class, superclass of $simpleName"); 281 "Requesting mirror on un-marked class, superclass of $simpleName");
330 } 282 }
331 return _data.classMirrors[_superclassIndex]; 283 return _data.classMirrors[_superclassIndex];
332 } 284 }
333 285
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after
401 return superclass.isSubclassOf(other); 353 return superclass.isSubclassOf(other);
402 } 354 }
403 } 355 }
404 356
405 @override 357 @override
406 bool get hasReflectedType => true; 358 bool get hasReflectedType => true;
407 359
408 @override 360 @override
409 Object invoke(String memberName, List positionalArguments, 361 Object invoke(String memberName, List positionalArguments,
410 [Map<Symbol, dynamic> namedArguments]) { 362 [Map<Symbol, dynamic> namedArguments]) {
411 StaticGetter getter = getters[memberName]; 363 _StaticGetter getter = getters[memberName];
412 if (getter == null) { 364 if (getter == null) {
413 throw new NoSuchInvokeCapabilityError( 365 throw new NoSuchInvokeCapabilityError(
414 reflectedType, memberName, positionalArguments, namedArguments); 366 reflectedType, memberName, positionalArguments, namedArguments);
415 } 367 }
416 return Function.apply( 368 return Function.apply(
417 getters[memberName](), positionalArguments, namedArguments); 369 getters[memberName](), positionalArguments, namedArguments);
418 } 370 }
419 371
420 @override 372 @override
421 Object invokeGetter(String getterName) { 373 Object invokeGetter(String getterName) {
422 StaticGetter getter = getters[getterName]; 374 _StaticGetter getter = getters[getterName];
423 if (getter == null) { 375 if (getter == null) {
424 throw new NoSuchInvokeCapabilityError(reflectedType, getterName, [], {}); 376 throw new NoSuchInvokeCapabilityError(reflectedType, getterName, [], {});
425 } 377 }
426 return getter(); 378 return getter();
427 } 379 }
428 380
429 @override 381 @override
430 Object invokeSetter(String setterName, Object value) { 382 Object invokeSetter(String setterName, Object value) {
431 StaticSetter setter = setters[setterName]; 383 _StaticSetter setter = setters[setterName];
432 if (setter == null) { 384 if (setter == null) {
433 throw new NoSuchInvokeCapabilityError( 385 throw new NoSuchInvokeCapabilityError(
434 reflectedType, setterName, [value], {}); 386 reflectedType, setterName, [value], {});
435 } 387 }
436 return setter(value); 388 return setter(value);
437 } 389 }
438 390
439 // TODO(eernst) feature: Implement `isAssignableTo`. 391 // TODO(eernst) feature: Implement `isAssignableTo`.
440 @override 392 @override
441 bool isAssignableTo(TypeMirror other) => _unsupported(); 393 bool isAssignableTo(TypeMirror other) => _unsupported();
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
493 return getter; 445 return getter;
494 } 446 }
495 447
496 String toString() => "ClassMirrorImpl($qualifiedName)"; 448 String toString() => "ClassMirrorImpl($qualifiedName)";
497 449
498 // Because we take care to only ever create one instance for each 450 // Because we take care to only ever create one instance for each
499 // type/reflector-combination we can rely on the default `hashCode` and `==` 451 // type/reflector-combination we can rely on the default `hashCode` and `==`
500 // operations. 452 // operations.
501 } 453 }
502 454
503 abstract class TypeVariableMirrorUnimpl extends TypeMirrorUnimpl 455 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. 456 /// An encoding of the attributes and kind of this mirror.
540 final int _descriptor; 457 final int _descriptor;
541 458
542 /// The name of this method. Setters names will end in '='. 459 /// The name of this method. Setters names will end in '='.
543 final String _name; 460 final String _name;
544 461
545 /// The index of the [ClassMirror] of the owner of this method, 462 /// The index of the [ClassMirror] of the owner of this method,
546 final int _ownerIndex; 463 final int _ownerIndex;
547 464
465 /// The indices of the [ParameterMirror]s describing the formal parameters
466 /// of this method.
467 final List<int> _parameterIndices;
468
548 /// The [Reflectable] associated with this mirror. 469 /// The [Reflectable] associated with this mirror.
549 final ReflectableImpl _reflector; 470 final ReflectableImpl _reflector;
550 471
551 /// A cache of the metadata of the mirrored method. The empty list means 472 /// A cache of the metadata of the mirrored method. The empty list means
552 /// no metadata, null means that [_reflector] does not have 473 /// no metadata, null means that [_reflector] does not have
553 /// [metadataCapability]. 474 /// [metadataCapability].
554 final List<Object> _metadata; 475 final List<Object> _metadata;
555 476
556 MethodMirrorImpl(this._name, this._descriptor, this._ownerIndex, 477 MethodMirrorImpl(this._name, this._descriptor, this._ownerIndex,
557 this._reflector, List<Object> metadata) 478 this._parameterIndices, this._reflector, List<Object> metadata)
558 : _metadata = (metadata == null) 479 : _metadata =
559 ? null 480 (metadata == null) ? null : new UnmodifiableListView(metadata);
560 : new UnmodifiableListView(metadata);
561 481
562 int get kind => constants.kindFromEncoding(_descriptor); 482 int get kind => constants.kindFromEncoding(_descriptor);
563 483
564 ClassMirror get owner => data[_reflector].classMirrors[_ownerIndex]; 484 ClassMirror get owner => _data.classMirrors[_ownerIndex];
565 485
566 @override 486 @override
567 String get constructorName => _name; 487 String get constructorName => _name;
568 488
569 @override 489 @override
570 bool get isAbstract => 0 != _descriptor & constants.abstractAttribute; 490 bool get isAbstract => (_descriptor & constants.abstractAttribute != 0);
571 491
572 @override 492 @override
573 bool get isConstConstructor => 0 != _descriptor & constants.constAttribute; 493 bool get isConstConstructor => (_descriptor & constants.constAttribute != 0);
574 494
575 @override 495 @override
576 bool get isConstructor => isFactoryConstructor || isGenerativeConstructor; 496 bool get isConstructor => isFactoryConstructor || isGenerativeConstructor;
577 497
578 @override 498 @override
579 bool get isFactoryConstructor => kind == constants.factoryConstructor; 499 bool get isFactoryConstructor => kind == constants.factoryConstructor;
580 500
581 @override 501 @override
582 bool get isGenerativeConstructor => kind == constants.generativeConstructor; 502 bool get isGenerativeConstructor => kind == constants.generativeConstructor;
583 503
584 @override 504 @override
585 bool get isGetter => kind == constants.getter; 505 bool get isGetter => kind == constants.getter;
586 506
587 @override 507 @override
588 bool get isOperator => isRegularMethod && 508 bool get isOperator => isRegularMethod &&
589 ["+", "-", "*", "/", "[", "<", ">", "=", "~", "%"].contains(_name[0]); 509 ["+", "-", "*", "/", "[", "<", ">", "=", "~", "%"].contains(_name[0]);
590 510
591 @override 511 @override
592 bool get isPrivate => 0 != _descriptor & constants.privateAttribute; 512 bool get isPrivate => (_descriptor & constants.privateAttribute != 0);
593 513
594 @override 514 @override
595 bool get isRedirectingConstructor => 515 bool get isRedirectingConstructor =>
596 0 != _descriptor & constants.redirectingConstructorAttribute; 516 (_descriptor & constants.redirectingConstructorAttribute != 0);
597 517
598 @override 518 @override
599 bool get isRegularMethod => kind == constants.method; 519 bool get isRegularMethod => kind == constants.method;
600 520
601 @override 521 @override
602 bool get isSetter => kind == constants.setter; 522 bool get isSetter => kind == constants.setter;
603 523
604 @override 524 @override
605 bool get isStatic => 0 != _descriptor & constants.staticAttribute; 525 bool get isStatic => (_descriptor & constants.staticAttribute != 0);
606 526
607 @override 527 @override
608 bool get isSynthetic => 0 != _descriptor & constants.syntheticAttribute; 528 bool get isSynthetic => (_descriptor & constants.syntheticAttribute != 0);
609 529
610 @override 530 @override
611 bool get isTopLevel => owner is LibraryMirror; 531 bool get isTopLevel => owner is LibraryMirror;
612 532
533 // It is allowed to return null.
613 @override 534 @override
614 SourceLocation get location => 535 SourceLocation get location => null;
615 throw new UnsupportedError("Location not supported");
616 536
617 @override 537 @override
618 List<Object> get metadata { 538 List<Object> get metadata {
619 if (_metadata == null) { 539 if (_metadata == null) {
620 throw new NoSuchCapabilityError( 540 throw new NoSuchCapabilityError(
621 "Requesting metadata of method $simpleName without capability"); 541 "Requesting metadata of method $simpleName without capability");
622 } 542 }
623 return _metadata; 543 return _metadata;
624 } 544 }
625 545
626 // TODO(sigurdm) feature: support `parameters`.
627 @override 546 @override
628 List<ParameterMirror> get parameters => throw new UnimplementedError(); 547 List<ParameterMirror> get parameters {
548 return _parameterIndices
549 .map((int parameterIndex) => _data.parameterMirrors[parameterIndex])
550 .toList();
551 }
629 552
630 @override 553 @override
631 String get qualifiedName => "${owner.qualifiedName}.$_name"; 554 String get qualifiedName => "${owner.qualifiedName}.$_name";
632 555
633 // TODO(sigurdm) feature: suport `returnType`. 556 // TODO(sigurdm) feature: suport `returnType`.
634 @override 557 @override
635 TypeMirror get returnType => throw new UnimplementedError(); 558 TypeMirror get returnType => throw new UnimplementedError();
636 559
637 @override 560 @override
638 String get simpleName => isConstructor 561 String get simpleName => isConstructor
639 ? (_name == '' ? "${owner.simpleName}" : "${owner.simpleName}.$_name") 562 ? (_name == '' ? "${owner.simpleName}" : "${owner.simpleName}.$_name")
640 : _name; 563 : _name;
641 564
642 @override 565 @override
643 String get source => null; 566 String get source => null;
644 567
645 @override 568 @override
646 String toString() => "MethodMirror($_name)"; 569 String toString() => "MethodMirror($_name)";
647 } 570 }
648 571
649 abstract class VariableMirrorUnimpl extends DeclarationMirrorUnimpl 572 abstract class VariableMirrorBase extends _DataCaching
650 implements VariableMirror { 573 implements VariableMirror {
651 TypeMirror get type => _unsupported(); 574 final String _name;
652 bool get isStatic => _unsupported(); 575 final int _descriptor;
653 bool get isFinal => _unsupported(); 576 final int _ownerIndex;
654 bool get isConst => _unsupported(); 577 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; 578 final List<Object> _metadata;
665 579
666 VariableMirrorImpl(this.name, this.descriptor, this.ownerIndex, 580 VariableMirrorBase(this._name, this._descriptor, this._ownerIndex,
667 this.reflectable, List<Object> metadata) 581 this._reflector, List<Object> metadata)
668 : _metadata = (metadata == null) 582 : _metadata =
669 ? null 583 (metadata == null) ? null : new UnmodifiableListView(metadata);
670 : new UnmodifiableListView(metadata);
671 584
672 int get kind => constants.kindFromEncoding(descriptor); 585 int get kind => constants.kindFromEncoding(_descriptor);
673
674 ClassMirror get owner => data[reflectable].classMirrors[ownerIndex];
675 586
676 @override 587 @override
677 String get qualifiedName => "${owner.qualifiedName}.$name"; 588 bool get isPrivate => (_descriptor & constants.privateAttribute != 0);
678
679 @override
680 bool get isPrivate => 0 != descriptor & constants.privateAttribute;
681 589
682 @override 590 @override
683 bool get isTopLevel => owner is LibraryMirror; 591 bool get isTopLevel => owner is LibraryMirror;
684 592
685 @override 593 @override
686 SourceLocation get location => 594 bool get isFinal => (_descriptor & constants.finalAttribute != 0);
687 throw new UnsupportedError("Location not supported"); 595
596 // It is allowed to return null.
597 @override
598 SourceLocation get location => null;
688 599
689 @override 600 @override
690 List<Object> get metadata { 601 List<Object> get metadata {
691 if (_metadata == null) { 602 if (_metadata == null) {
692 throw new NoSuchCapabilityError( 603 throw new NoSuchCapabilityError(
693 "Requesting metadata of field $simpleName without capability"); 604 "Requesting metadata of field $simpleName without capability");
694 } 605 }
695 return _metadata; 606 return _metadata;
696 } 607 }
697 608
698 @override 609 @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(); 610 bool operator ==(other) => _unsupported();
712 611
713 @override 612 @override
714 int get hashCode => _unsupported(); 613 int get hashCode => _unsupported();
715 614
716 @override 615 @override
717 String get simpleName => name; 616 String get simpleName => _name;
718 } 617 }
719 618
720 abstract class ParameterMirrorUnimpl extends VariableMirrorUnimpl 619 class VariableMirrorImpl extends VariableMirrorBase {
721 implements ParameterMirror { 620 @override
621 ClassMirror get owner => _data.classMirrors[_ownerIndex];
622
623 @override
624 String get qualifiedName => "${owner.qualifiedName}.$_name";
625
626 @override
722 TypeMirror get type => _unsupported(); 627 TypeMirror get type => _unsupported();
723 bool get isOptional => _unsupported(); 628
724 bool get isNamed => _unsupported(); 629 @override
725 bool get hasDefaultValue => _unsupported(); 630 bool get isStatic => (_descriptor & constants.staticAttribute != 0);
726 Object get defaultValue => _unsupported(); 631
632 @override
633 bool get isConst => (_descriptor & constants.constAttribute != 0);
634
635 VariableMirrorImpl(String name, int descriptor, int ownerIndex,
636 ReflectableImpl reflectable, List<Object> metadata)
637 : super(name, descriptor, ownerIndex, reflectable, metadata);
727 } 638 }
728 639
729 abstract class SourceLocationUnimpl { 640 class ParameterMirrorImpl extends VariableMirrorBase
730 int get line => _unsupported(); 641 implements ParameterMirror {
731 int get column => _unsupported(); 642 final _classMirrorIndex;
732 Uri get sourceUri => _unsupported(); 643
644 @override
645 final defaultValue;
646
647 @override
648 bool get isStatic => (_descriptor & constants.staticAttribute != 0);
649
650 @override
651 bool get isConst => (_descriptor & constants.constAttribute != 0);
652
653 @override
654 bool get hasDefaultValue =>
655 (_descriptor & constants.hasDefaultValueAttribute != 0);
656
657 @override
658 bool get isOptional => (_descriptor & constants.optionalAttribute != 0);
659
660 @override
661 bool get isNamed => (_descriptor & constants.namedAttribute != 0);
662
663 bool get _isDynamic => (_descriptor & constants.dynamicAttribute != 0);
664
665 bool get _isClassType => (_descriptor & constants.classTypeAttribute != 0);
666
667 // TODO(eernst) clarify: A parameter cannot be accessed using dot
668 // notation, and hence it has no qualified name. So is the following
669 // behavior correct?
670 @override
671 String get qualifiedName {
672 throw new NoSuchCapabilityError(
673 "Attempting to get the `qualifiedName` of the parameter $_name.");
674 }
675
676 @override
677 MethodMirror get owner => _data.memberMirrors[_ownerIndex];
678
679 @override
680 TypeMirror get type {
681 if (_isDynamic) return new DynamicMirrorImpl();
682 if (_isClassType) {
683 if (_classMirrorIndex == -1) {
684 throw new NoSuchCapabilityError(
685 "Attempt to get class mirror for un-marked class (type of $_name)");
686 }
687 return _data.classMirrors[_classMirrorIndex];
688 }
689 return _unsupported();
690 }
691
692 ParameterMirrorImpl(
693 String name,
694 int descriptor,
695 int ownerIndex,
696 ReflectableImpl reflectable,
697 List<Object> metadata,
698 this.defaultValue,
699 this._classMirrorIndex)
700 : super(name, descriptor, ownerIndex, reflectable, metadata);
701 }
702
703 class DynamicMirrorImpl implements TypeMirror {
704 @override
705 bool get isPrivate => false;
706
707 @override
708 bool get isTopLevel => true;
709
710 // TODO(eernst) implement: test what 'dart:mirrors' does, then do the same.
711 @override
712 bool get isOriginalDeclaration => true;
713
714 @override
715 bool get hasReflectedType => true;
716
717 @override
718 Type get reflectedType => dynamic;
719
720 @override
721 String get simpleName => "dynamic";
722
723 // TODO(eernst) implement: do as in 'dart:mirrors'.
724 @override
725 List<TypeVariableMirror> get typeVariables => <TypeVariableMirror>[];
726
727 @override
728 List<TypeMirror> get typeArguments => <TypeMirror>[];
729
730 // TODO(eernst) implement: do as in 'dart:mirrors'.
731 @override
732 TypeMirror get originalDeclaration => null;
733
734 // It is allowed to return null.
735 @override
736 SourceLocation get location => null;
737
738 @override
739 bool isSubtypeOf(TypeMirror other) => true;
740
741 @override
742 bool isAssignableTo(TypeMirror other) => true;
743
744 // TODO(eernst) implement: do as 'dart:mirrors' does.
745 @override
746 DeclarationMirror get owner => null;
747
748 @override
749 String get qualifiedName => simpleName;
750
751 @override
752 List<Object> get metadata => <Object>[];
733 } 753 }
734 754
735 abstract class ReflectableImpl extends ReflectableBase 755 abstract class ReflectableImpl extends ReflectableBase
736 implements ReflectableInterface { 756 implements ReflectableInterface {
737
738 /// Const constructor, to enable usage as metadata, allowing for varargs 757 /// Const constructor, to enable usage as metadata, allowing for varargs
739 /// style invocation with up to ten arguments. 758 /// style invocation with up to ten arguments.
740 const ReflectableImpl([ReflectCapability cap0 = null, 759 const ReflectableImpl(
741 ReflectCapability cap1 = null, ReflectCapability cap2 = null, 760 [ReflectCapability cap0 = null,
742 ReflectCapability cap3 = null, ReflectCapability cap4 = null, 761 ReflectCapability cap1 = null,
743 ReflectCapability cap5 = null, ReflectCapability cap6 = null, 762 ReflectCapability cap2 = null,
744 ReflectCapability cap7 = null, ReflectCapability cap8 = null, 763 ReflectCapability cap3 = null,
764 ReflectCapability cap4 = null,
765 ReflectCapability cap5 = null,
766 ReflectCapability cap6 = null,
767 ReflectCapability cap7 = null,
768 ReflectCapability cap8 = null,
745 ReflectCapability cap9 = null]) 769 ReflectCapability cap9 = null])
746 : super(cap0, cap1, cap2, cap3, cap4, cap5, cap6, cap7, cap8, cap9); 770 : super(cap0, cap1, cap2, cap3, cap4, cap5, cap6, cap7, cap8, cap9);
747 771
748 const ReflectableImpl.fromList(List<ReflectCapability> capabilities) 772 const ReflectableImpl.fromList(List<ReflectCapability> capabilities)
749 : super.fromList(capabilities); 773 : super.fromList(capabilities);
750 774
751 @override 775 @override
752 InstanceMirror reflect(Object reflectee) { 776 InstanceMirror reflect(Object reflectee) {
753 return new InstanceMirrorImpl(reflectee, this); 777 return new _InstanceMirrorImpl(reflectee, this);
754 } 778 }
755 779
756 @override 780 @override
757 bool canReflect(Object reflectee) { 781 bool canReflect(Object reflectee) {
758 return data[this].classMirrorForType(reflectee.runtimeType) != null; 782 return data[this].classMirrorForType(reflectee.runtimeType) != null;
759 } 783 }
760 784
761 @override 785 @override
762 ClassMirror reflectType(Type type) { 786 ClassMirror reflectType(Type type) {
763 ClassMirror result = data[this].classMirrorForType(type); 787 ClassMirror result = data[this].classMirrorForType(type);
(...skipping 13 matching lines...) Expand all
777 LibraryMirror findLibrary(String library) => _unsupported(); 801 LibraryMirror findLibrary(String library) => _unsupported();
778 802
779 @override 803 @override
780 Map<Uri, LibraryMirror> get libraries => _unsupported(); 804 Map<Uri, LibraryMirror> get libraries => _unsupported();
781 805
782 @override 806 @override
783 Iterable<ClassMirror> get annotatedClasses { 807 Iterable<ClassMirror> get annotatedClasses {
784 return new UnmodifiableListView<ClassMirror>(data[this].classMirrors); 808 return new UnmodifiableListView<ClassMirror>(data[this].classMirrors);
785 } 809 }
786 } 810 }
OLDNEW
« no previous file with comments | « reflectable/lib/src/encoding_constants.dart ('k') | reflectable/lib/src/reflectable_implementation.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698