@Override public void syntaxError(Recognizer<?, ?> recognizer, Object offendingSymbol, int line, int charPositionInLine, String msg, RecognitionException e) { throw new SyntaxErrorException("line " + line + ":" + charPositionInLine + " " + msg); }
private Expression createExpression(RuleInvoker ruleInvoker, String expression, boolean allowCaseWhen, boolean allowQuantifiedPredicates, boolean allowTreatJoinExtension, MacroConfiguration macroConfiguration, Set<String> usedMacros) { if (expression == null) { throw new NullPointerException("expression"); } if (expression.isEmpty()) { throw new IllegalArgumentException("expression"); } JPQLSelectExpressionLexer l = new JPQLSelectExpressionLexer(new ANTLRInputStream(expression)); configureLexer(l); CommonTokenStream tokens = new CommonTokenStream(l); JPQLSelectExpressionParser p = new JPQLSelectExpressionParser(tokens, allowCaseWhen, allowQuantifiedPredicates, allowTreatJoinExtension); configureParser(p); ParserRuleContext ctx; try { ctx = ruleInvoker.invokeRule(p); } catch (SyntaxErrorException ex) { throw new SyntaxErrorException("Could not parse expression '" + expression + "', " + ex.getMessage(), ex); } if (LOG.isLoggable(Level.FINEST)) { LOG.finest(ctx.toStringTree()); } JPQLSelectExpressionVisitorImpl visitor = new JPQLSelectExpressionVisitorImpl(aggregateFunctions, enumTypes, entityTypes, minEnumSegmentCount, minEntitySegmentCount, macroConfiguration == null ? Collections.EMPTY_MAP : macroConfiguration.macros, usedMacros); Expression parsedExpression = visitor.visit(ctx); if (optimize) { parsedExpression = parsedExpression.accept(optimizer); } return parsedExpression; }
context.getExpressionFactory().createSimpleExpression(mapping, false); context.addError("Invalid mapping expression '" + mapping + "' of the " + getLocation() + " for an updatable attribute. Consider annotating the attribute with @UpdatableMapping(updatable = false) or simplify the mapping expression to a simple path expression. Encountered error: " + ex.getMessage()); } catch (SyntaxErrorException ex2) { context.addError("Syntax error in mapping expression '" + mapping + "' of the " + getLocation() + ": " + ex.getMessage());
private Expression createExpression(RuleInvoker ruleInvoker, String expression, boolean allowCaseWhen, boolean allowQuantifiedPredicates, boolean allowTreatJoinExtension, MacroConfiguration macroConfiguration, Set<String> usedMacros) { if (expression == null) { throw new NullPointerException("expression"); } if (expression.isEmpty()) { throw new IllegalArgumentException("expression"); } JPQLSelectExpressionLexer l = new JPQLSelectExpressionLexer(new ANTLRInputStream(expression)); configureLexer(l); CommonTokenStream tokens = new CommonTokenStream(l); JPQLSelectExpressionParser p = new JPQLSelectExpressionParser(tokens, allowCaseWhen, allowQuantifiedPredicates, allowTreatJoinExtension); configureParser(p); ParserRuleContext ctx; try { ctx = ruleInvoker.invokeRule(p); } catch (SyntaxErrorException ex) { throw new SyntaxErrorException("Could not parse expression '" + expression + "', " + ex.getMessage(), ex); } if (LOG.isLoggable(Level.FINEST)) { LOG.finest(ctx.toStringTree()); } JPQLSelectExpressionVisitorImpl visitor = new JPQLSelectExpressionVisitorImpl(aggregateFunctions, enumTypes, entityTypes, minEnumSegmentCount, minEntitySegmentCount, macroConfiguration == null ? Collections.EMPTY_MAP : macroConfiguration.macros, usedMacros); Expression parsedExpression = visitor.visit(ctx); if (optimize) { parsedExpression = parsedExpression.accept(optimizer); } return parsedExpression; }
private static void validateTypesCompatible(ManagedType<?> managedType, String expression, Class<?> targetType, Class<?> targetElementType, boolean subtypesAllowed, boolean singular, MetamodelBuildingContext context, ExpressionLocation expressionLocation, String location) { if (expression.isEmpty()) { if (isCompatible(managedType.getJavaType(), null, targetType, targetElementType, subtypesAllowed, singular)) { return; } context.addError(typeCompatibilityError( Arrays.<TargetType>asList(new ScalarTargetResolvingExpressionVisitor.TargetTypeImpl( false, null, managedType.getJavaType(), null, null )), targetType, targetElementType, expressionLocation, location )); return; } ScalarTargetResolvingExpressionVisitor visitor = new ScalarTargetResolvingExpressionVisitor(managedType, context.getEntityMetamodel(), context.getJpqlFunctions()); try { context.getTypeValidationExpressionFactory().createSimpleExpression(expression, false).accept(visitor); } catch (SyntaxErrorException ex) { context.addError("Syntax error in " + expressionLocation + " '" + expression + "' of the " + location + ": " + ex.getMessage()); } catch (IllegalArgumentException ex) { context.addError("An error occurred while trying to resolve the " + expressionLocation + " of the " + location + ": " + ex.getMessage()); } validateTypesCompatible(visitor.getPossibleTargetTypes(), targetType, targetElementType, subtypesAllowed, singular, context, expressionLocation, location); }
@Override public void syntaxError(Recognizer<?, ?> recognizer, Object offendingSymbol, int line, int charPositionInLine, String msg, RecognitionException e) { throw new SyntaxErrorException("line " + line + ":" + charPositionInLine + " " + msg); }
context.getExpressionFactory().createSimpleExpression(fetch, false); context.addError("Invalid fetch expression '" + fetch + "' of the " + getLocation() + ". Simplify the fetch expression to a simple path expression. Encountered error: " + ex.getMessage()); } catch (SyntaxErrorException ex2) { context.addError("Syntax error in " + errorLocation + " '" + fetch + "' of the " + getLocation() + ": " + ex.getMessage()); context.addError("Syntax error in " + ExpressionLocation.CORRELATION_EXPRESSION + " '" + correlationExpression + "' of the " + getLocation() + ": " + ex.getMessage()); } catch (IllegalArgumentException ex) { context.addError("An error occurred while trying to resolve the " + ExpressionLocation.CORRELATION_EXPRESSION + " of the " + getLocation() + ": " + ex.getMessage()); context.getExpressionFactory().createSimpleExpression(mapping, false); context.addError("Invalid mapping expression '" + mapping + "' of the " + getLocation() + " for an updatable attribute. Consider annotating the attribute with @UpdatableMapping(updatable = false) or simplify the mapping expression to a simple path expression. Encountered error: " + ex.getMessage()); } catch (SyntaxErrorException ex2) { context.addError("Syntax error in mapping expression '" + mapping + "' of the " + getLocation() + ": " + ex.getMessage());
private Expression makeCollectionValued(Expression expr) { if (expr instanceof PathExpression) { ((PathExpression) expr).setUsedInCollectionFunction(true); } else { throw new SyntaxErrorException("Function expects collection valued path and cannot be applied to predicate [" + expr + "]"); } return expr; }
@Override public Expression visitErrorNode(ErrorNode node) { throw new SyntaxErrorException("Parsing failed: " + node.getText()); }
@Override public Expression visitErrorNode(ErrorNode node) { throw new SyntaxErrorException("Parsing failed: " + node.getText()); }
@Override public Expression visitEntity_type_or_literal_expression(JPQLSelectExpressionParser.Entity_type_or_literal_expressionContext ctx) { JPQLSelectExpressionParser.Entity_type_expressionContext context = ctx.entity_type_expression(); if (context != null) { return context.accept(this); } String literalStr = ctx.getText(); Expression literalExpression = createEntityTypeLiteral(literalStr); if (literalExpression != null) { return literalExpression; } throw new SyntaxErrorException("Could not interpret '" + literalStr + "' as entity literal!"); }
@Override public Expression visitEntity_type_or_literal_expression(JPQLSelectExpressionParser.Entity_type_or_literal_expressionContext ctx) { JPQLSelectExpressionParser.Entity_type_expressionContext context = ctx.entity_type_expression(); if (context != null) { return context.accept(this); } String literalStr = ctx.getText(); Expression literalExpression = createEntityTypeLiteral(literalStr); if (literalExpression != null) { return literalExpression; } throw new SyntaxErrorException("Could not interpret '" + literalStr + "' as entity literal!"); }
@Override public Expression visitLiteral(JPQLSelectExpressionParser.LiteralContext ctx) { JPQLSelectExpressionParser.Simple_literalContext literalContext = ctx.simple_literal(); if (literalContext != null) { return literalContext.accept(this); } String literalStr = ctx.getText(); if (ctx.pathElem.size() > minEnumSegmentCount) { Expression literalExpression = createEnumLiteral(literalStr); if (literalExpression != null) { return literalExpression; } } Expression literalExpression = createEntityTypeLiteral(literalStr); if (literalExpression != null) { return literalExpression; } throw new SyntaxErrorException("Could not interpret '" + literalStr + "' as enum or entity literal!"); }
public Expression visitMacroExpression(String macroName, ParserRuleContext ctx) { List<Expression> funcArgs = new ArrayList<Expression>(ctx.getChildCount()); // Special handling of empty invocation, the position 2 contains an empty child node if (ctx.getChildCount() != 4 || !ctx.getChild(2).getText().isEmpty()) { for (int i = 0; i < ctx.getChildCount(); i++) { if (!(ctx.getChild(i) instanceof TerminalNode)) { funcArgs.add(ctx.getChild(i).accept(this)); } } } MacroFunction macro = macros.get(macroName); if (macro == null) { throw new SyntaxErrorException("The macro '" + macroName + "' could not be found in the macro map!"); } if (usedMacros != null) { usedMacros.add(macroName); } try { return macro.apply(funcArgs); } catch (RuntimeException ex) { throw new IllegalArgumentException("Could not apply the macro for the expression: " + ctx.getText(), ex); } }
public Expression visitMacroExpression(String macroName, ParserRuleContext ctx) { List<Expression> funcArgs = new ArrayList<Expression>(ctx.getChildCount()); // Special handling of empty invocation, the position 2 contains an empty child node if (ctx.getChildCount() != 4 || !ctx.getChild(2).getText().isEmpty()) { for (int i = 0; i < ctx.getChildCount(); i++) { if (!(ctx.getChild(i) instanceof TerminalNode)) { funcArgs.add(ctx.getChild(i).accept(this)); } } } MacroFunction macro = macros.get(macroName); if (macro == null) { throw new SyntaxErrorException("The macro '" + macroName + "' could not be found in the macro map!"); } if (usedMacros != null) { usedMacros.add(macroName); } try { return macro.apply(funcArgs); } catch (RuntimeException ex) { throw new IllegalArgumentException("Could not apply the macro for the expression: " + ctx.getText(), ex); } }
@Override public Expression visitLiteral(JPQLSelectExpressionParser.LiteralContext ctx) { JPQLSelectExpressionParser.Simple_literalContext literalContext = ctx.simple_literal(); if (literalContext != null) { return literalContext.accept(this); } String literalStr = ctx.getText(); if (ctx.pathElem.size() > minEnumSegmentCount) { Expression literalExpression = createEnumLiteral(literalStr); if (literalExpression != null) { return literalExpression; } } Expression literalExpression = createEntityTypeLiteral(literalStr); if (literalExpression != null) { return literalExpression; } throw new SyntaxErrorException("Could not interpret '" + literalStr + "' as enum or entity literal!"); }
@Override public Expression visitComparisonExpression_path_type(ComparisonExpression_path_typeContext ctx) { BinaryExpressionPredicate pred = (EqPredicate) ctx.equality_comparison_operator().accept(this); String literalStr = ctx.left.getText(); Expression expression = createEntityTypeLiteral(literalStr); if (expression == null) { throw new SyntaxErrorException("Could not interpret '" + literalStr + "' as entity literal!"); } pred.setLeft(expression); pred.setRight(ctx.right.accept(this)); return pred; }
@Override public Expression visitComparisonExpression_type_path(ComparisonExpression_type_pathContext ctx) { BinaryExpressionPredicate pred = (EqPredicate) ctx.equality_comparison_operator().accept(this); pred.setLeft(ctx.left.accept(this)); String literalStr = ctx.right.getText(); Expression expression = createEntityTypeLiteral(literalStr); if (expression == null) { throw new SyntaxErrorException("Could not interpret '" + literalStr + "' as entity literal!"); } pred.setRight(expression); return pred; }
@Override public Expression visitComparisonExpression_path_type(ComparisonExpression_path_typeContext ctx) { BinaryExpressionPredicate pred = (EqPredicate) ctx.equality_comparison_operator().accept(this); String literalStr = ctx.left.getText(); Expression expression = createEntityTypeLiteral(literalStr); if (expression == null) { throw new SyntaxErrorException("Could not interpret '" + literalStr + "' as entity literal!"); } pred.setLeft(expression); pred.setRight(ctx.right.accept(this)); return pred; }
@Override public Expression visitComparisonExpression_type_path(ComparisonExpression_type_pathContext ctx) { BinaryExpressionPredicate pred = (EqPredicate) ctx.equality_comparison_operator().accept(this); pred.setLeft(ctx.left.accept(this)); String literalStr = ctx.right.getText(); Expression expression = createEntityTypeLiteral(literalStr); if (expression == null) { throw new SyntaxErrorException("Could not interpret '" + literalStr + "' as entity literal!"); } pred.setRight(expression); return pred; }