import {AnnotationAccessor} from "./AnnotationAccessor"; import { AccessorType, AnnotationTarget, AttributeDecorator, AttributeDecoratorRef, DecoratorClazz, IAccessorType, Invokable, MethodDecorator, ParameterDecorator } from "./Interfaces"; import {propertiesDescriptor} from "../decorator/DecoratorInterfaces"; import {Constructor} from "../Constructor"; import {Annotation} from "./Annotation"; import {Method} from "../Reflect/Method"; import {Member} from "../Reflect/Interfaces"; import {RuntimeException, UnsupportedOperationException} from "../Exception"; import {Class} from "../Class"; import {ObjectsReflector} from "../Reflect/ObjectsReflector"; import {Field} from "../Reflect/Field"; import {Parameter} from "../Reflect/Parameter"; import {Objects} from "../type/Objects"; import {Risk} from "../init/Risk"; interface DecoratorClass{ (constructor:Function,propertyName:string ):void } export abstract class AnnotationHandlers { /*** * Constant * @type {number} */ private static readonly CLASS:number = 0x04; private static readonly ATTRIBUTE:number = 0x08; private static readonly METHOD:number = 0x10; private static readonly PARAM:number = 0x20; private static readonly AccessorType = class AccessorType implements IAccessorType{ protected construct:Constructor; protected type: number; constructor(constructor:Constructor, type: number) { this.construct = constructor; this.type = type; } getConstructor():Constructor{return this.construct;} getType():number{ return this.type; } }; protected static staticAccessorType(construct:Constructor):AccessorType{ return new class StaticAccessorType extends AnnotationHandlers.AccessorType{ constructor() {super(construct,null);} getType(): number {return ObjectsReflector.STATIC;} }; } protected static instancedAccessorType(construct:Constructor):AccessorType{ return new class InstancedAccessorType extends AnnotationHandlers.AccessorType{ constructor() {super(construct,null);} getType(): number {return ObjectsReflector.INSTANCED;} }; } protected static get(target:Object):AccessorType{ return typeof target === "function" ? AnnotationHandlers.staticAccessorType(target.class()) : AnnotationHandlers.instancedAccessorType(target.getClass().getConstructor()); } protected static getMember(target:Object, propertyName:string = null, index:number, memberType:number):Member{ let currentTarget:AccessorType = AnnotationHandlers.get(Objects.requireNotNull(target)); switch (memberType) { case AnnotationHandlers.PARAM: return currentTarget .getConstructor() .getMethod(propertyName, currentTarget.getType()) .getParameter()[index]; case AnnotationHandlers.METHOD: return currentTarget .getConstructor() .getMethod(propertyName, currentTarget.getType()); case AnnotationHandlers.ATTRIBUTE: let member:Member = currentTarget.getConstructor() .getField(propertyName, currentTarget.getType()); // may this var was not be initialized with real data if(member==null) return new Field( propertyName, null, currentTarget.getType(), currentTarget.getConstructor().getDeclaringClass(), currentTarget.getConstructor() ); return currentTarget .getConstructor() .getField(propertyName, currentTarget.getType()); case AnnotationHandlers.CLASS: return new class ClassField implements Member{ getDeclaringClass(): Class {return currentTarget.getConstructor().getDeclaringClass();} getDeclaringConstructor(): Constructor {return currentTarget.getConstructor();} getModifiers(): number {return AnnotationHandlers.CLASS;} getName(): string {return currentTarget.getConstructor().getName();} } default: throw new UnsupportedOperationException(`unknown member type [${memberType}]`); } } /*** * @class */ public static clazz(invoke:Invokable):DecoratorClazz{ return (target: Object) => Risk.try(()=>invoke.invoke(AnnotationAccessor.clazz(AnnotationHandlers.getMember(target, null, null, AnnotationHandlers.CLASS)))); } /*** * @attribute */ public static attributeRef(invoke:Invokable):AttributeDecoratorRef{ return (target: T, propertyName:string ) => Risk.try(()=>invoke.invoke(AnnotationAccessor.attribute(AnnotationHandlers.getMember(target,propertyName, null, AnnotationHandlers.ATTRIBUTE)))); } /**/ public static attribute(invoke:Invokable):AttributeDecorator{ return function(target: Object, propertyName:string ) { Risk.try(()=> invoke.invoke(AnnotationAccessor.attribute(AnnotationHandlers.getMember(target, propertyName, null, AnnotationHandlers.ATTRIBUTE)))); } } /*** * @method * @throws NullPointerException : if method doesn't exist */ public static method( invoke:Invokable ):MethodDecorator{ return (target: Object, propertyName:string, desc:propertiesDescriptor ) => Risk.try(()=>invoke.invoke(AnnotationAccessor.method(AnnotationHandlers.getMember(target, propertyName, null, AnnotationHandlers.METHOD)))); } public static parameter(invoke:Invokable):ParameterDecorator{ return (target: Object, propertyName:string, index:number ) =>{ Risk.try(()=>invoke.invoke(AnnotationAccessor.parameterAnnotation(AnnotationHandlers.getMember(target, propertyName, index, AnnotationHandlers.PARAM)))); } } static all(invoke:Invokable){ return (target: Object, propertyName:string, desc?:propertiesDescriptor ) =>{ }; } public static annotationClazz(annotation:Constructor, ...args: Object[]):DecoratorClazz{ let inv:Invokable = new class AnnotationClazz implements Invokable{ public invoke(target: AnnotationTarget): void { let classT:Constructor = target.getReflector().getDeclaringConstructor(); // error if(!annotation.isAnnotation()) throw new RuntimeException( `Bad implementation of annotation [ @${annotation.getPackage().getName()}`+ `.${annotation.getName()} ] from ${classT.toString()}`+ `$${target.getReflector().getName()} : @${annotation.getName()}`+ ` should be it an extends of Annotation class` ); target .getReflector() .getDeclaringConstructor() .getClassLoader() .setAnnotation(annotation.newInstance(...args)); } }; return this.clazz(inv); } public static annotationMethod(annotation:Constructor, ...args: Object[]):MethodDecorator{ let inv:Invokable = new class AnnotationMethod implements Invokable{ public invoke(target: AnnotationTarget): void { let classT:Constructor = target.getReflector().getDeclaringConstructor(); // error if(!annotation.isAnnotation()) throw new RuntimeException( `Bad implementation of annotation [ @${annotation.getPackage().getName()}`+ `.${annotation.getName()} ] from ${classT.toString()}`+ `$${target.getReflector().getName()} : @${annotation.getName()}`+ ` should be it an extends of Annotation class` ); target.getReflector().setAnnotation(annotation.newInstance(...args)); } }; return this.method(inv); } public static annotationAttribute(annotation:Constructor, ...args: Object[]):AttributeDecorator{ let inv:Invokable = new class AnnotationMethod implements Invokable{ public invoke(target: AnnotationTarget): void { let classT:Constructor = target.getReflector().getDeclaringConstructor(); // error if(!annotation.isAnnotation()) throw new RuntimeException( `Bad implementation of annotation [ @${annotation.getPackage().getName()}`+ `.${annotation.getName()} ] from ${classT.toString()}`+ `$${target.getReflector().getName()} : @${annotation.getName()}`+ ` should be it an extends of Annotation class` ); target.getReflector().setAnnotation(annotation.newInstance(...args)); } }; return this.attribute(inv); } } Object.package(this);