All files / src/evaluation-context reflective-property-accessor.ts

100% Statements 42/42
100% Branches 23/23
100% Functions 5/5
100% Lines 42/42

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50  1x   1x 1x   1x 1x 1x 1x   1x 89x 89x 82x 89x   1x 80x 3x 3x 3x 3x 3x 3x 77x 80x 2x 2x 75x 80x   1x 7x 5x 7x   1x 6x 6x 6x 6x 6x 6x 1x 1x 5x 6x 1x  
import type { EvaluationContext } from './evaluation-context.js';
import { TypedValue } from '../typed-value.js';
import type { PropertyAccessor } from './property-accessor.js';
import { SpelEvaluationException } from '../error/spel-evaluation-exception.js';
import { SpelMessage } from '../error/spel-message.js';
 
export class ReflectivePropertyAccessor implements PropertyAccessor {
  public getSpecificTargetClasses(): null {
    return null; // Generic handling
  }
 
  public canRead(_context: EvaluationContext, target: unknown, name: string): boolean {
    if (target === null || target === undefined) return false;
    if (typeof target !== 'object' && typeof target !== 'function') return false;
    return name in Object(target);
  }
 
  public read(_context: EvaluationContext, target: unknown, name: string): TypedValue {
    if (target === null || target === undefined) {
      throw new SpelEvaluationException(
        -1,
        SpelMessage.PROPERTY_OR_FIELD_NOT_READABLE_ON_NULL,
        name,
      );
    }
    const obj = target as Record<string, unknown>;
    if (!(name in obj)) {
      throw new SpelEvaluationException(-1, SpelMessage.PROPERTY_OR_FIELD_NOT_READABLE, name);
    }
    return new TypedValue(obj[name]);
  }
 
  public canWrite(_context: EvaluationContext, target: unknown, _name: string): boolean {
    if (target === null || target === undefined) return false;
    return typeof target === 'object';
  }
 
  public write(
    _context: EvaluationContext,
    target: unknown,
    name: string,
    newValue: unknown,
  ): void {
    if (target === null || target === undefined || typeof target !== 'object') {
      throw new SpelEvaluationException(-1, SpelMessage.PROPERTY_OR_FIELD_NOT_WRITABLE, name);
    }
    (target as Record<string, unknown>)[name] = newValue;
  }
}