import {propertiesDescriptor} from "../decorator/DecoratorInterfaces"; import {Member} from "./Interfaces"; import {Method} from "./Method"; import {Field} from "./Field"; import {IOException} from "../Exception"; export class ObjectDescriptor{ private static readonly STATIC:number = 1; public static readonly PropertiesDescriptor = class PropertiesDescriptor implements propertiesDescriptor { enumerable?: boolean = true; writable?: boolean = true; configurable?: boolean = true; value?: T; get?():T set?(value:T):void }; private prop:propertiesDescriptor = null; private readonly target:Member; private constructor(target:Member) { this.target = target; } private static EMPTY_DESC:propertiesDescriptor = new ObjectDescriptor.PropertiesDescriptor(); private getProp():propertiesDescriptor{ if(this.prop!=null) return this.prop; return ( this.prop = Object.getOwnPropertyDescriptor(this.getInstance(), this.target.getName()) || new ObjectDescriptor.PropertiesDescriptor()); } private getInstance():Object{ return (this.target.getModifiers() === ObjectDescriptor.STATIC ? this.target.getDeclaringConstructor() : this.target.getDeclaringClass()).getInstance(); } public isEnumerable():boolean{ if(this.prop==null)return ObjectDescriptor.EMPTY_DESC.enumerable; return this.prop.enumerable; } public enumerable(state:boolean):ObjectDescriptor{ if(!this.isConfigurable()) throw new IOException(""); this.getProp().enumerable = state; return this; } public isWritable():boolean{ if(this.prop==null)return ObjectDescriptor.EMPTY_DESC.writable; return this.prop.writable; } public writable(state:boolean):ObjectDescriptor{ if(!this.isConfigurable()) throw new IOException(); this.getProp().writable = state; return this; } public isConfigurable():boolean{ if(this.prop==null)return ObjectDescriptor.EMPTY_DESC.configurable; return this.prop.configurable; } public configurable(state:boolean):ObjectDescriptor{ this.getProp().configurable = state; return this; } public readonly():ObjectDescriptor{return this.writable(false).configurable(false); } public final():ObjectDescriptor{ return this.readonly();} public value(value:T):ObjectDescriptor{ if(this.getProp()==null)return this; if(!this.isConfigurable()&&!this.isWritable()&&this.prop.value!=undefined) throw new IOException(`${ObjectDescriptor.class().getName()} has failed, target '${this.target.toString()}' is already defined on ${this.target.getDeclaringConstructor().toString()}`); this.getProp().value = value; return this; } public set():F{ if(this.prop!=null) Object.defineProperty( this.getInstance(), this.target.getName(), this.getProp() ); return this.target; } public toString():string { return `ObjectDescriptor[ `+ `enumerable = ${this.isEnumerable()}, ` + `configurable = ${this.isConfigurable()}, ` + `writable = ${this.isWritable()} ] for ` + this.target.toString(); } public static ofMethod(method:Method):ObjectDescriptor{return new ObjectDescriptor(method);} public static ofAttribute(field:Field):ObjectDescriptor{return new ObjectDescriptor(field);} } Object.package(this);