Search in sources :

Example 6 with CastExpr

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());
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) ClassOrInterfaceType(com.github.javaparser.ast.type.ClassOrInterfaceType) IntersectionType(com.github.javaparser.ast.type.IntersectionType) Type(com.github.javaparser.ast.type.Type) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) CastExpr(com.github.javaparser.ast.expr.CastExpr) Test(org.junit.Test)

Example 7 with CastExpr

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));
}
Also used : Type(com.github.javaparser.ast.type.Type) Expression(com.github.javaparser.ast.expr.Expression) CastExpr(com.github.javaparser.ast.expr.CastExpr) Test(org.junit.Test)

Example 8 with CastExpr

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;
}
Also used : TemplateExpression(com.axellience.vuegwt.processors.component.template.parser.result.TemplateExpression) Expression(com.github.javaparser.ast.expr.Expression) BinaryExpr(com.github.javaparser.ast.expr.BinaryExpr) CastExpr(com.github.javaparser.ast.expr.CastExpr)

Aggregations

CastExpr (com.github.javaparser.ast.expr.CastExpr)8 Test (org.junit.Test)6 CompilationUnit (com.github.javaparser.ast.CompilationUnit)5 MethodDeclaration (com.github.javaparser.ast.body.MethodDeclaration)5 Expression (com.github.javaparser.ast.expr.Expression)2 LambdaExpr (com.github.javaparser.ast.expr.LambdaExpr)2 Type (com.github.javaparser.ast.type.Type)2 TemplateExpression (com.axellience.vuegwt.processors.component.template.parser.result.TemplateExpression)1 LocalVariableInfo (com.axellience.vuegwt.processors.component.template.parser.variable.LocalVariableInfo)1 VariableInfo (com.axellience.vuegwt.processors.component.template.parser.variable.VariableInfo)1 BinaryExpr (com.github.javaparser.ast.expr.BinaryExpr)1 ClassOrInterfaceType (com.github.javaparser.ast.type.ClassOrInterfaceType)1 IntersectionType (com.github.javaparser.ast.type.IntersectionType)1