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

There is a declared method but, MethodAmbiguityException occurred #3083

Closed
JeewoongKim opened this issue Feb 16, 2021 · 3 comments · Fixed by #3084
Closed

There is a declared method but, MethodAmbiguityException occurred #3083

JeewoongKim opened this issue Feb 16, 2021 · 3 comments · Fixed by #3084
Milestone

Comments

@JeewoongKim
Copy link

JeewoongKim commented Feb 16, 2021

Hi, this is my first time using JSS 3.18.
This may a question rather than an issue.

I faced a problem when resolving the arguments in a method call expression.
It seems that the CollectionUtils.get(expected, 0) at line 6 is a cause.
The related JARS are added through lines 22-25 including a classpath (i.e., CollectionUtils' parent dir) that is declared the method

Sorry for not being able to explainable the problem as descriptive

Here are my codes and a stack trace:

Method call

1 public void getFromMap() {  
2      final Map<String, String> expected = new HashMap<>();
3      expected.put("zeroKey", "zero");
4      expected.put("oneKey", "one");
5      final Map<String, String> found = new HashMap<>();
6      Map.Entry<String, String> entry = CollectionUtils.get(expected, 0);
7 }

Method declaration (CollectionUtils.java)

69  public static <K,V> Map.Entry<K, V> get(final Map<K,V> map, final int index) {  ...  }

JSS part

20  TypeSolver typeSolver = new CombinedTypeSolver(
21                 new ReflectionTypeSolver(),
22                 JarTypeSolver.getJarTypeSolver("NECESSARY-JARS"),
...                   ... );
26   JavaSymbolSolver symbolSolver = new JavaSymbolSolver(typeSolver);
27   StaticJavaParser.getConfiguration().setSymbolResolver(symbolSolver);
28   CompilationUnit cu = StaticJavaParser.parse(new File(FILE_PATH));

Stack trace

com.github.javaparser.resolution.MethodAmbiguityException: Ambiguous method call: cannot find a most applicable method: 
JavassistMethodDeclaration{ctMethod=javassist.CtMethod@fc1cfe2e[public static get (Ljava/lang/Object;I)Ljava/lang/Object;]}, 
JavassistMethodDeclaration{ctMethod=javassist.CtMethod@46eaf0f9[public static get (Ljava/util/Map;I)Ljava/util/Map$Entry;]}

    at com.github.javaparser.symbolsolver.resolution.MethodResolutionLogic.findMostApplicable (MethodResolutionLogic.java:587)
    at com.github.javaparser.symbolsolver.resolution.MethodResolutionLogic.findMostApplicable (MethodResolutionLogic.java:501)
    at com.github.javaparser.symbolsolver.javassistmodel.JavassistClassDeclaration.solveMethod (JavassistClassDeclaration.java:259)
    at com.github.javaparser.symbolsolver.resolution.MethodResolutionLogic.solveMethodInType (MethodResolutionLogic.java:787)
    at com.github.javaparser.symbolsolver.javaparsermodel.contexts.MethodCallExprContext.solveMethod (MethodCallExprContext.java:177)
    at com.github.javaparser.symbolsolver.javaparsermodel.JavaParserFacade.solve (JavaParserFacade.java:321)
    at com.github.javaparser.symbolsolver.javaparsermodel.JavaParserFacade.solve (JavaParserFacade.java:183)
    at com.github.javaparser.symbolsolver.JavaSymbolSolver.resolveDeclaration (JavaSymbolSolver.java:161)
    at com.github.javaparser.ast.expr.MethodCallExpr.resolve (MethodCallExpr.java:313)
    at org.myMain.lambda$GetMethodCallExprList$3 (myMain.java:183)

Do I have to add the java.util.* to within lines 22-25?
Could you give me any advice or materials to solve the problem?
I read a tutorial (JavaParser-visited)

@jlerbsc
Copy link
Collaborator

jlerbsc commented Feb 16, 2021

Hi @JeewoongKim

Do I have to add the java.util.* to within lines 22-25?
No you don't have to declare this package because ReflectionTypeSolver can scan it for you.
Unfortunately this exception is probably caused by a bug! We will have to isolate the problem first with a simpler test case before we can help you solve this problem. It would have been faster if you had sent us a simplified test case :-)

@jlerbsc
Copy link
Collaborator

jlerbsc commented Feb 16, 2021

We can reproduce it with this unit test

@Test
void test() {
    String code = 
            "class A {\n" + 
            "        public void getFromMap() {\n" + 
            "            final java.util.Map<String, String> expected = new java.util.HashMap<>();\n" + 
            "            java.util.Map.Entry<String, String> entry = get(expected, 0);\n" + 
            "        }\n" + 
            "        <V, K> java.util.Map.Entry<K,V> get(java.util.Map<K,V> map, int index) {\n" + 
            "            return null;\n" + 
            "        }\n" + 
            "        Object get(Object map, int index) {\n" + 
            "            return null;\n" + 
            "        }\n" + 
            "    }";
    ParserConfiguration config = new ParserConfiguration();
    StaticJavaParser.getConfiguration().setSymbolResolver(new JavaSymbolSolver(new ReflectionTypeSolver()));
    CompilationUnit cu = StaticJavaParser.parse(code);
    MethodCallExpr mce = cu.findFirst(MethodCallExpr.class).get();
    System.out.println(mce.resolve().getQualifiedName());
}

@JeewoongKim
Copy link
Author

@jlerbsc Thanks for fixing the issue.

@MysterAitch MysterAitch added this to the next release milestone Feb 16, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants