Search in sources :

Example 6 with UnaryExpr

use of com.github.javaparser.ast.expr.UnaryExpr in project drools by kiegroup.

the class ConstraintUtil method processExpression.

private static void processExpression(Expression expr) {
    if (expr instanceof MethodCallExpr) {
        MethodCallExpr mcExpr = (MethodCallExpr) expr;
        if (canInverse(mcExpr)) {
            inverseMethodCallExpr(mcExpr);
        }
    } else if (expr instanceof BinaryExpr) {
        BinaryExpr bExpr = (BinaryExpr) expr;
        if (bExpr.getOperator() == BinaryExpr.Operator.AND || bExpr.getOperator() == BinaryExpr.Operator.OR) {
            Expression left = bExpr.getLeft();
            processExpression(left);
            Expression right = bExpr.getRight();
            processExpression(right);
        }
    } else if (expr instanceof UnaryExpr) {
        Expression expression = ((UnaryExpr) expr).getExpression();
        processExpression(expression);
    } else if (expr instanceof EnclosedExpr) {
        Expression inner = ((EnclosedExpr) expr).getInner();
        processExpression(inner);
    }
}
Also used : Expression(com.github.javaparser.ast.expr.Expression) BinaryExpr(com.github.javaparser.ast.expr.BinaryExpr) EnclosedExpr(com.github.javaparser.ast.expr.EnclosedExpr) DrlxParseUtil.stripEnclosedExpr(org.drools.modelcompiler.builder.generator.DrlxParseUtil.stripEnclosedExpr) UnaryExpr(com.github.javaparser.ast.expr.UnaryExpr) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr)

Example 7 with UnaryExpr

use of com.github.javaparser.ast.expr.UnaryExpr in project checker-framework by typetools.

the class JointJavacJavaParserVisitor method visitUnary.

@Override
public Void visitUnary(UnaryTree javacTree, Node javaParserNode) {
    UnaryExpr node = castNode(UnaryExpr.class, javaParserNode, javacTree);
    processUnary(javacTree, node);
    javacTree.getExpression().accept(this, node.getExpression());
    return null;
}
Also used : UnaryExpr(com.github.javaparser.ast.expr.UnaryExpr)

Example 8 with UnaryExpr

use of com.github.javaparser.ast.expr.UnaryExpr in project drools by kiegroup.

the class ConstraintParser method parseUnaryExpr.

private DrlxParseResult parseUnaryExpr(UnaryExpr unaryExpr, Class<?> patternType, String bindingId, ConstraintExpression constraint, Expression drlxExpr, boolean hasBind, boolean isPositional) {
    TypedExpressionResult typedExpressionResult = new ExpressionTyper(context, patternType, bindingId, isPositional).toTypedExpression(unaryExpr);
    Optional<TypedExpression> opt = typedExpressionResult.getTypedExpression();
    if (!opt.isPresent()) {
        return new DrlxParseFail(new ParseExpressionErrorResult(drlxExpr));
    }
    TypedExpression typedExpression = opt.get();
    SingleDrlxParseSuccess innerResult = (SingleDrlxParseSuccess) compileToJavaRecursive(patternType, bindingId, constraint, unaryExpr.getExpression(), hasBind, isPositional);
    Expression innerExpression;
    if (unaryExpr.getExpression() instanceof EnclosedExpr && !(innerResult.getExpr() instanceof EnclosedExpr)) {
        // inner EnclosedExpr could be stripped
        innerExpression = new EnclosedExpr(innerResult.getExpr());
    } else {
        innerExpression = innerResult.getExpr();
    }
    return new SingleDrlxParseSuccess(patternType, bindingId, new UnaryExpr(innerExpression, unaryExpr.getOperator()), typedExpression.getType()).setDecodeConstraintType(Index.ConstraintType.UNKNOWN).setUsedDeclarations(typedExpressionResult.getUsedDeclarations()).setReactOnProperties(typedExpressionResult.getReactOnProperties()).setLeft(new TypedExpression(innerResult.getExpr(), innerResult.getExprType())).setIsPredicate(innerResult.isPredicate());
}
Also used : Expression(com.github.javaparser.ast.expr.Expression) TypedExpression(org.drools.modelcompiler.builder.generator.TypedExpression) DrlxExpression(org.drools.mvel.parser.ast.expr.DrlxExpression) ExpressionTyper(org.drools.modelcompiler.builder.generator.expressiontyper.ExpressionTyper) ParseExpressionErrorResult(org.drools.modelcompiler.builder.errors.ParseExpressionErrorResult) DrlxParseUtil.stripEnclosedExpr(org.drools.modelcompiler.builder.generator.DrlxParseUtil.stripEnclosedExpr) EnclosedExpr(com.github.javaparser.ast.expr.EnclosedExpr) TypedExpressionResult(org.drools.modelcompiler.builder.generator.expressiontyper.TypedExpressionResult) TypedExpression(org.drools.modelcompiler.builder.generator.TypedExpression) UnaryExpr(com.github.javaparser.ast.expr.UnaryExpr)

Example 9 with UnaryExpr

use of com.github.javaparser.ast.expr.UnaryExpr in project drools by kiegroup.

the class NativeOperatorSpec method getExpression.

public Expression getExpression(RuleContext context, PointFreeExpr pointFreeExpr, TypedExpression left, ExpressionTyper expressionTyper) {
    MethodCallExpr methodCallExpr = createDslTopLevelMethod(EVAL_CALL);
    String opName = pointFreeExpr.getOperator().asString();
    Operator operator = addOperatorArgument(context, methodCallExpr, opName);
    if (operator != null && !operator.isCompatibleWithType(left.getRawClass())) {
        context.addCompilationError(new InvalidExpressionErrorResult("Cannot use contains on class " + left.getRawClass() + " in expression '" + PrintUtil.printNode(pointFreeExpr) + "'"));
    }
    methodCallExpr.addArgument(left.getExpression());
    for (Expression rightExpr : pointFreeExpr.getRight()) {
        final Optional<TypedExpression> optionalRight = expressionTyper.toTypedExpression(rightExpr).getTypedExpression();
        optionalRight.ifPresent(right -> {
            final TypedExpression coercedRight;
            if (operator != null && operator.requiresCoercion()) {
                final CoercedExpression.CoercedExpressionResult coerced = new CoercedExpression(left, right, false).coerce();
                coercedRight = getCoercedRightExpression(context.getPackageModel(), coerced);
            } else {
                coercedRight = right;
            }
            methodCallExpr.addArgument(coercedRight.getExpression());
        });
    }
    return pointFreeExpr.isNegated() ? new UnaryExpr(methodCallExpr, UnaryExpr.Operator.LOGICAL_COMPLEMENT) : methodCallExpr;
}
Also used : Operator(org.drools.model.functions.Operator) InvalidExpressionErrorResult(org.drools.modelcompiler.builder.errors.InvalidExpressionErrorResult) ConstraintParser.getCoercedRightExpression(org.drools.modelcompiler.builder.generator.drlxparse.ConstraintParser.getCoercedRightExpression) CoercedExpression(org.drools.modelcompiler.builder.generator.drlxparse.CoercedExpression) TypedExpression(org.drools.modelcompiler.builder.generator.TypedExpression) Expression(com.github.javaparser.ast.expr.Expression) TypedExpression(org.drools.modelcompiler.builder.generator.TypedExpression) UnaryExpr(com.github.javaparser.ast.expr.UnaryExpr) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr) CoercedExpression(org.drools.modelcompiler.builder.generator.drlxparse.CoercedExpression)

Example 10 with UnaryExpr

use of com.github.javaparser.ast.expr.UnaryExpr in project checker-framework by typetools.

the class AnnotationFileParser method getValueOfExpressionInAnnotation.

/**
 * Returns the value of {@code expr}.
 *
 * @param name the name of an annotation element/argument, used for diagnostic messages
 * @param expr the expression to determine the value of
 * @param valueKind the type of the result
 * @return the value of {@code expr}
 * @throws AnnotationFileParserException if a problem occurred getting the value
 */
private Object getValueOfExpressionInAnnotation(String name, Expression expr, TypeKind valueKind) throws AnnotationFileParserException {
    if (expr instanceof FieldAccessExpr || expr instanceof NameExpr) {
        VariableElement elem;
        if (expr instanceof NameExpr) {
            elem = findVariableElement((NameExpr) expr);
        } else {
            elem = findVariableElement((FieldAccessExpr) expr);
        }
        if (elem == null) {
            throw new AnnotationFileParserException(String.format("variable %s not found", expr));
        }
        Object value = elem.getConstantValue() != null ? elem.getConstantValue() : elem;
        if (value instanceof Number) {
            return convert((Number) value, valueKind);
        } else {
            return value;
        }
    } else if (expr instanceof StringLiteralExpr) {
        return ((StringLiteralExpr) expr).asString();
    } else if (expr instanceof BooleanLiteralExpr) {
        return ((BooleanLiteralExpr) expr).getValue();
    } else if (expr instanceof CharLiteralExpr) {
        return convert((int) ((CharLiteralExpr) expr).asChar(), valueKind);
    } else if (expr instanceof DoubleLiteralExpr) {
        // double, too.
        return ((DoubleLiteralExpr) expr).asDouble();
    } else if (expr instanceof IntegerLiteralExpr) {
        return convert(((IntegerLiteralExpr) expr).asNumber(), valueKind);
    } else if (expr instanceof LongLiteralExpr) {
        return convert(((LongLiteralExpr) expr).asNumber(), valueKind);
    } else if (expr instanceof UnaryExpr) {
        switch(expr.toString()) {
            // the smallest member of an integral type is larger than the largest value.
            case "-9223372036854775808L":
            case "-9223372036854775808l":
                return convert(Long.MIN_VALUE, valueKind, false);
            case "-2147483648":
                return convert(Integer.MIN_VALUE, valueKind, false);
            default:
                if (((UnaryExpr) expr).getOperator() == UnaryExpr.Operator.MINUS) {
                    Object value = getValueOfExpressionInAnnotation(name, ((UnaryExpr) expr).getExpression(), valueKind);
                    if (value instanceof Number) {
                        return convert((Number) value, valueKind, true);
                    }
                }
                throw new AnnotationFileParserException("unexpected Unary annotation expression: " + expr);
        }
    } else if (expr instanceof ClassExpr) {
        ClassExpr classExpr = (ClassExpr) expr;
        // Type.toString(): @FullyQualifiedName
        @SuppressWarnings("signature") @FullyQualifiedName String className = classExpr.getType().toString();
        if (importedTypes.containsKey(className)) {
            return importedTypes.get(className).asType();
        }
        TypeElement typeElement = findTypeOfName(className);
        if (typeElement == null) {
            throw new AnnotationFileParserException("unknown class name " + className);
        }
        return typeElement.asType();
    } else if (expr instanceof NullLiteralExpr) {
        throw new AnnotationFileParserException("Illegal annotation value null, for " + name);
    } else {
        throw new AnnotationFileParserException("Unexpected annotation expression: " + expr);
    }
}
Also used : IntegerLiteralExpr(com.github.javaparser.ast.expr.IntegerLiteralExpr) TypeElement(javax.lang.model.element.TypeElement) NameExpr(com.github.javaparser.ast.expr.NameExpr) StringLiteralExpr(com.github.javaparser.ast.expr.StringLiteralExpr) FullyQualifiedName(org.checkerframework.checker.signature.qual.FullyQualifiedName) CharLiteralExpr(com.github.javaparser.ast.expr.CharLiteralExpr) VariableElement(javax.lang.model.element.VariableElement) UnaryExpr(com.github.javaparser.ast.expr.UnaryExpr) NullLiteralExpr(com.github.javaparser.ast.expr.NullLiteralExpr) DoubleLiteralExpr(com.github.javaparser.ast.expr.DoubleLiteralExpr) BooleanLiteralExpr(com.github.javaparser.ast.expr.BooleanLiteralExpr) LongLiteralExpr(com.github.javaparser.ast.expr.LongLiteralExpr) FieldAccessExpr(com.github.javaparser.ast.expr.FieldAccessExpr) ClassExpr(com.github.javaparser.ast.expr.ClassExpr)

Aggregations

UnaryExpr (com.github.javaparser.ast.expr.UnaryExpr)11 Expression (com.github.javaparser.ast.expr.Expression)8 BinaryExpr (com.github.javaparser.ast.expr.BinaryExpr)6 MethodCallExpr (com.github.javaparser.ast.expr.MethodCallExpr)6 EnclosedExpr (com.github.javaparser.ast.expr.EnclosedExpr)5 NameExpr (com.github.javaparser.ast.expr.NameExpr)5 StringLiteralExpr (com.github.javaparser.ast.expr.StringLiteralExpr)5 TypedExpression (org.drools.modelcompiler.builder.generator.TypedExpression)5 DrlxExpression (org.drools.mvel.parser.ast.expr.DrlxExpression)5 HalfBinaryExpr (org.drools.mvel.parser.ast.expr.HalfBinaryExpr)5 FieldAccessExpr (com.github.javaparser.ast.expr.FieldAccessExpr)4 NullLiteralExpr (com.github.javaparser.ast.expr.NullLiteralExpr)4 DrlNameExpr (org.drools.mvel.parser.ast.expr.DrlNameExpr)4 NodeList (com.github.javaparser.ast.NodeList)3 CharLiteralExpr (com.github.javaparser.ast.expr.CharLiteralExpr)3 DoubleLiteralExpr (com.github.javaparser.ast.expr.DoubleLiteralExpr)3 LiteralExpr (com.github.javaparser.ast.expr.LiteralExpr)3 ObjectCreationExpr (com.github.javaparser.ast.expr.ObjectCreationExpr)3 NodeWithArguments (com.github.javaparser.ast.nodeTypes.NodeWithArguments)3 ArrayList (java.util.ArrayList)3