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 sorted() in streams #2716

Open
d-kay23 opened this issue Jun 4, 2020 · 2 comments
Open

Cannot resolve sorted() in streams #2716

d-kay23 opened this issue Jun 4, 2020 · 2 comments

Comments

@d-kay23
Copy link

@d-kay23 d-kay23 commented Jun 4, 2020

Hello everyone.
Here is one more issue (looks like a bug) in Symbol Solver (reproduced in the latest version 3.16.1). The sorted() method can't be resolved in a stream. I've prepared simple class example where the problem is appeared:

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

public class ComparatorTest {

    private static class A {
        int id;
        String name;

        public A(int id, String name) {
            this.id = id;
            this.name = name;
        }

        public int getId() {
            return id;
        }

        public String getName() {
            return name;
        }
    }

    public static void main(String args[]) {
        List<A> aList = new ArrayList<A>() {{
            add(new A (0, "aaa"));
            add(new A (1, "ccc"));
            add(new A (2, "bbb"));
        }};
        
        String listStr = aList
                .stream()
                .sorted(Comparator.comparing(a -> a.getName()))
                .map(a -> a.getId() + "," + a.getName())
                .collect(Collectors.joining(";\n"));

        System.out.println(listStr);
    }
}

When I try to resolve the methods of the class using the following code, Solver throws the exception:

import com.github.javaparser.StaticJavaParser;
import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.ast.expr.MethodCallExpr;
import com.github.javaparser.resolution.declarations.ResolvedMethodDeclaration;
import com.github.javaparser.symbolsolver.JavaSymbolSolver;
import com.github.javaparser.symbolsolver.model.resolution.TypeSolver;
import com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver;

import java.util.HashSet;
import java.util.Set;

public class Parser {

    public static void main(String[] args) {

        String s =
                "import java.util.ArrayList;\n" +
                        "import java.util.Comparator;\n" +
                        "import java.util.List;\n" +
                        "import java.util.stream.Collectors;\n" +
                        "\n" +
                        "public class ComparatorTest {\n" +
                        "\n" +
                        "    private static class A {\n" +
                        "        int id;\n" +
                        "        String name;\n" +
                        "\n" +
                        "        public A(int id, String name) {\n" +
                        "            this.id = id;\n" +
                        "            this.name = name;\n" +
                        "        }\n" +
                        "\n" +
                        "        public int getId() {\n" +
                        "            return id;\n" +
                        "        }\n" +
                        "\n" +
                        "        public String getName() {\n" +
                        "            return name;\n" +
                        "        }\n" +
                        "    }\n" +
                        "\n" +
                        "    public static void main(String args[]) {\n" +
                        "        List<A> aList = new ArrayList<A>() {{\n" +
                        "            add(new A (0, \"aaa\"));\n" +
                        "            add(new A (1, \"ccc\"));\n" +
                        "            add(new A (2, \"bbb\"));\n" +
                        "        }};\n" +
                        "\n" +
                        "        String listStr = aList\n" +
                        "                .stream()\n" +
                        "                .sorted(Comparator.comparing(a -> a.getName()))\n" +
                        "                .map(a -> a.getId() + \",\" + a.getName())\n" +
                        "                .collect(Collectors.joining(\";\\n\"));\n" +
                        "\n" +
                        "        System.out.println(listStr);\n" +
                        "    }\n" +
                        "}\n";
        TypeSolver typeSolver = new ReflectionTypeSolver(false);
        StaticJavaParser.getConfiguration().setSymbolResolver(new JavaSymbolSolver(typeSolver));
        CompilationUnit cu = StaticJavaParser.parse(s);
        Set<MethodCallExpr> methodCallExpr = new HashSet<>(cu.findAll(MethodCallExpr.class));
        for (MethodCallExpr expr : methodCallExpr) {
            ResolvedMethodDeclaration rd = expr.resolve();
            System.out.println("\t Solved : " + rd.getQualifiedSignature());
        }
    }
}

Exception:

	 Solved : java.util.Collection.stream()
	 Solved : java.util.ArrayList.add(E)
Exception in thread "main" java.lang.UnsupportedOperationException: TypeVariable {ReflectionTypeParameter{typeVariable=T}} is not a Reference Type
	at com.github.javaparser.resolution.types.ResolvedType.asReferenceType(ResolvedType.java:123)
	at com.github.javaparser.symbolsolver.javaparsermodel.contexts.AbstractJavaParserContext.findTypeDeclarations(AbstractJavaParserContext.java:199)
	at com.github.javaparser.symbolsolver.javaparsermodel.contexts.MethodCallExprContext.solveMethod(MethodCallExprContext.java:155)
	at com.github.javaparser.symbolsolver.javaparsermodel.JavaParserFacade.solve(JavaParserFacade.java:270)
	at com.github.javaparser.symbolsolver.javaparsermodel.JavaParserFacade.solve(JavaParserFacade.java:132)
	at com.github.javaparser.symbolsolver.JavaSymbolSolver.resolveDeclaration(JavaSymbolSolver.java:161)
	at com.github.javaparser.ast.expr.MethodCallExpr.resolve(MethodCallExpr.java:313)
	at Parser.main(Parser.java:63)

The exception is caused by resolving method .sorted(Comparator.comparing(a -> a.getName())), because the example works without the line.
Is this a bug or I miss something?
Thank you in advice and for previous fixes.

@MysterAitch
Copy link
Member

@MysterAitch MysterAitch commented Jun 4, 2020

I'll mark it as a bug because it sounds like it is, but I'm not sure of the cause or if it is another manifestation of an existing report.

@MornSunShine
Copy link

@MornSunShine MornSunShine commented Jun 28, 2020

maybe, it failed to solve the call expression with calls as arguments. I run into similar problems

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
3 participants
You can’t perform that action at this time.