Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cockpit NG: Added field level code contribution for wizards using POJO & Java 14 Record #709

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
- Adjusted reference resolution for wizards using POJO [#706](https://github.com/epam/sap-commerce-intellij-idea-plugin/pull/706)
- Enabled field level reference resolution for wizards using POJO [#707](https://github.com/epam/sap-commerce-intellij-idea-plugin/pull/707)
- Enabled field level reference resolution for wizards using Java 14 Record [#708](https://github.com/epam/sap-commerce-intellij-idea-plugin/pull/708)
- Added field level code contribution for wizards using POJO & Java 14 Record [#709](https://github.com/epam/sap-commerce-intellij-idea-plugin/pull/709)

### Fixes
- Disable reference and code completion for `order` property of the Spring Interceptor bean declaration [#697](https://github.com/epam/sap-commerce-intellij-idea-plugin/pull/697)
Expand Down
49 changes: 49 additions & 0 deletions src/com/intellij/idea/plugin/hybris/java/psi/JavaPsiHelper.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* This file is part of "SAP Commerce Developers Toolset" plugin for Intellij IDEA.
* Copyright (C) 2019-2023 EPAM Systems <[email protected]> and contributors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

package com.intellij.idea.plugin.hybris.java.psi

import com.intellij.psi.PsiClass
import com.intellij.psi.PsiField
import com.intellij.psi.PsiModifier

object JavaPsiHelper {

fun hasGetter(psiClass: PsiClass, psiField: PsiField): Boolean {
val name = getFieldName(psiField)
val getterMethod = psiClass.findMethodsByName("get$name", false)
.any { it.hasModifierProperty(PsiModifier.PUBLIC) && it.returnType == psiField.type }

if (getterMethod) return true

return psiClass.findMethodsByName("is$name", false)
.any { it.hasModifierProperty(PsiModifier.PUBLIC) && it.returnType == psiField.type }
}

fun hasSetter(psiClass: PsiClass, psiField: PsiField): Boolean {
val name = getFieldName(psiField)
return psiClass.findMethodsByName("set$name", false)
.any {
it.hasModifierProperty(PsiModifier.PUBLIC)
&& it.parameterList.parameters.size == 1
&& it.parameterList.parameters[0].type == psiField.type
}
}

private fun getFieldName(psiField: PsiField) = psiField.name.replaceFirstChar { it.uppercase() }
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,15 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

package com.intellij.idea.plugin.hybris.psi.reference
package com.intellij.idea.plugin.hybris.java.psi.reference

import com.intellij.codeInsight.highlighting.HighlightedReference
import com.intellij.idea.plugin.hybris.common.HybrisConstants
import com.intellij.idea.plugin.hybris.java.psi.JavaPsiHelper
import com.intellij.openapi.util.TextRange
import com.intellij.psi.*
import com.intellij.psi.JavaPsiFacade
import com.intellij.psi.PsiElement
import com.intellij.psi.PsiReferenceBase
import com.intellij.psi.search.GlobalSearchScope

class JavaClassReference(element: PsiElement, private val className: String) : PsiReferenceBase<PsiElement>(element), HighlightedReference {
Expand All @@ -38,30 +41,8 @@ class JavaClassReference(element: PsiElement, private val className: String) : P
val field = psiClass.findFieldByName(value, false)
return@let if (psiClass.isRecord) field
else field
?.takeIf { hasGetter(psiClass, it) && hasSetter(psiClass, it) }
?.takeIf { JavaPsiHelper.hasGetter(psiClass, it) && JavaPsiHelper.hasSetter(psiClass, it) }
}
}

private fun hasGetter(psiClass: PsiClass, psiField: PsiField): Boolean {
val name = getFieldName(psiField)
val getterMethod = psiClass.findMethodsByName("get$name", false)
.any { it.hasModifierProperty(PsiModifier.PUBLIC) && it.returnType == psiField.type }

if (getterMethod) return true

return psiClass.findMethodsByName("is$name", false)
.any { it.hasModifierProperty(PsiModifier.PUBLIC) && it.returnType == psiField.type }
}

private fun hasSetter(psiClass: PsiClass, psiField: PsiField): Boolean {
val name = getFieldName(psiField)
return psiClass.findMethodsByName("set$name", false)
.any {
it.hasModifierProperty(PsiModifier.PUBLIC)
&& it.parameterList.parameters.size == 1
&& it.parameterList.parameters[0].type == psiField.type
}
}

private fun getFieldName(psiField: PsiField) = psiField.name.replaceFirstChar { it.uppercase() }
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,50 @@ package com.intellij.idea.plugin.hybris.system.cockpitng.codeInsight.completion.

import com.intellij.codeInsight.completion.CompletionParameters
import com.intellij.codeInsight.completion.CompletionProvider
import com.intellij.codeInsight.completion.CompletionResultSet
import com.intellij.codeInsight.completion.JavaLookupElementBuilder
import com.intellij.idea.plugin.hybris.common.HybrisConstants
import com.intellij.idea.plugin.hybris.java.psi.JavaPsiHelper
import com.intellij.idea.plugin.hybris.project.utils.PluginCommon
import com.intellij.idea.plugin.hybris.system.cockpitng.psi.CngPsiHelper
import com.intellij.idea.plugin.hybris.system.type.codeInsight.completion.provider.AttributeDeclarationCompletionProvider
import com.intellij.openapi.application.ApplicationManager
import com.intellij.openapi.project.Project
import com.intellij.psi.JavaPsiFacade
import com.intellij.psi.PsiElement
import com.intellij.psi.search.GlobalSearchScope
import com.intellij.util.ProcessingContext

class CngFlowPropertyQualifierCompletionProvider : AttributeDeclarationCompletionProvider() {

override fun resolveType(element: PsiElement) = CngPsiHelper.resolveContextTypeForNewItemInWizardFlow(element)

override fun addCompletions(parameters: CompletionParameters, context: ProcessingContext, result: CompletionResultSet) {
val project = parameters.editor.project ?: return
val type = resolveType(parameters.position) ?: return

if (type.contains(".")
&& type != HybrisConstants.COCKPIT_NG_INITIALIZE_CONTEXT_TYPE
&& PluginCommon.isPluginActive(PluginCommon.JAVA_PLUGIN_ID)
) addJavaPojoCompletions(project, type, result)
else super.addCompletions(parameters, context, result)
}

private fun addJavaPojoCompletions(project: Project, className: String, result: CompletionResultSet) {
JavaPsiFacade.getInstance(project)
.findClass(className, GlobalSearchScope.allScope(project))
?.let { psiClass ->
val fields = psiClass.fields

return@let if (psiClass.isRecord) fields.toList()
else fields
.filter { JavaPsiHelper.hasGetter(psiClass, it) && JavaPsiHelper.hasSetter(psiClass, it) }
}
?.map { JavaLookupElementBuilder.forField(it) }
?.forEach { result.addElement(it) }
}


companion object {
val instance: CompletionProvider<CompletionParameters> =
ApplicationManager.getApplication().getService(CngFlowPropertyQualifierCompletionProvider::class.java)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
package com.intellij.idea.plugin.hybris.system.cockpitng.psi.provider

import com.intellij.idea.plugin.hybris.common.HybrisConstants
import com.intellij.idea.plugin.hybris.java.psi.reference.JavaClassReference
import com.intellij.idea.plugin.hybris.project.utils.PluginCommon
import com.intellij.idea.plugin.hybris.psi.reference.JavaClassReference
import com.intellij.idea.plugin.hybris.system.cockpitng.psi.CngPsiHelper
import com.intellij.idea.plugin.hybris.system.cockpitng.psi.reference.CngFlowTSItemAttributeReference
import com.intellij.openapi.application.ApplicationManager
Expand Down