Skip to content

Commit 34d75e8

Browse files
committed
feat(reflector): added a method to get type's interfaces
1 parent 2c25055 commit 34d75e8

File tree

5 files changed

+41
-3
lines changed

5 files changed

+41
-3
lines changed

modules/angular2/src/reflection/reflection_capabilities.dart

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,11 @@ class ReflectionCapabilities {
7171
return meta.map((m) => m.reflectee).toList();
7272
}
7373

74+
List interfaces(type) {
75+
ClassMirror classMirror = reflectType(type);
76+
return classMirror.superinterfaces.map((si) => si.reflectedType).toList();
77+
}
78+
7479
GetterFn getter(String name) {
7580
var symbol = new Symbol(name);
7681
return (receiver) => reflect(receiver).getField(symbol).reflectee;

modules/angular2/src/reflection/reflection_capabilities.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {Type, isPresent, global, stringify} from 'angular2/src/facade/lang';
1+
import {Type, isPresent, global, stringify, BaseException} from 'angular2/src/facade/lang';
22
import {List, ListWrapper} from 'angular2/src/facade/collection';
33
import {GetterFn, SetterFn, MethodFn} from './types';
44

@@ -97,6 +97,10 @@ export class ReflectionCapabilities {
9797
return [];
9898
}
9999

100+
interfaces(type): List<any> {
101+
throw new BaseException("JavaScript does not support interfaces");
102+
}
103+
100104
getter(name: string): GetterFn { return new Function('o', 'return o.' + name + ';'); }
101105

102106
setter(name: string): SetterFn { return new Function('o', 'v', 'return o.' + name + ' = v;'); }

modules/angular2/src/reflection/reflector.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,14 @@ export class Reflector {
5252
}
5353
}
5454

55+
interfaces(type): List<any> {
56+
if (MapWrapper.contains(this._typeInfo, type)) {
57+
return MapWrapper.get(this._typeInfo, type)["interfaces"];
58+
} else {
59+
return this.reflectionCapabilities.interfaces(type);
60+
}
61+
}
62+
5563
getter(name: string): GetterFn {
5664
if (MapWrapper.contains(this._getters, name)) {
5765
return MapWrapper.get(this._getters, name);

modules/angular2/src/transform/template_compiler/recording_reflection_capabilities.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ class RecordingReflectionCapabilities implements ReflectionCapabilities {
2020

2121
List<List> parameters(typeOrFunc) => _notImplemented('parameters');
2222

23+
List<List> interfaces(typeOrFunc) => _notImplemented('interfaces');
24+
2325
List annotations(typeOrFunc) => _notImplemented('annotations');
2426

2527
GetterFn getter(String name) {

modules/angular2/test/reflection/reflector_spec.ts

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {describe, it, iit, ddescribe, expect, beforeEach} from 'angular2/test_lib';
1+
import {describe, it, iit, ddescribe, expect, beforeEach, IS_DARTIUM} from 'angular2/test_lib';
22
import {Reflector} from 'angular2/src/reflection/reflection';
33
import {ReflectionCapabilities} from 'angular2/src/reflection/reflection_capabilities';
44
import {ClassDecorator, ParamDecorator, classDecorator, paramDecorator} from './reflector_common';
@@ -40,6 +40,11 @@ class TestObj {
4040
identity(arg) { return arg; }
4141
}
4242

43+
class Interface {}
44+
45+
class ClassImplementingInterface implements Interface {
46+
}
47+
4348
export function main() {
4449
describe('Reflector', () => {
4550
var reflector;
@@ -93,12 +98,26 @@ export function main() {
9398
expect(reflector.annotations(TestObj)).toEqual([1, 2]);
9499
});
95100

96-
it("should work for a clas without annotations", () => {
101+
it("should work for a class without annotations", () => {
97102
var p = reflector.annotations(ClassWithoutDecorators);
98103
expect(p).toEqual([]);
99104
});
100105
});
101106

107+
if (IS_DARTIUM) {
108+
describe("interfaces", () => {
109+
it("should return an array of interfaces for a type", () => {
110+
var p = reflector.interfaces(ClassImplementingInterface);
111+
expect(p).toEqual([Interface]);
112+
});
113+
114+
it("should return an empty array otherwise", () => {
115+
var p = reflector.interfaces(ClassWithDecorators);
116+
expect(p).toEqual([]);
117+
});
118+
});
119+
}
120+
102121
describe("getter", () => {
103122
it("returns a function reading a property", () => {
104123
var getA = reflector.getter('a');

0 commit comments

Comments
 (0)