Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot resolve MethodCallExpr with generic type argument. #2960

Open
qzchenwl opened this issue Nov 29, 2020 · 1 comment · May be fixed by #2965
Open

Cannot resolve MethodCallExpr with generic type argument. #2960

qzchenwl opened this issue Nov 29, 2020 · 1 comment · May be fixed by #2965

Comments

@qzchenwl
Copy link
Contributor

qzchenwl commented Nov 29, 2020

Parsing the following code:

package foo;
import foo.A;
import foo.B;
public class Test {
    public void foo() {
        A a = new A();
        B b = new B();
        a.a(b.b());
    }
}

while A and B is defined:

// -------- File: foo/A.java -----------
package foo;
public class A {
    public void a(Long x) {
    }
}
// -------- File: foo/B.java -----------
package foo;
public class B {
    public <T> T b() {
        return null;
    }
}

when solve MethodCallExpr a.a(b.b()), throws exception:

Unsolved symbol : We are unable to find the method declaration corresponding to a.a(b.b())
UnsolvedSymbolException{context='null', name='We are unable to find the method declaration corresponding to a.a(b.b())', cause='null'}
	at com.github.javaparser.symbolsolver.JavaSymbolSolver.resolveDeclaration(JavaSymbolSolver.java:167)
	at com.github.javaparser.ast.expr.MethodCallExpr.resolve(MethodCallExpr.java:313)
...

Code to reproduce:

    @Test
    public void testIt() {
        ParserConfiguration config = new ParserConfiguration();
        CombinedTypeSolver typeResolver = new CombinedTypeSolver(new ReflectionTypeSolver(false));
        typeResolver.add(new JavaParserTypeSolver("src/test/resources/issue2960"));
        config.setSymbolResolver(new JavaSymbolSolver(typeResolver));
        StaticJavaParser.setConfiguration(config);

        String code = "package foo;\n"
                + "import foo.A;\n"
                + "import foo.B;\n"
                + "public class Test {\n"
                + "    public void foo() {\n"
                + "        A a = new A();\n"
                + "        B b = new B();\n"
                + "        a.a(b.b());\n"
                + "    }\n"
                + "}";

        CompilationUnit cu = StaticJavaParser.parse(code);
        for (MethodCallExpr methodCallExpr : cu.findAll(MethodCallExpr.class)) {
            if (methodCallExpr.getNameAsString().equals("a")) {
                methodCallExpr.resolve();
            }
        }
    }
@qzchenwl
Copy link
Contributor Author

qzchenwl commented Nov 29, 2020

Maybe ReferenceTypeImpl(Long).isAssignableBy(TypeVariable(T)) should return true.

Reference issue: javaparser/javasymbolsolver#247

@qzchenwl qzchenwl linked a pull request Nov 30, 2020 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants