refactor(Type): merge Type and ConcreType<?> into Type<?> (#10616)
Closes #9729 BREAKING CHANGE: `Type` is now `Type<T>` which means that in most cases you have to use `Type<any>` in place of `Type`. We don't expect that any user applications use the `Type` type.
This commit is contained in:
@ -6,20 +6,19 @@
|
||||
* found in the LICENSE file at https://angular.io/license
|
||||
*/
|
||||
|
||||
import {Type} from '../facade/lang';
|
||||
|
||||
import {Type} from '../type';
|
||||
import {GetterFn, MethodFn, SetterFn} from './types';
|
||||
|
||||
export interface PlatformReflectionCapabilities {
|
||||
isReflectionEnabled(): boolean;
|
||||
factory(type: Type): Function;
|
||||
interfaces(type: Type): any[];
|
||||
hasLifecycleHook(type: any, lcInterface: /*Type*/ any, lcProperty: string): boolean;
|
||||
parameters(type: any): any[][];
|
||||
annotations(type: any): any[];
|
||||
propMetadata(typeOrFunc: any): {[key: string]: any[]};
|
||||
factory(type: Type<any>): Function;
|
||||
interfaces(type: Type<any>): any[];
|
||||
hasLifecycleHook(type: any, lcInterface: Type<any>, lcProperty: string): boolean;
|
||||
parameters(type: Type<any>): any[][];
|
||||
annotations(type: Type<any>): any[];
|
||||
propMetadata(typeOrFunc: Type<any>): {[key: string]: any[]};
|
||||
getter(name: string): GetterFn;
|
||||
setter(name: string): SetterFn;
|
||||
method(name: string): MethodFn;
|
||||
importUri(type: any): string;
|
||||
importUri(type: Type<any>): string;
|
||||
}
|
||||
|
@ -6,8 +6,8 @@
|
||||
* found in the LICENSE file at https://angular.io/license
|
||||
*/
|
||||
|
||||
import {BaseException} from '../facade/exceptions';
|
||||
import {ConcreteType, Type, global, isFunction, isPresent, stringify} from '../facade/lang';
|
||||
import {global, isFunction, isPresent, stringify} from '../facade/lang';
|
||||
import {Type} from '../type';
|
||||
|
||||
import {PlatformReflectionCapabilities} from './platform_reflection_capabilities';
|
||||
import {GetterFn, MethodFn, SetterFn} from './types';
|
||||
@ -15,92 +15,17 @@ import {GetterFn, MethodFn, SetterFn} from './types';
|
||||
export class ReflectionCapabilities implements PlatformReflectionCapabilities {
|
||||
private _reflect: any;
|
||||
|
||||
constructor(reflect?: any) { this._reflect = isPresent(reflect) ? reflect : global.Reflect; }
|
||||
constructor(reflect?: any) { this._reflect = reflect || global.Reflect; }
|
||||
|
||||
isReflectionEnabled(): boolean { return true; }
|
||||
|
||||
factory(t: ConcreteType<any>): Function {
|
||||
switch (t.length) {
|
||||
case 0:
|
||||
return () => new t();
|
||||
case 1:
|
||||
return (a1: any) => new t(a1);
|
||||
case 2:
|
||||
return (a1: any, a2: any) => new t(a1, a2);
|
||||
case 3:
|
||||
return (a1: any, a2: any, a3: any) => new t(a1, a2, a3);
|
||||
case 4:
|
||||
return (a1: any, a2: any, a3: any, a4: any) => new t(a1, a2, a3, a4);
|
||||
case 5:
|
||||
return (a1: any, a2: any, a3: any, a4: any, a5: any) => new t(a1, a2, a3, a4, a5);
|
||||
case 6:
|
||||
return (a1: any, a2: any, a3: any, a4: any, a5: any, a6: any) =>
|
||||
new t(a1, a2, a3, a4, a5, a6);
|
||||
case 7:
|
||||
return (a1: any, a2: any, a3: any, a4: any, a5: any, a6: any, a7: any) =>
|
||||
new t(a1, a2, a3, a4, a5, a6, a7);
|
||||
case 8:
|
||||
return (a1: any, a2: any, a3: any, a4: any, a5: any, a6: any, a7: any, a8: any) =>
|
||||
new t(a1, a2, a3, a4, a5, a6, a7, a8);
|
||||
case 9:
|
||||
return (a1: any, a2: any, a3: any, a4: any, a5: any, a6: any, a7: any, a8: any, a9: any) =>
|
||||
new t(a1, a2, a3, a4, a5, a6, a7, a8, a9);
|
||||
case 10:
|
||||
return (a1: any, a2: any, a3: any, a4: any, a5: any, a6: any, a7: any, a8: any, a9: any,
|
||||
a10: any) => new t(a1, a2, a3, a4, a5, a6, a7, a8, a9, a10);
|
||||
case 11:
|
||||
return (a1: any, a2: any, a3: any, a4: any, a5: any, a6: any, a7: any, a8: any, a9: any,
|
||||
a10: any, a11: any) => new t(a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11);
|
||||
case 12:
|
||||
return (a1: any, a2: any, a3: any, a4: any, a5: any, a6: any, a7: any, a8: any, a9: any,
|
||||
a10: any, a11: any, a12: any) =>
|
||||
new t(a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12);
|
||||
case 13:
|
||||
return (a1: any, a2: any, a3: any, a4: any, a5: any, a6: any, a7: any, a8: any, a9: any,
|
||||
a10: any, a11: any, a12: any, a13: any) =>
|
||||
new t(a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13);
|
||||
case 14:
|
||||
return (a1: any, a2: any, a3: any, a4: any, a5: any, a6: any, a7: any, a8: any, a9: any,
|
||||
a10: any, a11: any, a12: any, a13: any, a14: any) =>
|
||||
new t(a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14);
|
||||
case 15:
|
||||
return (a1: any, a2: any, a3: any, a4: any, a5: any, a6: any, a7: any, a8: any, a9: any,
|
||||
a10: any, a11: any, a12: any, a13: any, a14: any, a15: any) =>
|
||||
new t(a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14, a15);
|
||||
case 16:
|
||||
return (a1: any, a2: any, a3: any, a4: any, a5: any, a6: any, a7: any, a8: any, a9: any,
|
||||
a10: any, a11: any, a12: any, a13: any, a14: any, a15: any, a16: any) =>
|
||||
new t(a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14, a15, a16);
|
||||
case 17:
|
||||
return (a1: any, a2: any, a3: any, a4: any, a5: any, a6: any, a7: any, a8: any, a9: any,
|
||||
a10: any, a11: any, a12: any, a13: any, a14: any, a15: any, a16: any, a17: any) =>
|
||||
new t(
|
||||
a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14, a15, a16, a17);
|
||||
case 18:
|
||||
return (a1: any, a2: any, a3: any, a4: any, a5: any, a6: any, a7: any, a8: any, a9: any,
|
||||
a10: any, a11: any, a12: any, a13: any, a14: any, a15: any, a16: any, a17: any,
|
||||
a18: any) =>
|
||||
new t(
|
||||
a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14, a15, a16, a17,
|
||||
a18);
|
||||
case 19:
|
||||
return (a1: any, a2: any, a3: any, a4: any, a5: any, a6: any, a7: any, a8: any, a9: any,
|
||||
a10: any, a11: any, a12: any, a13: any, a14: any, a15: any, a16: any, a17: any,
|
||||
a18: any, a19: any) =>
|
||||
new t(
|
||||
a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14, a15, a16, a17,
|
||||
a18, a19);
|
||||
case 20:
|
||||
return (a1: any, a2: any, a3: any, a4: any, a5: any, a6: any, a7: any, a8: any, a9: any,
|
||||
a10: any, a11: any, a12: any, a13: any, a14: any, a15: any, a16: any, a17: any,
|
||||
a18: any, a19: any, a20: any) =>
|
||||
new t(
|
||||
a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14, a15, a16, a17,
|
||||
a18, a19, a20);
|
||||
factory(t: Type<any>): Function {
|
||||
var prototype = t.prototype;
|
||||
return function(...args: any[]) {
|
||||
var instance = Object.create(prototype);
|
||||
t.apply(instance, args);
|
||||
return instance;
|
||||
};
|
||||
|
||||
throw new Error(
|
||||
`Cannot create a factory for '${stringify(t)}' because its constructor has more than 20 arguments`);
|
||||
}
|
||||
|
||||
/** @internal */
|
||||
@ -132,7 +57,7 @@ export class ReflectionCapabilities implements PlatformReflectionCapabilities {
|
||||
return result;
|
||||
}
|
||||
|
||||
parameters(typeOrFunc: Type): any[][] {
|
||||
parameters(typeOrFunc: Type<any>): any[][] {
|
||||
// Prefer the direct API.
|
||||
if (isPresent((<any>typeOrFunc).parameters)) {
|
||||
return (<any>typeOrFunc).parameters;
|
||||
@ -163,7 +88,7 @@ export class ReflectionCapabilities implements PlatformReflectionCapabilities {
|
||||
return parameters;
|
||||
}
|
||||
|
||||
annotations(typeOrFunc: Type): any[] {
|
||||
annotations(typeOrFunc: Type<any>): any[] {
|
||||
// Prefer the direct API.
|
||||
if (isPresent((<any>typeOrFunc).annotations)) {
|
||||
var annotations = (<any>typeOrFunc).annotations;
|
||||
@ -217,9 +142,9 @@ export class ReflectionCapabilities implements PlatformReflectionCapabilities {
|
||||
// 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[] { return []; }
|
||||
interfaces(type: Type<any>): any[] { return []; }
|
||||
|
||||
hasLifecycleHook(type: any, lcInterface: Type, lcProperty: string): boolean {
|
||||
hasLifecycleHook(type: any, lcInterface: Type<any>, lcProperty: string): boolean {
|
||||
if (!(type instanceof Type)) return false;
|
||||
|
||||
var proto = (<any>type).prototype;
|
||||
|
@ -8,8 +8,8 @@
|
||||
|
||||
import {Map, MapWrapper, Set, SetWrapper, StringMapWrapper} from '../facade/collection';
|
||||
import {BaseException} from '../facade/exceptions';
|
||||
import {Type, isPresent} from '../facade/lang';
|
||||
|
||||
import {isPresent} from '../facade/lang';
|
||||
import {Type} from '../type';
|
||||
import {PlatformReflectionCapabilities} from './platform_reflection_capabilities';
|
||||
import {ReflectorReader} from './reflector_reader';
|
||||
import {GetterFn, MethodFn, SetterFn} from './types';
|
||||
@ -77,7 +77,7 @@ export class Reflector extends ReflectorReader {
|
||||
this._injectableInfo.set(func, funcInfo);
|
||||
}
|
||||
|
||||
registerType(type: Type, typeInfo: ReflectionInfo): void {
|
||||
registerType(type: Type<any>, typeInfo: ReflectionInfo): void {
|
||||
this._injectableInfo.set(type, typeInfo);
|
||||
}
|
||||
|
||||
@ -87,7 +87,7 @@ export class Reflector extends ReflectorReader {
|
||||
|
||||
registerMethods(methods: {[key: string]: MethodFn}): void { _mergeMaps(this._methods, methods); }
|
||||
|
||||
factory(type: Type): Function {
|
||||
factory(type: Type<any>): Function {
|
||||
if (this._containsReflectionInfo(type)) {
|
||||
var res = this._getReflectionInfo(type).factory;
|
||||
return isPresent(res) ? res : null;
|
||||
@ -96,7 +96,7 @@ export class Reflector extends ReflectorReader {
|
||||
}
|
||||
}
|
||||
|
||||
parameters(typeOrFunc: /*Type*/ any): any[][] {
|
||||
parameters(typeOrFunc: Type<any>): any[][] {
|
||||
if (this._injectableInfo.has(typeOrFunc)) {
|
||||
var res = this._getReflectionInfo(typeOrFunc).parameters;
|
||||
return isPresent(res) ? res : [];
|
||||
@ -105,7 +105,7 @@ export class Reflector extends ReflectorReader {
|
||||
}
|
||||
}
|
||||
|
||||
annotations(typeOrFunc: /*Type*/ any): any[] {
|
||||
annotations(typeOrFunc: Type<any>): any[] {
|
||||
if (this._injectableInfo.has(typeOrFunc)) {
|
||||
var res = this._getReflectionInfo(typeOrFunc).annotations;
|
||||
return isPresent(res) ? res : [];
|
||||
@ -114,7 +114,7 @@ export class Reflector extends ReflectorReader {
|
||||
}
|
||||
}
|
||||
|
||||
propMetadata(typeOrFunc: /*Type*/ any): {[key: string]: any[]} {
|
||||
propMetadata(typeOrFunc: Type<any>): {[key: string]: any[]} {
|
||||
if (this._injectableInfo.has(typeOrFunc)) {
|
||||
var res = this._getReflectionInfo(typeOrFunc).propMetadata;
|
||||
return isPresent(res) ? res : {};
|
||||
@ -123,7 +123,7 @@ export class Reflector extends ReflectorReader {
|
||||
}
|
||||
}
|
||||
|
||||
interfaces(type: /*Type*/ any): any[] {
|
||||
interfaces(type: Type<any>): any[] {
|
||||
if (this._injectableInfo.has(type)) {
|
||||
var res = this._getReflectionInfo(type).interfaces;
|
||||
return isPresent(res) ? res : [];
|
||||
@ -132,7 +132,7 @@ export class Reflector extends ReflectorReader {
|
||||
}
|
||||
}
|
||||
|
||||
hasLifecycleHook(type: any, lcInterface: Type, lcProperty: string): boolean {
|
||||
hasLifecycleHook(type: any, lcInterface: Type<any>, lcProperty: string): boolean {
|
||||
var interfaces = this.interfaces(type);
|
||||
if (interfaces.indexOf(lcInterface) !== -1) {
|
||||
return true;
|
||||
|
Reference in New Issue
Block a user