Skip to content

Commit 4637dcd

Browse files
author
Sergey Mashkov
committed
EA-86479 - ISE: PomFile.<init>
don't apply any inspections for inappropriate pom files
1 parent ff9fe85 commit 4637dcd

File tree

8 files changed

+27
-27
lines changed

8 files changed

+27
-27
lines changed

idea/idea-maven/src/org/jetbrains/kotlin/idea/maven/PomFile.kt

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,9 @@ import java.util.*
4444
fun kotlinPluginId(version: String?) = MavenId(KotlinMavenConfigurator.GROUP_ID, KotlinMavenConfigurator.MAVEN_PLUGIN_ID, version)
4545

4646

47-
class PomFile(val xmlFile: XmlFile) {
48-
val domModel = MavenDomUtil.getMavenDomProjectModel(xmlFile.project, xmlFile.virtualFile) ?: throw IllegalStateException("No DOM model found for pom ${xmlFile.name}")
47+
class PomFile private constructor(val xmlFile: XmlFile, val domModel: MavenDomProjectModel) {
48+
constructor(xmlFile: XmlFile) : this(xmlFile, MavenDomUtil.getMavenDomProjectModel(xmlFile.project, xmlFile.virtualFile) ?: throw IllegalStateException("No DOM model found for pom ${xmlFile.name}"))
49+
4950
private val nodesByName = HashMap<String, XmlTag>()
5051
private val projectElement: XmlTag
5152

@@ -483,6 +484,8 @@ class PomFile(val xmlFile: XmlFile) {
483484
}
484485

485486
companion object {
487+
fun forFileOrNull(xmlFile: XmlFile): PomFile? = MavenDomUtil.getMavenDomProjectModel(xmlFile.project, xmlFile.virtualFile)?.let { PomFile(xmlFile, it) }
488+
486489
@Deprecated("We shouldn't use phase but additional compiler configuration in most cases")
487490
fun getPhase(hasJavaFiles: Boolean, isTest: Boolean) = when {
488491
hasJavaFiles -> when {

idea/idea-maven/src/org/jetbrains/kotlin/idea/maven/actions/MavenPluginSourcesSwap.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ class MavenPluginSourcesMoveToExecutionIntention : PsiElementBaseIntentionAction
5454
return false
5555
}
5656

57-
val pom = PomFile(file as XmlFile)
57+
val pom = PomFile.forFileOrNull(file as XmlFile) ?: return false
5858
if (domElement.getParentOfType(MavenDomBuild::class.java, false)?.sourceDirectory === domElement) {
5959
return pom.findKotlinExecutions(PomFile.KotlinGoals.Compile, PomFile.KotlinGoals.Js).isNotEmpty()
6060
}
@@ -69,7 +69,7 @@ class MavenPluginSourcesMoveToExecutionIntention : PsiElementBaseIntentionAction
6969

7070
override fun invoke(project: Project, editor: Editor, element: PsiElement) {
7171
val xmlFile = element.containingFile as? XmlFile ?: return
72-
val pomFile = PomFile(xmlFile)
72+
val pomFile = PomFile.forFileOrNull(xmlFile) ?: return
7373

7474
val tag = element.getParentOfType<XmlTag>(false) ?: return
7575
val domElement = DomManager.getDomManager(project).getDomElement(tag) as? GenericDomValue<*> ?: return
@@ -126,7 +126,7 @@ class MavenPluginSourcesMoveToBuild : PsiElementBaseIntentionAction() {
126126
.filterIsInstance<XmlTag>()
127127
.firstOrNull { it.localName == "sourceDirs" } ?: return false
128128

129-
val pom = PomFile(element.containingFile as XmlFile)
129+
val pom = PomFile.forFileOrNull(element.containingFile as XmlFile) ?: return false
130130
val sourceDirsToMove = pom.executionSourceDirs(execution)
131131

132132
if (sourceDirsToMove.size != 1) {

idea/idea-maven/src/org/jetbrains/kotlin/idea/maven/actions/MavenPomActions.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ private class KotlinMavenPluginProvider : AbstractDomGenerateProvider<MavenDomPl
6161
else -> knownVersion
6262
}
6363

64-
val pom = PomFile(DomUtil.getFile(parent))
64+
val pom = PomFile.forFileOrNull(DomUtil.getFile(parent)) ?: return null
6565
return pom.addPlugin(MavenId(KotlinMavenConfigurator.GROUP_ID, KotlinMavenConfigurator.MAVEN_PLUGIN_ID, version))
6666
}
6767

@@ -78,7 +78,7 @@ private class KotlinMavenPluginProvider : AbstractDomGenerateProvider<MavenDomPl
7878
override fun isAvailableForElement(contextElement: DomElement): Boolean {
7979
val parent = contextElement.findProject() ?: return false
8080

81-
return parent.build.plugins.plugins.none { plugin -> plugin.isKotlinMavenPlugin() }
81+
return parent.build.plugins.plugins.none(MavenDomPlugin::isKotlinMavenPlugin)
8282
}
8383
}
8484

@@ -89,7 +89,7 @@ private class KotlinMavenExecutionProvider(val goal: String, val phase: String)
8989
return null
9090
}
9191

92-
val file = PomFile(DomUtil.getFile(parent))
92+
val file = PomFile.forFileOrNull(DomUtil.getFile(parent)) ?: return null
9393
val execution = file.addExecution(parent, goal, phase, listOf(goal))
9494

9595
if (editor != null) {

idea/idea-maven/src/org/jetbrains/kotlin/idea/maven/configuration/KotlinMavenConfigurator.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ abstract class KotlinMavenConfigurator
6868

6969
private fun checkKotlinPlugin(module: Module): ConfigureKotlinStatus {
7070
val psi = findModulePomFile(module) as? XmlFile ?: return ConfigureKotlinStatus.BROKEN
71-
val pom = PomFile(psi)
71+
val pom = PomFile.forFileOrNull(psi) ?: return ConfigureKotlinStatus.NON_APPLICABLE
7272

7373
if (hasKotlinPlugin(pom)) {
7474
return ConfigureKotlinStatus.CONFIGURED
@@ -137,7 +137,7 @@ abstract class KotlinMavenConfigurator
137137
return
138138
}
139139

140-
val pom = PomFile(file as XmlFile)
140+
val pom = PomFile.forFileOrNull(file as XmlFile) ?: return
141141
pom.addProperty(KOTLIN_VERSION_PROPERTY, version)
142142

143143
pom.addDependency(MavenId(GROUP_ID, getStdlibArtifactId(module), "\${$KOTLIN_VERSION_PROPERTY}"), MavenArtifactScope.COMPILE, null, false, null)

idea/idea-maven/src/org/jetbrains/kotlin/idea/maven/inspections/DifferentKotlinMavenVersionInspection.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ class DifferentKotlinMavenVersionInspection : DomElementsInspection<MavenDomProj
4545
return
4646
}
4747

48-
val pomFile = PomFile(domFileElement.file)
48+
val pomFile = PomFile.forFileOrNull(domFileElement.file) ?: return
4949
pomFile.findKotlinPlugins().filter { it.version.exists() && it.version.stringValue != idePluginVersion }.forEach { plugin ->
5050
createProblem(holder, plugin)
5151
}

idea/idea-maven/src/org/jetbrains/kotlin/idea/maven/inspections/DifferentMavenStdlibVersionInspection.kt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ import org.jetbrains.idea.maven.model.MavenId
3030
import org.jetbrains.idea.maven.project.MavenProject
3131
import org.jetbrains.idea.maven.project.MavenProjectsManager
3232
import org.jetbrains.kotlin.idea.maven.PomFile
33-
import org.jetbrains.kotlin.idea.maven.configuration.KotlinJavaMavenConfigurator
3433
import org.jetbrains.kotlin.idea.maven.configuration.KotlinMavenConfigurator
3534
import org.jetbrains.kotlin.idea.versions.MAVEN_STDLIB_ID
3635

@@ -52,7 +51,7 @@ class DifferentMavenStdlibVersionInspection : DomElementsInspection<MavenDomProj
5251
return
5352
}
5453

55-
val pomFile = PomFile(file)
54+
val pomFile = PomFile.forFileOrNull(file) ?: return
5655
pomFile.findKotlinPlugins().filter { it.version.stringValue != stdlibVersion.singleOrNull() }.forEach { plugin ->
5756
val fixes = plugin.version.stringValue?.let { version ->
5857
createFixes(project, plugin.version, stdlibVersion + version)

idea/idea-maven/src/org/jetbrains/kotlin/idea/maven/inspections/KotlinMavenPluginPhaseInspection.kt

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ class KotlinMavenPluginPhaseInspection : DomElementsInspection<MavenDomProjectMo
5454
val manager = MavenProjectsManager.getInstance(module.project)
5555
val mavenProject = manager.findProject(module) ?: return
5656

57-
val pom = PomFile(domFileElement.file)
57+
val pom = PomFile.forFileOrNull(domFileElement.file) ?: return
5858
val hasJavaFiles = module.hasJavaFiles()
5959

6060
// all executions including inherited
@@ -167,9 +167,7 @@ class KotlinMavenPluginPhaseInspection : DomElementsInspection<MavenDomProjectMo
167167
override fun getFamilyName() = "Create kotlin execution"
168168

169169
override fun applyFix(project: Project, descriptor: ProblemDescriptor) {
170-
val pom = PomFile(file)
171-
172-
pom.addKotlinExecution(module, kotlinPlugin, goal, PomFile.getPhase(module.hasJavaFiles(), false), false, listOf(goal))
170+
PomFile.forFileOrNull(file)?.addKotlinExecution(module, kotlinPlugin, goal, PomFile.getPhase(module.hasJavaFiles(), false), false, listOf(goal))
173171
}
174172
}
175173

@@ -188,7 +186,7 @@ class KotlinMavenPluginPhaseInspection : DomElementsInspection<MavenDomProjectMo
188186
override fun getFamilyName() = getName()
189187

190188
override fun applyFix(project: Project, descriptor: ProblemDescriptor) {
191-
PomFile(file).addJavacExecutions(module, kotlinPlugin)
189+
PomFile.forFileOrNull(file)?.addJavacExecutions(module, kotlinPlugin)
192190
}
193191
}
194192

@@ -197,8 +195,7 @@ class KotlinMavenPluginPhaseInspection : DomElementsInspection<MavenDomProjectMo
197195
override fun getFamilyName() = "Add dependency"
198196

199197
override fun applyFix(project: Project, descriptor: ProblemDescriptor) {
200-
val file = PomFile(pomFile)
201-
file.addDependency(MavenId(KotlinMavenConfigurator.GROUP_ID, id, version), MavenArtifactScope.COMPILE)
198+
PomFile.forFileOrNull(pomFile)?.addDependency(MavenId(KotlinMavenConfigurator.GROUP_ID, id, version), MavenArtifactScope.COMPILE)
202199
}
203200
}
204201

@@ -207,9 +204,10 @@ class KotlinMavenPluginPhaseInspection : DomElementsInspection<MavenDomProjectMo
207204
override fun getFamilyName() = "Create kotlin execution"
208205

209206
override fun applyFix(project: Project, descriptor: ProblemDescriptor) {
210-
val pom = PomFile(xmlFile)
211-
val plugin = pom.addKotlinPlugin(version)
212-
pom.addKotlinExecution(module, plugin, "compile", PomFile.getPhase(module.hasJavaFiles(), false), false, listOf(goal))
207+
PomFile.forFileOrNull(xmlFile)?.let { pom ->
208+
val plugin = pom.addKotlinPlugin(version)
209+
pom.addKotlinExecution(module, plugin, "compile", PomFile.getPhase(module.hasJavaFiles(), false), false, listOf(goal))
210+
}
213211
}
214212
}
215213
}

idea/idea-maven/src/org/jetbrains/kotlin/idea/maven/inspections/KotlinMavenUnresolvedReferenceQuickFixProvider.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -100,10 +100,10 @@ class AddMavenDependencyQuickFix(val className: String, val smartPsiElementPoint
100100
val isTestSource = ProjectRootManager.getInstance(project).fileIndex.isInTestSourceContent(virtualFile)
101101
val scope = if (isTestSource) MavenArtifactScope.TEST else null
102102

103-
val pom = PomFile(xmlFile)
104-
105-
ids.forEach {
106-
pom.addDependency(it, scope)
103+
PomFile.forFileOrNull(xmlFile)?.let { pom ->
104+
ids.forEach {
105+
pom.addDependency(it, scope)
106+
}
107107
}
108108
}
109109
}

0 commit comments

Comments
 (0)