Class AbstractExpression

java.lang.Object
org.eclipse.persistence.jpa.jpql.parser.AbstractExpression
All Implemented Interfaces:
Expression
Direct Known Subclasses:
AbstractConditionalClause, AbstractEncapsulatedExpression, AbstractFromClause, AbstractOrderByClause, AbstractPathExpression, AbstractSchemaName, AbstractSelectClause, AbstractSelectStatement, ArithmeticFactor, AsOfClause, BadExpression, BetweenExpression, CaseExpression, CollectionExpression, CollectionMemberDeclaration, CollectionMemberExpression, CompoundExpression, ConnectByClause, ConstructorExpression, DateTime, DefaultStringExpression, DeleteClause, DeleteStatement, EmptyCollectionComparisonExpression, EntityTypeLiteral, GroupByClause, HierarchicalQueryClause, IdentificationVariable, IdentificationVariableDeclaration, InExpression, InputParameter, Join, JPQLExpression, KeywordExpression, LikeExpression, NotExpression, NullComparisonExpression, NullExpression, NumericLiteral, OrderByItem, RangeVariableDeclaration, RegexpExpression, ResultVariable, StringLiteral, TableVariableDeclaration, UnionClause, UnknownExpression, UpdateClause, UpdateItem, UpdateStatement, WhenClause

public abstract class AbstractExpression extends Object implements Expression
This is the abstract definition of all the parts used to create the tree hierarchy representing the parsed JPQL query.
Version:
2.6
See Also:
Author:
Pascal Filion
  • Field Details

    • COMMA

      public static final char COMMA
      The constant for ','.
      See Also:
    • DOT

      public static final char DOT
      The constant for '.'.
      See Also:
    • DOUBLE_QUOTE

      public static final char DOUBLE_QUOTE
      The constant for '"'.
      See Also:
    • LEFT_CURLY_BRACKET

      public static final char LEFT_CURLY_BRACKET
      The constant for '{'.
      See Also:
    • LEFT_PARENTHESIS

      public static final char LEFT_PARENTHESIS
      The constant for '('.
      See Also:
    • NOT_DEFINED

      public static final char NOT_DEFINED
      The constant for a character that is not defined.
      See Also:
    • RIGHT_CURLY_BRACKET

      public static final char RIGHT_CURLY_BRACKET
      The constant for '}'.
      See Also:
    • RIGHT_PARENTHESIS

      public static final char RIGHT_PARENTHESIS
      The constant for ')'.
      See Also:
    • SINGLE_QUOTE

      public static final char SINGLE_QUOTE
      The constant for '''.
      See Also:
    • SPACE

      public static final char SPACE
      The constant for ' '.
      See Also:
    • UNDERSCORE

      public static final char UNDERSCORE
      The constant for '_'.
      See Also:
  • Constructor Details

    • AbstractExpression

      protected AbstractExpression(AbstractExpression parent)
      Creates a new AbstractExpression.
      Parameters:
      parent - The parent of this expression
    • AbstractExpression

      protected AbstractExpression(AbstractExpression parent, String text)
      Creates a new AbstractExpression.
      Parameters:
      parent - The parent of this expression
      text - The text to be stored in this expression, null cannot be passed
  • Method Details

    • acceptUnknownVisitor

      protected boolean acceptUnknownVisitor(ExpressionVisitor visitor)
      The given ExpressionVisitor needs to visit this class but it is defined by a third- party provider. This method will programmatically invoke the visit method defined on the visitor. The method signature should be:
      {public|protected|private} void visit(ThirdPartyExpression expression)

      or

      {public|protected|private} void visit(Expression expression)

      Note: The package protected visibility (default) should be used with care, if the code is running inside OSGi, then the method will not be accessible, even through reflection.

      Parameters:
      visitor - The ExpressionVisitor to visit this Expression programmatically
      Returns:
      true if the call was successfully executed; false otherwise
    • acceptUnknownVisitor

      protected void acceptUnknownVisitor(ExpressionVisitor visitor, Class<?> type, Class<?> parameterType) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException
      The given ExpressionVisitor needs to visit this class but it is defined by a third- party provider. This method will programmatically invoke the visit method defined on the visitor. The method signature should be:
      {public|protected|private} void visit(ThirdPartyExpression expression)

      or

      {public|protected|private} void visit(Expression expression)

      Parameters:
      visitor - The ExpressionVisitor to visit this Expression programmatically
      type - The type found in the hierarchy of the given ExpressionVisitor that will be used to retrieve the visit method
      parameterType - The parameter type of the visit method
      Throws:
      NoSuchMethodException
      IllegalAccessException
      InvocationTargetException
      See Also:
    • addChildrenTo

      protected void addChildrenTo(Collection<Expression> children)
      Adds the children of this AbstractExpression to the given collection.
      Parameters:
      children - The collection used to store the children
    • addOrderedChildrenTo

      protected void addOrderedChildrenTo(List<Expression> children)
      Adds the children of this AbstractExpression to the given list.
      Parameters:
      children - The list used to store the string representation of this AbstractExpression
    • buildExpressionFromFallingBack

      protected final AbstractExpression buildExpressionFromFallingBack(WordParser wordParser, String word, JPQLQueryBNF queryBNF, AbstractExpression expression, boolean tolerant)
      No factories were found to create an Expression with the content of WordParser, this method will retrieve the fallback ExpressionFactory defined in the given BNF.
      Parameters:
      wordParser - The text to parse based on the current position of the cursor
      word - The word that was retrieved from the given text, which is the first word in the text
      queryBNF - The JPQLQueryBNF used to determine how to parse from the current position of the cursor within the JPQL query
      expression - The Expression that has just been parsed or null
      tolerant - Determines whether the parsing system should be tolerant, meaning if it should try to parse invalid or incomplete queries
      Returns:
      The Expression representing the given sub-query
    • buildNullExpression

      protected final AbstractExpression buildNullExpression()
      Creates a new null-Expression parented with this one.
      Returns:
      A new null version of an Expression
    • buildStringExpression

      protected final Expression buildStringExpression(char value)
      Creates a new Expression wrapping the given character value.
      Parameters:
      value - The character to wrap as a Expression
      Returns:
      The Expression representation of the given identifier where the owning Expression is this one
    • buildStringExpression

      protected final Expression buildStringExpression(String value)
      Creates a new Expression wrapping the given string value.
      Parameters:
      value - The string to wrap as a Expression
      Returns:
      The Expression representation of the given identifier where the owning Expression is this one
    • buildUnknownExpression

      protected final AbstractExpression buildUnknownExpression(String text)
      Creates an Expression that contains a malformed expression.
      Parameters:
      text - The text causing the expression to be malformed
      Returns:
      A new Expression where toActualText() returns the given text
    • calculatePosition

      protected final int calculatePosition(Expression expression, int length)
      Calculates the position of the given Expression by calculating the length of what is before.
      Parameters:
      expression - The Expression for which its position within the parsed tree needs to be determined
      length - The current cursor position within the JPQL query while digging into the tree until the search reaches the expression
      Returns:
      The length of the string representation for what is coming before the given Expression
    • children

      public final ListIterable<Expression> children()
      Description copied from interface: Expression
      Returns the children of this Expression.
      Specified by:
      children in interface Expression
      Returns:
      The children of this Expression or an empty ListIterable
    • findFallBackExpressionFactory

      protected final ExpressionFactory findFallBackExpressionFactory(JPQLQueryBNF queryBNF)
      Retrieve the ExpressionFactory from the given JPQLQueryBNF by following the path of fallback JPQLQueryBNFs and then returns the ExpressionFactory from the leaf JPQLQueryBNF.
      Parameters:
      queryBNF - The JPQLQueryBNF for which its associated fallback ExpressionFactory will be searched
      Returns:
      Either the fallback ExpressionFactory linked to the given JPQLQueryBNF or null if none was declared
    • findQueryBNF

      public JPQLQueryBNF findQueryBNF(Expression expression)
      Description copied from interface: Expression
      Retrieves the JPQLQueryBNF that represents the fragment of this Expression that was used when parsing the given Expression.
      Specified by:
      findQueryBNF in interface Expression
      Parameters:
      expression - The Expression that is a descendant of this one
      Returns:
      The JPQLQueryBNF that was used to parse the given expression
    • getExpressionFactory

      protected final ExpressionFactory getExpressionFactory(String expressionFactoryId)
      Retrieves the registered ExpressionFactory that was registered for the given unique identifier.
      Parameters:
      expressionFactoryId - The unique identifier of the ExpressionFactory to retrieve
      Returns:
      The ExpressionFactory mapped with the given unique identifier
      See Also:
    • getExpressionRegistry

      protected final ExpressionRegistry getExpressionRegistry()
      Returns the registry containing the JPQLQueryBNFs and the ExpressionFactories that are used to properly parse a JPQL query.
      Returns:
      The registry containing the information related to the JPQL grammar
    • getGrammar

      public JPQLGrammar getGrammar()
      Description copied from interface: Expression
      Returns the JPQLGrammar that defines how the JPQL query was parsed.
      Specified by:
      getGrammar in interface Expression
      Returns:
      The JPQLGrammar that was used to parse this Expression
    • getIdentifierVersion

      public JPAVersion getIdentifierVersion(String identifier)
      Retrieves the JPA version in which the identifier was first introduced.
      Returns:
      The version in which the identifier was introduced
    • getJPAVersion

      protected JPAVersion getJPAVersion()
      Returns the version of the Java Persistence to support.
      Returns:
      The JPA version supported by the grammar
      See Also:
    • getLength

      public final int getLength()
      Description copied from interface: Expression
      Returns the length of the string representation of this Expression, which is the length of the text generated by Expression.toActualText().
      Specified by:
      getLength in interface Expression
      Returns:
      The length of the string representation of this Expression
    • getOffset

      public final int getOffset()
      Description copied from interface: Expression
      Returns the position of this Expression within its parent hierarchy.
      Specified by:
      getOffset in interface Expression
      Returns:
      The length of the string representation of what is coming before this object
    • getParent

      public final AbstractExpression getParent()
      Description copied from interface: Expression
      Returns the parent of this Expression.
      Specified by:
      getParent in interface Expression
      Returns:
      The parent of this Expression, which is never null except for the root of the tree
    • getQueryBNF

      public JPQLQueryBNF getQueryBNF(String queryBNFID)
      Retrieves the BNF object that was registered for the given unique identifier.
      Parameters:
      queryBNFID - The unique identifier of the JPQLQueryBNF to retrieve
      Returns:
      The JPQLQueryBNF representing a section of the grammar
    • getRoot

      public final JPQLExpression getRoot()
      Description copied from interface: Expression
      Retrieves the root node of the parsed tree hierarchy.
      Specified by:
      getRoot in interface Expression
      Returns:
      The root of the Expression tree
    • getText

      protected String getText()
      Returns the encapsulated text of this AbstractExpression, which can be used in various ways, it can be a keyword, a literal, etc.
      Returns:
      Either the JPQL identifier for this AbstractExpression, the literal it encapsulates or an empty string
    • handleAggregate

      protected boolean handleAggregate(JPQLQueryBNF queryBNF)
      Determines whether the given JPQLQueryBNF handles aggregate expressions.
      Parameters:
      queryBNF - The JPQLQueryBNF used to determine if the parsing should handle aggregate expressions
      Returns:
      true if the given BNF handles aggregate expressions; false otherwise
    • handleCollection

      protected boolean handleCollection(JPQLQueryBNF queryBNF)
      Determines whether the given JPQLQueryBNF handles a collection of sub-expressions that are separated by commas.
      Parameters:
      queryBNF - The JPQLQueryBNF used to determine if the parsing should handle collection of sub-expressions
      Returns:
      true if the sub-expression to parse might have several sub-expressions separated by commas; false otherwise
    • isAncestor

      public boolean isAncestor(Expression expression)
      Description copied from interface: Expression
      Determines whether this Expression is a parent of the given Expression.
      Specified by:
      isAncestor in interface Expression
      Parameters:
      expression - The Expression to verify its paternity with this Expression
      Returns:
      true if this Expression is the same as the given Expression or one of its parent; false otherwise
    • isIdentifier

      protected final boolean isIdentifier(String word)
      Determines if the given word is a JPQL identifier. The check is case insensitive.
      Parameters:
      word - The word to test if it is a JPQL identifier
      Returns:
      true if the word is an identifier, false otherwise
      See Also:
    • isNull

      protected boolean isNull()
      Determines whether this expression is a null Expression or any other subclass.
      Returns:
      false by default
    • isParsingComplete

      protected boolean isParsingComplete(WordParser wordParser, String word, Expression expression)
      Determines whether the parsing is complete based on what is left in the given text. The text is never empty.
      Parameters:
      wordParser - The text to parse based on the current position of the cursor
      word - The word that was retrieved from the given text, which is the first word in the text
      expression - The Expression that has already been parsed
      Returns:
      true if the text no longer can't be parsed by the current expression; false if more can be parsed
    • isTolerant

      protected boolean isTolerant()
      Determines if the parser is in tolerant mode or is in fast mode. When the tolerant is turned on, it means the parser will attempt to parse incomplete or invalid queries.
      Returns:
      true if the parsing system should parse invalid or incomplete queries; false when the query is well-formed and valid
    • isUnknown

      protected boolean isUnknown()
      Determines whether this expression is an unknown Expression or any other subclass.
      Returns:
      false by default
    • isVirtual

      protected boolean isVirtual()
      Determines whether this AbstractExpression is virtual, meaning it's not part of the query but is required for proper navigability.
      Returns:
      true if this AbstractExpression was virtually created to fully qualify path expression; false if it was parsed
    • orderedChildren

      public final ListIterable<Expression> orderedChildren()
      Description copied from interface: Expression
      Returns the list representing this Expression and its children.
      Specified by:
      orderedChildren in interface Expression
      Returns:
      The Expressions representing this Expression
    • parse

      protected abstract void parse(WordParser wordParser, boolean tolerant)
      Parses the query by starting at the current position, which is part of the given WordParser.
      Parameters:
      wordParser - The text to parse based on the current position of the cursor
      tolerant - Determines whether the parsing system should be tolerant, meaning if it should try to parse invalid or incomplete queries
    • parse

      protected AbstractExpression parse(WordParser wordParser, String queryBNFId, boolean tolerant)
      Parses the given text by using the specified BNF.
      Parameters:
      wordParser - The text to parse based on the current position of the cursor
      queryBNFId - The unique identifier of the JPQLQueryBNF that is used to determine how to parse the text at the current cursor position within the JPQL query
      tolerant - Determines whether the parsing system should be tolerant, meaning if it should try to parse invalid or incomplete queries
      Returns:
      The Expression representing the given sub-query
    • parseUsingExpressionFactory

      protected AbstractExpression parseUsingExpressionFactory(WordParser wordParser, String queryBNFId, boolean tolerant)
      Right away parses the text by retrieving the ExpressionFactory for the first word that is extracted from WordParser at the current location.
      Parameters:
      wordParser - The text to parse based on the current position of the cursor
      queryBNFId - The unique identifier of the JPQLQueryBNF that is used to determine how to parse the text at the current cursor position within the JPQL query
      tolerant - Determines whether the parsing system should be tolerant, meaning if it should try to parse invalid or incomplete queries
      Returns:
      The Expression representing the given sub-query
    • populatePosition

      public void populatePosition(QueryPosition queryPosition, int position)
      Description copied from interface: Expression
      Retrieves the Expression located at the given position using the actual query, which may have extra whitespace.
      Specified by:
      populatePosition in interface Expression
      position - The array has one element and is the position of the Expression to retrieve
    • rebuildActualText

      protected final void rebuildActualText()
      Rebuilds the actual parsed text if it has been cached.
    • rebuildParsedText

      protected final void rebuildParsedText()
      Rebuilds the parsed parsed text if it has been cached.
    • setParent

      protected final void setParent(AbstractExpression parent)
      Re-parents this Expression to be a child of the given Expression.
      Parameters:
      parent - The new parent of this object
    • setText

      protected final void setText(String text)
      Sets the text of this Expression.
      Parameters:
      text - The immutable text wrapped by this Expression, which cannot be null
    • shouldParseWithFactoryFirst

      protected boolean shouldParseWithFactoryFirst()
      Determines whether the parsing of the query should be performed using the factories first or it should automatically fallback to the fallback factory.
      Returns:
      true is returned by default so the factories are used before falling back
    • shouldSkipLiteral

      protected boolean shouldSkipLiteral(AbstractExpression expression)
      When parsing an invalid or incomplete query, it is possible two literals would be parsed but in some cases, a CollectionExpression should not be created and the parsing should actually stop here. Example: BETWEEN 10 20, when parsing 20, it should not be parsed as part of the lower bound expression.
      Parameters:
      expression - The Expression that has just been parsed or null
      Returns:
      true
    • toActualText

      public String toActualText()
      Description copied from interface: Expression
      Generates a string representation of this Expression, which needs to include any characters that are considered virtual, i.e. that was parsed when the query is incomplete and is needed for functionality like content assist.
      Specified by:
      toActualText in interface Expression
      Returns:
      The string representation of this Expression
    • toParsedText

      public String toParsedText()
      Description copied from interface: Expression
      Returns a string representation of this Expression and its children. The expression should contain whitespace even if the beautified version would not have any. For instance, "SELECT e " should be returned where Expression.toParsedText() would return "SELECT e".
      Specified by:
      toParsedText in interface Expression
      Returns:
      The string representation of this Expression
    • toParsedText

      protected abstract void toParsedText(StringBuilder writer, boolean actual)
      Generates a string representation of this Expression, including its children, if it has any.
      Parameters:
      writer - The buffer used to append this Expression's string representation
      actual - Determines whether the string representation should represent what was parsed, i.e. include any "virtual" whitespace (such as ending whitespace) and the actual case of the JPQL identifiers
    • toString

      public final String toString()
      Overrides:
      toString in class Object