forked from angular/angular
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreflector.ts
More file actions
162 lines (139 loc) · 4.93 KB
/
reflector.ts
File metadata and controls
162 lines (139 loc) · 4.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
import {Type, isPresent, stringify, BaseException} from 'angular2/src/core/facade/lang';
import {
ListWrapper,
Map,
MapWrapper,
Set,
SetWrapper,
StringMap,
StringMapWrapper
} from 'angular2/src/core/facade/collection';
import {SetterFn, GetterFn, MethodFn} from './types';
import {PlatformReflectionCapabilities} from './platform_reflection_capabilities';
export {SetterFn, GetterFn, MethodFn} from './types';
export {PlatformReflectionCapabilities} from './platform_reflection_capabilities';
export class ReflectionInfo {
constructor(public annotations?: any[], public parameters?: any[][], public factory?: Function,
public interfaces?: any[], public propMetadata?: StringMap<string, any[]>) {}
}
export class Reflector {
_injectableInfo: Map<any, ReflectionInfo>;
_getters: Map<string, GetterFn>;
_setters: Map<string, SetterFn>;
_methods: Map<string, MethodFn>;
_usedKeys: Set<any>;
reflectionCapabilities: PlatformReflectionCapabilities;
constructor(reflectionCapabilities: PlatformReflectionCapabilities) {
this._injectableInfo = new Map();
this._getters = new Map();
this._setters = new Map();
this._methods = new Map();
this._usedKeys = null;
this.reflectionCapabilities = reflectionCapabilities;
}
isReflectionEnabled(): boolean { return this.reflectionCapabilities.isReflectionEnabled(); }
/**
* Causes `this` reflector to track keys used to access
* {@link ReflectionInfo} objects.
*/
trackUsage(): void { this._usedKeys = new Set(); }
/**
* Lists types for which reflection information was not requested since
* {@link #trackUsage} was called. This list could later be audited as
* potential dead code.
*/
listUnusedKeys(): any[] {
if (this._usedKeys == null) {
throw new BaseException('Usage tracking is disabled');
}
var allTypes = MapWrapper.keys(this._injectableInfo);
return ListWrapper.filter(allTypes, (key) => { return !SetWrapper.has(this._usedKeys, key); });
}
registerFunction(func: Function, funcInfo: ReflectionInfo): void {
this._injectableInfo.set(func, funcInfo);
}
registerType(type: Type, typeInfo: ReflectionInfo): void {
this._injectableInfo.set(type, typeInfo);
}
registerGetters(getters: StringMap<string, GetterFn>): void {
_mergeMaps(this._getters, getters);
}
registerSetters(setters: StringMap<string, SetterFn>): void {
_mergeMaps(this._setters, setters);
}
registerMethods(methods: StringMap<string, MethodFn>): void {
_mergeMaps(this._methods, methods);
}
factory(type: Type): Function {
if (this._containsReflectionInfo(type)) {
var res = this._getReflectionInfo(type).factory;
return isPresent(res) ? res : null;
} else {
return this.reflectionCapabilities.factory(type);
}
}
parameters(typeOrFunc: /*Type*/ any): any[] {
if (this._injectableInfo.has(typeOrFunc)) {
var res = this._getReflectionInfo(typeOrFunc).parameters;
return isPresent(res) ? res : [];
} else {
return this.reflectionCapabilities.parameters(typeOrFunc);
}
}
annotations(typeOrFunc: /*Type*/ any): any[] {
if (this._injectableInfo.has(typeOrFunc)) {
var res = this._getReflectionInfo(typeOrFunc).annotations;
return isPresent(res) ? res : [];
} else {
return this.reflectionCapabilities.annotations(typeOrFunc);
}
}
propMetadata(typeOrFunc: /*Type*/ any): StringMap<string, any[]> {
if (this._injectableInfo.has(typeOrFunc)) {
var res = this._getReflectionInfo(typeOrFunc).propMetadata;
return isPresent(res) ? res : {};
} else {
return this.reflectionCapabilities.propMetadata(typeOrFunc);
}
}
interfaces(type: Type): any[] {
if (this._injectableInfo.has(type)) {
var res = this._getReflectionInfo(type).interfaces;
return isPresent(res) ? res : [];
} else {
return this.reflectionCapabilities.interfaces(type);
}
}
getter(name: string): GetterFn {
if (this._getters.has(name)) {
return this._getters.get(name);
} else {
return this.reflectionCapabilities.getter(name);
}
}
setter(name: string): SetterFn {
if (this._setters.has(name)) {
return this._setters.get(name);
} else {
return this.reflectionCapabilities.setter(name);
}
}
method(name: string): MethodFn {
if (this._methods.has(name)) {
return this._methods.get(name);
} else {
return this.reflectionCapabilities.method(name);
}
}
_getReflectionInfo(typeOrFunc) {
if (isPresent(this._usedKeys)) {
this._usedKeys.add(typeOrFunc);
}
return this._injectableInfo.get(typeOrFunc);
}
_containsReflectionInfo(typeOrFunc) { return this._injectableInfo.has(typeOrFunc); }
importUri(type: Type): string { return this.reflectionCapabilities.importUri(type); }
}
function _mergeMaps(target: Map<any, any>, config: StringMap<string, Function>): void {
StringMapWrapper.forEach(config, (v, k) => target.set(k, v));
}