• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 package com.github.javaparser.symbolsolver;
2 
3 import com.github.javaparser.*;
4 import com.github.javaparser.ast.CompilationUnit;
5 import com.github.javaparser.ast.body.VariableDeclarator;
6 import com.github.javaparser.ast.expr.VariableDeclarationExpr;
7 import com.github.javaparser.resolution.declarations.ResolvedValueDeclaration;
8 import com.github.javaparser.resolution.types.ResolvedType;
9 import com.github.javaparser.symbolsolver.model.resolution.TypeSolver;
10 import com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver;
11 import org.junit.jupiter.api.BeforeEach;
12 import org.junit.jupiter.api.Test;
13 
14 import static org.junit.jupiter.api.Assertions.assertEquals;
15 import static org.junit.jupiter.api.Assertions.assertTrue;
16 
17 class Issue1668Test {
18 
19     private JavaParser javaParser;
20 
21     @BeforeEach
setUp()22     void setUp() {
23         TypeSolver typeSolver = new ReflectionTypeSolver();
24         ParserConfiguration config = new ParserConfiguration();
25         config.setSymbolResolver(new JavaSymbolSolver(typeSolver));
26         javaParser = new JavaParser(config);
27     }
28 
29     @Test
testResolveArrayDeclaration()30     void testResolveArrayDeclaration() {
31         String code = String.join(System.lineSeparator(),
32                 "public class X {",
33                 "   public static void main(String[] args) {",
34                 "       String s = \"a,b,c,d,e\";",
35                 "       String[] stringArray = s.split(',');",
36                 "   }",
37                 "}"
38         );
39 
40         ParseResult<CompilationUnit> parseResult = javaParser.parse(ParseStart.COMPILATION_UNIT, Providers.provider(code));
41         assertTrue(parseResult.isSuccessful());
42         assertTrue(parseResult.getResult().isPresent());
43 
44         CompilationUnit compilationUnit = parseResult.getResult().get();
45         VariableDeclarator variableDeclarator = compilationUnit.findFirst(VariableDeclarator.class, v ->
46                 v.getNameAsString().equals("stringArray")).get();
47         VariableDeclarationExpr variableDeclarationExpr = (VariableDeclarationExpr) variableDeclarator.getParentNode().get();
48         ResolvedType resolvedType = variableDeclarationExpr.calculateResolvedType();
49         assertEquals("java.lang.String[]", resolvedType.describe());
50         ResolvedValueDeclaration resolve = variableDeclarator.resolve();
51         assertEquals("java.lang.String[]", resolve.getType().describe());
52     }
53 }
54