Skip to content

Commit

Permalink
Merge pull request #2874 from jlerbsc/master
Browse files Browse the repository at this point in the history
Fix issue Resolution error for non-generic constructor if generic con…
  • Loading branch information
jlerbsc committed Nov 3, 2020
2 parents 27dfddc + 30742f9 commit dcaa3e1
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@

package com.github.javaparser.symbolsolver.resolution;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.github.javaparser.resolution.MethodAmbiguityException;
import com.github.javaparser.resolution.declarations.ResolvedConstructorDeclaration;
import com.github.javaparser.resolution.declarations.ResolvedTypeParameterDeclaration;
Expand All @@ -29,12 +35,6 @@
import com.github.javaparser.symbolsolver.model.resolution.SymbolReference;
import com.github.javaparser.symbolsolver.model.resolution.TypeSolver;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
* @author Fred Lefévère-Laoide
*/
Expand Down Expand Up @@ -187,17 +187,18 @@ public static SymbolReference<ResolvedConstructorDeclaration> findMostApplicable
// we expect the methods to be ordered such that inherited methods are later in the list
}
}
}
if (possibleAmbiguity) {
// pick the first exact match if it exists
if (!MethodResolutionLogic.isExactMatch(winningCandidate, argumentsTypes)) {
if (MethodResolutionLogic.isExactMatch(other, argumentsTypes)) {
winningCandidate = other;
} else {
throw new MethodAmbiguityException("Ambiguous constructor call: cannot find a most applicable constructor: " + winningCandidate + ", " + other);
if (possibleAmbiguity) {
// pick the first exact match if it exists
if (!MethodResolutionLogic.isExactMatch(winningCandidate, argumentsTypes)) {
if (MethodResolutionLogic.isExactMatch(other, argumentsTypes)) {
winningCandidate = other;
} else {
throw new MethodAmbiguityException("Ambiguous constructor call: cannot find a most applicable constructor: " + winningCandidate + ", " + other);
}
}
}
}

return SymbolReference.solved(winningCandidate);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.github.javaparser.symbolsolver;

import static org.junit.jupiter.api.Assertions.assertEquals;

import java.nio.file.Path;
import java.util.List;

import org.junit.jupiter.api.Test;

import com.github.javaparser.ParserConfiguration;
import com.github.javaparser.StaticJavaParser;
import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.ast.expr.ObjectCreationExpr;
import com.github.javaparser.symbolsolver.model.resolution.TypeSolver;
import com.github.javaparser.symbolsolver.resolution.typesolvers.CombinedTypeSolver;
import com.github.javaparser.symbolsolver.resolution.typesolvers.JavaParserTypeSolver;
import com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver;

public class Issue2236Test extends AbstractSymbolResolutionTest {

@Test
public void test() {
final Path testRoot = adaptPath("src/test/resources/issue2236");
TypeSolver reflectionTypeSolver = new ReflectionTypeSolver();
JavaParserTypeSolver javaParserTypeSolver = new JavaParserTypeSolver(testRoot);
CombinedTypeSolver combinedTypeSolver = new CombinedTypeSolver(reflectionTypeSolver, javaParserTypeSolver);
ParserConfiguration configuration = new ParserConfiguration()
.setSymbolResolver(new JavaSymbolSolver(combinedTypeSolver));
StaticJavaParser.setConfiguration(configuration);

String src = "class X {public void f(){ " +
"new A<Object>(new Boolean(true)); }}";

CompilationUnit cu = StaticJavaParser.parse(src);

List<ObjectCreationExpr> oces = cu.findAll(ObjectCreationExpr .class);
assertEquals("A.A(java.lang.Boolean)", oces.get(0).resolve().getQualifiedSignature());

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
public class A<T>
{
A(boolean b) {
}

A(Boolean b) {
}

A(T t) {
}
}

0 comments on commit dcaa3e1

Please sign in to comment.