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

Unified Diff: reflectable/lib/src/reflectable_implementation.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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « reflectable/lib/src/mirrors_unimpl.dart ('k') | reflectable/lib/src/transformer_implementation.dart » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: reflectable/lib/src/reflectable_implementation.dart
diff --git a/reflectable/lib/src/reflectable_implementation.dart b/reflectable/lib/src/reflectable_implementation.dart
index 2d85ab1916c5fae6f6fb3bdf1eb260af9dd3008c..0898a3a5ee37106dee29dc806864b73fae22d064 100644
--- a/reflectable/lib/src/reflectable_implementation.dart
+++ b/reflectable/lib/src/reflectable_implementation.dart
@@ -22,18 +22,18 @@ rm.ClassMirror wrapClassMirror(
return new _FunctionTypeMirrorImpl(classMirror, reflectable);
} else {
assert(classMirror is dm.ClassMirror);
- return new _ClassMirrorImpl(classMirror, reflectable);
+ return new ClassMirrorImpl(classMirror, reflectable);
}
}
rm.DeclarationMirror wrapDeclarationMirror(
dm.DeclarationMirror declarationMirror, ReflectableImpl reflectable) {
if (declarationMirror is dm.MethodMirror) {
- return new _MethodMirrorImpl(declarationMirror, reflectable);
+ return new MethodMirrorImpl(declarationMirror, reflectable);
} else if (declarationMirror is dm.ParameterMirror) {
return new _ParameterMirrorImpl(declarationMirror, reflectable);
} else if (declarationMirror is dm.VariableMirror) {
- return new _VariableMirrorImpl(declarationMirror, reflectable);
+ return new VariableMirrorImpl(declarationMirror, reflectable);
} else if (declarationMirror is dm.TypeVariableMirror) {
return new _TypeVariableMirrorImpl(declarationMirror, reflectable);
} else if (declarationMirror is dm.TypedefMirror) {
@@ -75,6 +75,8 @@ rm.TypeMirror wrapTypeMirror(
return new _TypeVariableMirrorImpl(typeMirror, reflectable);
} else if (typeMirror is dm.TypedefMirror) {
return new _TypedefMirrorImpl(typeMirror, reflectable);
+ } else if (typeMirror.reflectedType == dynamic) {
+ return new _SpecialTypeMirrorImpl(typeMirror, reflectable);
} else {
assert(typeMirror is dm.ClassMirror);
return wrapClassMirror(typeMirror, reflectable);
@@ -86,7 +88,7 @@ rm.CombinatorMirror wrapCombinatorMirror(dm.CombinatorMirror combinatorMirror) {
}
dm.ClassMirror unwrapClassMirror(rm.ClassMirror m) {
- if (m is _ClassMirrorImpl) {
+ if (m is ClassMirrorImpl) {
// This also works for _FunctionTypeMirrorImpl
return m._classMirror;
} else {
@@ -175,6 +177,11 @@ bool reflectableSupportsConstructorInvoke(
});
}
+bool reflectableSupportsDeclarations(Reflectable reflectable) {
+ return reflectable.capabilities.any(
+ (ReflectCapability capability) => capability == declarationsCapability);
+}
+
/// Returns [setterName] with final "=" removed.
/// If the it does not have a final "=" it is returned as is.
String _setterToGetter(String setterName) {
@@ -194,12 +201,18 @@ String _getterToSetter(String getterName) {
return '$getterName=';
}
+bool _supportsType(List<ReflectCapability> capabilities) {
+ return capabilities.any((ReflectCapability capability) =>
+ capability is TypeCapability || capabilities is TypingCapability);
+}
+
abstract class _ObjectMirrorImplMixin implements rm.ObjectMirror {
ReflectableImpl get _reflectable;
}
class _LibraryMirrorImpl extends _DeclarationMirrorImpl
- with _ObjectMirrorImplMixin implements rm.LibraryMirror {
+ with _ObjectMirrorImplMixin
+ implements rm.LibraryMirror {
dm.LibraryMirror get _libraryMirror => _declarationMirror;
_LibraryMirrorImpl(dm.LibraryMirror m, ReflectableImpl reflectable)
@@ -210,6 +223,10 @@ class _LibraryMirrorImpl extends _DeclarationMirrorImpl
@override
Map<String, rm.DeclarationMirror> get declarations {
+ if (!reflectableSupportsDeclarations(_reflectable)) {
+ throw new NoSuchCapabilityError(
+ "Attempt to get declarations without capability");
+ }
Map<Symbol, dm.DeclarationMirror> decls = _libraryMirror.declarations;
Iterable<Symbol> relevantKeys = decls.keys.where((k) {
List<dm.InstanceMirror> metadata = decls[k].metadata;
@@ -232,8 +249,9 @@ class _LibraryMirrorImpl extends _DeclarationMirrorImpl
_receiver, memberName, positionalArguments, namedArguments);
}
- return _libraryMirror.invoke(
- new Symbol(memberName), positionalArguments, namedArguments).reflectee;
+ return _libraryMirror
+ .invoke(new Symbol(memberName), positionalArguments, namedArguments)
+ .reflectee;
}
@override
@@ -358,8 +376,9 @@ class _InstanceMirrorImpl extends _ObjectMirrorImplMixin
if (reflectableSupportsInstanceInvoke(
_reflectable, memberName, _instanceMirror.type)) {
Symbol memberNameSymbol = new Symbol(memberName);
- return _instanceMirror.invoke(
- memberNameSymbol, positionalArguments, namedArguments).reflectee;
+ return _instanceMirror
+ .invoke(memberNameSymbol, positionalArguments, namedArguments)
+ .reflectee;
}
throw new NoSuchInvokeCapabilityError(
_receiver, memberName, positionalArguments, namedArguments);
@@ -403,11 +422,12 @@ class _InstanceMirrorImpl extends _ObjectMirrorImplMixin
String toString() => "_InstanceMirrorImpl('${_instanceMirror}')";
}
-class _ClassMirrorImpl extends _TypeMirrorImpl with _ObjectMirrorImplMixin
+class ClassMirrorImpl extends _TypeMirrorImpl
+ with _ObjectMirrorImplMixin
implements rm.ClassMirror {
dm.ClassMirror get _classMirror => _declarationMirror;
- _ClassMirrorImpl(dm.ClassMirror cm, ReflectableImpl reflectable)
+ ClassMirrorImpl(dm.ClassMirror cm, ReflectableImpl reflectable)
: super(cm, reflectable) {}
@override
@@ -419,13 +439,13 @@ class _ClassMirrorImpl extends _TypeMirrorImpl with _ObjectMirrorImplMixin
@override
List<rm.TypeMirror> get typeArguments => _classMirror.typeArguments.map((a) {
- return wrapTypeMirror(a, _reflectable);
- }).toList();
+ return wrapTypeMirror(a, _reflectable);
+ }).toList();
@override
- rm.TypeMirror get superclass {
+ rm.ClassMirror get superclass {
dm.ClassMirror sup = _classMirror.superclass;
- if (sup == null) return null; // For `Object`, do as `dm`.
+ if (sup == null) return null; // For `Object`, do as `dm`.
return wrapClassMirror(sup, _reflectable);
}
@@ -442,6 +462,10 @@ class _ClassMirrorImpl extends _TypeMirrorImpl with _ObjectMirrorImplMixin
@override
Map<String, rm.DeclarationMirror> get declarations {
+ if (!reflectableSupportsDeclarations(_reflectable)) {
+ throw new NoSuchCapabilityError(
+ "Attempt to get declarations without capability");
+ }
// TODO(sigurdm) future: Possibly cache this.
Map<String, rm.DeclarationMirror> result =
new Map<String, rm.DeclarationMirror>();
@@ -513,7 +537,7 @@ class _ClassMirrorImpl extends _TypeMirrorImpl with _ObjectMirrorImplMixin
Map<Symbol, dm.MethodMirror> members = _classMirror.instanceMembers;
return new Map<String, rm.MethodMirror>.fromIterable(members.keys,
key: (k) => dm.MirrorSystem.getName(k),
- value: (v) => new _MethodMirrorImpl(members[v], _reflectable));
+ value: (v) => new MethodMirrorImpl(members[v], _reflectable));
}
@override
@@ -523,7 +547,7 @@ class _ClassMirrorImpl extends _TypeMirrorImpl with _ObjectMirrorImplMixin
Map<Symbol, dm.MethodMirror> members = _classMirror.staticMembers;
return new Map<String, rm.MethodMirror>.fromIterable(members.keys,
key: (k) => dm.MirrorSystem.getName(k),
- value: (v) => new _MethodMirrorImpl(members[v], _reflectable));
+ value: (v) => new MethodMirrorImpl(members[v], _reflectable));
}
@override
@@ -538,8 +562,9 @@ class _ClassMirrorImpl extends _TypeMirrorImpl with _ObjectMirrorImplMixin
_classMirror, constructorName, positionalArguments, namedArguments);
}
Symbol constructorNameSymbol = new Symbol(constructorName);
- return _classMirror.newInstance(
- constructorNameSymbol, positionalArguments, namedArguments).reflectee;
+ return _classMirror
+ .newInstance(constructorNameSymbol, positionalArguments, namedArguments)
+ .reflectee;
}
@override
@@ -550,8 +575,9 @@ class _ClassMirrorImpl extends _TypeMirrorImpl with _ObjectMirrorImplMixin
List<Object> metadata = declaration == null ? null : declaration.metadata;
if (reflectableSupportsStaticInvoke(_reflectable, memberName, metadata)) {
Symbol memberNameSymbol = new Symbol(memberName);
- return _classMirror.invoke(
- memberNameSymbol, positionalArguments, namedArguments).reflectee;
+ return _classMirror
+ .invoke(memberNameSymbol, positionalArguments, namedArguments)
+ .reflectee;
}
throw new NoSuchInvokeCapabilityError(
_receiver, memberName, positionalArguments, namedArguments);
@@ -580,7 +606,7 @@ class _ClassMirrorImpl extends _TypeMirrorImpl with _ObjectMirrorImplMixin
@override
bool operator ==(other) {
- return other is _ClassMirrorImpl
+ return other is ClassMirrorImpl
? _classMirror == other._classMirror
: false;
}
@@ -596,7 +622,7 @@ class _ClassMirrorImpl extends _TypeMirrorImpl with _ObjectMirrorImplMixin
get _receiver => _classMirror.reflectedType;
@override
- String toString() => "_ClassMirrorImpl('${_classMirror}')";
+ String toString() => "ClassMirrorImpl('${_classMirror}')";
@override
Function invoker(String memberName) {
@@ -611,7 +637,7 @@ class _ClassMirrorImpl extends _TypeMirrorImpl with _ObjectMirrorImplMixin
}
}
-class _FunctionTypeMirrorImpl extends _ClassMirrorImpl
+class _FunctionTypeMirrorImpl extends ClassMirrorImpl
implements rm.FunctionTypeMirror {
dm.FunctionTypeMirror get _functionTypeMirror => _classMirror;
@@ -621,7 +647,7 @@ class _FunctionTypeMirrorImpl extends _ClassMirrorImpl
@override
rm.MethodMirror get callMethod {
- return new _MethodMirrorImpl(_functionTypeMirror.callMethod, _reflectable);
+ return new MethodMirrorImpl(_functionTypeMirror.callMethod, _reflectable);
}
@override
@@ -682,11 +708,11 @@ abstract class _DeclarationMirrorImpl implements rm.DeclarationMirror {
}
}
-class _MethodMirrorImpl extends _DeclarationMirrorImpl
+class MethodMirrorImpl extends _DeclarationMirrorImpl
implements rm.MethodMirror {
dm.MethodMirror get _methodMirror => _declarationMirror;
- _MethodMirrorImpl(dm.MethodMirror mm, ReflectableImpl reflectable)
+ MethodMirrorImpl(dm.MethodMirror mm, ReflectableImpl reflectable)
: super(mm, reflectable);
@override
@@ -745,7 +771,7 @@ class _MethodMirrorImpl extends _DeclarationMirrorImpl
@override
bool operator ==(other) {
- return other is _MethodMirrorImpl
+ return other is MethodMirrorImpl
? _methodMirror == other._methodMirror
: false;
}
@@ -773,22 +799,27 @@ class _ClosureMirrorImpl extends _InstanceMirrorImpl
@override
rm.MethodMirror get function {
- return new _MethodMirrorImpl(_closureMirror.function, _reflectable);
+ return new MethodMirrorImpl(_closureMirror.function, _reflectable);
}
@override
String toString() => "_ClosureMirrorImpl('${_closureMirror}')";
}
-class _VariableMirrorImpl extends _DeclarationMirrorImpl
+class VariableMirrorImpl extends _DeclarationMirrorImpl
implements rm.VariableMirror {
dm.VariableMirror get _variableMirror => _declarationMirror;
- _VariableMirrorImpl(dm.VariableMirror vm, ReflectableImpl reflectable)
+ VariableMirrorImpl(dm.VariableMirror vm, ReflectableImpl reflectable)
: super(vm, reflectable);
@override
- rm.TypeMirror get type => wrapTypeMirror(_variableMirror.type, _reflectable);
+ rm.TypeMirror get type {
+ if (_supportsType(_reflectable.capabilities)) {
+ return wrapTypeMirror(_variableMirror.type, _reflectable);
+ }
+ throw new NoSuchCapabilityError("Attempt to get a type without capability");
+ }
@override
bool get isStatic => _variableMirror.isStatic;
@@ -800,7 +831,7 @@ class _VariableMirrorImpl extends _DeclarationMirrorImpl
bool get isConst => _variableMirror.isConst;
@override
- bool operator ==(other) => other is _VariableMirrorImpl
+ bool operator ==(other) => other is VariableMirrorImpl
? _variableMirror == other._variableMirror
: false;
@@ -808,10 +839,10 @@ class _VariableMirrorImpl extends _DeclarationMirrorImpl
int get hashCode => _variableMirror.hashCode;
@override
- String toString() => "_VariableMirrorImpl('${_variableMirror}')";
+ String toString() => "VariableMirrorImpl('${_variableMirror}')";
}
-class _ParameterMirrorImpl extends _VariableMirrorImpl
+class _ParameterMirrorImpl extends VariableMirrorImpl
implements rm.ParameterMirror {
dm.ParameterMirror get _parameterMirror => _declarationMirror;
@@ -951,6 +982,23 @@ class _TypedefMirrorImpl extends _TypeMirrorImpl implements rm.TypedefMirror {
}
}
+class _SpecialTypeMirrorImpl extends _TypeMirrorImpl {
+ _SpecialTypeMirrorImpl(dm.TypeMirror typeMirror, ReflectableImpl reflectable)
+ : super(typeMirror, reflectable);
+
+ @override operator ==(other) {
+ return other is _SpecialTypeMirrorImpl && other._typeMirror == _typeMirror;
+ }
+
+ @override
+ int get hashCode => _typeMirror.hashCode;
+
+ @override
+ String toString() {
+ return "_SpecialTypeMirrorImpl('${_typeMirror}')";
+ }
+}
+
class _CombinatorMirrorImpl implements rm.CombinatorMirror {
dm.CombinatorMirror _combinatorMirror;
@@ -1021,11 +1069,16 @@ class ReflectableImpl extends ReflectableBase implements ReflectableInterface {
/// Const constructor, to enable usage as metadata, allowing for varargs
/// style invocation with up to ten arguments.
- const ReflectableImpl([ReflectCapability cap0 = null,
- ReflectCapability cap1 = null, ReflectCapability cap2 = null,
- ReflectCapability cap3 = null, ReflectCapability cap4 = null,
- ReflectCapability cap5 = null, ReflectCapability cap6 = null,
- ReflectCapability cap7 = null, ReflectCapability cap8 = null,
+ const ReflectableImpl(
+ [ReflectCapability cap0 = null,
+ ReflectCapability cap1 = null,
+ ReflectCapability cap2 = null,
+ ReflectCapability cap3 = null,
+ ReflectCapability cap4 = null,
+ ReflectCapability cap5 = null,
+ ReflectCapability cap6 = null,
+ ReflectCapability cap7 = null,
+ ReflectCapability cap8 = null,
ReflectCapability cap9 = null])
: super(cap0, cap1, cap2, cap3, cap4, cap5, cap6, cap7, cap8, cap9);
« no previous file with comments | « reflectable/lib/src/mirrors_unimpl.dart ('k') | reflectable/lib/src/transformer_implementation.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698