From acc4808ee8cc1dc0bf604ba1f5d0da3cf231c65a Mon Sep 17 00:00:00 2001 From: Dmitry Batkovich Date: Tue, 6 Feb 2018 16:36:03 +0300 Subject: [PATCH] JavaSimplePropertyIndex should store only reference/this/super expressions --- .../psi/impl/JavaSimplePropertyIndex.kt | 40 ++++++--------- .../util/JavaPropertyDetectionTest.kt | 51 ++++++++++++------- 2 files changed, 47 insertions(+), 44 deletions(-) diff --git a/java/java-indexing-impl/src/com/intellij/psi/impl/JavaSimplePropertyIndex.kt b/java/java-indexing-impl/src/com/intellij/psi/impl/JavaSimplePropertyIndex.kt index 7d5ffe41522f..6c846ad403d4 100644 --- a/java/java-indexing-impl/src/com/intellij/psi/impl/JavaSimplePropertyIndex.kt +++ b/java/java-indexing-impl/src/com/intellij/psi/impl/JavaSimplePropertyIndex.kt @@ -1,18 +1,4 @@ -/* - * 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. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ +// 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. package com.intellij.psi.impl import com.intellij.ide.highlighter.JavaFileType @@ -33,6 +19,7 @@ import com.intellij.psi.impl.source.tree.LightTreeUtil import com.intellij.psi.impl.source.tree.RecursiveLighterASTNodeWalkingVisitor import com.intellij.psi.search.GlobalSearchScope import com.intellij.psi.stub.JavaStubImplUtil +import com.intellij.psi.tree.TokenSet import com.intellij.psi.util.PropertyUtil import com.intellij.psi.util.PropertyUtilBase import com.intellij.util.containers.ContainerUtil @@ -46,6 +33,7 @@ import java.io.DataOutput private val indexId = ID.create("java.simple.property") private val log = Logger.getInstance(JavaSimplePropertyIndex::class.java) +private val allowedExpressions = TokenSet.create(ElementType.REFERENCE_EXPRESSION, ElementType.THIS_EXPRESSION, ElementType.SUPER_EXPRESSION) fun getFieldOfGetter(method: PsiMethodImpl): PsiField? = resolveFieldFromIndexValue(method, true) @@ -166,20 +154,22 @@ class JavaSimplePropertyIndex : FileBasedIndexExtension return lhsText } - private fun getGetterPropertyRefText(codeBlock: LighterASTNode): String? = tree - .getChildren(codeBlock) - .singleOrNull { ElementType.JAVA_STATEMENT_BIT_SET.contains(it.tokenType) } - ?.takeIf { it.tokenType == JavaElementType.RETURN_STATEMENT} - ?.let { LightTreeUtil.firstChildOfType(tree, it, ElementType.EXPRESSION_BIT_SET) } - ?.takeIf(this::doesNotContainMethodCalls) - ?.let { LightTreeUtil.toFilteredString(tree, it, null) } + private fun getGetterPropertyRefText(codeBlock: LighterASTNode): String? { + return tree + .getChildren(codeBlock) + .singleOrNull { ElementType.JAVA_STATEMENT_BIT_SET.contains(it.tokenType) } + ?.takeIf { it.tokenType == JavaElementType.RETURN_STATEMENT} + ?.let { LightTreeUtil.firstChildOfType(tree, it, allowedExpressions) } + ?.takeIf(this::checkQulifiers) + ?.let { LightTreeUtil.toFilteredString(tree, it, null) } + } - private fun doesNotContainMethodCalls(expression: LighterASTNode): Boolean { - if (expression.tokenType == JavaElementType.METHOD_CALL_EXPRESSION) { + private fun checkQulifiers(expression: LighterASTNode): Boolean { + if (!allowedExpressions.contains(expression.tokenType)) { return false } val qualifier = JavaLightTreeUtil.findExpressionChild(tree, expression) - return qualifier == null || doesNotContainMethodCalls(qualifier) + return qualifier == null || checkQulifiers(qualifier) } }.visitNode(tree.root) result diff --git a/java/java-tests/testSrc/com/intellij/util/JavaPropertyDetectionTest.kt b/java/java-tests/testSrc/com/intellij/util/JavaPropertyDetectionTest.kt index 067155f988ba..8a5a90ba6a89 100644 --- a/java/java-tests/testSrc/com/intellij/util/JavaPropertyDetectionTest.kt +++ b/java/java-tests/testSrc/com/intellij/util/JavaPropertyDetectionTest.kt @@ -1,33 +1,16 @@ -/* - * 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. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ +// 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. package com.intellij.util import com.intellij.ide.highlighter.JavaFileType import com.intellij.psi.PsiMethod import com.intellij.psi.impl.JavaSimplePropertyIndex import com.intellij.psi.impl.PropertyIndexValue -import com.intellij.psi.impl.search.JavaNullMethodArgumentIndex import com.intellij.psi.util.PropertyMemberType import com.intellij.psi.util.PropertyUtil import com.intellij.psi.util.PsiTreeUtil import com.intellij.testFramework.fixtures.LightCodeInsightFixtureTestCase -import com.intellij.util.indexing.FileBasedIndex import com.intellij.util.indexing.FileContentImpl import com.intellij.util.indexing.IndexingDataKeys -import junit.framework.TestCase import kotlin.test.assertNotEquals class JavaPropertyDetectionTest : LightCodeInsightFixtureTestCase() { @@ -128,6 +111,14 @@ class JavaPropertyDetectionTest : LightCodeInsightFixtureTestCase() { return name; } + public String getBoo() { + return Boo.Foo.CONST; + } + + public String getXxx() { + return xxx().yyy; + } + public class Bar { public String getName() { return Foo.this.getName(); @@ -138,7 +129,29 @@ class JavaPropertyDetectionTest : LightCodeInsightFixtureTestCase() { } } } - """.trimIndent(), mapOf(Pair(0, PropertyIndexValue("name", true)))) + """.trimIndent(), mapOf(Pair(0, PropertyIndexValue("name", true)), Pair(2, PropertyIndexValue("Boo.Foo.CONST", true)))) + } + + fun testIndexDoesntContainPolyadicExpressions() { + assertJavaSimplePropertyIndex(""" + public class Foo { + public String getName() { + return n + a + m + e; + } + + public String getName1() { + return --i; + } + + public String getName2() { + return 1 == 1 ? 1 : 1; + } + + public String getName3() { + return new String(); + } + } + """.trimIndent(), emptyMap()) } private fun assertPropertyMember(text: String, memberType: PropertyMemberType) {