Standard Libraries

import default
import semmle.code.java.frameworks.spring.SpringXMLElement
import semmle.code.java.frameworks.spring.SpringBean

/** This class is the supertype of the SpringRef and SpringIdRef elements*/
class SpringAbstractRef extends SpringXMLElement {
  SpringAbstractRef() { //needed to do this to avoid instanceof checks every time this class is used
    this.getName() = "idref" or
    this.getName() = "ref"
  }

  /** true if the reference has a bean attribute*/
  predicate hasBeanName() {
    this.hasAttribute("bean")
  }
  /** returns the value of the bean attribute*/
  string getBeanName() {
    result = this.getAttributeValue("bean")
  }
  
  /** true if the reference has a local attribute*/
  predicate hasBeanLocalName() {
    this.hasAttribute("local")
  }
  /** returns the balue of the local attribute*/
  string getBeanLocalName() {
    result = this.getAttributeValue("local")
  }

  /** returns the bean pointed to by the reference.*/
  SpringBean getBean() {
    if this.hasBeanLocalName()
    then result.getBeanId() = this.getBeanLocalName()
    else result.getBeanIdentifier() = this.getBeanName()
  }

  /** true if "other" is also a reference, and it points to the same bean as this
    * reference.*/
  predicate isSimilar(SpringXMLElement other) {
    exists (SpringAbstractRef otherRef |
      otherRef = other and
      this.getBean() = otherRef.getBean()
		)
	}
}