This commit is contained in:
crusader 2017-12-30 20:22:22 +09:00
parent fa37e1ba73
commit fd5f14800d
5 changed files with 53 additions and 43 deletions

View File

@ -37,7 +37,7 @@ export class Application {
} }
let instanceFactory = this._applicationContext.instanceFactory; let instanceFactory = this._applicationContext.instanceFactory;
let clazz: Class = Class.forClass(this._primaryClass); let clazz: Class = Class.forClassType(this._primaryClass);
let instance = clazz.getConstructor().newInstance(); let instance = clazz.getConstructor().newInstance();
instanceFactory.applyInstance(this._primaryClass, instance); instanceFactory.applyInstance(this._primaryClass, instance);
@ -47,7 +47,7 @@ export class Application {
} }
private findRunner(): Method | undefined { private findRunner(): Method | undefined {
let clazz: Class = Class.forClass(this._primaryClass); let clazz: Class = Class.forClassType(this._primaryClass);
let methods = clazz.getOwnMethods(); let methods = clazz.getOwnMethods();
for (let key of Array.from(methods.keys())) { for (let key of Array.from(methods.keys())) {
@ -63,7 +63,7 @@ export class Application {
} }
private createContext(): ApplicationContext { private createContext(): ApplicationContext {
let clazz: Class = Class.forClass(this._primaryClass); let clazz: Class = Class.forClassType(this._primaryClass);
let wa: WebApplicationAnnotation = clazz.getOwnAnnotation(WebApplicationAnnotation); let wa: WebApplicationAnnotation = clazz.getOwnAnnotation(WebApplicationAnnotation);
if (undefined === wa) { if (undefined === wa) {
@ -94,7 +94,7 @@ export class Application {
let instanceFactory = this._applicationContext.instanceFactory; let instanceFactory = this._applicationContext.instanceFactory;
configurationTypes.forEach(configurationType => { configurationTypes.forEach(configurationType => {
let clazz: Class = Class.forClass(configurationType); let clazz: Class = Class.forClassType(configurationType);
let configAnnotation: ConfigurationAnnotation = clazz.getOwnAnnotation(ConfigurationAnnotation); let configAnnotation: ConfigurationAnnotation = clazz.getOwnAnnotation(ConfigurationAnnotation);
if (undefined === configAnnotation) { if (undefined === configAnnotation) {
throw new Error(`Class is not Configuration type. add @Configuration annotation to class[${configurationType.name}]`); throw new Error(`Class is not Configuration type. add @Configuration annotation to class[${configurationType.name}]`);

View File

@ -2,6 +2,10 @@ import {
ClassType, ClassType,
} from '@overflow/commons/core/type'; } from '@overflow/commons/core/type';
import {
Class,
} from '@overflow/commons/core/reflect';
import { import {
InstanceDefinition, InstanceDefinition,
InstanceFactory, InstanceFactory,
@ -21,10 +25,7 @@ export class ApplicationContext {
} }
injectables.forEach(injectable => { injectables.forEach(injectable => {
let name = injectable.name; let name = injectable.name;
let definition: InstanceDefinition = new InstanceDefinition(); let definition: InstanceDefinition = new InstanceDefinition(injectable);
definition.instanceClass = injectable;
definition.name = name;
this._instanceFactory.registerInstanceDefinition(name, definition); this._instanceFactory.registerInstanceDefinition(name, definition);
}); });
} }

View File

@ -23,7 +23,7 @@ export class Class extends AccessibleObject {
/** /**
* forClass * forClass
*/ */
public static forClass(clazzType: ClassType): Class | undefined { public static forClassType(clazzType: ClassType): Class | undefined {
return systemClassRegistry.get(clazzType); return systemClassRegistry.get(clazzType);
} }
@ -31,7 +31,7 @@ export class Class extends AccessibleObject {
* _defineClass * _defineClass
*/ */
public static _defineClass(clazzType: ClassType): Class { public static _defineClass(clazzType: ClassType): Class {
let clazz: Class = Class.forClass(clazzType); let clazz: Class = Class.forClassType(clazzType);
if (undefined === clazz) { if (undefined === clazz) {
clazz = new Class(clazzType); clazz = new Class(clazzType);
systemClassRegistry.set(clazzType, clazz); systemClassRegistry.set(clazzType, clazz);
@ -124,7 +124,7 @@ export class Class extends AccessibleObject {
} }
for (let i = 0; i < clazzTypes.length; i++) { for (let i = 0; i < clazzTypes.length; i++) {
const tClazzType = clazzTypes[i]; const tClazzType = clazzTypes[i];
let tClazz = Class.forClass(tClazzType); let tClazz = Class.forClassType(tClazzType);
if (undefined === tClazz) { if (undefined === tClazz) {
continue; continue;
} }
@ -160,7 +160,7 @@ export class Class extends AccessibleObject {
} }
for (let i = 0; i < clazzTypes.length; i++) { for (let i = 0; i < clazzTypes.length; i++) {
const tClazzType = clazzTypes[i]; const tClazzType = clazzTypes[i];
let tClazz = Class.forClass(tClazzType); let tClazz = Class.forClassType(tClazzType);
if (undefined === tClazz) { if (undefined === tClazz) {
continue; continue;
} }
@ -188,7 +188,7 @@ export class Class extends AccessibleObject {
} }
for (let i = 0; i < clazzTypes.length; i++) { for (let i = 0; i < clazzTypes.length; i++) {
const tClazzType = clazzTypes[i]; const tClazzType = clazzTypes[i];
let tClazz = Class.forClass(tClazzType); let tClazz = Class.forClassType(tClazzType);
if (undefined === tClazz) { if (undefined === tClazz) {
continue; continue;
} }

View File

@ -1,27 +1,42 @@
import { import {
ClassType, ClassType, PropertyKeyType,
} from '@overflow/commons/core/type'; } from '@overflow/commons/core/type';
import {
Class,
} from '@overflow/commons/core/reflect';
import { import {
InstanceNameType, InstanceNameType,
} from '@overflow/commons/di/type'; } from '@overflow/commons/di/type';
import { InjectableAnnotation } from '@overflow/commons/di/decorators';
export class InstanceDefinition { export class InstanceDefinition {
private _instanceClass: ClassType; private _instanceClass: Class;
private _name: InstanceNameType; private _injectableAnnotation: InjectableAnnotation;
public get instanceClass(): ClassType { public constructor(instanceClassType: ClassType) {
let clazz = Class.forClassType(instanceClassType);
if (undefined === clazz) {
throw new Error(`Class[${instanceClassType.name}] is not injectable type. Add @Injectable annotation.`);
}
let injectableAnnotation: InjectableAnnotation = clazz.getOwnAnnotation(InjectableAnnotation);
if (undefined === injectableAnnotation) {
throw new Error(`Class is not Injectable type. Add @Injectable annotation to class[${instanceClassType.name}]`);
}
this._injectableAnnotation = injectableAnnotation;
}
public get instanceClass(): Class {
return this._instanceClass; return this._instanceClass;
} }
public set instanceClass(instanceClass: ClassType) {
this._instanceClass = instanceClass;
}
public get name(): InstanceNameType { /**
return this._name; * getAttribute
} */
public set name(name: InstanceNameType) { public get annotation(): InjectableAnnotation {
this._name = name; return this._injectableAnnotation;
} }
} }

View File

@ -27,7 +27,11 @@ export class InstanceFactory implements InstanceDefinitionRegistry {
/** /**
* getInstance * getInstance
*/ */
public getInstance(name: InstanceNameType, requiredType: ClassType, ...args: any[]): any | undefined { public getInstance(name: InstanceNameType | undefined, requiredType: ClassType | undefined, ...args: any[]): any | undefined {
if (undefined === name && undefined === requiredType) {
throw new Error('One of name or requiredType must be specified.');
}
if (undefined === name) { if (undefined === name) {
name = requiredType.name; name = requiredType.name;
} }
@ -38,11 +42,7 @@ export class InstanceFactory implements InstanceDefinitionRegistry {
} }
let instanceDefinition = this.getInstanceDefinition(name); let instanceDefinition = this.getInstanceDefinition(name);
let clazzType = instanceDefinition.instanceClass; let clazz = instanceDefinition.instanceClass;
let clazz = Class.forClass(clazzType);
if (undefined === clazz) {
return undefined;
}
let ctor = clazz.getConstructor(); let ctor = clazz.getConstructor();
let ctorParams = ctor.getParameters(); let ctorParams = ctor.getParameters();
let ctorArgs = []; let ctorArgs = [];
@ -52,28 +52,22 @@ export class InstanceFactory implements InstanceDefinitionRegistry {
let instance = ctor.newInstance(ctorArgs); let instance = ctor.newInstance(ctorArgs);
this.applyInstance(clazzType, instance); this.applyInstance(clazz.getType(), instance);
return instance; return instance;
} }
private getInstanceByName(requiredType: ClassType): any | undefined {
return undefined;
}
private getInstanceByType(requiredType: ClassType): any | undefined {
return undefined;
}
/** /**
* applyInstance * applyInstance
*/ */
public injectByName(clazzType: ClassType, instance: any): void { public injectByName(name: InstanceNameType, instanceDefinition: InstanceDefinition): void {
//
} }
public injectByType(clazzType: ClassType, instance: any): void { public injectByType(name: InstanceNameType, instanceDefinition: InstanceDefinition): void {
//
} }
/** /**
* applyInstance * applyInstance
*/ */