SSR: fix reference expression variable with qualifier search

This commit is contained in:
Bas Leijdekkers
2017-05-27 09:13:25 +02:00
parent ce5d505f44
commit fb308f366d
2 changed files with 29 additions and 20 deletions
@@ -287,20 +287,18 @@ public class JavaMatchingVisitor extends JavaElementVisitor {
@Override
public void visitAnnotation(PsiAnnotation annotation) {
final PsiAnnotation psiAnnotation = (PsiAnnotation)myMatchingVisitor.getElement();
myMatchingVisitor.setResult(myMatchingVisitor.match(annotation.getNameReferenceElement(), psiAnnotation.getNameReferenceElement()) &&
myMatchingVisitor
.matchInAnyOrder(annotation.getParameterList().getAttributes(),
psiAnnotation.getParameterList().getAttributes()));
final PsiAnnotation other = (PsiAnnotation)myMatchingVisitor.getElement();
myMatchingVisitor.setResult(myMatchingVisitor.match(annotation.getNameReferenceElement(), other.getNameReferenceElement()) &&
myMatchingVisitor.matchInAnyOrder(annotation.getParameterList().getAttributes(),
other.getParameterList().getAttributes()));
}
@Override
public void visitNameValuePair(PsiNameValuePair pair) {
final PsiNameValuePair elementNameValuePair = (PsiNameValuePair)myMatchingVisitor.getElement();
final PsiAnnotationMemberValue annotationInitializer = pair.getValue();
myMatchingVisitor.setResult(myMatchingVisitor.match(annotationInitializer, elementNameValuePair.getValue()));
final PsiAnnotationMemberValue value = pair.getValue();
myMatchingVisitor.setResult(myMatchingVisitor.match(value, elementNameValuePair.getValue()));
if (myMatchingVisitor.getResult()) {
final PsiIdentifier nameIdentifier = pair.getNameIdentifier();
final PsiIdentifier otherIdentifier = elementNameValuePair.getNameIdentifier();
@@ -592,20 +590,21 @@ public class JavaMatchingVisitor extends JavaElementVisitor {
@Override
public void visitReferenceExpression(final PsiReferenceExpression reference) {
final PsiExpression qualifier = reference.getQualifierExpression();
final PsiElement nameElement = reference.getReferenceNameElement();
final MatchContext context = myMatchingVisitor.getMatchContext();
MatchingHandler _handler = nameElement != null ? context.getPattern().getHandlerSimple(nameElement) : null;
if (!(_handler instanceof SubstitutionHandler)) _handler = context.getPattern().getHandlerSimple(reference);
final CompiledPattern pattern = context.getPattern();
MatchingHandler _handler = pattern.getHandlerSimple(reference.getReferenceNameElement());
boolean special = false;
if (_handler == null) {
_handler = pattern.getHandlerSimple(reference);
special = true;
}
final PsiElement element = myMatchingVisitor.getElement();
PsiElement other = element instanceof PsiExpression && context.getOptions().isLooseMatching() ?
PsiUtil.skipParenthesizedExprDown((PsiExpression)element) :
element;
if (_handler instanceof SubstitutionHandler &&
!(context.getPattern().getHandlerSimple(qualifier) instanceof SubstitutionHandler) &&
!(qualifier instanceof PsiThisExpression)) {
final PsiExpression qualifier = reference.getQualifierExpression();
if (_handler instanceof SubstitutionHandler && (qualifier == null || special)) {
final SubstitutionHandler handler = (SubstitutionHandler)_handler;
if (handler.isSubtype() || handler.isStrictSubtype()) {
myMatchingVisitor.setResult(checkMatchWithinHierarchy(other, handler, reference));
@@ -644,7 +643,7 @@ public class JavaMatchingVisitor extends JavaElementVisitor {
final PsiElement referenceElement = reference.getReferenceNameElement();
final PsiElement referenceElement2 = reference2.getReferenceNameElement();
if (context.getPattern().isTypedVar(referenceElement)) {
if (pattern.isTypedVar(referenceElement)) {
myMatchingVisitor.setResult(myMatchingVisitor.handleTypedElement(referenceElement, referenceElement2));
}
else {
@@ -666,7 +665,7 @@ public class JavaMatchingVisitor extends JavaElementVisitor {
return;
}
}
final MatchingHandler handler = context.getPattern().getHandler(qualifier);
final MatchingHandler handler = pattern.getHandler(qualifier);
matchImplicitQualifier(handler, referencedElement, context);
}
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2016 JetBrains s.r.o.
* Copyright 2000-2017 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -17,7 +17,6 @@ package com.intellij.structuralsearch;
import com.intellij.openapi.fileTypes.StdFileTypes;
import com.intellij.psi.*;
import com.intellij.structuralsearch.impl.matcher.MatcherImplUtil;
import com.intellij.testFramework.PlatformTestUtil;
import org.jetbrains.annotations.NotNull;
@@ -2394,4 +2393,15 @@ public class StructuralSearchTest extends StructuralSearchTestCase {
assertEquals("find method with super call and matching parameter", 1,
findMatchesCount(source2, "'_rt '_m('_t '_p*) { return super.'_m('_p); }"));
}
public void testFindWithQualifiers() {
String source1 = "class Two {" +
" Two x;" +
" void f() {" +
" Two a = x.x.x;" +
" Two b = x.x.x.x;" +
" }" +
"}";
assertEquals(1, findMatchesCount(source1, "x.x.x.'_x", true));
}
}