feat(compiler): Resolvers now use DI to create reflector

Also introduced ReflectorReader when only read-only access to the reflector
is needed.

Closes #7762
This commit is contained in:
Chuck Jazdzewski 2016-03-24 13:32:47 -07:00 committed by Kara Erickson
parent a0387d2835
commit 506f4ce1e5
7 changed files with 59 additions and 10 deletions

View File

@ -16,6 +16,7 @@ import {
ViewChildMetadata ViewChildMetadata
} from 'angular2/src/core/metadata'; } from 'angular2/src/core/metadata';
import {reflector} from 'angular2/src/core/reflection/reflection'; import {reflector} from 'angular2/src/core/reflection/reflection';
import {ReflectorReader} from 'angular2/src/core/reflection/reflector_reader';
function _isDirectiveMetadata(type: any): boolean { function _isDirectiveMetadata(type: any): boolean {
return type instanceof DirectiveMetadata; return type instanceof DirectiveMetadata;
@ -30,15 +31,25 @@ function _isDirectiveMetadata(type: any): boolean {
*/ */
@Injectable() @Injectable()
export class DirectiveResolver { export class DirectiveResolver {
private _reflector: ReflectorReader;
constructor(_reflector?: ReflectorReader) {
if (isPresent(_reflector)) {
this._reflector = _reflector;
} else {
this._reflector = reflector;
}
}
/** /**
* Return {@link DirectiveMetadata} for a given `Type`. * Return {@link DirectiveMetadata} for a given `Type`.
*/ */
resolve(type: Type): DirectiveMetadata { resolve(type: Type): DirectiveMetadata {
var typeMetadata = reflector.annotations(resolveForwardRef(type)); var typeMetadata = this._reflector.annotations(resolveForwardRef(type));
if (isPresent(typeMetadata)) { if (isPresent(typeMetadata)) {
var metadata = typeMetadata.find(_isDirectiveMetadata); var metadata = typeMetadata.find(_isDirectiveMetadata);
if (isPresent(metadata)) { if (isPresent(metadata)) {
var propertyMetadata = reflector.propMetadata(type); var propertyMetadata = this._reflector.propMetadata(type);
return this._mergeWithPropertyMetadata(metadata, propertyMetadata, type); return this._mergeWithPropertyMetadata(metadata, propertyMetadata, type);
} }
} }
@ -155,4 +166,4 @@ export class DirectiveResolver {
} }
} }
export var CODEGEN_DIRECTIVE_RESOLVER = new DirectiveResolver(); export var CODEGEN_DIRECTIVE_RESOLVER = new DirectiveResolver(reflector);

View File

@ -2,6 +2,7 @@ import {resolveForwardRef, Injectable} from 'angular2/src/core/di';
import {Type, isPresent, stringify} from 'angular2/src/facade/lang'; import {Type, isPresent, stringify} from 'angular2/src/facade/lang';
import {BaseException} from 'angular2/src/facade/exceptions'; import {BaseException} from 'angular2/src/facade/exceptions';
import {PipeMetadata} from 'angular2/src/core/metadata'; import {PipeMetadata} from 'angular2/src/core/metadata';
import {ReflectorReader} from 'angular2/src/core/reflection/reflector_reader';
import {reflector} from 'angular2/src/core/reflection/reflection'; import {reflector} from 'angular2/src/core/reflection/reflection';
function _isPipeMetadata(type: any): boolean { function _isPipeMetadata(type: any): boolean {
@ -17,11 +18,20 @@ function _isPipeMetadata(type: any): boolean {
*/ */
@Injectable() @Injectable()
export class PipeResolver { export class PipeResolver {
private _reflector: ReflectorReader;
constructor(_reflector?: ReflectorReader) {
if (isPresent(_reflector)) {
this._reflector = _reflector;
} else {
this._reflector = reflector;
}
}
/** /**
* Return {@link PipeMetadata} for a given `Type`. * Return {@link PipeMetadata} for a given `Type`.
*/ */
resolve(type: Type): PipeMetadata { resolve(type: Type): PipeMetadata {
var metas = reflector.annotations(resolveForwardRef(type)); var metas = this._reflector.annotations(resolveForwardRef(type));
if (isPresent(metas)) { if (isPresent(metas)) {
var annotation = metas.find(_isPipeMetadata); var annotation = metas.find(_isPipeMetadata);
if (isPresent(annotation)) { if (isPresent(annotation)) {
@ -32,4 +42,4 @@ export class PipeResolver {
} }
} }
export var CODEGEN_PIPE_RESOLVER = new PipeResolver(); export var CODEGEN_PIPE_RESOLVER = new PipeResolver(reflector);

View File

@ -6,17 +6,27 @@ import {Type, stringify, isBlank, isPresent} from 'angular2/src/facade/lang';
import {BaseException} from 'angular2/src/facade/exceptions'; import {BaseException} from 'angular2/src/facade/exceptions';
import {Map} from 'angular2/src/facade/collection'; import {Map} from 'angular2/src/facade/collection';
import {ReflectorReader} from 'angular2/src/core/reflection/reflector_reader';
import {reflector} from 'angular2/src/core/reflection/reflection'; import {reflector} from 'angular2/src/core/reflection/reflection';
/** /**
* Resolves types to {@link ViewMetadata}. * Resolves types to {@link ViewMetadata}.
*/ */
@Injectable() @Injectable()
export class ViewResolver { export class ViewResolver {
private _reflector: ReflectorReader;
/** @internal */ /** @internal */
_cache = new Map<Type, ViewMetadata>(); _cache = new Map<Type, ViewMetadata>();
constructor(_reflector?: ReflectorReader) {
if (isPresent(_reflector)) {
this._reflector = _reflector;
} else {
this._reflector = reflector;
}
}
resolve(component: Type): ViewMetadata { resolve(component: Type): ViewMetadata {
var view = this._cache.get(component); var view = this._cache.get(component);
@ -33,7 +43,7 @@ export class ViewResolver {
var compMeta: ComponentMetadata; var compMeta: ComponentMetadata;
var viewMeta: ViewMetadata; var viewMeta: ViewMetadata;
reflector.annotations(component).forEach(m => { this._reflector.annotations(component).forEach(m => {
if (m instanceof ViewMetadata) { if (m instanceof ViewMetadata) {
viewMeta = m; viewMeta = m;
} }

View File

@ -2,6 +2,7 @@ import {Type, isBlank, isPresent, assertionsEnabled, CONST_EXPR} from 'angular2/
import {provide, Provider, Injector, OpaqueToken} from 'angular2/src/core/di'; import {provide, Provider, Injector, OpaqueToken} from 'angular2/src/core/di';
import {Console} from 'angular2/src/core/console'; import {Console} from 'angular2/src/core/console';
import {Reflector, reflector} from './reflection/reflection'; import {Reflector, reflector} from './reflection/reflection';
import {ReflectorReader} from './reflection/reflector_reader';
import {TestabilityRegistry} from 'angular2/src/core/testability/testability'; import {TestabilityRegistry} from 'angular2/src/core/testability/testability';
function _reflector(): Reflector { function _reflector(): Reflector {
@ -11,5 +12,9 @@ function _reflector(): Reflector {
/** /**
* A default set of providers which should be included in any Angular platform. * A default set of providers which should be included in any Angular platform.
*/ */
export const PLATFORM_COMMON_PROVIDERS: Array<Type | Provider | any[]> = CONST_EXPR( export const PLATFORM_COMMON_PROVIDERS: Array<Type | Provider | any[]> = CONST_EXPR([
[new Provider(Reflector, {useFactory: _reflector, deps: []}), TestabilityRegistry, Console]); new Provider(Reflector, {useFactory: _reflector, deps: []}),
new Provider(ReflectorReader, {useExisting: Reflector}),
TestabilityRegistry,
Console
]);

View File

@ -9,6 +9,7 @@ import {
StringMapWrapper StringMapWrapper
} from 'angular2/src/facade/collection'; } from 'angular2/src/facade/collection';
import {SetterFn, GetterFn, MethodFn} from './types'; import {SetterFn, GetterFn, MethodFn} from './types';
import {ReflectorReader} from './reflector_reader';
import {PlatformReflectionCapabilities} from './platform_reflection_capabilities'; import {PlatformReflectionCapabilities} from './platform_reflection_capabilities';
export {SetterFn, GetterFn, MethodFn} from './types'; export {SetterFn, GetterFn, MethodFn} from './types';
export {PlatformReflectionCapabilities} from './platform_reflection_capabilities'; export {PlatformReflectionCapabilities} from './platform_reflection_capabilities';
@ -25,7 +26,7 @@ export class ReflectionInfo {
* Provides access to reflection data about symbols. Used internally by Angular * Provides access to reflection data about symbols. Used internally by Angular
* to power dependency injection and compilation. * to power dependency injection and compilation.
*/ */
export class Reflector { export class Reflector extends ReflectorReader {
/** @internal */ /** @internal */
_injectableInfo = new Map<any, ReflectionInfo>(); _injectableInfo = new Map<any, ReflectionInfo>();
/** @internal */ /** @internal */
@ -39,6 +40,7 @@ export class Reflector {
reflectionCapabilities: PlatformReflectionCapabilities; reflectionCapabilities: PlatformReflectionCapabilities;
constructor(reflectionCapabilities: PlatformReflectionCapabilities) { constructor(reflectionCapabilities: PlatformReflectionCapabilities) {
super();
this._usedKeys = null; this._usedKeys = null;
this.reflectionCapabilities = reflectionCapabilities; this.reflectionCapabilities = reflectionCapabilities;
} }

View File

@ -0,0 +1,9 @@
/**
* Provides read-only access to reflection data about symbols. Used internally by Angular
* to power dependency injection and compilation.
*/
export abstract class ReflectorReader {
abstract parameters(typeOrFunc: /*Type*/ any): any[][];
abstract annotations(typeOrFunc: /*Type*/ any): any[];
abstract propMetadata(typeOrFunc: /*Type*/ any): {[key: string]: any[]};
}

View File

@ -169,6 +169,7 @@ const CORE = [
'DirectiveMetadata.queries:{[key:string]:any}', 'DirectiveMetadata.queries:{[key:string]:any}',
'DirectiveMetadata.selector:string', 'DirectiveMetadata.selector:string',
'DirectiveResolver', 'DirectiveResolver',
'DirectiveResolver.constructor(_reflector:ReflectorReader)',
'DirectiveResolver.resolve(type:Type):DirectiveMetadata', 'DirectiveResolver.resolve(type:Type):DirectiveMetadata',
'DoCheck', 'DoCheck',
'DoCheck.ngDoCheck():any', 'DoCheck.ngDoCheck():any',
@ -493,6 +494,7 @@ const CORE = [
'ViewRef.changeDetectorRef:ChangeDetectorRef', 'ViewRef.changeDetectorRef:ChangeDetectorRef',
'ViewRef.destroyed:boolean', 'ViewRef.destroyed:boolean',
'ViewResolver', 'ViewResolver',
'ViewResolver.constructor(_reflector:ReflectorReader)',
'ViewResolver.resolve(component:Type):ViewMetadata', 'ViewResolver.resolve(component:Type):ViewMetadata',
'WrappedException', 'WrappedException',
'WrappedException.constructor(_wrapperMessage:string, _originalException:any, _originalStack:any, _context:any)', 'WrappedException.constructor(_wrapperMessage:string, _originalException:any, _originalStack:any, _context:any)',