Class DefaultSemanticValidator

All Implemented Interfaces:
ExpressionVisitor

public class DefaultSemanticValidator extends AbstractSemanticValidator
This validator is responsible to gather the problems found in a JPQL query by validating the content to make sure it is semantically valid. The grammar is not validated by this visitor.

For instance, the function AVG accepts a state field path. The property it represents has to be of numeric type. AVG(e.name) is parsable but is not semantically valid because the type of name is a string (the property signature is: "private String name").

Provisional API: This interface is part of an interim API that is still under development and expected to change significantly before reaching stability. It is available at this early stage to solicit feedback from pioneering adopters on the understanding that any code that uses this API will almost certainly be broken (repeatedly) as the API evolves.

Since:
2.3
Version:
2.5
See Also:
  • Field Details

  • Constructor Details

    • DefaultSemanticValidator

      public DefaultSemanticValidator(JPQLQueryContext queryContext)
      Creates a new DefaultSemanticValidator.
      Parameters:
      queryContext - The context used to query information about the JPQL query
      Throws:
      NullPointerException - The given JPQLQueryContext cannot be null
    • DefaultSemanticValidator

      public DefaultSemanticValidator(SemanticValidatorHelper helper)
      Creates a new DefaultSemanticValidator.
      Parameters:
      helper - The given helper allows the validator to access the JPA artifacts without using Hermes SPI directly
      Throws:
      NullPointerException - The given SemanticValidatorHelper cannot be null
      Since:
      2.4
  • Method Details

    • areTypesEquivalent

      protected boolean areTypesEquivalent(Object[] typeDeclarations1, Object[] typeDeclarations2)
    • buildLiteralVisitor

      protected LiteralVisitor buildLiteralVisitor()
      Description copied from class: AbstractValidator
      Creates the visitor that can retrieve some information about various literal.
      Specified by:
      buildLiteralVisitor in class AbstractValidator
      Returns:
      A new LiteralVisitor
    • buildOwningClauseVisitor

      protected AbstractValidator.OwningClauseVisitor buildOwningClauseVisitor()
      Description copied from class: AbstractValidator
      Creates the visitor that traverses the parent hierarchy of any Expression and stops at the first Expression that is a clause.
      Specified by:
      buildOwningClauseVisitor in class AbstractValidator
      Returns:
      A new AbstractValidator.OwningClauseVisitor
    • buildResultVariableInOrderByVisitor

      protected DefaultSemanticValidator.ResultVariableInOrderByVisitor buildResultVariableInOrderByVisitor()
    • findAbstractSchemaName

      protected AbstractSchemaName findAbstractSchemaName(UpdateItem expression)
    • getNullValueVisitor

      protected DefaultSemanticValidator.NullValueVisitor getNullValueVisitor()
    • getType

      protected Object getType(Expression expression)
    • getTypeHelper

      protected ITypeHelper getTypeHelper()
    • getUpdateClauseAbstractSchemaNameFinder

      protected DefaultSemanticValidator.UpdateClauseAbstractSchemaNameFinder getUpdateClauseAbstractSchemaNameFinder()
    • getValidator

      protected DefaultSemanticValidator.TypeValidator getValidator(Class<? extends DefaultSemanticValidator.TypeValidator> validatorClass)
    • initialize

      protected void initialize()
      Description copied from class: AbstractValidator
      Initializes this validator.
      Overrides:
      initialize in class AbstractSemanticValidator
    • isBooleanType

      protected boolean isBooleanType(Expression expression)
      Determines whether the given Expression is of the correct type based on these rules:
      Parameters:
      expression - The Expression to validate
      Returns:
      true if the given Expression passes the checks; false otherwise
    • isComparisonEquivalentType

      protected boolean isComparisonEquivalentType(Expression expression1, Expression expression2)
    • isEquivalentBetweenType

      protected boolean isEquivalentBetweenType(Expression expression1, Expression expression2)
    • isIntegralType

      protected boolean isIntegralType(Expression expression)
      Determines whether the given Expression is of the correct type based on these rules:
      • The Expression's type is an integral type (long or integer).
      Parameters:
      expression - The Expression to validate
      Returns:
      true if the given Expression passes the checks; false otherwise
    • isNullValue

      protected boolean isNullValue(Expression expression)
    • isNumericType

      protected boolean isNumericType(Expression expression)
      Determines whether the given Expression is of the correct type based on these rules:
      Parameters:
      expression - The Expression to validate
      Returns:
      true if the given Expression passes the checks; false otherwise
    • isStringType

      protected boolean isStringType(Expression expression)
      Determines whether the given Expression is of the correct type based on these rules:
      Parameters:
      expression - The Expression to validate
      Returns:
      true if the given Expression passes the checks; false otherwise
    • isValid

      protected boolean isValid(Expression expression, Class<? extends DefaultSemanticValidator.TypeValidator> validatorClass)
      Determines whether the given Expression is of the correct type by using the DefaultSemanticValidator.TypeValidator.
      Parameters:
      expression - The Expression to validate
      validatorClass - The Java class of the DefaultSemanticValidator.TypeValidator that will determine if the given Expression has the right type
      Returns:
      true if the given Expression passes the checks; false otherwise
    • isValidWithFindQueryBNF

      protected boolean isValidWithFindQueryBNF(AbstractExpression expression, String queryBNF)
    • selectClausePathExpressionPathType

      protected AbstractSemanticValidator.PathType selectClausePathExpressionPathType()
      Description copied from class: AbstractSemanticValidator
      Returns the type of path expression that is allowed in the SELECT clause.
      Specified by:
      selectClausePathExpressionPathType in class AbstractSemanticValidator
      Returns:
      The type of path expressions allowed. The spec defines it as basic or object mapping only, i.e. collection-valued path expression is not allowed
    • validateAbsExpression

      protected boolean validateAbsExpression(AbsExpression expression)
      Description copied from class: AbstractSemanticValidator
      Validates the encapsulated expression of the given ABS expression. The test to perform is:
      • If the encapsulated expression is a path expression, validation makes sure it is a basic mapping, an association field is not allowed.
      • If the encapsulated expression is not a path expression, validation will be redirected to that expression but the returned status will not be changed.
      Overrides:
      validateAbsExpression in class AbstractSemanticValidator
      Parameters:
      expression - The AbsExpression to validate by validating its encapsulated expression
      Returns:
      false if the encapsulated expression was validated and is invalid; true otherwise
    • validateArithmeticExpression

      protected int validateArithmeticExpression(ArithmeticExpression expression, String leftExpressionWrongTypeMessageKey, String rightExpressionWrongTypeMessageKey)
      Description copied from class: AbstractSemanticValidator
      Validates the type of the left and right expressions defined by the given ArithmeticExpression. The test to perform is:
      • If the encapsulated expression is a path expression, validation makes sure it is a basic mapping, an association field is not allowed.
      • If the encapsulated expression is not a path expression, validation will be redirected to that expression but the returned status will not be changed.
      Overrides:
      validateArithmeticExpression in class AbstractSemanticValidator
      Parameters:
      expression - The ArithmeticExpression to validate
      leftExpressionWrongTypeMessageKey - The key used to describe the left expression does not have a valid type
      rightExpressionWrongTypeMessageKey - The key used to describe the right expression does not have a valid type
      Returns:
      A number indicating the validation result. AbstractSemanticValidator.isValid(int, int) can be used to determine the validation status of an expression based on its position
    • validateAvgFunction

      protected boolean validateAvgFunction(AvgFunction expression)
      Description copied from class: AbstractSemanticValidator
      Validates the encapsulated expression of the given AVG expression. The test to perform is:
      • If the encapsulated expression is a path expression, validation makes sure it is a basic mapping, an association field is not allowed.
      • If the encapsulated expression is not a path expression, validation will be redirected to that expression but the returned status will not be changed.
      Overrides:
      validateAvgFunction in class AbstractSemanticValidator
      Parameters:
      expression - The AvgFunction to validate by validating its encapsulated expression
      Returns:
      false if the encapsulated expression was validated and is invalid; true otherwise
    • validateBetweenExpression

      protected int validateBetweenExpression(BetweenExpression expression)
      Description copied from class: AbstractSemanticValidator
      Validates the given BetweenExpression. The test to perform is:
      • If the "first" expression is a path expression, validation makes sure it is a basic mapping, an association field is not allowed.
      Overrides:
      validateBetweenExpression in class AbstractSemanticValidator
      Parameters:
      expression - The BetweenExpression to validate
      Returns:
      A number indicating the validation result. AbstractSemanticValidator.isValid(int, int) can be used to determine the validation status of an expression based on its position
    • validateBooleanType

      protected boolean validateBooleanType(Expression expression, String messageKey)
      Determines whether the given Expression is of the correct type based on these rules:
      Parameters:
      expression - The Expression to validate
      messageKey - The key used to retrieve the localized message describing the problem
    • validateCollectionMemberExpression

      protected int validateCollectionMemberExpression(CollectionMemberExpression expression)
      Description copied from class: AbstractSemanticValidator
      Validates the given CollectionMemberExpression. Only the collection-valued path expression is validated.
      Overrides:
      validateCollectionMemberExpression in class AbstractSemanticValidator
      Parameters:
      expression - The CollectionMemberExpression to validate
      Returns:
      A number indicating the validation result. AbstractSemanticValidator.isValid(int, int) can be used to determine the validation status of an expression based on its position
    • validateComparisonExpression

      protected boolean validateComparisonExpression(ComparisonExpression expression)
      Description copied from class: AbstractSemanticValidator
      Validates the left and right expressions of the given ComparisonExpression. The tests to perform are:
      • If the comparison operator is either '=' or '<>'. The expressions can only be
        • Two identification variables;
        • Two path expressions resolving to an association field;
        • One can be a path expression resolving to a basic field and the other one has to resolve to a basic value.
      • If the comparison operator is either '<', '<=', '>=', '>'. The expressions cannot be
        • Two identification variables;
        • Two path expressions resolving to an association field;
      Overrides:
      validateComparisonExpression in class AbstractSemanticValidator
      Parameters:
      expression - The ConcatExpression to validate by validating its left and right expressions
      Returns:
      The status of the comparison between the left and right expression: true if the two expressions pass the rules defined by this method; false otherwise
    • validateConcatExpression

      protected boolean validateConcatExpression(ConcatExpression expression)
      Description copied from class: AbstractSemanticValidator
      Validates the encapsulated expression of the given CONCAT expression. The tests to perform are:
      • If the encapsulated expression is a path expression, validation makes sure it is a basic mapping, an association field is not allowed.
      • If the encapsulated expression is not a path expression, validation will be redirected to that expression but the returned status will not be changed.
      Overrides:
      validateConcatExpression in class AbstractSemanticValidator
      Parameters:
      expression - The ConcatExpression to validate by validating its encapsulated expression
      Returns:
      false if the first encapsulated expression was validated and is invalid; true otherwise
    • validateConstructorExpression

      protected void validateConstructorExpression(ConstructorExpression expression)
      Description copied from class: AbstractSemanticValidator
      Validates the given ConstructorExpression. The default behavior does not require to semantically validate it.
      Overrides:
      validateConstructorExpression in class AbstractSemanticValidator
      Parameters:
      expression - The ConstructorExpression to validate
    • validateCountFunction

      protected void validateCountFunction(CountFunction expression)
      Description copied from class: AbstractSemanticValidator
      Validates the given CountFunction. The default behavior does not require to semantically validate it.
      Overrides:
      validateCountFunction in class AbstractSemanticValidator
      Parameters:
      expression - The CountFunction to validate
    • validateEntryExpression

      protected void validateEntryExpression(EntryExpression expression)
      Description copied from class: AbstractSemanticValidator
      Validates the given EntryExpression. The default behavior does not require to semantically validate it.
      Overrides:
      validateEntryExpression in class AbstractSemanticValidator
      Parameters:
      expression - The EntryExpression to validate
    • validateIdentificationVariable

      protected boolean validateIdentificationVariable(IdentificationVariable expression, String variable)
      Description copied from class: AbstractSemanticValidator
      Validates the given identification variable. The default behavior is to not validate it.
      Overrides:
      validateIdentificationVariable in class AbstractSemanticValidator
      Parameters:
      expression - The IdentificationVariable that is being visited
      variable - The actual identification variable, which is never an empty string
      Returns:
      true if the given identification variable is valid; false otherwise
    • validateIntegralType

      protected boolean validateIntegralType(Expression expression, String queryBNF, String messageKey)
      Determines whether the given Expression is of the correct type based on these rules:
      • The Expression returns a integral value;
      • The Expression's type is an integral type (long or integer).
      Parameters:
      expression - The Expression to validate
      queryBNF - The unique identifier of the query BNF used to validate the type
      messageKey - The key used to retrieve the localized message describing the problem
      Returns:
      false if the given expression was validated and is invalid; true otherwise
    • validateKeyExpression

      protected void validateKeyExpression(KeyExpression expression)
      Description copied from class: AbstractSemanticValidator
      Validates the given KeyExpression. The default behavior does not require to semantically validate it.
      Overrides:
      validateKeyExpression in class AbstractSemanticValidator
      Parameters:
      expression - The KeyExpression to validate
    • validateLengthExpression

      protected boolean validateLengthExpression(LengthExpression expression)
      Description copied from class: AbstractSemanticValidator
      Validates the encapsulated expression of the given LENGTH expression. The test to perform is:
      • If the encapsulated expression is a path expression, validation makes sure it is a basic mapping, an association field is not allowed.
      • If the encapsulated expression is not a path expression, validation will be redirected to that expression but the returned status will not be changed.
      Overrides:
      validateLengthExpression in class AbstractSemanticValidator
      Parameters:
      expression - The LengthExpression to validate by validating its encapsulated expression
      Returns:
      false if the encapsulated expression was validated and is invalid; true otherwise
    • validateLocateExpression

      protected int validateLocateExpression(LocateExpression expression)
      Description copied from class: AbstractSemanticValidator
      Validates the encapsulated expression of the given LOCATE expression. The test to perform is:
      • If the encapsulated expression is a path expression, validation makes sure it is a basic mapping, an association field is not allowed.
      • If the encapsulated expression is not a path expression, validation will be redirected to that expression but the returned status will not be changed.
      Overrides:
      validateLocateExpression in class AbstractSemanticValidator
      Parameters:
      expression - The LocateExpression to validate by validating its encapsulated expression
      Returns:
      A number indicating the validation result. AbstractSemanticValidator.isValid(int, int) can be used to determine the validation status of an expression based on its position
    • validateLowerExpression

      protected boolean validateLowerExpression(LowerExpression expression)
      Description copied from class: AbstractSemanticValidator
      Validates the encapsulated expression of the given LOWER expression. The test to perform is:
      • If the encapsulated expression is a path expression, validation makes sure it is a basic mapping, an association field is not allowed.
      • If the encapsulated expression is not a path expression, validation will be redirected to that expression but the returned status will not be changed.
      Overrides:
      validateLowerExpression in class AbstractSemanticValidator
      Parameters:
      expression - The LowerExpression to validate by validating its encapsulated expression
      Returns:
      false if the encapsulated expression was validated and is invalid; true otherwise
    • validateMapIdentificationVariable

      protected void validateMapIdentificationVariable(EncapsulatedIdentificationVariableExpression expression)
    • validateModExpression

      protected int validateModExpression(ModExpression expression)
      Description copied from class: AbstractSemanticValidator
      Validates the encapsulated expression of the given MOD expression. The test to perform is:
      • If the encapsulated expression is a path expression, validation makes sure it is a basic mapping, an association field is not allowed.
      • If the encapsulated expression is not a path expression, validation will be redirected to that expression but the returned status will not be changed.
      Overrides:
      validateModExpression in class AbstractSemanticValidator
      Parameters:
      expression - The ModExpression to validate by validating its encapsulated expression
      Returns:
      A number indicating the validation result. AbstractSemanticValidator.isValid(int, int) can be used to determine the validation status of an expression based on its position
    • validateNotExpression

      protected void validateNotExpression(NotExpression expression)
      Description copied from class: AbstractSemanticValidator
      Validates the given NotExpression. The default behavior does not require to semantically validate it.
      Overrides:
      validateNotExpression in class AbstractSemanticValidator
      Parameters:
      expression - The NotExpression to validate
    • validateNullComparisonExpression

      protected void validateNullComparisonExpression(NullComparisonExpression expression)
      Description copied from class: AbstractSemanticValidator
      Validates the given NullComparisonExpression. The default behavior does not require to semantically validate it.
      Overrides:
      validateNullComparisonExpression in class AbstractSemanticValidator
      Parameters:
      expression - The NullComparisonExpression to validate
    • validateNumericType

      protected boolean validateNumericType(Expression expression, String messageKey)
      Determines whether the given Expression is of the correct type based on these rules:
      Parameters:
      expression - The Expression to validate
      messageKey - The key used to retrieve the localized message describing the problem
      Returns:
      false if the given expression was validated and is invalid; true otherwise
    • validateSqrtExpression

      protected boolean validateSqrtExpression(SqrtExpression expression)
      Description copied from class: AbstractSemanticValidator
      Validates the encapsulated expression of the given SQRT expression. The test to perform is:
      • If the encapsulated expression is a path expression, validation makes sure it is a basic mapping, an association field is not allowed.
      • If the encapsulated expression is not a path expression, validation will be redirected to that expression but the returned status will not be changed.
      Overrides:
      validateSqrtExpression in class AbstractSemanticValidator
      Parameters:
      expression - The SqrtExpression to validate by validating its encapsulated expression
      Returns:
      false if the encapsulated expression was validated and is invalid; true otherwise
    • validateStringType

      protected boolean validateStringType(Expression expression, String messageKey)
      Determines whether the given Expression is of the correct type based on these rules:
      Parameters:
      expression - The Expression to validate
      messageKey - The key used to retrieve the localized message describing the problem
      Returns:
      false if the given expression was validated and is invalid; true otherwise
    • validateSubstringExpression

      protected int validateSubstringExpression(SubstringExpression expression)
      Description copied from class: AbstractSemanticValidator
      Validates the encapsulated expression of the given SUBSTRING expression. The test to perform is:
      • If the encapsulated expression is a path expression, validation makes sure it is a basic mapping, an association field is not allowed.
      • If the encapsulated expression is not a path expression, validation will be redirected to that expression but the returned status will not be changed.
      Overrides:
      validateSubstringExpression in class AbstractSemanticValidator
      Parameters:
      expression - The SubstringExpression to validate by validating its encapsulated expression
      Returns:
      A number indicating the validation result. AbstractSemanticValidator.isValid(int, int) can be used to determine the validation status of an expression based on its position
    • validateSumFunction

      protected boolean validateSumFunction(SumFunction expression)
      Description copied from class: AbstractSemanticValidator
      Validates the encapsulated expression of the given MOD expression. The test to perform is:
      • If the encapsulated expression is a path expression, validation makes sure it is a basic mapping, an association field is not allowed.
      • If the encapsulated expression is not a path expression, validation will be redirected to that expression but the returned status will not be changed.
      Overrides:
      validateSumFunction in class AbstractSemanticValidator
      Parameters:
      expression - The ModExpression to validate by validating its encapsulated expression
      Returns:
      false if the encapsulated expression was validated and is invalid; true otherwise
    • validateUpdateItem

      protected boolean validateUpdateItem(UpdateItem expression)
      Description copied from class: AbstractSemanticValidator
      Validates the given UpdateItem by validating the traversability of the path expression. The path expression is valid if it follows one of the following rules:
      • The identification variable is omitted if it's not defined in the FROM clause;
      • The last path is a state field;
      • Only embedded field can be traversed.
      Overrides:
      validateUpdateItem in class AbstractSemanticValidator
      Parameters:
      expression - UpdateItem to validate its path expression
      Returns:
      true if the path expression is valid; false otherwise
    • validateUpdateItemTypes

      protected void validateUpdateItemTypes(UpdateItem expression, Object type)
    • validateUpperExpression

      protected boolean validateUpperExpression(UpperExpression expression)
      Description copied from class: AbstractSemanticValidator
      Validates the encapsulated expression of the given UPPER expression. The test to perform is:
      • If the encapsulated expression is a path expression, validation makes sure it is a basic mapping, an association field is not allowed.
      • If the encapsulated expression is not a path expression, validation will be redirected to that expression but the returned status will not be changed.
      Overrides:
      validateUpperExpression in class AbstractSemanticValidator
      Parameters:
      expression - The UpperExpression to validate by validating its encapsulated expression
      Returns:
      false if the encapsulated expression was validated and is invalid; true otherwise
    • validateValueExpression

      protected void validateValueExpression(ValueExpression expression)
      Description copied from class: AbstractSemanticValidator
      Validates the given ValueExpression. The default behavior does not require to semantically validate it.
      Overrides:
      validateValueExpression in class AbstractSemanticValidator
      Parameters:
      expression - The ValueExpression to validate