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