This commit is contained in:
crusader 2018-01-12 18:50:02 +09:00
parent 61fb53a49c
commit f0247ea2bd
5 changed files with 51 additions and 4 deletions

View File

@ -9,6 +9,11 @@ import {
AnnotationUtils, AnnotationUtils,
} from '@overflow/commons/core/util'; } from '@overflow/commons/core/util';
import {
InjectableAnnotation,
QualifierAnnotation,
} from '@overflow/commons/di/decorators';
import { import {
InstanceDefinition, InstanceDefinition,
InstanceDefinitionRegistry, InstanceDefinitionRegistry,
@ -21,8 +26,6 @@ import {
} from '@overflow/commons/di/type'; } from '@overflow/commons/di/type';
import { ScopeMetadataResolver } from './scope_metadata_resolver'; import { ScopeMetadataResolver } from './scope_metadata_resolver';
import { InjectableAnnotation, QualifierAnnotation } from '@overflow/commons/di/decorators';
export class InstanceDefinitionReader { export class InstanceDefinitionReader {
private instanceDefinitionRegistry: InstanceDefinitionRegistry; private instanceDefinitionRegistry: InstanceDefinitionRegistry;
@ -40,9 +43,10 @@ export class InstanceDefinitionReader {
}); });
} }
public registerInjectable(injectable: ClassType, name: InstanceNameType | undefined, ...qualifiers: QualifierAnnotation[]): void { public registerInjectable<QT extends Annotation>
(injectable: ClassType, name: InstanceNameType | undefined, ...qualifierTypes: ClassType<QT>[]): void {
if (!AnnotationUtils.hasAnnotation(injectable, InjectableAnnotation)) { if (!AnnotationUtils.hasAnnotation(injectable, InjectableAnnotation)) {
throw new NotInjectableError(); throw new NotInjectableError(`Class[${injectable.name}] does not contains @Injectable`);
} }
let definition: InstanceDefinition = new InstanceDefinition(injectable); let definition: InstanceDefinition = new InstanceDefinition(injectable);

View File

@ -31,6 +31,23 @@ export abstract class AccessibleObject implements AnnotatedElement {
return this._annotations; return this._annotations;
} }
public getOwnAnnotationsByType<AnnotationType extends Annotation>(annotationClass: ClassType<AnnotationType>)
: AnnotationType[] | undefined {
if (0 === this._annotations.size) {
return undefined;
}
let results: AnnotationType[] = [];
for (const classType of Array.from(this._annotations.keys())) {
if (classType === annotationClass) {
results.push(<AnnotationType>this._annotations.get(classType));
}
}
if (0 === results.length) {
return undefined;
}
return results;
}
public getAnnotation<AnnotationType extends Annotation>(annotationClass: ClassType<AnnotationType>): AnnotationType | undefined { public getAnnotation<AnnotationType extends Annotation>(annotationClass: ClassType<AnnotationType>): AnnotationType | undefined {
return this.getOwnAnnotation(annotationClass); return this.getOwnAnnotation(annotationClass);
} }
@ -39,4 +56,8 @@ export abstract class AccessibleObject implements AnnotatedElement {
return this.getOwnAnnotations(); return this.getOwnAnnotations();
} }
public getAnnotationsByType<AnnotationType extends Annotation>(annotationClass: ClassType<AnnotationType>)
: AnnotationType[] | undefined {
return this.getOwnAnnotationsByType(annotationClass);
}
} }

View File

@ -10,6 +10,8 @@ export interface AnnotatedElement {
isAnnotationPresent<AnnotationType extends Annotation>(annotationClass: ClassType<AnnotationType>): boolean; isAnnotationPresent<AnnotationType extends Annotation>(annotationClass: ClassType<AnnotationType>): boolean;
getOwnAnnotation<AnnotationType extends Annotation>(annotationClass: ClassType<AnnotationType>): AnnotationType | undefined; getOwnAnnotation<AnnotationType extends Annotation>(annotationClass: ClassType<AnnotationType>): AnnotationType | undefined;
getOwnAnnotations(): Map<ClassType, Annotation>; getOwnAnnotations(): Map<ClassType, Annotation>;
getOwnAnnotationsByType<AnnotationType extends Annotation>(annotationClass: ClassType<AnnotationType>): AnnotationType[] | undefined;
getAnnotation<AnnotationType extends Annotation>(annotationClass: ClassType<AnnotationType>): AnnotationType | undefined; getAnnotation<AnnotationType extends Annotation>(annotationClass: ClassType<AnnotationType>): AnnotationType | undefined;
getAnnotations(): Map<ClassType, Annotation>; getAnnotations(): Map<ClassType, Annotation>;
getAnnotationsByType<AnnotationType extends Annotation>(annotationClass: ClassType<AnnotationType>): AnnotationType[] | undefined;
} }

View File

@ -201,6 +201,23 @@ export class Class extends AccessibleObject {
return annotations; return annotations;
} }
public getAnnotationsByType<AnnotationType extends Annotation>(annotationClass: ClassType<AnnotationType>)
: AnnotationType[] | undefined {
let annotations = this.getAnnotations();
if (0 === annotations.size) {
return undefined;
}
let results: AnnotationType[] = [];
for (const classType of Array.from(annotations.keys())) {
if (classType === annotationClass) {
results.push(<AnnotationType>annotations.get(classType));
}
}
if (0 === results.length) {
return undefined;
}
return results;
}
public getName(): string { public getName(): string {
return this._clazzType.name; return this._clazzType.name;

View File

@ -33,4 +33,7 @@ export abstract class AnnotationUtils {
return undefined; return undefined;
} }
} }