Skip to content

Commit

Permalink
refactor(core): delete unused reflector code
Browse files Browse the repository at this point in the history
  • Loading branch information
vicb authored and IgorMinar committed Oct 18, 2016
1 parent 853e201 commit 8d72217
Show file tree
Hide file tree
Showing 9 changed files with 22 additions and 277 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,10 @@ export class StaticAndDynamicReflectionCapabilities {

isReflectionEnabled(): boolean { return true; }
factory(type: any): Function { return this.dynamicDelegate.factory(type); }
interfaces(type: any): any[] { return this.dynamicDelegate.interfaces(type); }
hasLifecycleHook(type: any, lcInterface: /*Type*/ any, lcProperty: string): boolean {
return isStaticType(type) ?
this.staticDelegate.hasLifecycleHook(type, lcInterface, lcProperty) :
this.dynamicDelegate.hasLifecycleHook(type, lcInterface, lcProperty);

hasLifecycleHook(type: any, lcProperty: string): boolean {
return isStaticType(type) ? this.staticDelegate.hasLifecycleHook(type, lcProperty) :
this.dynamicDelegate.hasLifecycleHook(type, lcProperty);
}
parameters(type: any): any[][] {
return isStaticType(type) ? this.staticDelegate.parameters(type) :
Expand Down
9 changes: 5 additions & 4 deletions modules/@angular/compiler-cli/src/static_reflector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,14 +159,15 @@ export class StaticReflector implements ReflectorReader {
}
}

hasLifecycleHook(type: any, lcInterface: /*Type*/ any, lcProperty: string): boolean {
hasLifecycleHook(type: any, lcProperty: string): boolean {
if (!(type instanceof StaticSymbol)) {
throw new Error(
`hasLifecycleHook received ${JSON.stringify(type)} which is not a StaticSymbol`);
}
let classMetadata = this.getTypeMetadata(type);
let members = classMetadata ? classMetadata['members'] : null;
let member: any[] = members ? members[lcProperty] : null;
const classMetadata = this.getTypeMetadata(type);
const members = classMetadata ? classMetadata['members'] : null;
const member: any[] =
members && members.hasOwnProperty(lcProperty) ? members[lcProperty] : null;
return member ? member.some(a => a['__symbolic'] == 'method') : false;
}

Expand Down
27 changes: 2 additions & 25 deletions modules/@angular/compiler/src/lifecycle_reflector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,11 @@
* found in the LICENSE file at https://angular.io/license
*/

import {AfterContentChecked, AfterContentInit, AfterViewChecked, AfterViewInit, DoCheck, OnChanges, OnDestroy, OnInit, Type} from '@angular/core';

import {LifecycleHooks, reflector} from './private_import_core';


export function hasLifecycleHook(hook: LifecycleHooks, token: any): boolean {
return reflector.hasLifecycleHook(token, getInterface(hook), getHookName(hook));
return reflector.hasLifecycleHook(token, getHookName(hook));
}

function getHookName(hook: LifecycleHooks): string {
Expand All @@ -34,25 +32,4 @@ function getHookName(hook: LifecycleHooks): string {
case LifecycleHooks.AfterViewChecked:
return 'ngAfterViewChecked';
}
}

function getInterface(hook: LifecycleHooks): any {
switch (hook) {
case LifecycleHooks.OnInit:
return OnInit;
case LifecycleHooks.OnDestroy:
return OnDestroy;
case LifecycleHooks.DoCheck:
return DoCheck;
case LifecycleHooks.OnChanges:
return OnChanges;
case LifecycleHooks.AfterContentInit:
return AfterContentInit;
case LifecycleHooks.AfterContentChecked:
return AfterContentChecked;
case LifecycleHooks.AfterViewInit:
return AfterViewInit;
case LifecycleHooks.AfterViewChecked:
return AfterViewChecked;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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[]};
Expand Down
3 changes: 1 addition & 2 deletions modules/@angular/core/src/reflection/reflection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
117 changes: 6 additions & 111 deletions modules/@angular/core/src/reflection/reflector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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;
}

return this.reflectionCapabilities.hasLifecycleHook(type, lcInterface, lcProperty);
}
getter(name: string): GetterFn { return this.reflectionCapabilities.getter(name); }

getter(name: string): GetterFn {
return this._getters.has(name) ? this._getters.get(name) :
this.reflectionCapabilities.getter(name);
}
setter(name: string): SetterFn { return this.reflectionCapabilities.setter(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); }

Expand All @@ -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]); });
}
Loading

0 comments on commit 8d72217

Please sign in to comment.