From 39ffc136054fc88515e9a1c9f07135b71c8d4981 Mon Sep 17 00:00:00 2001 From: Daniil Ovchinnikov Date: Thu, 15 Feb 2018 15:02:25 +0300 Subject: [PATCH] [jvm-lang] convert api interfaces to java step 3: implementations --- .../lang/jvm/actions/annotationRequests.kt | 20 ++----- .../lang/jvm/actions/expectedTypes.kt | 10 ++-- .../lang/jvm/actions/methodRequests.kt | 32 ++++++---- .../CreateConstructorFromJavaUsageRequest.kt | 6 +- .../CreateExecutableFromJavaUsageRequest.kt | 60 ++++++++++--------- .../CreateFieldFromJavaUsageRequest.kt | 36 ++++------- .../CreateMethodFromJavaUsageRequest.kt | 20 +++---- .../lang/java/request/ExpectedJavaType.kt | 29 +++------ .../lang/java/request/createFieldFromUsage.kt | 2 +- ...FxEventHandlerReferenceQuickFixProvider.kt | 28 ++++----- 10 files changed, 103 insertions(+), 140 deletions(-) diff --git a/java/java-analysis-api/src/com/intellij/lang/jvm/actions/annotationRequests.kt b/java/java-analysis-api/src/com/intellij/lang/jvm/actions/annotationRequests.kt index f589e29797ed..f9327ed1cdaa 100644 --- a/java/java-analysis-api/src/com/intellij/lang/jvm/actions/annotationRequests.kt +++ b/java/java-analysis-api/src/com/intellij/lang/jvm/actions/annotationRequests.kt @@ -1,20 +1,8 @@ -/* - * 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.lang.jvm.actions -private data class SimpleAnnotationRequest(override val qualifiedName: String) : AnnotationRequest +private data class SimpleAnnotationRequest(private val qualifiedName: String) : AnnotationRequest { + override fun getQualifiedName() = qualifiedName +} fun annotationRequest(fqn: String): AnnotationRequest = SimpleAnnotationRequest(fqn) diff --git a/java/java-analysis-api/src/com/intellij/lang/jvm/actions/expectedTypes.kt b/java/java-analysis-api/src/com/intellij/lang/jvm/actions/expectedTypes.kt index 591858d2c837..579d44d98ef7 100644 --- a/java/java-analysis-api/src/com/intellij/lang/jvm/actions/expectedTypes.kt +++ b/java/java-analysis-api/src/com/intellij/lang/jvm/actions/expectedTypes.kt @@ -1,4 +1,4 @@ -// Copyright 2000-2017 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-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.lang.jvm.actions import com.intellij.lang.jvm.types.JvmType @@ -9,7 +9,7 @@ fun expectedType(type: JvmType, kind: ExpectedType.Kind = ExpectedType.Kind.EXAC fun expectedTypes(type: JvmType): ExpectedTypes = listOf(expectedType(type)) -private class SimpleExpectedType( - override val theType: JvmType, - override val theKind: ExpectedType.Kind -) : ExpectedType +private class SimpleExpectedType(private val theType: JvmType, private val theKind: ExpectedType.Kind) : ExpectedType { + override fun getTheType(): JvmType = theType + override fun getTheKind(): ExpectedType.Kind = theKind +} diff --git a/java/java-analysis-api/src/com/intellij/lang/jvm/actions/methodRequests.kt b/java/java-analysis-api/src/com/intellij/lang/jvm/actions/methodRequests.kt index 8e4cec266fe5..6c581e8f6f5a 100644 --- a/java/java-analysis-api/src/com/intellij/lang/jvm/actions/methodRequests.kt +++ b/java/java-analysis-api/src/com/intellij/lang/jvm/actions/methodRequests.kt @@ -1,4 +1,4 @@ -// Copyright 2000-2017 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-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.lang.jvm.actions import com.intellij.lang.jvm.JvmModifier @@ -11,23 +11,29 @@ import com.intellij.psi.PsiSubstitutor import com.intellij.psi.PsiType private class SimpleMethodRequest( - override val methodName: String, - override val modifiers: Collection = emptyList(), - override val returnType: ExpectedTypes = emptyList(), - override val annotations: Collection = emptyList(), - override val parameters: List = emptyList(), - override val targetSubstitutor: JvmSubstitutor + private val methodName: String, + private val modifiers: Collection, + private val returnType: ExpectedTypes, + private val targetSubstitutor: JvmSubstitutor ) : CreateMethodRequest { - override val isValid: Boolean = true + override fun isValid(): Boolean = true + override fun getMethodName() = methodName + override fun getModifiers() = modifiers + override fun getReturnType() = returnType + override fun getAnnotations() = emptyList() + override fun getParameters() = emptyList() + override fun getTargetSubstitutor() = targetSubstitutor } private class SimpleConstructorRequest( - override val parameters: ExpectedParameters, - override val targetSubstitutor: JvmSubstitutor + private val parameters: ExpectedParameters, + private val targetSubstitutor: JvmSubstitutor ) : CreateConstructorRequest { - override val isValid: Boolean get() = true - override val modifiers: List get() = emptyList() - override val annotations: Collection = emptyList() + override fun isValid(): Boolean = true + override fun getModifiers() = emptyList() + override fun getAnnotations() = emptyList() + override fun getTargetSubstitutor() = targetSubstitutor + override fun getParameters() = parameters } fun methodRequest(project: Project, methodName: String, modifier: JvmModifier, returnType: JvmType): CreateMethodRequest { diff --git a/java/java-impl/src/com/intellij/lang/java/request/CreateConstructorFromJavaUsageRequest.kt b/java/java-impl/src/com/intellij/lang/java/request/CreateConstructorFromJavaUsageRequest.kt index 07f11449d3d4..66ea79fd005f 100644 --- a/java/java-impl/src/com/intellij/lang/java/request/CreateConstructorFromJavaUsageRequest.kt +++ b/java/java-impl/src/com/intellij/lang/java/request/CreateConstructorFromJavaUsageRequest.kt @@ -1,4 +1,4 @@ -// Copyright 2000-2017 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-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.lang.java.request import com.intellij.lang.jvm.JvmModifier @@ -7,5 +7,5 @@ import com.intellij.psi.PsiConstructorCall internal class CreateConstructorFromJavaUsageRequest( call: PsiConstructorCall, - override val modifiers: Collection -) : CreateExecutableFromJavaUsageRequest(call), CreateConstructorRequest + modifiers: Collection +) : CreateExecutableFromJavaUsageRequest(call, modifiers), CreateConstructorRequest diff --git a/java/java-impl/src/com/intellij/lang/java/request/CreateExecutableFromJavaUsageRequest.kt b/java/java-impl/src/com/intellij/lang/java/request/CreateExecutableFromJavaUsageRequest.kt index 8484ed78efae..3d74563c48f4 100644 --- a/java/java-impl/src/com/intellij/lang/java/request/CreateExecutableFromJavaUsageRequest.kt +++ b/java/java-impl/src/com/intellij/lang/java/request/CreateExecutableFromJavaUsageRequest.kt @@ -1,9 +1,9 @@ -// Copyright 2000-2017 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-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.lang.java.request import com.intellij.codeInsight.daemon.impl.quickfix.CreateMethodFromUsageFix.getTargetSubstitutor +import com.intellij.lang.jvm.JvmModifier import com.intellij.lang.jvm.actions.* -import com.intellij.lang.jvm.types.JvmSubstitutor import com.intellij.openapi.components.service import com.intellij.psi.* import com.intellij.psi.codeStyle.JavaCodeStyleManager @@ -12,41 +12,45 @@ import com.intellij.psi.util.createSmartPointer import com.intellij.psi.util.parentOfType import com.intellij.refactoring.util.RefactoringUtil -abstract internal class CreateExecutableFromJavaUsageRequest(call: T) : CreateExecutableRequest { +internal abstract class CreateExecutableFromJavaUsageRequest( + call: T, + private val modifiers: Collection +) : CreateExecutableRequest { private val psiManager = call.manager - protected val project = psiManager.project - protected val callPointer: SmartPsiElementPointer = call.createSmartPointer(project) + private val project = psiManager.project + private val callPointer: SmartPsiElementPointer = call.createSmartPointer(project) protected val call: T get() = callPointer.element ?: error("dead pointer") - override val isValid: Boolean get() = callPointer.element != null + override fun isValid() = callPointer.element != null - override val annotations: Collection get() = emptyList() + override fun getAnnotations() = emptyList() - override val targetSubstitutor: JvmSubstitutor get() = PsiJvmSubstitutor(project, getTargetSubstitutor(call)) + override fun getModifiers() = modifiers - override val parameters: List - get() { - val argumentList = call.argumentList ?: return emptyList() - val scope = call.resolveScope - val codeStyleManager: JavaCodeStyleManager = project.service() - return argumentList.expressions.map { expression -> - var argType: PsiType? = RefactoringUtil.getTypeByExpression(expression) - val names = codeStyleManager.suggestVariableName(VariableKind.PARAMETER, null, expression, argType) - if (argType == null || PsiType.NULL == argType || LambdaUtil.notInferredType(argType)) { - argType = PsiType.getJavaLangObject(psiManager, scope) - } - else if (argType is PsiDisjunctionType) { - argType = argType.leastUpperBound - } - else if (argType is PsiWildcardType) { - argType = if (argType.isBounded) argType.bound else PsiType.getJavaLangObject(psiManager, scope) - } - val expectedTypeInfo = argType?.let { expectedType(it, ExpectedType.Kind.SUPERTYPE) } - val expectedTypes = expectedTypeInfo?.let { listOf(it) } ?: emptyList() - ExpectedParameter(names, expectedTypes) + override fun getTargetSubstitutor() = PsiJvmSubstitutor(project, getTargetSubstitutor(call)) + + override fun getParameters(): List { + val argumentList = call.argumentList ?: return emptyList() + val scope = call.resolveScope + val codeStyleManager: JavaCodeStyleManager = project.service() + return argumentList.expressions.map { expression -> + var argType: PsiType? = RefactoringUtil.getTypeByExpression(expression) + val names = codeStyleManager.suggestVariableName(VariableKind.PARAMETER, null, expression, argType) + if (argType == null || PsiType.NULL == argType || LambdaUtil.notInferredType(argType)) { + argType = PsiType.getJavaLangObject(psiManager, scope) } + else if (argType is PsiDisjunctionType) { + argType = argType.leastUpperBound + } + else if (argType is PsiWildcardType) { + argType = if (argType.isBounded) argType.bound else PsiType.getJavaLangObject(psiManager, scope) + } + val expectedTypeInfo = argType?.let { expectedType(it, ExpectedType.Kind.SUPERTYPE) } + val expectedTypes = expectedTypeInfo?.let { listOf(it) } ?: emptyList() + ExpectedParameter(names, expectedTypes) } + } val context get() = call.parentOfType(PsiMethod::class, PsiClass::class) } diff --git a/java/java-impl/src/com/intellij/lang/java/request/CreateFieldFromJavaUsageRequest.kt b/java/java-impl/src/com/intellij/lang/java/request/CreateFieldFromJavaUsageRequest.kt index 4167a9005c37..725d4c23a831 100644 --- a/java/java-impl/src/com/intellij/lang/java/request/CreateFieldFromJavaUsageRequest.kt +++ b/java/java-impl/src/com/intellij/lang/java/request/CreateFieldFromJavaUsageRequest.kt @@ -1,26 +1,10 @@ -/* - * 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.lang.java.request import com.intellij.codeInsight.daemon.impl.quickfix.CreateFromUsageBaseFix.getTargetSubstitutor import com.intellij.codeInsight.daemon.impl.quickfix.CreateFromUsageUtils.guessExpectedTypes import com.intellij.lang.jvm.JvmModifier import com.intellij.lang.jvm.actions.CreateFieldRequest -import com.intellij.lang.jvm.actions.ExpectedTypes -import com.intellij.lang.jvm.types.JvmSubstitutor import com.intellij.psi.PsiElement import com.intellij.psi.PsiJvmSubstitutor import com.intellij.psi.PsiReferenceExpression @@ -28,22 +12,26 @@ import com.intellij.psi.util.createSmartPointer internal class CreateFieldFromJavaUsageRequest( reference: PsiReferenceExpression, - override val modifiers: Collection, - override val constant: Boolean, + private val modifiers: Collection, + private val isConstant: Boolean, private val useAnchor: Boolean ) : CreateFieldRequest { private val myReference = reference.createSmartPointer() - override val isValid: Boolean get() = myReference.element?.referenceName != null + override fun isValid() = myReference.element?.referenceName != null - val reference: PsiReferenceExpression get() = myReference.element!! + val reference get() = myReference.element!! val anchor: PsiElement? get() = if (useAnchor) reference else null - override val fieldName: String get() = reference.referenceName!! + override fun getModifiers() = modifiers - override val fieldType: ExpectedTypes get() = guessExpectedTypes(reference, false).map(::ExpectedJavaType) + override fun getFieldName() = reference.referenceName!! - override val targetSubstitutor: JvmSubstitutor get() = PsiJvmSubstitutor(reference.project, getTargetSubstitutor(reference)) + override fun getFieldType() = guessExpectedTypes(reference, false).map(::ExpectedJavaType) + + override fun getTargetSubstitutor() = PsiJvmSubstitutor(reference.project, getTargetSubstitutor(reference)) + + override fun isConstant(): Boolean = isConstant } diff --git a/java/java-impl/src/com/intellij/lang/java/request/CreateMethodFromJavaUsageRequest.kt b/java/java-impl/src/com/intellij/lang/java/request/CreateMethodFromJavaUsageRequest.kt index 205dace7a5ff..a96415bee784 100644 --- a/java/java-impl/src/com/intellij/lang/java/request/CreateMethodFromJavaUsageRequest.kt +++ b/java/java-impl/src/com/intellij/lang/java/request/CreateMethodFromJavaUsageRequest.kt @@ -1,11 +1,10 @@ -// Copyright 2000-2017 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-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.lang.java.request import com.intellij.codeInsight.daemon.impl.quickfix.CreateFromUsageUtils.guessExpectedTypes import com.intellij.codeInsight.daemon.impl.quickfix.CreateMethodFromUsageFix.hasErrorsInArgumentList import com.intellij.lang.jvm.JvmModifier import com.intellij.lang.jvm.actions.CreateMethodRequest -import com.intellij.lang.jvm.actions.ExpectedTypes import com.intellij.psi.* import com.intellij.psi.util.parentOfType import com.intellij.psi.util.parents @@ -13,19 +12,16 @@ import com.intellij.util.withPrevious internal class CreateMethodFromJavaUsageRequest( methodCall: PsiMethodCallExpression, - override val modifiers: Collection -) : CreateExecutableFromJavaUsageRequest(methodCall), CreateMethodRequest { + modifiers: Collection +) : CreateExecutableFromJavaUsageRequest(methodCall, modifiers), CreateMethodRequest { - override val isValid: Boolean - get() { - val call = callPointer.element ?: return false - call.methodExpression.referenceName ?: return false - return !hasErrorsInArgumentList(call) - } + override fun isValid() = super.isValid() && call.let { + it.methodExpression.referenceName != null && !hasErrorsInArgumentList(it) + } - override val methodName: String get() = call.methodExpression.referenceName!! + override fun getMethodName() = call.methodExpression.referenceName!! - override val returnType: ExpectedTypes get() = guessExpectedTypes(call, call.parent is PsiStatement).map(::ExpectedJavaType) + override fun getReturnType() = guessExpectedTypes(call, call.parent is PsiStatement).map(::ExpectedJavaType) fun getAnchor(targetClass: PsiClass): PsiElement? { val enclosingMember = call.parentOfType(PsiMethod::class, PsiField::class, PsiClassInitializer::class) ?: return null diff --git a/java/java-impl/src/com/intellij/lang/java/request/ExpectedJavaType.kt b/java/java-impl/src/com/intellij/lang/java/request/ExpectedJavaType.kt index c3051d6c30be..a5c682bbca65 100644 --- a/java/java-impl/src/com/intellij/lang/java/request/ExpectedJavaType.kt +++ b/java/java-impl/src/com/intellij/lang/java/request/ExpectedJavaType.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.lang.java.request import com.intellij.codeInsight.ExpectedTypeInfo @@ -21,12 +7,11 @@ import com.intellij.lang.jvm.types.JvmType internal class ExpectedJavaType(val info: ExpectedTypeInfo) : ExpectedType { - override val theType: JvmType get() = info.defaultType + override fun getTheType(): JvmType = info.defaultType - override val theKind: ExpectedType.Kind - get() = when (info.kind) { - ExpectedTypeInfo.TYPE_OR_SUBTYPE -> ExpectedType.Kind.SUBTYPE - ExpectedTypeInfo.TYPE_OR_SUPERTYPE -> ExpectedType.Kind.SUPERTYPE - else -> ExpectedType.Kind.EXACT - } + override fun getTheKind(): ExpectedType.Kind = when (info.kind) { + ExpectedTypeInfo.TYPE_OR_SUBTYPE -> ExpectedType.Kind.SUBTYPE + ExpectedTypeInfo.TYPE_OR_SUPERTYPE -> ExpectedType.Kind.SUPERTYPE + else -> ExpectedType.Kind.EXACT + } } diff --git a/java/java-impl/src/com/intellij/lang/java/request/createFieldFromUsage.kt b/java/java-impl/src/com/intellij/lang/java/request/createFieldFromUsage.kt index f72567da35eb..f1c3528d2dd7 100644 --- a/java/java-impl/src/com/intellij/lang/java/request/createFieldFromUsage.kt +++ b/java/java-impl/src/com/intellij/lang/java/request/createFieldFromUsage.kt @@ -112,7 +112,7 @@ private class CreateFieldRequests(val myRef: PsiReferenceExpression) { modifiers = modifiers, reference = myRef, useAnchor = target.toJavaClassOrNull() == ownerClass, - constant = false + isConstant = false ) requests[target] = request } diff --git a/plugins/javaFX/src/org/jetbrains/plugins/javaFX/fxml/refs/JavaFxEventHandlerReferenceQuickFixProvider.kt b/plugins/javaFX/src/org/jetbrains/plugins/javaFX/fxml/refs/JavaFxEventHandlerReferenceQuickFixProvider.kt index b5314373fb95..a508f4ebbcae 100644 --- a/plugins/javaFX/src/org/jetbrains/plugins/javaFX/fxml/refs/JavaFxEventHandlerReferenceQuickFixProvider.kt +++ b/plugins/javaFX/src/org/jetbrains/plugins/javaFX/fxml/refs/JavaFxEventHandlerReferenceQuickFixProvider.kt @@ -5,7 +5,6 @@ import com.intellij.codeInsight.daemon.QuickFixActionRegistrar import com.intellij.codeInsight.quickfix.UnresolvedReferenceQuickFixProvider import com.intellij.lang.jvm.JvmModifier import com.intellij.lang.jvm.actions.* -import com.intellij.lang.jvm.types.JvmSubstitutor import com.intellij.openapi.project.Project import com.intellij.psi.PsiJvmSubstitutor import com.intellij.psi.PsiModifier @@ -41,18 +40,17 @@ class CreateEventHandlerRequest(element: XmlAttributeValue) : CreateMethodReques private val myVisibility = getVisibility(myProject) private val myPointer = element.createSmartPointer(myProject) - override val isValid: Boolean get() { - val element = myPointer.element - return element != null && element.value != null + override fun isValid(): Boolean = myPointer.element.let { + it != null && it.value != null } private val myElement get() = myPointer.element!! - override val methodName: String get() = myElement.value!!.substring(1) + override fun getMethodName() = myElement.value!!.substring(1) - override val returnType: ExpectedTypes get() = listOf(expectedType(PsiType.VOID, ExpectedType.Kind.EXACT)) + override fun getReturnType() = listOf(expectedType(PsiType.VOID, ExpectedType.Kind.EXACT)) - override val parameters: List get() { + override fun getParameters(): List { val eventType = getEventType(myElement) val expectedType = expectedType(eventType, ExpectedType.Kind.EXACT) val nameInfo = suggestParamName(myProject, eventType) @@ -60,18 +58,16 @@ class CreateEventHandlerRequest(element: XmlAttributeValue) : CreateMethodReques return listOf(parameter) } - override val modifiers: Collection get() = setOf(myVisibility) + override fun getModifiers() = setOf(myVisibility) - override val annotations: Collection get() { - return if (myVisibility != JvmModifier.PUBLIC) { - listOf(annotationRequest(JavaFxCommonNames.JAVAFX_FXML_ANNOTATION)) - } - else { - emptyList() - } + override fun getAnnotations() = if (myVisibility != JvmModifier.PUBLIC) { + listOf(annotationRequest(JavaFxCommonNames.JAVAFX_FXML_ANNOTATION)) + } + else { + emptyList() } - override val targetSubstitutor: JvmSubstitutor get() = PsiJvmSubstitutor(myProject, PsiSubstitutor.EMPTY) + override fun getTargetSubstitutor() = PsiJvmSubstitutor(myProject, PsiSubstitutor.EMPTY) } private fun getVisibility(project: Project): JvmModifier {