All files / src/ast/reference bean-reference.ts

94.28% Statements 33/35
90% Branches 9/10
100% Functions 6/6
94.28% Lines 33/35

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  1x 1x 1x 1x 1x         1x 35x 35x   35x 35x 35x 35x 35x   35x 18x 18x   35x 18x 18x   35x 8x 8x 8x 8x 1x 1x 1x     8x   35x 3x 3x 3x 35x  
import type { ExpressionState } from '../../expression-state.js';
import { TypedValue } from '../../typed-value.js';
import { SpelNodeImpl } from '../spel-node.js';
import { NodeType } from '../../language/node-type.js';
import { SpelEvaluationException } from '../../error/spel-evaluation-exception.js';
import { SpelMessage } from '../../error/spel-message.js';
 
/**
 * Bean reference node — parallels Spring BeanReference
 */
export class BeanReference extends SpelNodeImpl {
  private readonly beanName: string;
  private readonly isFactoryBean: boolean;
 
  constructor(startPos: number, endPos: number, beanName: string, isFactoryBean = false) {
    super(NodeType.BEAN_REFERENCE, startPos, endPos);
    this.beanName = beanName;
    this.isFactoryBean = isFactoryBean;
  }
 
  public getBeanName(): string {
    return this.beanName;
  }
 
  public isFactory(): boolean {
    return this.isFactoryBean;
  }
 
  public getValueInternal(state: ExpressionState): TypedValue {
    try {
      const bean = state.resolveBean(this.beanName, this.isFactoryBean);
      return new TypedValue(bean);
    } catch (e) {
      if (e instanceof SpelEvaluationException) {
        throw e;
      }
      throw new SpelEvaluationException(this.startPos, SpelMessage.BEAN_NOT_FOUND, this.beanName);
    }
  }
 
  public toStringAST(): string {
    const prefix = this.isFactoryBean ? '&@' : '@';
    return `${prefix}${this.beanName}`;
  }
}