From 34d75e89185cb08ccd125c4ced3eae5fd66da7ec Mon Sep 17 00:00:00 2001 From: vsavkin Date: Wed, 27 May 2015 08:07:11 -0700 Subject: [PATCH] feat(reflector): added a method to get type's interfaces --- .../reflection/reflection_capabilities.dart | 5 ++++ .../src/reflection/reflection_capabilities.ts | 6 ++++- modules/angular2/src/reflection/reflector.ts | 8 +++++++ .../recording_reflection_capabilities.dart | 2 ++ .../test/reflection/reflector_spec.ts | 23 +++++++++++++++++-- 5 files changed, 41 insertions(+), 3 deletions(-) diff --git a/modules/angular2/src/reflection/reflection_capabilities.dart b/modules/angular2/src/reflection/reflection_capabilities.dart index 3aa12c029a..e450e96265 100644 --- a/modules/angular2/src/reflection/reflection_capabilities.dart +++ b/modules/angular2/src/reflection/reflection_capabilities.dart @@ -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; diff --git a/modules/angular2/src/reflection/reflection_capabilities.ts b/modules/angular2/src/reflection/reflection_capabilities.ts index 70f082e166..c6d3aa5a05 100644 --- a/modules/angular2/src/reflection/reflection_capabilities.ts +++ b/modules/angular2/src/reflection/reflection_capabilities.ts @@ -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 { + 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;'); } diff --git a/modules/angular2/src/reflection/reflector.ts b/modules/angular2/src/reflection/reflector.ts index 78712ca82f..c8f9bc32ab 100644 --- a/modules/angular2/src/reflection/reflector.ts +++ b/modules/angular2/src/reflection/reflector.ts @@ -52,6 +52,14 @@ export class Reflector { } } + interfaces(type): List { + 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); diff --git a/modules/angular2/src/transform/template_compiler/recording_reflection_capabilities.dart b/modules/angular2/src/transform/template_compiler/recording_reflection_capabilities.dart index 9f3a4f63dc..8398206bc0 100644 --- a/modules/angular2/src/transform/template_compiler/recording_reflection_capabilities.dart +++ b/modules/angular2/src/transform/template_compiler/recording_reflection_capabilities.dart @@ -20,6 +20,8 @@ class RecordingReflectionCapabilities implements ReflectionCapabilities { List parameters(typeOrFunc) => _notImplemented('parameters'); + List interfaces(typeOrFunc) => _notImplemented('interfaces'); + List annotations(typeOrFunc) => _notImplemented('annotations'); GetterFn getter(String name) { diff --git a/modules/angular2/test/reflection/reflector_spec.ts b/modules/angular2/test/reflection/reflector_spec.ts index 7342d6e9d2..6664362762 100644 --- a/modules/angular2/test/reflection/reflector_spec.ts +++ b/modules/angular2/test/reflection/reflector_spec.ts @@ -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');