refactor(core): delete unused reflector code

This commit is contained in:
Victor Berchet
2016-10-12 10:05:32 -07:00
committed by Igor Minar
parent bd1dcb5f11
commit 38e2203b24
9 changed files with 22 additions and 277 deletions

View File

@ -12,8 +12,7 @@ import {GetterFn, MethodFn, SetterFn} from './types';
export interface PlatformReflectionCapabilities {
isReflectionEnabled(): boolean;
factory(type: Type<any>): Function;
interfaces(type: Type<any>): any[];
hasLifecycleHook(type: any, lcInterface: Type<any>, lcProperty: string): boolean;
hasLifecycleHook(type: any, lcProperty: string): boolean;
parameters(type: Type<any>): any[][];
annotations(type: Type<any>): any[];
propMetadata(typeOrFunc: Type<any>): {[key: string]: any[]};

View File

@ -9,8 +9,7 @@
import {ReflectionCapabilities} from './reflection_capabilities';
import {Reflector} from './reflector';
export {ReflectionInfo, Reflector} from './reflector';
export {Reflector} from './reflector';
/**
* The {@link Reflector} used internally in Angular to access metadata

View File

@ -128,12 +128,7 @@ export class ReflectionCapabilities implements PlatformReflectionCapabilities {
return {};
}
// Note: JavaScript does not support to query for interfaces during runtime.
// However, we can't throw here as the reflector will always call this method
// when asked for a lifecycle interface as this is what we check in Dart.
interfaces(type: Type<any>): any[] { return []; }
hasLifecycleHook(type: any, lcInterface: Type<any>, lcProperty: string): boolean {
hasLifecycleHook(type: any, lcProperty: string): boolean {
return type instanceof Type && lcProperty in type.prototype;
}

View File

@ -6,7 +6,6 @@
* found in the LICENSE file at https://angular.io/license
*/
import {MapWrapper} from '../facade/collection';
import {Type} from '../type';
import {PlatformReflectionCapabilities} from './platform_reflection_capabilities';
import {ReflectorReader} from './reflector_reader';
@ -15,138 +14,38 @@ import {GetterFn, MethodFn, SetterFn} from './types';
export {PlatformReflectionCapabilities} from './platform_reflection_capabilities';
export {GetterFn, MethodFn, SetterFn} from './types';
/**
* Reflective information about a symbol, including annotations, interfaces, and other metadata.
*/
export class ReflectionInfo {
constructor(
public annotations?: any[], public parameters?: any[][], public factory?: Function,
public interfaces?: any[], public propMetadata?: {[name: string]: any[]}) {}
}
/**
* Provides access to reflection data about symbols. Used internally by Angular
* to power dependency injection and compilation.
*/
export class Reflector extends ReflectorReader {
/** @internal */
_injectableInfo = new Map<any, ReflectionInfo>();
/** @internal */
_getters = new Map<string, GetterFn>();
/** @internal */
_setters = new Map<string, SetterFn>();
/** @internal */
_methods = new Map<string, MethodFn>();
/** @internal */
_usedKeys: Set<any> = null;
constructor(public reflectionCapabilities: PlatformReflectionCapabilities) { super(); }
updateCapabilities(caps: PlatformReflectionCapabilities) { this.reflectionCapabilities = caps; }
/**
* 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) {
throw new Error('Usage tracking is disabled');
}
const allTypes = MapWrapper.keys(this._injectableInfo);
return allTypes.filter(key => !this._usedKeys.has(key));
}
registerType(type: Type<any>, typeInfo: ReflectionInfo): void {
this._injectableInfo.set(type, typeInfo);
}
registerGetters(getters: {[key: string]: GetterFn}): void { _mergeMaps(this._getters, getters); }
registerSetters(setters: {[key: string]: SetterFn}): void { _mergeMaps(this._setters, setters); }
registerMethods(methods: {[key: string]: MethodFn}): void { _mergeMaps(this._methods, methods); }
factory(type: Type<any>): Function {
if (this._containsReflectionInfo(type)) {
return this._getReflectionInfo(type).factory || null;
}
return this.reflectionCapabilities.factory(type);
}
factory(type: Type<any>): Function { return this.reflectionCapabilities.factory(type); }
parameters(typeOrFunc: Type<any>): any[][] {
if (this._injectableInfo.has(typeOrFunc)) {
return this._getReflectionInfo(typeOrFunc).parameters || [];
}
return this.reflectionCapabilities.parameters(typeOrFunc);
}
annotations(typeOrFunc: Type<any>): any[] {
if (this._injectableInfo.has(typeOrFunc)) {
return this._getReflectionInfo(typeOrFunc).annotations || [];
}
return this.reflectionCapabilities.annotations(typeOrFunc);
}
propMetadata(typeOrFunc: Type<any>): {[key: string]: any[]} {
if (this._injectableInfo.has(typeOrFunc)) {
return this._getReflectionInfo(typeOrFunc).propMetadata || {};
}
return this.reflectionCapabilities.propMetadata(typeOrFunc);
}
interfaces(type: Type<any>): any[] {
if (this._injectableInfo.has(type)) {
return this._getReflectionInfo(type).interfaces || [];
}
return this.reflectionCapabilities.interfaces(type);
hasLifecycleHook(type: any, lcProperty: string): boolean {
return this.reflectionCapabilities.hasLifecycleHook(type, lcProperty);
}
hasLifecycleHook(type: any, lcInterface: Type<any>, lcProperty: string): boolean {
if (this.interfaces(type).indexOf(lcInterface) !== -1) {
return true;
}
getter(name: string): GetterFn { return this.reflectionCapabilities.getter(name); }
return this.reflectionCapabilities.hasLifecycleHook(type, lcInterface, lcProperty);
}
setter(name: string): SetterFn { return this.reflectionCapabilities.setter(name); }
getter(name: string): GetterFn {
return this._getters.has(name) ? this._getters.get(name) :
this.reflectionCapabilities.getter(name);
}
setter(name: string): SetterFn {
return this._setters.has(name) ? this._setters.get(name) :
this.reflectionCapabilities.setter(name);
}
method(name: string): MethodFn {
return this._methods.has(name) ? this._methods.get(name) :
this.reflectionCapabilities.method(name);
}
/** @internal */
_getReflectionInfo(typeOrFunc: any): ReflectionInfo {
if (this._usedKeys) {
this._usedKeys.add(typeOrFunc);
}
return this._injectableInfo.get(typeOrFunc);
}
/** @internal */
_containsReflectionInfo(typeOrFunc: any) { return this._injectableInfo.has(typeOrFunc); }
method(name: string): MethodFn { return this.reflectionCapabilities.method(name); }
importUri(type: any): string { return this.reflectionCapabilities.importUri(type); }
@ -158,7 +57,3 @@ export class Reflector extends ReflectorReader {
return this.reflectionCapabilities.resolveEnum(identifier, name);
}
}
function _mergeMaps(target: Map<string, Function>, config: {[key: string]: Function}): void {
Object.keys(config).forEach(k => { target.set(k, config[k]); });
}