Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit b22db5a

Browse filesBrowse files
authored
Merge pull request #4727 from johannescoetzee/johannes/fix-4703
Fix constructor resolution issue 4703
2 parents 322e7fa + a0cab53 commit b22db5a
Copy full SHA for b22db5a

File tree

Expand file treeCollapse file tree

3 files changed

+57
-38
lines changed
Filter options
Expand file treeCollapse file tree

3 files changed

+57
-38
lines changed

‎javaparser-core/src/main/java/com/github/javaparser/resolution/logic/ConstructorResolutionLogic.java

Copy file name to clipboardExpand all lines: javaparser-core/src/main/java/com/github/javaparser/resolution/logic/ConstructorResolutionLogic.java
+2-36Lines changed: 2 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -184,9 +184,9 @@ public static SymbolReference<ResolvedConstructorDeclaration> findMostApplicable
184184
boolean possibleAmbiguity = false;
185185
for (int i = 1; i < applicableConstructors.size(); i++) {
186186
other = applicableConstructors.get(i);
187-
if (isMoreSpecific(winningCandidate, other, typeSolver)) {
187+
if (MethodResolutionLogic.isMoreSpecific(winningCandidate, other, argumentsTypes)) {
188188
possibleAmbiguity = false;
189-
} else if (isMoreSpecific(other, winningCandidate, typeSolver)) {
189+
} else if (MethodResolutionLogic.isMoreSpecific(other, winningCandidate, argumentsTypes)) {
190190
possibleAmbiguity = false;
191191
winningCandidate = other;
192192
} else {
@@ -214,38 +214,4 @@ public static SymbolReference<ResolvedConstructorDeclaration> findMostApplicable
214214
}
215215
return SymbolReference.solved(winningCandidate);
216216
}
217-
218-
private static boolean isMoreSpecific(
219-
ResolvedConstructorDeclaration constructorA,
220-
ResolvedConstructorDeclaration constructorB,
221-
TypeSolver typeSolver) {
222-
boolean oneMoreSpecificFound = false;
223-
if (constructorA.getNumberOfParams() < constructorB.getNumberOfParams()) {
224-
return true;
225-
}
226-
if (constructorA.getNumberOfParams() > constructorB.getNumberOfParams()) {
227-
return false;
228-
}
229-
for (int i = 0; i < constructorA.getNumberOfParams(); i++) {
230-
ResolvedType tdA = constructorA.getParam(i).getType();
231-
ResolvedType tdB = constructorB.getParam(i).getType();
232-
// B is more specific
233-
if (tdB.isAssignableBy(tdA) && !tdA.isAssignableBy(tdB)) {
234-
oneMoreSpecificFound = true;
235-
}
236-
// A is more specific
237-
if (tdA.isAssignableBy(tdB) && !tdB.isAssignableBy(tdA)) {
238-
return false;
239-
}
240-
// if it matches a variadic and a not variadic I pick the not variadic
241-
if (!tdA.isArray() && tdB.isArray()) {
242-
return true;
243-
}
244-
// FIXME
245-
if (i == (constructorA.getNumberOfParams() - 1) && tdA.arrayLevel() > tdB.arrayLevel()) {
246-
return true;
247-
}
248-
}
249-
return oneMoreSpecificFound;
250-
}
251217
}

‎javaparser-core/src/main/java/com/github/javaparser/resolution/logic/MethodResolutionLogic.java

Copy file name to clipboardExpand all lines: javaparser-core/src/main/java/com/github/javaparser/resolution/logic/MethodResolutionLogic.java
+4-2Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -739,8 +739,10 @@ private static ResolvedType getMethodsExplicitAndVariadicParameterType(
739739
return null;
740740
}
741741

742-
private static boolean isMoreSpecific(
743-
ResolvedMethodDeclaration methodA, ResolvedMethodDeclaration methodB, List<ResolvedType> argumentTypes) {
742+
static boolean isMoreSpecific(
743+
ResolvedMethodLikeDeclaration methodA,
744+
ResolvedMethodLikeDeclaration methodB,
745+
List<ResolvedType> argumentTypes) {
744746
final boolean aVariadic = methodA.hasVariadicParameter();
745747
final boolean bVariadic = methodB.hasVariadicParameter();
746748
final int aNumberOfParams = methodA.getNumberOfParams();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package com.github.javaparser.symbolsolver.resolution;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
5+
import com.github.javaparser.StaticJavaParser;
6+
import com.github.javaparser.ast.CompilationUnit;
7+
import com.github.javaparser.ast.body.ConstructorDeclaration;
8+
import com.github.javaparser.ast.stmt.ExplicitConstructorInvocationStmt;
9+
import com.github.javaparser.symbolsolver.JavaSymbolSolver;
10+
import com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver;
11+
import java.util.List;
12+
import java.util.Optional;
13+
import org.junit.jupiter.api.Test;
14+
15+
public class Issue4703Test {
16+
@Test
17+
void ecisResolutionTest() {
18+
String clazz = "public class Test {\n" + " public Test(Test test, int... values) {\n"
19+
+ " System.out.println(test);\n"
20+
+ " System.out.println(values);\n"
21+
+ " }\n"
22+
+ " public Test(int... values) {\n"
23+
+ " this(null, values);\n"
24+
+ " }\n"
25+
+ "}";
26+
StaticJavaParser.getParserConfiguration().setSymbolResolver(new JavaSymbolSolver(new ReflectionTypeSolver()));
27+
final CompilationUnit cu = StaticJavaParser.parse(clazz);
28+
final List<ConstructorDeclaration> all = cu.findAll(ConstructorDeclaration.class);
29+
for (ConstructorDeclaration cd : all) System.out.println(cd.getSignature());
30+
final Optional<ExplicitConstructorInvocationStmt> ecis = cu.findFirst(ExplicitConstructorInvocationStmt.class);
31+
assertEquals("Test(Test, int...)", ecis.get().resolve().getSignature());
32+
}
33+
34+
@Test
35+
void specificEcisResolutionTest() {
36+
String clazz = "public class Test {\n" + " public Test(Test test, int... values) {\n"
37+
+ " System.out.println(test);\n"
38+
+ " System.out.println(values);\n"
39+
+ " }\n"
40+
+ " public Test(int... values) {\n"
41+
+ " this(new Test(), values);\n"
42+
+ " }\n"
43+
+ "}";
44+
StaticJavaParser.getParserConfiguration().setSymbolResolver(new JavaSymbolSolver(new ReflectionTypeSolver()));
45+
final CompilationUnit cu = StaticJavaParser.parse(clazz);
46+
final List<ConstructorDeclaration> all = cu.findAll(ConstructorDeclaration.class);
47+
for (ConstructorDeclaration cd : all) System.out.println(cd.getSignature());
48+
final Optional<ExplicitConstructorInvocationStmt> ecis = cu.findFirst(ExplicitConstructorInvocationStmt.class);
49+
assertEquals("Test(Test, int...)", ecis.get().resolve().getSignature());
50+
}
51+
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.