110 lines
3.1 KiB
TypeScript
Raw Normal View History

2015-04-24 13:17:36 -07:00
import {Type, isPresent, stringify, BaseException} from 'angular2/src/facade/lang';
import {
List,
ListWrapper,
Map,
MapWrapper,
StringMap,
StringMapWrapper
} from 'angular2/src/facade/collection';
2015-04-24 13:17:36 -07:00
import {SetterFn, GetterFn, MethodFn} from './types';
export class Reflector {
2015-05-15 12:46:50 +02:00
_typeInfo: Map<Type, any>;
_getters: Map<string, GetterFn>;
_setters: Map<string, SetterFn>;
_methods: Map<string, MethodFn>;
2015-04-24 13:17:36 -07:00
reflectionCapabilities: any;
constructor(reflectionCapabilities) {
this._typeInfo = MapWrapper.create();
this._getters = MapWrapper.create();
this._setters = MapWrapper.create();
this._methods = MapWrapper.create();
this.reflectionCapabilities = reflectionCapabilities;
}
registerType(type: Type, typeInfo: StringMap<string, any>): void {
2015-05-15 12:46:50 +02:00
MapWrapper.set(this._typeInfo, type, typeInfo);
}
2015-04-24 13:17:36 -07:00
registerGetters(getters: StringMap<string, GetterFn>): void {
_mergeMaps(this._getters, getters);
}
2015-04-24 13:17:36 -07:00
registerSetters(setters: StringMap<string, SetterFn>): void {
_mergeMaps(this._setters, setters);
}
2015-04-24 13:17:36 -07:00
registerMethods(methods: StringMap<string, MethodFn>): void {
_mergeMaps(this._methods, methods);
}
2015-04-24 13:17:36 -07:00
factory(type: Type): Function {
if (this._containsTypeInfo(type)) {
return this._getTypeInfoField(type, "factory", null);
2015-04-24 13:17:36 -07:00
} else {
return this.reflectionCapabilities.factory(type);
}
}
parameters(typeOrFunc): List<any> {
if (MapWrapper.contains(this._typeInfo, typeOrFunc)) {
return this._getTypeInfoField(typeOrFunc, "parameters", []);
2015-04-24 13:17:36 -07:00
} else {
return this.reflectionCapabilities.parameters(typeOrFunc);
2015-04-24 13:17:36 -07:00
}
}
annotations(typeOrFunc): List<any> {
if (MapWrapper.contains(this._typeInfo, typeOrFunc)) {
return this._getTypeInfoField(typeOrFunc, "annotations", []);
2015-04-24 13:17:36 -07:00
} else {
return this.reflectionCapabilities.annotations(typeOrFunc);
2015-04-24 13:17:36 -07:00
}
}
interfaces(type): List<any> {
if (MapWrapper.contains(this._typeInfo, type)) {
return this._getTypeInfoField(type, "interfaces", []);
} else {
return this.reflectionCapabilities.interfaces(type);
}
}
2015-04-24 13:17:36 -07:00
getter(name: string): GetterFn {
if (MapWrapper.contains(this._getters, name)) {
return MapWrapper.get(this._getters, name);
} else {
return this.reflectionCapabilities.getter(name);
}
}
setter(name: string): SetterFn {
if (MapWrapper.contains(this._setters, name)) {
return MapWrapper.get(this._setters, name);
} else {
return this.reflectionCapabilities.setter(name);
}
}
method(name: string): MethodFn {
if (MapWrapper.contains(this._methods, name)) {
return MapWrapper.get(this._methods, name);
} else {
return this.reflectionCapabilities.method(name);
}
}
_getTypeInfoField(typeOrFunc, key, defaultValue) {
var res = MapWrapper.get(this._typeInfo, typeOrFunc)[key];
return isPresent(res) ? res : defaultValue;
}
_containsTypeInfo(typeOrFunc) { return MapWrapper.contains(this._typeInfo, typeOrFunc); }
2015-04-24 13:17:36 -07:00
}
function _mergeMaps(target: Map<any, any>, config: StringMap<string, Function>): void {
2015-04-24 13:17:36 -07:00
StringMapWrapper.forEach(config, (v, k) => MapWrapper.set(target, k, v));
}