JavaSimplePropertyIndex should store only reference/this/super expressions

This commit is contained in:
Dmitry Batkovich
2018-06-07 12:52:16 +03:00
parent 1addcbc86c
commit acc4808ee8
2 changed files with 47 additions and 44 deletions
@@ -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<Int, PropertyIndexValue>("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<Int, PropertyIndexValue>
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
@@ -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) {