8000 Method call resolution fails with `UnsolvedSymbolException` if passed parameter is a list of two or more elements · Issue #3183 · javaparser/javaparser · GitHub
[go: up one dir, main page]

Skip to content
Method call resolution fails with UnsolvedSymbolException if passed parameter is a list of two or more elements #3183
@mfarsikov

Description

@mfarsikov

I'm using Javaparser 3.18.0.
The file to parse src/test/resources/MyClass.java:

import java.io.IOException;
import java.nio.file.Files;
import java.util.Arrays;

class MyClass {
    public void writeFile() throws IOException {
        Files.write(null, Arrays.asList("", ""));
    }
}

The parser (in Kotlin):

package my.package

import com.github.javaparser.ParserConfiguration
import com.github.javaparser.ast.body.ClassOrInterfaceDeclaration
import com.github.javaparser.ast.expr.MethodCallExpr
import com.github.javaparser.symbolsolver.JavaSymbolSolver
import com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver
import com.github.javaparser.utils.SourceRoot
import java.nio.file.Path

fun main() {
    val symbolSolver = JavaSymbolSolver(ReflectionTypeSolver())

    val sourceRoot = SourceRoot(
        Path.of("src/test/resources/"),
        ParserConfiguration().setSymbolResolver(symbolSolver)
    )

    val compilationUnit = sourceRoot.tryToParse()
        .single()
        .result.get()

    val methodCall = (compilationUnit.childNodes[3] as ClassOrInterfaceDeclaration)
        .methods.single().body.get()
        .childNodes.single()
        .childNodes.single() as MethodCallExpr

    methodCall.resolve()
}

In the attempt to resolve the method call it fails:

Exception in thread "main" UnsolvedSymbolException{context='null', name='We are unable to find the method declaration corresponding to Files.write(null, Arrays.asList("", ""))', cause='null'}
	at com.github.javaparser.symbolsolver.JavaSymbolSolver.resolveDeclaration(JavaSymbolSolver.java:167)
	at com.github.javaparser.ast.expr.MethodCallExpr.resolve(MethodCallExpr.java:313)
	at my.package.MainKt.main(Main.kt:28)
	at my.package.MainKt.main(Main.kt)

But if in parsed file list consists of a single element:
Files.write(null, Arrays.asList("", "")); -> Files.write(null, Arrays.asList(""));
it is solved correctly.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0