Search in sources :

Example 11 with VariableDeclarationExpr

use of com.github.javaparser.ast.expr.VariableDeclarationExpr in project javaparser by javaparser.

the class JavaParserSymbolDeclaration method getType.

@Override
public ResolvedType getType() {
    if (wrappedNode instanceof Parameter) {
        Parameter parameter = (Parameter) wrappedNode;
        if (requireParentNode(wrappedNode) instanceof LambdaExpr) {
            int pos = getParamPos(parameter);
            ResolvedType lambdaType = JavaParserFacade.get(typeSolver).getType(requireParentNode(wrappedNode));
            // MethodDeclaration methodCalled = JavaParserFacade.get(typeSolver).solve()
            throw new UnsupportedOperationException(wrappedNode.getClass().getCanonicalName());
        } else {
            final ResolvedType rawType;
            if (parameter.getType() instanceof com.github.javaparser.ast.type.PrimitiveType) {
                rawType = ResolvedPrimitiveType.byName(((com.github.javaparser.ast.type.PrimitiveType) parameter.getType()).getType().name());
            } else {
                rawType = JavaParserFacade.get(typeSolver).convertToUsage(parameter.getType(), wrappedNode);
            }
            if (parameter.isVarArgs()) {
                return new ResolvedArrayType(rawType);
            }
            return rawType;
        }
    } else if (wrappedNode instanceof VariableDeclarator) {
        VariableDeclarator variableDeclarator = (VariableDeclarator) wrappedNode;
        if (requireParentNode(wrappedNode) instanceof VariableDeclarationExpr) {
            return JavaParserFacade.get(typeSolver).convert(variableDeclarator.getType(), JavaParserFactory.getContext(wrappedNode, typeSolver));
        } else if (requireParentNode(wrappedNode) instanceof FieldDeclaration) {
            return JavaParserFacade.get(typeSolver).convert(variableDeclarator.getType(), JavaParserFactory.getContext(wrappedNode, typeSolver));
        }
    }
    throw new UnsupportedOperationException(wrappedNode.getClass().getCanonicalName());
}
Also used : VariableDeclarationExpr(com.github.javaparser.ast.expr.VariableDeclarationExpr) LambdaExpr(com.github.javaparser.ast.expr.LambdaExpr) FieldDeclaration(com.github.javaparser.ast.body.FieldDeclaration) VariableDeclarator(com.github.javaparser.ast.body.VariableDeclarator) ResolvedArrayType(com.github.javaparser.resolution.types.ResolvedArrayType) Parameter(com.github.javaparser.ast.body.Parameter) ResolvedPrimitiveType(com.github.javaparser.resolution.types.ResolvedPrimitiveType) ResolvedType(com.github.javaparser.resolution.types.ResolvedType)

Example 12 with VariableDeclarationExpr

use of com.github.javaparser.ast.expr.VariableDeclarationExpr in project javaparser by javaparser.

the class NodeWithVariablesTest method getElementTypeFailsOnInvalidEmptyVariableList.

@Test(expected = AssertionError.class)
public void getElementTypeFailsOnInvalidEmptyVariableList() {
    VariableDeclarationExpr declaration = parseVariableDeclarationExpr("int a");
    declaration.getVariables().clear();
    declaration.getElementType();
}
Also used : VariableDeclarationExpr(com.github.javaparser.ast.expr.VariableDeclarationExpr) Test(org.junit.Test)

Example 13 with VariableDeclarationExpr

use of com.github.javaparser.ast.expr.VariableDeclarationExpr in project javaparser by javaparser.

the class NodeWithVariablesTest method getElementTypeWorksForArrayTypes.

@Test
public void getElementTypeWorksForArrayTypes() {
    VariableDeclarationExpr declaration = parseVariableDeclarationExpr("int a[],b[]");
    assertEquals(PrimitiveType.intType(), declaration.getElementType());
}
Also used : VariableDeclarationExpr(com.github.javaparser.ast.expr.VariableDeclarationExpr) Test(org.junit.Test)

Example 14 with VariableDeclarationExpr

use of com.github.javaparser.ast.expr.VariableDeclarationExpr in project javaparser by javaparser.

the class NodeWithVariablesTest method getElementTypeFailsOnDodgySetterUsage.

@Test(expected = AssertionError.class)
public void getElementTypeFailsOnDodgySetterUsage() {
    VariableDeclarationExpr declaration = parseVariableDeclarationExpr("int a,b");
    declaration.getVariable(1).setType(String.class);
    declaration.getElementType();
}
Also used : VariableDeclarationExpr(com.github.javaparser.ast.expr.VariableDeclarationExpr) Test(org.junit.Test)

Example 15 with VariableDeclarationExpr

use of com.github.javaparser.ast.expr.VariableDeclarationExpr in project javaparser by javaparser.

the class ArrayTypeTest method getVariableDeclarationWithArrays.

@Test
public void getVariableDeclarationWithArrays() {
    ExpressionStmt variableDeclarationStatement = parseStatement("@C int @A[] @B[] a @X[] @Y[];").asExpressionStmt();
    VariableDeclarationExpr variableDeclarationExpr = variableDeclarationStatement.getExpression().asVariableDeclarationExpr();
    ArrayType arrayType1 = variableDeclarationExpr.getVariable(0).getType().asArrayType();
    ArrayType arrayType2 = arrayType1.getComponentType().asArrayType();
    ArrayType arrayType3 = arrayType2.getComponentType().asArrayType();
    ArrayType arrayType4 = arrayType3.getComponentType().asArrayType();
    PrimitiveType elementType = arrayType4.getComponentType().asPrimitiveType();
    assertThat(arrayType1.getAnnotations()).containsExactly(new MarkerAnnotationExpr(parseName("A")));
    assertThat(arrayType2.getAnnotations()).containsExactly(new MarkerAnnotationExpr(parseName("B")));
    assertThat(arrayType3.getAnnotations()).containsExactly(new MarkerAnnotationExpr(parseName("X")));
    assertThat(arrayType4.getAnnotations()).containsExactly(new MarkerAnnotationExpr(parseName("Y")));
    assertThat(elementType.getType()).isEqualTo(PrimitiveType.Primitive.INT);
    assertThat(variableDeclarationExpr.getAnnotations()).containsExactly(new MarkerAnnotationExpr(parseName("C")));
    assertThat(arrayType1.getParentNode().get().getParentNode().get()).isSameAs(variableDeclarationExpr);
}
Also used : VariableDeclarationExpr(com.github.javaparser.ast.expr.VariableDeclarationExpr) MarkerAnnotationExpr(com.github.javaparser.ast.expr.MarkerAnnotationExpr) ExpressionStmt(com.github.javaparser.ast.stmt.ExpressionStmt) Test(org.junit.Test)

Aggregations

VariableDeclarationExpr (com.github.javaparser.ast.expr.VariableDeclarationExpr)15 Test (org.junit.Test)12 VariableDeclarator (com.github.javaparser.ast.body.VariableDeclarator)3 ExpressionStmt (com.github.javaparser.ast.stmt.ExpressionStmt)3 Parameter (com.github.javaparser.ast.body.Parameter)2 LambdaExpr (com.github.javaparser.ast.expr.LambdaExpr)2 JavaParser (com.github.javaparser.JavaParser)1 JavaParser.parseVariableDeclarationExpr (com.github.javaparser.JavaParser.parseVariableDeclarationExpr)1 ParserConfiguration (com.github.javaparser.ParserConfiguration)1 Node (com.github.javaparser.ast.Node)1 FieldDeclaration (com.github.javaparser.ast.body.FieldDeclaration)1 VariableDeclaratorId (com.github.javaparser.ast.body.VariableDeclaratorId)1 ArrayCreationExpr (com.github.javaparser.ast.expr.ArrayCreationExpr)1 MarkerAnnotationExpr (com.github.javaparser.ast.expr.MarkerAnnotationExpr)1 NullLiteralExpr (com.github.javaparser.ast.expr.NullLiteralExpr)1 ForStmt (com.github.javaparser.ast.stmt.ForStmt)1 ForeachStmt (com.github.javaparser.ast.stmt.ForeachStmt)1 Java5Validator (com.github.javaparser.ast.validator.Java5Validator)1 ResolvedArrayType (com.github.javaparser.resolution.types.ResolvedArrayType)1 ResolvedPrimitiveType (com.github.javaparser.resolution.types.ResolvedPrimitiveType)1