SSR: fix finding parameterized new expressions

This commit is contained in:
Bas Leijdekkers
2018-05-18 12:55:26 +02:00
parent 7754aa912e
commit 2ebd3d54be
2 changed files with 23 additions and 15 deletions
@@ -978,7 +978,8 @@ public class JavaMatchingVisitor extends JavaElementVisitor {
final PsiType type1 = new1.getType();
final PsiType type2 = new2.getType();
myMatchingVisitor.setResult(type1 != null && type2 != null && type1.getArrayDimensions() == type2.getArrayDimensions() &&
myMatchingVisitor.matchSons(new1.getArgumentList(), new2.getArgumentList()));
myMatchingVisitor.matchSons(new1.getArgumentList(), new2.getArgumentList()) &&
myMatchingVisitor.setResult(matchTypeParameters(new1, new2)));
}
}
@@ -1027,27 +1028,22 @@ public class JavaMatchingVisitor extends JavaElementVisitor {
}
if (!myMatchingVisitor.setResult(myMatchingVisitor.matchSons(mcall.getArgumentList(), mcall2.getArgumentList()))) return;
if (!myMatchingVisitor.setResult(matchTypeParameters(mcallRef1, mcallRef2))) return;
if (!myMatchingVisitor.setResult(matchTypeParameters(mcall, mcall2))) return;
if (isTypedVar) {
myMatchingVisitor.setResult(myMatchingVisitor.handleTypedElement(patternMethodName, mcallRef2.getReferenceNameElement()));
}
}
private boolean matchTypeParameters(PsiJavaCodeReferenceElement mcallRef1, PsiJavaCodeReferenceElement mcallRef2) {
final PsiReferenceParameterList patternParameterList = mcallRef1.getParameterList();
if (patternParameterList == null) {
return true;
}
private boolean matchTypeParameters(PsiCallExpression call1, PsiCallExpression call2) {
final PsiReferenceParameterList patternParameterList = call1.getTypeArgumentList();
final PsiTypeElement[] patternTypeElements = patternParameterList.getTypeParameterElements();
if (patternTypeElements.length == 0) {
return true;
}
PsiReferenceParameterList matchedParameterList = mcallRef2.getParameterList();
if (matchedParameterList == null) {
return false;
}
if (matchedParameterList.getFirstChild() == null) { // check inferred type parameters
final JavaResolveResult resolveResult = mcallRef2.advancedResolve(false);
PsiReferenceParameterList matchedParameterList = call2.getTypeArgumentList();
if (matchedParameterList.getFirstChild() == null && myMatchingVisitor.getMatchContext().getOptions().isLooseMatching()) {
// check inferred type parameters
final JavaResolveResult resolveResult = call2.resolveMethodGenerics();
final PsiMethod targetMethod = (PsiMethod)resolveResult.getElement();
if (targetMethod == null) {
return false;
@@ -1064,7 +1060,7 @@ public class JavaMatchingVisitor extends JavaElementVisitor {
if (type == null) {
return false;
}
final PsiTypeElement matchedTypeElement = JavaPsiFacade.getElementFactory(mcallRef1.getProject()).createTypeElement(type);
final PsiTypeElement matchedTypeElement = JavaPsiFacade.getElementFactory(call1.getProject()).createTypeElement(type);
matchedParameterList.add(matchedTypeElement);
}
}
@@ -2700,20 +2700,32 @@ public class StructuralSearchTest extends StructuralSearchTestCase {
public void testFindParameterizedMethodCalls() {
String source = "interface Foo {" +
" <T> T bar();" +
" <S, T> void bar2(S, T);" +
" <S, T> void bar2(S s, T t);" +
"}" +
"class X {" +
" <T> X(T t) {}" +
" X() {}" +
" void x(Foo foo) {" +
" foo.<String>bar();" +
" foo.<Integer>bar();" +
" String s = foo.bar();" +
" foo.bar2(1, 2);" +
" }" +
" void y(String s) {" +
" new <String>X();" +
" new <String>X();" +
" new X();" +
" new X(s);" +
" }" +
"}";
assertEquals("find parameterized method calls 1", 1, findMatchesCount(source, "foo.<Integer>bar()"));
assertEquals("find parameterized method calls 2", 2, findMatchesCount(source, "foo.<String>bar()"));
assertEquals("find parameterized method calls 3", 3, findMatchesCount(source, "'_a.<'_b>'_c('_d*)"));
assertEquals("find parameterized method calls 4", 4, findMatchesCount(source, "'_a.<'_b+>'_c('_d*)"));
assertEquals("find parameterized constructor calls 1", 2, findMatchesCount(source, "new <String>X()"));
assertEquals("find parameterized constructor calls 2", 1, findMatchesCount(source, "new <String>X(s)"));
assertEquals("find constructor calls 3", 3, findMatchesCount(source, "new X()"));
}
public void testFindDiamondTypes() {