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

View File

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

View File

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

View File

@ -1,27 +1,42 @@
import {
ClassType,
ClassType, PropertyKeyType,
} from '@overflow/commons/core/type';
import {
Class,
} from '@overflow/commons/core/reflect';
import {
InstanceNameType,
} from '@overflow/commons/di/type';
import { InjectableAnnotation } from '@overflow/commons/di/decorators';
export class InstanceDefinition {
private _instanceClass: ClassType;
private _name: InstanceNameType;
private _instanceClass: Class;
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;
}
public set instanceClass(instanceClass: ClassType) {
this._instanceClass = instanceClass;
}
public get name(): InstanceNameType {
return this._name;
}
public set name(name: InstanceNameType) {
this._name = name;
/**
* getAttribute
*/
public get annotation(): InjectableAnnotation {
return this._injectableAnnotation;
}
}

View File

@ -27,7 +27,11 @@ export class InstanceFactory implements InstanceDefinitionRegistry {
/**
* 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) {
name = requiredType.name;
}
@ -38,11 +42,7 @@ export class InstanceFactory implements InstanceDefinitionRegistry {
}
let instanceDefinition = this.getInstanceDefinition(name);
let clazzType = instanceDefinition.instanceClass;
let clazz = Class.forClass(clazzType);
if (undefined === clazz) {
return undefined;
}
let clazz = instanceDefinition.instanceClass;
let ctor = clazz.getConstructor();
let ctorParams = ctor.getParameters();
let ctorArgs = [];
@ -52,28 +52,22 @@ export class InstanceFactory implements InstanceDefinitionRegistry {
let instance = ctor.newInstance(ctorArgs);
this.applyInstance(clazzType, instance);
this.applyInstance(clazz.getType(), instance);
return instance;
}
private getInstanceByName(requiredType: ClassType): any | undefined {
return undefined;
}
private getInstanceByType(requiredType: ClassType): any | undefined {
return undefined;
}
/**
* 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
*/