• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 package com.github.javaparser.symbolsolver;
2 
3 import com.github.javaparser.JavaParser;
4 import com.github.javaparser.ParseResult;
5 import com.github.javaparser.ParseStart;
6 import com.github.javaparser.ParserConfiguration;
7 import com.github.javaparser.ast.CompilationUnit;
8 import com.github.javaparser.ast.expr.MethodCallExpr;
9 import com.github.javaparser.symbolsolver.model.resolution.TypeSolver;
10 import com.github.javaparser.symbolsolver.resolution.typesolvers.CombinedTypeSolver;
11 import com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver;
12 import org.junit.jupiter.api.Assertions;
13 import org.junit.jupiter.api.BeforeEach;
14 import org.junit.jupiter.api.Test;
15 
16 import java.util.List;
17 import java.util.stream.Collectors;
18 
19 import static com.github.javaparser.Providers.provider;
20 import static org.junit.jupiter.api.Assumptions.assumeFalse;
21 
22 class Issue2035Test {
23 
24     private JavaParser javaParser;
25 
26     @BeforeEach
setUp()27     void setUp() {
28         TypeSolver typeSolver = new CombinedTypeSolver(new ReflectionTypeSolver());
29         ParserConfiguration configuration = new ParserConfiguration().setSymbolResolver(new JavaSymbolSolver(typeSolver));
30 
31         javaParser = new JavaParser(configuration);
32     }
33 
34     @Test
test()35     void test() {
36         String x = "" +
37                 "class X {\n" +
38                 "    \n" +
39                 "    private void a(int a){ }\n" +
40                 "    private void b(Integer a){ }\n" +
41                 "    \n" +
42                 "    private void c(){\n" +
43                 "        int x=0;\n" +
44                 "        Integer y=0;\n" +
45                 "        \n" +
46                 "        a(x);\n" +
47                 "        a(y);\n" +
48                 "        \n" +
49                 "        b(x);\n" +
50                 "        b(y);\n" +
51                 "        \n" +
52                 "    }\n" +
53                 "}" +
54                 "";
55 
56         ParseResult<CompilationUnit> parseResult = javaParser.parse(
57                 ParseStart.COMPILATION_UNIT,
58                 provider(x)
59         );
60 
61         parseResult.getResult().ifPresent(compilationUnit -> {
62             final List<MethodCallExpr> matches = compilationUnit
63                     .findAll(MethodCallExpr.class);
64 
65             assumeFalse(matches.isEmpty(), "Cannot attempt resolving types if no matches.");
66             matches.forEach(methodCallExpr -> {
67                 try {
68                     methodCallExpr.resolve().getReturnType();
69                     methodCallExpr.calculateResolvedType(); //
70                 } catch (UnsupportedOperationException e) {
71                     Assertions.fail("Resolution failed.", e);
72                 }
73             });
74         });
75 
76     }
77 
78 
79     @Test
test_int()80     void test_int() {
81         String x_int = "" +
82                 "import java.util.*;\n" +
83                 "\n" +
84                 "class X {\n" +
85                 "    \n" +
86                 "    private void a(){\n" +
87                 "        ArrayList<String> abc = new ArrayList<>();\n" +
88                 "        int x = 0; \n" +
89                 "        abc.get(x);\n" +
90                 "    }\n" +
91                 "}" +
92                 "";
93 
94         ParseResult<CompilationUnit> parseResult = javaParser.parse(
95                 ParseStart.COMPILATION_UNIT,
96                 provider(x_int)
97         );
98 
99         parseResult.getResult().ifPresent(compilationUnit -> {
100             final List<MethodCallExpr> matches = compilationUnit
101                     .findAll(MethodCallExpr.class)
102                     .stream()
103                     .filter(methodCallExpr -> methodCallExpr.getNameAsString().equals("get"))
104                     .collect(Collectors.toList());
105 
106             assumeFalse(matches.isEmpty(), "Cannot attempt resolving types if no matches.");
107             matches.forEach(methodCallExpr -> {
108                 try {
109                     methodCallExpr.resolve().getReturnType();
110                     methodCallExpr.calculateResolvedType();
111                 } catch (UnsupportedOperationException e) {
112                     Assertions.fail("Resolution failed.", e);
113                 }
114             });
115         });
116 
117     }
118 
119     @Test
test_Integer()120     void test_Integer() {
121         String x_Integer = "" +
122                 "import java.util.*;\n" +
123                 "\n" +
124                 "class X {\n" +
125                 "    \n" +
126                 "    private void a(){\n" +
127                 "        ArrayList<String> abc = new ArrayList<>();\n" +
128                 "        Integer x = 0; \n" +
129                 "        abc.get(x);\n" +
130                 "    }\n" +
131                 "}" +
132                 "";
133 
134         ParseResult<CompilationUnit> parseResult = javaParser.parse(
135                 ParseStart.COMPILATION_UNIT,
136                 provider(x_Integer)
137         );
138 
139         parseResult.getResult().ifPresent(compilationUnit -> {
140             final List<MethodCallExpr> matches = compilationUnit
141                     .findAll(MethodCallExpr.class)
142                     .stream()
143                     .filter(methodCallExpr -> methodCallExpr.getNameAsString().equals("get"))
144                     .collect(Collectors.toList());
145 
146             assumeFalse(matches.isEmpty(), "Cannot attempt resolving types if no matches.");
147             matches.forEach(methodCallExpr -> {
148                 try {
149                     methodCallExpr.resolve().getReturnType();
150                     methodCallExpr.calculateResolvedType();
151                 } catch (UnsupportedOperationException e) {
152                     Assertions.fail("Resolution failed.", e);
153                 }
154             });
155         });
156     }
157 
158 }
159