Skip to content

Commit 5b5de66

Browse files
committed
chore(transformer): Use class for reflection info instead of a map
closes angular#906
1 parent a8b75c3 commit 5b5de66

File tree

95 files changed

+551
-891
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

95 files changed

+551
-891
lines changed
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import {Type, isPresent} from 'angular2/src/facade/lang';
22
import {List, ListWrapper} from 'angular2/src/facade/collection';
33
import {Reflector} from './reflector';
4-
export {Reflector} from './reflector';
4+
export {Reflector, ReflectionInfo} from './reflector';
55
import {ReflectionCapabilities} from './reflection_capabilities';
66

77
export var reflector = new Reflector(new ReflectionCapabilities());

modules/angular2/src/reflection/reflector.ts

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,23 @@ import {PlatformReflectionCapabilities} from './platform_reflection_capabilities
1212
export {SetterFn, GetterFn, MethodFn} from './types';
1313
export {PlatformReflectionCapabilities} from './platform_reflection_capabilities';
1414

15+
export class ReflectionInfo {
16+
_factory: Function;
17+
_annotations: List<any>;
18+
_parameters: List<List<any>>;
19+
_interfaces: List<any>;
20+
21+
constructor(annotations?: List<any>, parameters?: List<List<any>>, factory?: Function,
22+
interfaces?: List<any>) {
23+
this._annotations = annotations;
24+
this._parameters = parameters;
25+
this._factory = factory;
26+
this._interfaces = interfaces;
27+
}
28+
}
29+
1530
export class Reflector {
16-
_injectableInfo: Map<any, StringMap<string, any>>;
31+
_injectableInfo: Map<any, ReflectionInfo>;
1732
_getters: Map<string, GetterFn>;
1833
_setters: Map<string, SetterFn>;
1934
_methods: Map<string, MethodFn>;
@@ -29,11 +44,11 @@ export class Reflector {
2944

3045
isReflectionEnabled(): boolean { return this.reflectionCapabilities.isReflectionEnabled(); }
3146

32-
registerFunction(func: Function, funcInfo: StringMap<string, any>): void {
47+
registerFunction(func: Function, funcInfo: ReflectionInfo): void {
3348
this._injectableInfo.set(func, funcInfo);
3449
}
3550

36-
registerType(type: Type, typeInfo: StringMap<string, any>): void {
51+
registerType(type: Type, typeInfo: ReflectionInfo): void {
3752
this._injectableInfo.set(type, typeInfo);
3853
}
3954

@@ -50,32 +65,36 @@ export class Reflector {
5065
}
5166

5267
factory(type: Type): Function {
53-
if (this._containsTypeInfo(type)) {
54-
return this._getTypeInfoField(type, "factory", null);
68+
if (this._containsReflectionInfo(type)) {
69+
var res = this._injectableInfo.get(type)._factory;
70+
return isPresent(res) ? res : null;
5571
} else {
5672
return this.reflectionCapabilities.factory(type);
5773
}
5874
}
5975

6076
parameters(typeOrFunc: /*Type*/ any): List<any> {
6177
if (this._injectableInfo.has(typeOrFunc)) {
62-
return this._getTypeInfoField(typeOrFunc, "parameters", []);
78+
var res = this._injectableInfo.get(typeOrFunc)._parameters;
79+
return isPresent(res) ? res : [];
6380
} else {
6481
return this.reflectionCapabilities.parameters(typeOrFunc);
6582
}
6683
}
6784

6885
annotations(typeOrFunc: /*Type*/ any): List<any> {
6986
if (this._injectableInfo.has(typeOrFunc)) {
70-
return this._getTypeInfoField(typeOrFunc, "annotations", []);
87+
var res = this._injectableInfo.get(typeOrFunc)._annotations;
88+
return isPresent(res) ? res : [];
7189
} else {
7290
return this.reflectionCapabilities.annotations(typeOrFunc);
7391
}
7492
}
7593

7694
interfaces(type: Type): List<any> {
7795
if (this._injectableInfo.has(type)) {
78-
return this._getTypeInfoField(type, "interfaces", []);
96+
var res = this._injectableInfo.get(type)._interfaces;
97+
return isPresent(res) ? res : [];
7998
} else {
8099
return this.reflectionCapabilities.interfaces(type);
81100
}
@@ -105,12 +124,7 @@ export class Reflector {
105124
}
106125
}
107126

108-
_getTypeInfoField(typeOrFunc, key, defaultValue) {
109-
var res = this._injectableInfo.get(typeOrFunc)[key];
110-
return isPresent(res) ? res : defaultValue;
111-
}
112-
113-
_containsTypeInfo(typeOrFunc) { return this._injectableInfo.has(typeOrFunc); }
127+
_containsReflectionInfo(typeOrFunc) { return this._injectableInfo.has(typeOrFunc); }
114128
}
115129

116130
function _mergeMaps(target: Map<any, any>, config: StringMap<string, Function>): void {

modules/angular2/src/transform/common/logging.dart

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,17 @@ dynamic initZoned(Transform t, _SimpleCallback fn, {String errorMessage: ''}) =>
1515
setZoned(new BuildLogger(t), fn, errorMessage: errorMessage);
1616

1717
dynamic setZoned(BuildLogger logger, _SimpleCallback fn,
18-
{String errorMessage: ''}) => runZoned(fn,
19-
zoneValues: {_key: logger}, onError: (e, stackTrace) {
20-
logger.error('$errorMessage\n'
21-
'Exception: $e\n'
22-
'Stack Trace: $stackTrace');
23-
});
18+
{String errorMessage}) {
19+
var onError;
20+
if (errorMessage != null) {
21+
onError = (e, stackTrace) {
22+
logger.error('$errorMessage\n'
23+
'Exception: $e\n'
24+
'Stack Trace: $stackTrace');
25+
};
26+
}
27+
return runZoned(fn, zoneValues: {_key: logger}, onError: onError);
28+
}
2429

2530
/// The logger for the current {@link Zone}.
2631
BuildLogger get logger {

modules/angular2/src/transform/common/registered_type.dart

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -63,26 +63,22 @@ class _ParseRegisterTypeVisitor extends Object
6363
typeName = node.argumentList.arguments[0] is Identifier
6464
? node.argumentList.arguments[0]
6565
: null;
66-
return super.visitMethodInvocation(node);
67-
}
6866

69-
@override
70-
Object visitMapLiteralEntry(MapLiteralEntry node) {
71-
if (node.key is StringLiteral) {
72-
var key = stringLiteralToString(node.key);
73-
switch (key) {
74-
case 'annotations':
75-
annotations = node.value;
76-
break;
77-
case 'factory':
78-
factoryFn = node.value;
79-
break;
80-
case 'parameters':
81-
parameters = node.value;
82-
break;
67+
// The second argument to a `registerType` call is the RegistrationInfo
68+
// object creation.
69+
var info = node.argumentList.arguments[1] as InstanceCreationExpression;
70+
var args = info.argumentList.arguments;
71+
for (int i = 0; i < args.length; i++) {
72+
var arg = args[i];
73+
if (i == 0) {
74+
annotations = arg;
75+
} else if (i == 1) {
76+
parameters = arg;
77+
} else if (i == 2) {
78+
factoryFn = arg;
8379
}
8480
}
85-
// Do not need to descend any further.
81+
8682
return null;
8783
}
8884
}

modules/angular2/src/transform/directive_processor/rewriter.dart

Lines changed: 20 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,9 @@ Future<String> createNgDeps(AssetReader reader, AssetId assetId,
3030
// TODO(kegluneq): Shortcut if we can determine that there are no
3131
// [Directive]s present, taking into account `export`s.
3232
var writer = new AsyncStringWriter();
33-
var visitor = new CreateNgDepsVisitor(
34-
writer,
35-
assetId,
36-
new XhrImpl(reader, assetId),
37-
annotationMatcher,
38-
_interfaceMatcher,
39-
ngMeta,
40-
inlineViews: inlineViews);
33+
var visitor = new CreateNgDepsVisitor(writer, assetId,
34+
new XhrImpl(reader, assetId), annotationMatcher, _interfaceMatcher,
35+
ngMeta, inlineViews: inlineViews);
4136
var code = await reader.readAsString(assetId);
4237
parseCompilationUnit(code, name: assetId.path).accept(visitor);
4338

@@ -81,14 +76,9 @@ class CreateNgDepsVisitor extends Object with SimpleAstVisitor<Object> {
8176
/// The assetId for the file which we are parsing.
8277
final AssetId assetId;
8378

84-
CreateNgDepsVisitor(
85-
AsyncStringWriter writer,
86-
AssetId assetId,
87-
XHR xhr,
88-
AnnotationMatcher annotationMatcher,
89-
InterfaceMatcher interfaceMatcher,
90-
this.ngMeta,
91-
{bool inlineViews})
79+
CreateNgDepsVisitor(AsyncStringWriter writer, AssetId assetId, XHR xhr,
80+
AnnotationMatcher annotationMatcher, InterfaceMatcher interfaceMatcher,
81+
this.ngMeta, {bool inlineViews})
9282
: writer = writer,
9383
_copyVisitor = new ToSourceVisitor(writer),
9484
_factoryVisitor = new FactoryTransformVisitor(writer),
@@ -215,31 +205,31 @@ class CreateNgDepsVisitor extends Object with SimpleAstVisitor<Object> {
215205
_maybeWriteReflector();
216206
writer.print('..registerType(');
217207
node.name.accept(this);
218-
writer.print(''', {'factory': ''');
208+
writer.print(', new ${_REF_PREFIX}.ReflectionInfo(');
209+
node.accept(_metaVisitor);
210+
writer.print(', ');
219211
if (ctor == null) {
220-
_generateEmptyFactory(node.name.toString());
212+
_generateEmptyParams();
221213
} else {
222-
ctor.accept(_factoryVisitor);
214+
ctor.accept(_paramsVisitor);
223215
}
224-
writer.print(''', 'parameters': ''');
216+
writer.print(', ');
225217
if (ctor == null) {
226-
_generateEmptyParams();
218+
_generateEmptyFactory(node.name.toString());
227219
} else {
228-
ctor.accept(_paramsVisitor);
220+
ctor.accept(_factoryVisitor);
229221
}
230-
writer.print(''', 'annotations': ''');
231-
node.accept(_metaVisitor);
232222
if (node.implementsClause != null &&
233223
node.implementsClause.interfaces != null &&
234224
node.implementsClause.interfaces.isNotEmpty) {
235225
writer
236-
..print(''', 'interfaces': const [''')
226+
..print(', const [')
237227
..print(node.implementsClause.interfaces
238228
.map((interface) => interface.name)
239229
.join(', '))
240230
..print(']');
241231
}
242-
writer.print('})');
232+
writer.print('))');
243233
return null;
244234
}
245235

@@ -307,11 +297,11 @@ class CreateNgDepsVisitor extends Object with SimpleAstVisitor<Object> {
307297
_maybeWriteReflector();
308298
writer.print('..registerFunction(');
309299
node.name.accept(this);
310-
writer.print(''', {'parameters': const [''');
311-
node.functionExpression.parameters.accept(_paramsVisitor);
312-
writer.print('''], 'annotations': ''');
300+
writer.print(', new ${_REF_PREFIX}.ReflectionInfo(');
313301
node.metadata.accept(_metaVisitor);
314-
writer.print('})');
302+
writer.print(', const [');
303+
node.functionExpression.parameters.accept(_paramsVisitor);
304+
writer.print(']))');
315305
return null;
316306
}
317307

modules/angular2/test/benchmark/transform/bind_generator/simple.dart

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,12 @@ void initReflector(reflector) {
3131
if (_visited) return;
3232
_visited = true;
3333
reflector
34-
..registerType(ToolTip, {
35-
'factory': () => new ToolTip(),
36-
'parameters': const [],
37-
'annotations': const [
34+
..registerType(ToolTip, new ReflectionInfo(
35+
const [
3836
const Decorator(
3937
selector: '[tool-tip]', bind: const {'text': 'tool-tip'})
40-
]
41-
});
38+
],
39+
const [],
40+
() => new ToolTip()
41+
));
4242
}''';

modules/angular2/test/benchmark/transform/directive_linker/simple.dart

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,11 @@ void initReflector(reflector) {
4646
if (_visited) return;
4747
_visited = true;
4848
reflector
49-
..registerType(DependencyComponent, {
50-
'factory': () => new DependencyComponent(),
51-
'parameters': const [],
52-
'annotations': const [const Component(selector: '[salad]')]
53-
});
49+
..registerType(DependencyComponent, new ReflectionInfo(
50+
const [const Component(selector: '[salad]')],
51+
const [],
52+
() => new DependencyComponent()
53+
));
5454
}
5555
''';
5656

@@ -66,12 +66,12 @@ void initReflector(reflector) {
6666
if (_visited) return;
6767
_visited = true;
6868
reflector
69-
..registerType(MyComponent, {
70-
'factory': () => new MyComponent(),
71-
'parameters': const [],
72-
'annotations': const [
69+
..registerType(MyComponent, new ReflectionInfo(
70+
const [
7371
const Component(
7472
selector: '[soup]', services: const [dep.DependencyComponent])
75-
]
76-
});
73+
],
74+
const [],
75+
() => new MyComponent()
76+
));
7777
}''';

modules/angular2/test/benchmark/transform/template_compiler/inline.dart

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,14 @@ void initReflector(reflector) {
3232
if (_visited) return;
3333
_visited = true;
3434
reflector
35-
..registerType(HelloCmp, {
36-
'factory': () => new HelloCmp(),
37-
'parameters': const [const []],
38-
'annotations': const [
35+
..registerType(HelloCmp, new ReflectionInfo(
36+
const [
3937
const Component(selector: 'hello-app'),
4038
const Template(
4139
inline: '<button (click)="action()">go</button>{{greeting}}')
42-
]
43-
});
40+
],
41+
const [const []],
42+
() => new HelloCmp()
43+
));
4444
}
4545
''';

modules/angular2/test/benchmark/transform/template_compiler/url.dart

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,14 @@ void initReflector(reflector) {
3535
if (_visited) return;
3636
_visited = true;
3737
reflector
38-
..registerType(HelloCmp, {
39-
'factory': () => new HelloCmp(),
40-
'parameters': const [const []],
41-
'annotations': const [
38+
..registerType(HelloCmp, new ReflectionInfo(
39+
const [
4240
const Component(selector: 'hello-app'),
4341
const Template(url: 'template.html')
44-
]
45-
});
42+
],
43+
const [const []],
44+
() => new HelloCmp()
45+
));
4646
}
4747
''';
4848

modules/angular2/test/reflection/reflector_spec.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import {describe, it, iit, ddescribe, expect, beforeEach, IS_DARTIUM} from 'angular2/test_lib';
2-
import {Reflector} from 'angular2/src/reflection/reflection';
2+
import {Reflector, ReflectionInfo} from 'angular2/src/reflection/reflection';
33
import {ReflectionCapabilities} from 'angular2/src/reflection/reflection_capabilities';
44
import {ClassDecorator, ParamDecorator, classDecorator, paramDecorator} from './reflector_common';
55
import {List} from 'angular2/src/facade/collection';
@@ -88,7 +88,7 @@ export function main() {
8888
() => { expect(() => reflector.factory(TestObjWith21Args)).toThrowError(); });
8989

9090
it("should return a registered factory if available", () => {
91-
reflector.registerType(TestObj, {"factory": () => "fake"});
91+
reflector.registerType(TestObj, new ReflectionInfo(null, null, () => "fake"));
9292
expect(reflector.factory(TestObj)()).toEqual("fake");
9393
});
9494
});
@@ -105,12 +105,12 @@ export function main() {
105105
});
106106

107107
it("should return registered parameters if available", () => {
108-
reflector.registerType(TestObj, {"parameters": [1, 2]});
109-
expect(reflector.parameters(TestObj)).toEqual([1, 2]);
108+
reflector.registerType(TestObj, new ReflectionInfo(null, [[1], [2]]));
109+
expect(reflector.parameters(TestObj)).toEqual([[1], [2]]);
110110
});
111111

112112
it("should return an empty list when no paramters field in the stored type info", () => {
113-
reflector.registerType(TestObj, {});
113+
reflector.registerType(TestObj, new ReflectionInfo());
114114
expect(reflector.parameters(TestObj)).toEqual([]);
115115
});
116116
});
@@ -122,7 +122,7 @@ export function main() {
122122
});
123123

124124
it("should return registered annotations if available", () => {
125-
reflector.registerType(TestObj, {"annotations": [1, 2]});
125+
reflector.registerType(TestObj, new ReflectionInfo([1, 2]));
126126
expect(reflector.annotations(TestObj)).toEqual([1, 2]);
127127
});
128128

0 commit comments

Comments
 (0)