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 vue-gwt by Axellience.
the class TemplateParser method getTypeFromCast.
/**
* Get the type of an expression from the cast at the beginning.
* (int) 12 -> 12 of type int
* (int) 15 + 5 -> 15 + 5 of type int
* (float) (12 + 3) -> 12 + 3 of type float
* ((int) 12) + 3 -> ((int) 12) + 3 of type Any
* ((JsArray) myArray).getAt(0) -> ((JsArray) myArray).getAt(0) of type Any
* @param expression The expression to process
* @return The modified expression (where cast has been removed if necessary)
*/
private Expression getTypeFromCast(Expression expression) {
if (expression instanceof BinaryExpr) {
Expression mostLeft = getLeftmostExpression(expression);
if (mostLeft instanceof CastExpr) {
CastExpr castExpr = (CastExpr) mostLeft;
currentExpressionReturnType = stringTypeToTypeName(castExpr.getType().toString());
BinaryExpr parent = (BinaryExpr) mostLeft.getParentNode().get();
parent.setLeft(castExpr.getExpression());
}
} else if (expression instanceof CastExpr) {
CastExpr castExpr = (CastExpr) expression;
currentExpressionReturnType = stringTypeToTypeName(castExpr.getType().toString());
expression = castExpr.getExpression();
}
return expression;
}
Aggregations