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);
}
}
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;
}
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());
}
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;
}
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);
}
}
Aggregations