|
21 | 21 |
|
22 | 22 | package com.github.javaparser.symbolsolver.resolution;
|
23 | 23 |
|
| 24 | +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; |
24 | 25 | import static org.junit.jupiter.api.Assertions.assertEquals;
|
25 | 26 |
|
26 | 27 | import com.github.javaparser.StaticJavaParser;
|
|
35 | 36 | import com.github.javaparser.symbolsolver.JavaSymbolSolver;
|
36 | 37 | import com.github.javaparser.symbolsolver.javaparsermodel.JavaParserFacade;
|
37 | 38 | import com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver;
|
| 39 | +import org.junit.jupiter.api.Disabled; |
38 | 40 | import org.junit.jupiter.api.Test;
|
39 | 41 |
|
40 | 42 | class LambdaResolutionTest extends AbstractResolutionTest {
|
@@ -211,4 +213,144 @@ void lambdaAsVararg() {
|
211 | 213 | "java.util.function.Consumer<java.lang.String>",
|
212 | 214 | lambda.calculateResolvedType().describe());
|
213 | 215 | }
|
| 216 | + |
| 217 | + @Test |
| 218 | + void lambdaOverloadsWithDifferentParameterCounts1() { |
| 219 | + String source = "import java.util.function.Consumer;\n" + "class Test {\n" |
| 220 | + + " void foo(Consumer<String> consumer) {}\n" |
| 221 | + + " void foo(Runnable r) {}\n" |
| 222 | + + " void test() {\n" |
| 223 | + + " foo(input -> {});\n" |
| 224 | + + " }\n" |
| 225 | + + "}"; |
| 226 | + |
| 227 | + StaticJavaParser.getParserConfiguration().setSymbolResolver(new JavaSymbolSolver(new ReflectionTypeSolver())); |
| 228 | + final CompilationUnit cu = StaticJavaParser.parse(source); |
| 229 | + final MethodCallExpr call = cu.findFirst(MethodCallExpr.class).get(); |
| 230 | + assertEquals( |
| 231 | + "Test.foo(java.util.function.Consumer<java.lang.String>)", |
| 232 | + call.resolve().getQualifiedSignature()); |
| 233 | + assertEquals("void", call.calculateResolvedType().describe()); |
| 234 | + } |
| 235 | + |
| 236 | + @Test |
| 237 | + void lambdaOverloadsWithDifferentParameterCounts2() { |
| 238 | + String source = "import java.util.function.Consumer;\n" + "class Test {\n" |
| 239 | + + " void foo(Consumer<java.lang.String> consumer) {}\n" |
| 240 | + + " void foo(Runnable r) {}\n" |
| 241 | + + " void test() {\n" |
| 242 | + + " foo(() -> {});\n" |
| 243 | + + " }\n" |
| 244 | + + "}"; |
| 245 | + |
| 246 | + StaticJavaParser.getParserConfiguration().setSymbolResolver(new JavaSymbolSolver(new ReflectionTypeSolver())); |
| 247 | + final CompilationUnit cu = StaticJavaParser.parse(source); |
| 248 | + final MethodCallExpr call = cu.findFirst(MethodCallExpr.class).get(); |
| 249 | + assertEquals("Test.foo(java.lang.Runnable)", call.resolve().getQualifiedSignature()); |
| 250 | + assertEquals("void", call.calculateResolvedType().describe()); |
| 251 | + } |
| 252 | + |
| 253 | + @Test |
| 254 | + void lambdaOverloadsWithDifferentReturnTypes1() { |
| 255 | + String source = "import java.util.function.Consumer;\n" + "import java.util.function.Function;\n" |
| 256 | + + "class Test {\n" |
| 257 | + + " void foo(Consumer<String> consumer) {}\n" |
| 258 | + + " void foo(Function<Integer, String> func) {}\n" |
| 259 | + + " void test() {\n" |
| 260 | + + " foo(input -> {});\n" |
| 261 | + + " }\n" |
| 262 | + + "}"; |
| 263 | + |
| 264 | + StaticJavaParser.getParserConfiguration().setSymbolResolver(new JavaSymbolSolver(new ReflectionTypeSolver())); |
| 265 | + final CompilationUnit cu = StaticJavaParser.parse(source); |
| 266 | + final MethodCallExpr call = cu.findFirst(MethodCallExpr.class).get(); |
| 267 | + assertEquals( |
| 268 | + "Test.foo(java.util.function.Consumer<java.lang.String>)", |
| 269 | + call.resolve().getQualifiedSignature()); |
| 270 | + assertEquals("void", call.calculateResolvedType().describe()); |
| 271 | + } |
| 272 | + |
| 273 | + @Test |
| 274 | + void lambdaOverloadsWithDifferentReturnTypes2() { |
| 275 | + String source = "import java.util.function.Consumer;\n" + "import java.util.function.Function;\n" |
| 276 | + + "class Test {\n" |
| 277 | + + " void foo(Consumer<String> consumer) {}\n" |
| 278 | + + " void foo(Function<Integer, String> func) {}\n" |
| 279 | + + " void test() {\n" |
| 280 | + + " foo(input -> { return \"\"; });\n" |
| 281 | + + " }\n" |
| 282 | + + "}"; |
| 283 | + |
| 284 | + StaticJavaParser.getParserConfiguration().setSymbolResolver(new JavaSymbolSolver(new ReflectionTypeSolver())); |
| 285 | + final CompilationUnit cu = StaticJavaParser.parse(source); |
| 286 | + final MethodCallExpr call = cu.findFirst(MethodCallExpr.class).get(); |
| 287 | + assertEquals( |
| 288 | + "Test.foo(java.util.function.Function<java.lang.Integer, java.lang.String>)", |
| 289 | + call.resolve().getQualifiedSignature()); |
| 290 | + assertEquals("void", call.calculateResolvedType().describe()); |
| 291 | + } |
| 292 | + |
| 293 | + @Test |
| 294 | + void lambdaUsedAsPolymorphicArgument() { |
| 295 | + String source = "import java.util.function.Consumer;\n" + "import java.util.HashMap;" |
| 296 | + + "class Test {\n" |
| 297 | + + " void test() {\n" |
| 298 | + + " HashMap<String, Consumer> map = new HashMap<>();" |
| 299 | + + " map.put(\"\", input -> {});\n" |
| 300 | + + " }\n" |
| 301 | + + "}"; |
| 302 | + |
| 303 | + StaticJavaParser.getParserConfiguration().setSymbolResolver(new JavaSymbolSolver(new ReflectionTypeSolver())); |
| 304 | + final CompilationUnit cu = StaticJavaParser.parse(source); |
| 305 | + final MethodCallExpr call = cu.findFirst(MethodCallExpr.class).get(); |
| 306 | + assertDoesNotThrow(() -> call.resolve().getQualifiedSignature()); |
| 307 | + assertDoesNotThrow(() -> call.calculateResolvedType().describe()); |
| 308 | + assertEquals("java.util.HashMap.put(K, V)", call.resolve().getQualifiedSignature()); |
| 309 | + } |
| 310 | + |
| 311 | + @Test |
| 312 | + void lambdaUsedAsOverloadedArrayAlternativeArgument() { |
| 313 | + String source = "import java.util.function.Consumer;\n" + "import java.util.function.Function;\n" |
| 314 | + + "class Foo<S extends Consumer, T> {\n" |
| 315 | + + " void foo(Object[] ts) {}\n" |
| 316 | + + " void foo(T t) {}\n" |
| 317 | + + "}\n" |
| 318 | + + "class Test {\n" |
| 319 | + + " void test() {\n" |
| 320 | + + " Foo<Consumer<Integer>, Function<Integer, Integer>> foo = new Foo<>();\n" |
| 321 | + + " foo.foo(value -> { return 2; });\n" |
| 322 | + + " }\n" |
| 323 | + + "}"; |
| 324 | + |
| 325 | + StaticJavaParser.getParserConfiguration().setSymbolResolver(new JavaSymbolSolver(new ReflectionTypeSolver())); |
| 326 | + final CompilationUnit cu = StaticJavaParser.parse(source); |
| 327 | + final MethodCallExpr call = cu.findFirst(MethodCallExpr.class).get(); |
| 328 | + assertDoesNotThrow(() -> call.resolve().getQualifiedSignature()); |
| 329 | + assertDoesNotThrow(() -> call.calculateResolvedType().describe()); |
| 330 | + assertEquals("Foo.foo(T)", call.resolve().getQualifiedSignature()); |
| 331 | + } |
| 332 | + |
| 333 | + @Disabled("Disambiguation for lambdas used as polymorphic is not supported yet.") |
| 334 | + @Test |
| 335 | + void lambdaUsedAsOverloadedPolymorphicArgument1() { |
| 336 | + |
| 337 | + String source = "import java.util.function.Consumer;\n" + "import java.util.function.Function;\n" |
| 338 | + + "class Foo<S extends Consumer, T> {\n" |
| 339 | + + " void foo(T t) {}\n" |
| 340 | + + " void foo(S s) {}\n" |
| 341 | + + "}\n" |
| 342 | + + "class Test {\n" |
| 343 | + + " void test() {\n" |
| 344 | + + " Foo<Consumer<Integer>, Function<Integer, Integer>> foo = new Foo<>();\n" |
| 345 | + + " foo.foo(value -> { return 2; });\n" |
| 346 | + + " }\n" |
| 347 | + + "}"; |
| 348 | + |
| 349 | + StaticJavaParser.getParserConfiguration().setSymbolResolver(new JavaSymbolSolver(new ReflectionTypeSolver())); |
| 350 | + final CompilationUnit cu = StaticJavaParser.parse(source); |
| 351 | + final MethodCallExpr call = cu.findFirst(MethodCallExpr.class).get(); |
| 352 | + assertDoesNotThrow(() -> call.resolve().getQualifiedSignature()); |
| 353 | + assertDoesNotThrow(() -> call.calculateResolvedType().describe()); |
| 354 | + assertEquals("Foo.foo(java.util.function.Function)", call.resolve().getQualifiedSignature()); |
| 355 | + } |
214 | 356 | }
|
0 commit comments