chore: Define ReflectionCapabilities interface

This commit is contained in:
Misko Hevery 2015-05-18 16:09:09 -07:00
parent 331a051e75
commit 7501ad11ca
6 changed files with 44 additions and 13 deletions

View File

@ -5,7 +5,7 @@ import 'types.dart';
export 'reflector.dart';
import 'package:angular2/src/facade/lang.dart';
class NoReflectionCapabilities {
class NoReflectionCapabilities implements IReflectionCapabilities {
Function factory(Type type) {
throw "Cannot find reflection information on ${stringify(type)}";
}

View File

@ -4,7 +4,7 @@ import 'package:angular2/src/facade/lang.dart';
import 'types.dart';
import 'dart:mirrors';
class ReflectionCapabilities {
class ReflectionCapabilities implements IReflectionCapabilities {
ReflectionCapabilities([metadataReader]) {}
Function factory(Type type) {

View File

@ -1,8 +1,8 @@
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';
import {GetterFn, SetterFn, MethodFn, IReflectionCapabilities} from './types';
export class ReflectionCapabilities {
export class ReflectionCapabilities implements IReflectionCapabilities {
private _reflect: any;
constructor(reflect?: any) { this._reflect = isPresent(reflect) ? reflect : global.Reflect; }
@ -99,13 +99,15 @@ export class ReflectionCapabilities {
interfaces(type): List<any> { throw new BaseException("JavaScript does not support interfaces"); }
getter(name: string): GetterFn { return new Function('o', 'return o.' + name + ';'); }
getter(name: string): GetterFn { return <GetterFn>new Function('o', 'return o.' + name + ';'); }
setter(name: string): SetterFn { return new Function('o', 'v', 'return o.' + name + ' = v;'); }
setter(name: string): SetterFn {
return <SetterFn>new Function('o', 'v', 'return o.' + name + ' = v;');
}
method(name: string): MethodFn {
let functionBody = `if (!o.${name}) throw new Error('"${name}" is undefined');
return o.${name}.apply(o, args);`;
return new Function('o', 'args', functionBody);
return <MethodFn>new Function('o', 'args', functionBody);
}
}

View File

@ -7,16 +7,17 @@ import {
StringMap,
StringMapWrapper
} from 'angular2/src/facade/collection';
import {SetterFn, GetterFn, MethodFn} from './types';
import {SetterFn, GetterFn, MethodFn, IReflectionCapabilities} from './types';
export {SetterFn, GetterFn, MethodFn, IReflectionCapabilities} from './types';
export class Reflector {
_typeInfo: Map<Type, any>;
_getters: Map<string, GetterFn>;
_setters: Map<string, SetterFn>;
_methods: Map<string, MethodFn>;
reflectionCapabilities: any;
reflectionCapabilities: IReflectionCapabilities;
constructor(reflectionCapabilities) {
constructor(reflectionCapabilities: IReflectionCapabilities) {
this._typeInfo = MapWrapper.create();
this._getters = MapWrapper.create();
this._setters = MapWrapper.create();

View File

@ -3,3 +3,13 @@ library reflection.types;
typedef SetterFn(Object obj, value);
typedef GetterFn(Object obj);
typedef MethodFn(Object obj, List args);
abstract class IReflectionCapabilities {
Function factory(Type type);
List<List<Type>> parameters(Type type);
List<Type> interfaces(Type type);
List annotations(Type type);
GetterFn getter(String name);
SetterFn setter(String name);
MethodFn method(String name);
}

View File

@ -1,3 +1,21 @@
export {Function as SetterFn};
export {Function as GetterFn};
export {Function as MethodFn};
import {Type} from 'angular2/src/facade/lang';
import {List} from 'angular2/src/facade/collection';
// HACK: workaround for Traceur behavior.
// It expects all transpiled modules to contain this marker.
// TODO: remove this when we no longer use traceur
export var __esModule = true;
export type SetterFn = (obj: any, value: any) => void;
export type GetterFn = (obj: any) => any;
export type MethodFn = (obj: any, args: List<any>) => any;
export interface IReflectionCapabilities {
factory(type: Type): Function;
parameters(type: Type): List<List<any>>;
interfaces(type: Type): List<any>;
annotations(type: Type): List<any>;
getter(name: string): GetterFn;
setter(name: string): SetterFn;
method(name: string): MethodFn;
}