## net.sf.saxon.expr
Class VariableReference

    java.lang.Object
      net.sf.saxon.expr.Expression
          net.sf.saxon.expr.VariableReference

All Implemented Interfaces:
  Serializable, SourceLocator, LocationProvider, SaxonLocator, BindingReference, EvaluableItem, SequenceIterable, InstructionInfo, Locator

Direct Known Subclasses:
  LocalVariableReference

    public class VariableReference

extends Expression
implements BindingReference

Variable reference: a reference to a variable. This may be an XSLT-defined variable, a range variable defined within the XPath expression, or a variable defined in some other static context.

See Also:
  Serialized Form

Field Summary
`protected Binding` `binding`
`protected Value` `constantValue`
`protected SequenceType` `staticType`

Fields inherited from class net.sf.saxon.expr.Expression
`EVALUATE_METHOD, EVENT_FEED_METHOD, ITEM_FEED_METHOD, ITERATE_METHOD, locationId, PROCESS_METHOD, PUSH_SELECTION, staticProperties, WATCH_METHOD`

Constructor Summary
`VariableReference()` Create a Variable Reference
`VariableReference(Binding binding)` Create a Variable Reference

Method Summary
`PathMap.PathMapNodeSet` `addToPathMap(PathMap pathMap, PathMap.PathMapNodeSet pathMapNodeSet)` Add a representation of this expression to a PathMap.
`int` `computeCardinality()` Get the static cardinality
`int` `computeSpecialProperties()` Determine the special properties of this expression
`Expression` `copy()` Create a clone copy of this VariableReference
`boolean` `equals(Object other)` Test if this expression is the same as another expression.
`Item` `evaluateItem(XPathContext c)` Evaluate an expression as a single item.
`ValueRepresentation` `evaluateVariable(XPathContext c)` Evaluate this variable
`void` `explain(ExpressionPresenter destination)` Diagnostic print of expression structure.
`void` `fixup(Binding binding)` Fix up this variable reference to a Binding object, which enables the value of the variable to be located at run-time.
`Binding` `getBinding()` Get the object bound to the variable
`String` `getDisplayName()` Get the display name of the variable.
`int` `getImplementationMethod()` An implementation of Expression must provide at least one of the methods evaluateItem(), iterate(), or process().
`int` `getIntrinsicDependencies()` Determine the intrinsic dependencies of an expression, that is, those which are not derived from the dependencies of its subexpressions.
`ItemType` `getItemType(TypeHierarchy th)` Determine the data type of the expression, if possible
`int` `hashCode()` get HashCode for comparing two expressions
`boolean` `isFiltered()` Determine whether this variable reference is filtered
`boolean` `isFlattened()` Test whether this variable reference is flattened - that is, whether it is atomized etc
`boolean` `isInLoop()` Determine whether this variable reference appears in a loop relative to its declaration.
`SequenceIterator` `iterate(XPathContext c)` Get the value of this variable in a given context.
`Expression` `optimize(ExpressionVisitor visitor, ItemType contextItemType)` Type-check the expression.
`void` `process(XPathContext c)` Process the instruction, without returning any tail calls
`Expression` `promote(PromotionOffer offer, Expression parent)` Promote this expression if possible
`void` `refineVariableType(ItemType type, int cardinality, Value constantValue, int properties, ExpressionVisitor visitor)` Provide additional information about the type of the variable, typically derived by analyzing the initializer of the variable binding
`void` `setFiltered(boolean filtered)` Mark an expression as filtered: that is, it appears as the base expression in a filter expression.
`void` `setFlattened(boolean flattened)` Mark an expression as being "flattened".
`void` `setStaticType(SequenceType type, Value value, int properties)` Set static type.
`String` `toString()` The toString() method for an expression attempts to give a representation of the expression in an XPath-like form, but there is no guarantee that the syntax will actually be true XPath.
`Expression` `typeCheck(ExpressionVisitor visitor, ItemType contextItemType)` Type-check the expression.

Methods inherited from class net.sf.saxon.expr.Expression

`adoptChildExpression, checkForUpdatingSubexpressions, checkPermittedContents, computeDependencies, computeStaticProperties, doPromotion, dynamicError, effectiveBooleanValue, evaluateAsString, evaluatePendingUpdates, explain, getCardinality, getColumnNumber, getColumnNumber, getConstructType, getContainer, getDependencies, getEvaluationMethod, getExecutable, getExpressionName, getHostLanguage, getLineNumber, getLineNumber, getLocationId, getLocationProvider, getObjectName, getProperties, getProperty, getPublicId, getSlotsUsed, getSpecialProperties, getSystemId, getSystemId, hasLoopingSubexpression, implementsStaticTypeCheck, isSubtreeExpression, isUpdatingExpression, isVacuousExpression, iterateEvents, iterateSameFocusSubExpressions, iterateSubExpressions, markTailFunctionCalls, replaceSubExpression, resetLocalStaticProperties, setContainer, setEvaluationMethod, setLocationId, simplify, staticTypeCheck, suppressValidation, typeError`

Methods inherited from class java.lang.Object
`clone, finalize, getClass, notify, notifyAll, wait, wait, wait`

Field Detail

### binding

    protected Binding binding

### staticType

    protected SequenceType staticType

### constantValue

    protected Value constantValue

Constructor Detail

### VariableReference

    public VariableReference()

  Create a Variable Reference

### VariableReference

    public VariableReference(Binding binding)

  Create a Variable Reference

  Parameters:
    `binding` - the variable binding to which this variable refers

Method Detail

### copy

    public Expression copy()

  Create a clone copy of this VariableReference

  Specified by:
    `copy` in class `Expression`
  Returns:
    the cloned copy

### setStaticType

    public void setStaticType(SequenceType type,
                              Value value,
                              int properties)

  Set static type. This is a callback from the variable declaration object. As well as supplying the static type, it may also supply a compile-time value for the variable. As well as the type information, other static properties of the value are supplied: for example, whether the value is an ordered node-set.

  Specified by:
    `setStaticType` in interface `BindingReference`
  Parameters:
    `type` - the static type of the variable
    `value` - the value of the variable if this is a compile-time constant
    `properties` - static properties of the expression to which the variable is bound

### setFlattened

    public void setFlattened(boolean flattened)

  Mark an expression as being "flattened". This is a collective term that includes extracting the string value or typed value, or operations such as simple value construction that concatenate text nodes before atomizing. The implication of all of these is that although the expression might return nodes, the identity of the nodes has no significance. This is called during type checking of the parent expression. At present, only variable references take any notice of this notification.

  Overrides:
    `setFlattened` in class `Expression`
  Parameters:
    `flattened` - set to true if the result of the expression is atomized or otherwise turned into an atomic value

### isFlattened

    public boolean isFlattened()

  Test whether this variable reference is flattened - that is, whether it is atomized etc

  Returns:
    true if the value of the variable is atomized, or converted to a string or number

### setFiltered

    public void setFiltered(boolean filtered)

  Mark an expression as filtered: that is, it appears as the base expression in a filter expression. This notification currently has no effect except when the expression is a variable reference.

  Overrides:
    `setFiltered` in class `Expression`
  Parameters:
    `filtered` - if true, marks this expression as the base of a filter expression

### isFiltered

    public boolean isFiltered()

  Determine whether this variable reference is filtered

  Returns:
    true if the value of the variable is filtered by a predicate

### isInLoop

    public boolean isInLoop()

  Determine whether this variable reference appears in a loop relative to its declaration. By default, when in doubt, returns true. This is calculated during type-checking.

  Returns:
    true if this variable reference occurs in a loop, where the variable declaration is outside the loop

### typeCheck

    public Expression typeCheck(ExpressionVisitor visitor,
                                ItemType contextItemType)
                         throws XPathException

  Type-check the expression. At this stage details of the static type must be known. If the variable has a compile-time value, this is substituted for the variable reference

  Overrides:
    `typeCheck` in class `Expression`
  Parameters:
    `visitor` - an expression visitor
    `contextItemType` - the static type of "." at the point where this expression is invoked. The parameter is set to null if it is known statically that the context item will be undefined. If the type of the context item is not known statically, the argument is set to `Type.ITEM_TYPE`
  Returns:
    the original expression, rewritten to perform necessary run-time type checks, and to perform other type-related optimizations
  Throws:
    `XPathException` - if an error is discovered during this phase (typically a type error)

### optimize

    public Expression optimize(ExpressionVisitor visitor,
                               ItemType contextItemType)
                        throws XPathException

  Type-check the expression. At this stage details of the static type must be known. If the variable has a compile-time value, this is substituted for the variable reference

  Overrides:
    `optimize` in class `Expression`
  Parameters:
    `visitor` - an expression visitor
    `contextItemType` - the static type of "." at the point where this expression is invoked. The parameter is set to null if it is known statically that the context item will be undefined. If the type of the context item is not known statically, the argument is set to `Type.ITEM_TYPE`
  Returns:
    the original expression, rewritten if appropriate to optimize execution
  Throws:
    `XPathException` - if an error is discovered during this phase (typically a type error)

### fixup

    public void fixup(Binding binding)

  Fix up this variable reference to a Binding object, which enables the value of the variable to be located at run-time.

  Specified by:
    `fixup` in interface `BindingReference`

### refineVariableType

    public void refineVariableType(ItemType type,
                                   int cardinality,
                                   Value constantValue,
                                   int properties,
                                   ExpressionVisitor visitor)

  Provide additional information about the type of the variable, typically derived by analyzing the initializer of the variable binding

  Parameters:
    `type` - the item type of the variable
    `cardinality` - the cardinality of the variable
    `constantValue` - the actual value of the variable, if this is known statically, otherwise null
    `properties` - additional static properties of the variable's initializer
    `visitor` - an ExpressionVisitor

### getItemType

    public ItemType getItemType(TypeHierarchy th)

  Determine the data type of the expression, if possible

  Specified by:
    `getItemType` in class `Expression`
  Parameters:
    `th` - the type hierarchy cache
  Returns:
    the type of the variable, if this can be determined statically; otherwise Type.ITEM (meaning not known in advance)

### computeCardinality

    public int computeCardinality()

  Get the static cardinality

  Specified by:
    `computeCardinality` in class `Expression`
  Returns:
    the computed cardinality, as one of the values `StaticProperty.ALLOWS_ZERO_OR_ONE`, `StaticProperty.EXACTLY_ONE`, `StaticProperty.ALLOWS_ONE_OR_MORE`, `StaticProperty.ALLOWS_ZERO_OR_MORE`

### computeSpecialProperties

    public int computeSpecialProperties()

  Determine the special properties of this expression

  Overrides:
    `computeSpecialProperties` in class `Expression`
  Returns:
    `StaticProperty.NON_CREATIVE` (unless the variable is assignable using saxon:assign)

### equals

    public boolean equals(Object other)

  Test if this expression is the same as another expression. (Note, we only compare expressions that have the same static and dynamic context).

  Overrides:
    `equals` in class `Object`

### hashCode

    public int hashCode()

  get HashCode for comparing two expressions

  Overrides:
    `hashCode` in class `Object`

### getIntrinsicDependencies

    public int getIntrinsicDependencies()

  Description copied from class: `Expression`
  Determine the intrinsic dependencies of an expression, that is, those which are not derived from the dependencies of its subexpressions. For example, position() has an intrinsic dependency on the context position, while (position()+1) does not. The default implementation of the method returns 0, indicating "no dependencies".

  Overrides:
    `getIntrinsicDependencies` in class `Expression`
  Returns:
    a set of bit-significant flags identifying the "intrinsic" dependencies. The flags are documented in class net.sf.saxon.value.StaticProperty

### promote

    public Expression promote(PromotionOffer offer,
                              Expression parent)
                       throws XPathException

  Promote this expression if possible

  Overrides:
    `promote` in class `Expression`
  Parameters:
    `offer` - details of the offer, for example the offer to move expressions that don't depend on the context to an outer level in the containing expression
  Returns:
    if the offer is not accepted, return this expression unchanged. Otherwise return the result of rewriting the expression to promote this subexpression
  Throws:
    `XPathException` - if any error is detected

### getImplementationMethod

    public int getImplementationMethod()

  An implementation of Expression must provide at least one of the methods evaluateItem(), iterate(), or process(). This method indicates which of these methods is provided. This implementation provides both all three methods natively.

  Overrides:
    `getImplementationMethod` in class `Expression`
  Returns:
    the implementation method, for example `Expression.ITERATE_METHOD` or `Expression.EVALUATE_METHOD` or `Expression.PROCESS_METHOD`

### addToPathMap

    public PathMap.PathMapNodeSet addToPathMap(PathMap pathMap,
                                               PathMap.PathMapNodeSet pathMapNodeSet)

  Add a representation of this expression to a PathMap. The PathMap captures a map of the nodes visited by an expression in a source tree.

  The default implementation of this method assumes that an expression does no navigation other than the navigation done by evaluating its subexpressions, and that the subexpressions are evaluated in the same context as the containing expression. The method must be overridden for any expression where these assumptions do not hold. For example, implementations exist for AxisExpression, ParentExpression, and RootExpression (because they perform navigation), and for the doc(), document(), and collection() functions because they create a new navigation root. Implementations also exist for PathExpression and FilterExpression because they have subexpressions that are evaluated in a different context from the calling expression.

  Overrides:
    `addToPathMap` in class `Expression`
  Parameters:
    `pathMap` - the PathMap to which the expression should be added
    `pathMapNodeSet` - the PathMapNodeSet to which the paths embodied in this expression should be added
  Returns:
    the pathMapNodeSet representing the points in the source document that are both reachable by this expression, and that represent possible results of this expression. For an expression that does navigation, it represents the end of the arc in the path map that describes the navigation route. For other expressions, it is the same as the input pathMapNode.

### iterate

    public SequenceIterator iterate(XPathContext c)
                             throws XPathException

  Get the value of this variable in a given context.

  Specified by:
    `iterate` in interface `SequenceIterable`
  Overrides:
    `iterate` in class `Expression`
  Parameters:
    `c` - the XPathContext which contains the relevant variable bindings
  Returns:
    the value of the variable, if it is defined
  Throws:
    `XPathException` - if the variable is undefined

### evaluateItem

    public Item evaluateItem(XPathContext c)
                      throws XPathException

  Description copied from class: `Expression`
  Evaluate an expression as a single item. This always returns either a single Item or null (denoting the empty sequence). No conversion is done. This method should not be used unless the static type of the expression is a subtype of "item" or "item?": that is, it should not be called if the expression may return a sequence. There is no guarantee that this condition will be detected.

  Specified by:
    `evaluateItem` in interface `EvaluableItem`
  Overrides:
    `evaluateItem` in class `Expression`
  Parameters:
    `c` - The context in which the expression is to be evaluated
  Returns:
    the node or atomic value that results from evaluating the expression; or null to indicate that the result is an empty sequence
  Throws:
    `XPathException` - if any dynamic error occurs evaluating the expression

### process

    public void process(XPathContext c)
                 throws XPathException

  Description copied from class: `Expression`
  Process the instruction, without returning any tail calls

  Overrides:
    `process` in class `Expression`
  Parameters:
    `c` - The dynamic context, giving access to the current node, the current variables, etc.
  Throws:
    `XPathException`

### evaluateVariable

    public ValueRepresentation evaluateVariable(XPathContext c)
                                         throws XPathException

  Evaluate this variable

  Parameters:
    `c` - the XPath dynamic context
  Returns:
    the value of the variable
  Throws:
    `XPathException` - if any error occurs

### getBinding

    public Binding getBinding()

  Get the object bound to the variable

  Returns:
    the Binding which declares this variable and associates it with a value

### getDisplayName

    public String getDisplayName()

  Get the display name of the variable. This is taken from the variable binding if possible

  Returns:
    the display name (a lexical QName

### toString

    public String toString()

  The toString() method for an expression attempts to give a representation of the expression in an XPath-like form, but there is no guarantee that the syntax will actually be true XPath. In the case of XSLT instructions, the toString() method gives an abstracted view of the syntax

  Overrides:
    `toString` in class `Expression`
  Returns:
    a representation of the expression as a string

### explain

    public void explain(ExpressionPresenter destination)

  Diagnostic print of expression structure. The abstract expression tree is written to the supplied output destination.

  Specified by:
    `explain` in class `Expression`
  Parameters:
    `destination` - the expression presenter used to display the structure

Copyright (c) 2004-2010 Saxonica Limited. All rights reserved.