fix(di): do not use exceptions to detect if reflection is enabled

This commit is contained in:
vsavkin 2015-07-10 08:54:53 -07:00
parent 71c65b47f9
commit a6210466c7
7 changed files with 23 additions and 9 deletions

View File

@ -450,16 +450,15 @@ function _extractToken(typeOrFunc, annotations /*List<any> | any*/,
throw new NoAnnotationError(typeOrFunc, params);
}
}
function _defaulVisiblity(typeOrFunc) {
try {
if (!(typeOrFunc instanceof Type)) return unbounded;
// TODO: vsavkin revisit this after clarifying lookup rules
if (!reflector.isReflectionEnabled()) return unbounded;
var f =
ListWrapper.filter(reflector.annotations(typeOrFunc), s => s instanceof InjectableMetadata);
return f.length === 0 ? unbounded : f[0].visibility;
} catch (e) {
return unbounded;
}
}
function _createDependency(token, optional, visibility, depProps): Dependency {

View File

@ -3,6 +3,7 @@ import {GetterFn, SetterFn, MethodFn} from './types';
import {List} from 'angular2/src/facade/collection';
export interface PlatformReflectionCapabilities {
isReflectionEnabled(): boolean;
factory(type: Type): Function;
interfaces(type: Type): List<any>;
parameters(type: Type): List<List<any>>;

View File

@ -7,6 +7,10 @@ import 'platform_reflection_capabilities.dart';
import 'package:angular2/src/facade/lang.dart';
class NoReflectionCapabilities implements PlatformReflectionCapabilities {
bool isReflectionEnabled() {
return false;
}
Function factory(Type type) {
throw "Cannot find reflection information on ${stringify(type)}";
}

View File

@ -8,6 +8,10 @@ import 'platform_reflection_capabilities.dart';
class ReflectionCapabilities implements PlatformReflectionCapabilities {
ReflectionCapabilities([metadataReader]) {}
bool isReflectionEnabled() {
return true;
}
Function factory(Type type) {
ClassMirror classMirror = reflectType(type);
MethodMirror ctor = classMirror.declarations[classMirror.simpleName];

View File

@ -15,6 +15,8 @@ export class ReflectionCapabilities implements PlatformReflectionCapabilities {
constructor(reflect?: any) { this._reflect = isPresent(reflect) ? reflect : global.Reflect; }
isReflectionEnabled(): boolean { return true; }
factory(t: Type): Function {
switch (t.length) {
case 0:

View File

@ -27,6 +27,8 @@ export class Reflector {
this.reflectionCapabilities = reflectionCapabilities;
}
isReflectionEnabled(): boolean { return this.reflectionCapabilities.isReflectionEnabled(); }
registerFunction(func: Function, funcInfo: StringMap<string, any>): void {
this._injectableInfo.set(func, funcInfo);
}

View File

@ -10,6 +10,8 @@ class NullReflectionCapabilities implements ReflectionCapabilities {
_notImplemented(String name) => throw 'Not implemented: $name';
bool isReflectionEnabled() { return false; }
Function factory(Type type) => _notImplemented('factory');
List<List> parameters(typeOrFunc) => _notImplemented('parameters');