feat(reflector): added a method to get type's interfaces

This commit is contained in:
vsavkin 2015-05-27 08:07:11 -07:00
parent 2c25055828
commit 34d75e8918
5 changed files with 41 additions and 3 deletions

View File

@ -71,6 +71,11 @@ class ReflectionCapabilities {
return meta.map((m) => m.reflectee).toList();
}
List interfaces(type) {
ClassMirror classMirror = reflectType(type);
return classMirror.superinterfaces.map((si) => si.reflectedType).toList();
}
GetterFn getter(String name) {
var symbol = new Symbol(name);
return (receiver) => reflect(receiver).getField(symbol).reflectee;

View File

@ -1,4 +1,4 @@
import {Type, isPresent, global, stringify} from 'angular2/src/facade/lang';
import {Type, isPresent, global, stringify, BaseException} from 'angular2/src/facade/lang';
import {List, ListWrapper} from 'angular2/src/facade/collection';
import {GetterFn, SetterFn, MethodFn} from './types';
@ -97,6 +97,10 @@ export class ReflectionCapabilities {
return [];
}
interfaces(type): List<any> {
throw new BaseException("JavaScript does not support interfaces");
}
getter(name: string): GetterFn { return new Function('o', 'return o.' + name + ';'); }
setter(name: string): SetterFn { return new Function('o', 'v', 'return o.' + name + ' = v;'); }

View File

@ -52,6 +52,14 @@ export class Reflector {
}
}
interfaces(type): List<any> {
if (MapWrapper.contains(this._typeInfo, type)) {
return MapWrapper.get(this._typeInfo, type)["interfaces"];
} else {
return this.reflectionCapabilities.interfaces(type);
}
}
getter(name: string): GetterFn {
if (MapWrapper.contains(this._getters, name)) {
return MapWrapper.get(this._getters, name);

View File

@ -20,6 +20,8 @@ class RecordingReflectionCapabilities implements ReflectionCapabilities {
List<List> parameters(typeOrFunc) => _notImplemented('parameters');
List<List> interfaces(typeOrFunc) => _notImplemented('interfaces');
List annotations(typeOrFunc) => _notImplemented('annotations');
GetterFn getter(String name) {

View File

@ -1,4 +1,4 @@
import {describe, it, iit, ddescribe, expect, beforeEach} from 'angular2/test_lib';
import {describe, it, iit, ddescribe, expect, beforeEach, IS_DARTIUM} from 'angular2/test_lib';
import {Reflector} from 'angular2/src/reflection/reflection';
import {ReflectionCapabilities} from 'angular2/src/reflection/reflection_capabilities';
import {ClassDecorator, ParamDecorator, classDecorator, paramDecorator} from './reflector_common';
@ -40,6 +40,11 @@ class TestObj {
identity(arg) { return arg; }
}
class Interface {}
class ClassImplementingInterface implements Interface {
}
export function main() {
describe('Reflector', () => {
var reflector;
@ -93,12 +98,26 @@ export function main() {
expect(reflector.annotations(TestObj)).toEqual([1, 2]);
});
it("should work for a clas without annotations", () => {
it("should work for a class without annotations", () => {
var p = reflector.annotations(ClassWithoutDecorators);
expect(p).toEqual([]);
});
});
if (IS_DARTIUM) {
describe("interfaces", () => {
it("should return an array of interfaces for a type", () => {
var p = reflector.interfaces(ClassImplementingInterface);
expect(p).toEqual([Interface]);
});
it("should return an empty array otherwise", () => {
var p = reflector.interfaces(ClassWithDecorators);
expect(p).toEqual([]);
});
});
}
describe("getter", () => {
it("returns a function reading a property", () => {
var getA = reflector.getter('a');