use of com.github.javaparser.ast.expr.CastExpr in project javaparser by javaparser.
the class JavaParserTest method rangeOfIntersectionType.
@Test
public void rangeOfIntersectionType() {
String code = "class A {" + EOL + " Object f() {" + EOL + " return (Comparator<Map.Entry<K, V>> & Serializable)(c1, c2) -> c1.getKey().compareTo(c2.getKey()); " + EOL + "}}";
CompilationUnit cu = JavaParser.parse(code);
MethodDeclaration methodDeclaration = cu.getClassByName("A").get().getMember(0).asMethodDeclaration();
ReturnStmt returnStmt = methodDeclaration.getBody().get().getStatement(0).asReturnStmt();
CastExpr castExpr = returnStmt.getExpression().get().asCastExpr();
Type type = castExpr.getType();
assertEquals(range(3, 13, 3, 54), type.getRange().get());
}
use of com.github.javaparser.ast.expr.CastExpr in project javaparser by javaparser.
the class PrettyPrintVisitorTest method printIntersectionType.
@Test
public void printIntersectionType() {
String code = "(Runnable & Serializable) (() -> {})";
Expression expression = JavaParser.parseExpression(code);
Type type = ((CastExpr) expression).getType();
assertEquals("Runnable & Serializable", print(type));
}
use of com.github.javaparser.ast.expr.CastExpr in project drools by kiegroup.
the class ExpressionTyper method processFirstNode.
private Optional<TypedExpressionCursor> processFirstNode(Expression drlxExpr, List<Node> childNodes, Node firstNode, boolean isInLineCast, java.lang.reflect.Type originalTypeCursor) {
Optional<TypedExpressionCursor> result;
if (isThisExpression(firstNode) || (firstNode instanceof DrlNameExpr && printNode(firstNode).equals(bindingId))) {
result = of(thisExpr(drlxExpr, childNodes, isInLineCast, originalTypeCursor));
} else if (firstNode instanceof DrlNameExpr) {
result = drlNameExpr(drlxExpr, (DrlNameExpr) firstNode, isInLineCast, originalTypeCursor);
} else if (firstNode instanceof NameExpr) {
result = drlNameExpr(drlxExpr, new DrlNameExpr(((NameExpr) firstNode).getName()), isInLineCast, originalTypeCursor);
} else if (firstNode instanceof FieldAccessExpr) {
if (((FieldAccessExpr) firstNode).getScope() instanceof ThisExpr) {
result = of(fieldAccessExpr(originalTypeCursor, ((FieldAccessExpr) firstNode).getName()));
} else {
try {
Class<?> resolvedType = ruleContext.getTypeResolver().resolveType(PrintUtil.printNode(firstNode));
result = of(new TypedExpressionCursor(new NameExpr(PrintUtil.printNode(firstNode)), resolvedType));
} catch (ClassNotFoundException e) {
result = empty();
}
}
} else if (firstNode instanceof NullSafeFieldAccessExpr && ((NullSafeFieldAccessExpr) firstNode).getScope() instanceof ThisExpr) {
result = of(fieldAccessExpr(originalTypeCursor, ((NullSafeFieldAccessExpr) firstNode).getName()));
} else if (firstNode instanceof MethodCallExpr) {
Optional<Expression> scopeExpr = ((MethodCallExpr) firstNode).getScope();
Optional<DeclarationSpec> scopeDecl = scopeExpr.flatMap(scope -> ruleContext.getDeclarationById(PrintUtil.printNode(scope)));
Expression scope;
java.lang.reflect.Type type;
if (scopeDecl.isPresent() && !scopeDecl.get().getBindingId().equals(bindingId)) {
type = scopeDecl.get().getDeclarationClass();
scope = new NameExpr(scopeDecl.get().getBindingId());
context.addUsedDeclarations(scopeDecl.get().getBindingId());
} else if (scopeExpr.isPresent()) {
TypedExpressionCursor parsedScope = processFirstNode(drlxExpr, childNodes, scopeExpr.get(), isInLineCast, originalTypeCursor).get();
type = parsedScope.typeCursor;
scope = parsedScope.expressionCursor;
} else {
type = originalTypeCursor;
scope = new NameExpr(THIS_PLACEHOLDER);
}
result = of(methodCallExpr((MethodCallExpr) firstNode, type, scope));
} else if (firstNode instanceof ObjectCreationExpr) {
result = of(objectCreationExpr((ObjectCreationExpr) firstNode));
} else if (firstNode instanceof StringLiteralExpr) {
result = of(stringLiteralExpr((StringLiteralExpr) firstNode));
} else if (firstNode instanceof EnclosedExpr) {
result = processFirstNode(drlxExpr, childNodes, ((EnclosedExpr) firstNode).getInner(), isInLineCast, originalTypeCursor);
} else if (firstNode instanceof CastExpr) {
result = castExpr((CastExpr) firstNode, drlxExpr, childNodes, isInLineCast, originalTypeCursor);
} else if (firstNode instanceof ArrayCreationExpr) {
result = of(arrayCreationExpr(((ArrayCreationExpr) firstNode)));
} else if (firstNode instanceof BinaryExpr) {
result = of(binaryExpr((BinaryExpr) firstNode));
} else if (firstNode instanceof ArrayAccessExpr) {
Optional<DeclarationSpec> scopeDecl = ruleContext.getDeclarationById(((ArrayAccessExpr) firstNode).getName().toString());
Expression scope;
java.lang.reflect.Type type;
if (scopeDecl.isPresent() && !scopeDecl.get().getBindingId().equals(bindingId)) {
type = scopeDecl.get().getDeclarationClass();
scope = new NameExpr(scopeDecl.get().getBindingId());
context.addUsedDeclarations(scopeDecl.get().getBindingId());
} else {
type = originalTypeCursor;
scope = new NameExpr(THIS_PLACEHOLDER);
}
result = arrayAccessExpr((ArrayAccessExpr) firstNode, type, scope);
} else if (firstNode instanceof MapCreationLiteralExpression) {
result = mapCreationLiteral((MapCreationLiteralExpression) firstNode, originalTypeCursor);
} else if (firstNode instanceof ListCreationLiteralExpression) {
result = listCreationLiteral((ListCreationLiteralExpression) firstNode, originalTypeCursor);
} else {
result = of(new TypedExpressionCursor((Expression) firstNode, getExpressionType(ruleContext, ruleContext.getTypeResolver(), (Expression) firstNode, context.getUsedDeclarations())));
}
if (result.isPresent()) {
processNullSafeDereferencing(drlxExpr);
}
return result.map(te -> {
if (isInLineCast) {
Expression exprWithInlineCast = addCastToExpression(toRawClass(te.typeCursor), te.expressionCursor, isInLineCast);
return new TypedExpressionCursor(exprWithInlineCast, te.typeCursor);
} else {
return te;
}
});
}
use of com.github.javaparser.ast.expr.CastExpr in project drools by kiegroup.
the class AbstractExpressionBuilder method narrowExpressionToType.
protected Expression narrowExpressionToType(TypedExpression right, java.lang.reflect.Type leftType) {
Expression expression = right.getExpression();
if (expression instanceof NullLiteralExpr) {
return expression;
}
if (leftType.equals(Double.class)) {
return new CastExpr(PrimitiveType.doubleType(), expression);
}
if (leftType.equals(Long.class)) {
if (right.getType().equals(Double.class) || right.getType().equals(double.class)) {
return new MethodCallExpr(expression, "longValue");
} else {
return new CastExpr(PrimitiveType.longType(), expression);
}
}
if (expression instanceof LiteralExpr) {
if (expression instanceof BigDecimalLiteralExpr) {
return toNewExpr(BigDecimal.class, toStringLiteral(((BigDecimalLiteralExpr) expression).asBigDecimal().toString()));
}
if (expression instanceof BigIntegerLiteralExpr) {
return toNewExpr(toRawClass(leftType), toStringLiteral(((BigIntegerLiteralExpr) expression).asBigInteger().toString()));
}
if (leftType.equals(BigDecimal.class)) {
String expressionString = stringValue(expression);
final BigDecimal bigDecimal = new BigDecimal(expressionString);
return toNewExpr(BigDecimal.class, toStringLiteral(bigDecimal.toString()));
}
if (leftType.equals(BigInteger.class)) {
String expressionString = stringValue(expression);
final BigInteger bigInteger = new BigDecimal(expressionString).toBigInteger();
return toNewExpr(BigInteger.class, toStringLiteral(bigInteger.toString()));
}
if (leftType.equals(float.class)) {
return new DoubleLiteralExpr(expression + "f");
}
}
if (expression instanceof NameExpr) {
if (leftType.equals(BigDecimal.class) && !right.getType().equals(BigDecimal.class)) {
return toNewExpr(BigDecimal.class, expression);
}
if (leftType.equals(BigInteger.class) && !right.getType().equals(BigInteger.class)) {
return toNewExpr(BigInteger.class, expression);
}
}
return expression;
}
use of com.github.javaparser.ast.expr.CastExpr in project drools by kiegroup.
the class CoercedExpression method coerceToString.
private static TypedExpression coerceToString(TypedExpression typedExpression) {
final Expression expression = typedExpression.getExpression();
TypedExpression coercedExpression;
if (expression instanceof CharLiteralExpr) {
coercedExpression = typedExpression.cloneWithNewExpression(toStringLiteral(((CharLiteralExpr) expression).getValue()));
} else if (typedExpression.isPrimitive()) {
coercedExpression = typedExpression.cloneWithNewExpression(new MethodCallExpr(new NameExpr("String"), "valueOf", NodeList.nodeList(expression)));
} else if (typedExpression.getType() == Object.class) {
coercedExpression = typedExpression.cloneWithNewExpression(new MethodCallExpr(expression, "toString"));
} else if (expression instanceof NameExpr) {
coercedExpression = typedExpression.cloneWithNewExpression(new CastExpr(toClassOrInterfaceType(String.class), expression));
} else {
coercedExpression = typedExpression.cloneWithNewExpression(toStringLiteral(expression.toString()));
}
return coercedExpression.setType(String.class);
}
Aggregations