Skip to content

Commit a621046

Browse files
committed
fix(di): do not use exceptions to detect if reflection is enabled
1 parent 71c65b4 commit a621046

File tree

7 files changed

+23
-9
lines changed

7 files changed

+23
-9
lines changed

modules/angular2/src/di/binding.ts

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -450,16 +450,15 @@ function _extractToken(typeOrFunc, annotations /*List<any> | any*/,
450450
throw new NoAnnotationError(typeOrFunc, params);
451451
}
452452
}
453-
454453
function _defaulVisiblity(typeOrFunc) {
455-
try {
456-
if (!(typeOrFunc instanceof Type)) return unbounded;
457-
var f =
458-
ListWrapper.filter(reflector.annotations(typeOrFunc), s => s instanceof InjectableMetadata);
459-
return f.length === 0 ? unbounded : f[0].visibility;
460-
} catch (e) {
461-
return unbounded;
462-
}
454+
if (!(typeOrFunc instanceof Type)) return unbounded;
455+
456+
// TODO: vsavkin revisit this after clarifying lookup rules
457+
if (!reflector.isReflectionEnabled()) return unbounded;
458+
459+
var f =
460+
ListWrapper.filter(reflector.annotations(typeOrFunc), s => s instanceof InjectableMetadata);
461+
return f.length === 0 ? unbounded : f[0].visibility;
463462
}
464463

465464
function _createDependency(token, optional, visibility, depProps): Dependency {

modules/angular2/src/reflection/platform_reflection_capabilities.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import {GetterFn, SetterFn, MethodFn} from './types';
33
import {List} from 'angular2/src/facade/collection';
44

55
export interface PlatformReflectionCapabilities {
6+
isReflectionEnabled(): boolean;
67
factory(type: Type): Function;
78
interfaces(type: Type): List<any>;
89
parameters(type: Type): List<List<any>>;

modules/angular2/src/reflection/reflection.dart

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ import 'platform_reflection_capabilities.dart';
77
import 'package:angular2/src/facade/lang.dart';
88

99
class NoReflectionCapabilities implements PlatformReflectionCapabilities {
10+
bool isReflectionEnabled() {
11+
return false;
12+
}
13+
1014
Function factory(Type type) {
1115
throw "Cannot find reflection information on ${stringify(type)}";
1216
}

modules/angular2/src/reflection/reflection_capabilities.dart

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ import 'platform_reflection_capabilities.dart';
88
class ReflectionCapabilities implements PlatformReflectionCapabilities {
99
ReflectionCapabilities([metadataReader]) {}
1010

11+
bool isReflectionEnabled() {
12+
return true;
13+
}
14+
1115
Function factory(Type type) {
1216
ClassMirror classMirror = reflectType(type);
1317
MethodMirror ctor = classMirror.declarations[classMirror.simpleName];

modules/angular2/src/reflection/reflection_capabilities.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ export class ReflectionCapabilities implements PlatformReflectionCapabilities {
1515

1616
constructor(reflect?: any) { this._reflect = isPresent(reflect) ? reflect : global.Reflect; }
1717

18+
isReflectionEnabled(): boolean { return true; }
19+
1820
factory(t: Type): Function {
1921
switch (t.length) {
2022
case 0:

modules/angular2/src/reflection/reflector.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ export class Reflector {
2727
this.reflectionCapabilities = reflectionCapabilities;
2828
}
2929

30+
isReflectionEnabled(): boolean { return this.reflectionCapabilities.isReflectionEnabled(); }
31+
3032
registerFunction(func: Function, funcInfo: StringMap<string, any>): void {
3133
this._injectableInfo.set(func, funcInfo);
3234
}

modules/angular2/src/transform/template_compiler/reflection_capabilities.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ class NullReflectionCapabilities implements ReflectionCapabilities {
1010

1111
_notImplemented(String name) => throw 'Not implemented: $name';
1212

13+
bool isReflectionEnabled() { return false; }
14+
1315
Function factory(Type type) => _notImplemented('factory');
1416

1517
List<List> parameters(typeOrFunc) => _notImplemented('parameters');

0 commit comments

Comments
 (0)