From bb64599d5c0adbc1cbe62647c5f90cc2e88e2f5d Mon Sep 17 00:00:00 2001 From: Bas Leijdekkers Date: Fri, 1 Feb 2019 13:12:07 +0100 Subject: [PATCH] SSR: add class keyword to search plan if part of a class object access expression (IDEA-206479) --- .../impl/matcher/compiler/JavaCompilingVisitor.java | 9 +++++---- .../structuralsearch/OptimizedSearchScanTest.java | 7 ++++++- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/java/structuralsearch-java/src/com/intellij/structuralsearch/impl/matcher/compiler/JavaCompilingVisitor.java b/java/structuralsearch-java/src/com/intellij/structuralsearch/impl/matcher/compiler/JavaCompilingVisitor.java index f15dc527fa24..3cf7471871e1 100644 --- a/java/structuralsearch-java/src/com/intellij/structuralsearch/impl/matcher/compiler/JavaCompilingVisitor.java +++ b/java/structuralsearch-java/src/com/intellij/structuralsearch/impl/matcher/compiler/JavaCompilingVisitor.java @@ -1,4 +1,4 @@ -// Copyright 2000-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. +// Copyright 2000-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. package com.intellij.structuralsearch.impl.matcher.compiler; import com.intellij.dupLocator.iterators.NodeIterator; @@ -45,7 +45,7 @@ public class JavaCompilingVisitor extends JavaRecursiveElementWalkingVisitor { PsiKeyword.THROWS, PsiKeyword.EXTENDS, PsiKeyword.IMPLEMENTS); public JavaCompilingVisitor(GlobalCompilingVisitor compilingVisitor) { - this.myCompilingVisitor = compilingVisitor; + myCompilingVisitor = compilingVisitor; } public void compile(PsiElement[] topLevelElements) { @@ -94,6 +94,7 @@ public class JavaCompilingVisitor extends JavaRecursiveElementWalkingVisitor { @Override public void visitCatchSection(PsiCatchSection section) { + // check parameter first and skip catch section if count is zero final PsiParameter parameter = section.getParameter(); if (parameter != null && !handleWord(parameter.getName(), CODE, myCompilingVisitor.getContext())) return; super.visitCatchSection(section); @@ -138,7 +139,7 @@ public class JavaCompilingVisitor extends JavaRecursiveElementWalkingVisitor { } else if (element instanceof PsiKeyword) { final String keyword = element.getText(); - if (!excludedKeywords.contains(keyword)) { + if (!excludedKeywords.contains(keyword) || element.getParent() instanceof PsiExpression) { GlobalCompilingVisitor.addFilesToSearchForGivenWord(keyword, true, CODE, myCompilingVisitor.getContext()); } } @@ -146,7 +147,7 @@ public class JavaCompilingVisitor extends JavaRecursiveElementWalkingVisitor { @Override public List getDescendantsOf(String className, boolean includeSelf, Project project) { - SmartList result = new SmartList<>(); + final SmartList result = new SmartList<>(); // use project and libraries scope, because super class may be outside the scope of the search final GlobalSearchScope projectAndLibraries = ProjectScope.getAllScope(project); diff --git a/platform/structuralsearch/testSource/com/intellij/structuralsearch/OptimizedSearchScanTest.java b/platform/structuralsearch/testSource/com/intellij/structuralsearch/OptimizedSearchScanTest.java index 6c5dc8d67480..baa3f14827f0 100644 --- a/platform/structuralsearch/testSource/com/intellij/structuralsearch/OptimizedSearchScanTest.java +++ b/platform/structuralsearch/testSource/com/intellij/structuralsearch/OptimizedSearchScanTest.java @@ -1,4 +1,4 @@ -// Copyright 2000-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. +// Copyright 2000-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. package com.intellij.structuralsearch; import com.intellij.openapi.projectRoots.Sdk; @@ -132,4 +132,9 @@ public class OptimizedSearchScanTest extends StructuralSearchTestCase { final String plan = findWordsToBeUsedWhenSearchingFor("assert '_exp != null && true: \"'_exp is null\";"); assertEquals("[in literals:null][in literals:is][in code:assert][in code:null][in code:true]", plan); } + + public void testClassObjectAccessExpression() { + final String plan = findWordsToBeUsedWhenSearchingFor("ArrayUtil.toObjectArray($var$, $class$.class)"); + assertEquals("[in code:toObjectArray][in code:ArrayUtil][in code:class]", plan); + } }