From 86ddfe2c41da43f0f3ffa9409faa5eeffd040653 Mon Sep 17 00:00:00 2001 From: Artur Date: Tue, 8 Oct 2024 15:25:37 +0300 Subject: [PATCH] chore: Do not use broken JBR in IntelliJ 2024.2.3 (#123) * chore: Do not use broken JBR in IntelliJ 2024.2.3 * Add missing file * formatter --------- Co-authored-by: marcin --- .../plugin/hotswapagent/BadJBRFoundDialog.kt | 37 +++++++++++++++++++ .../HotswapAgentProgramPatcher.kt | 10 +++-- .../plugin/hotswapagent/HotswapAgentRunner.kt | 24 +++++++----- .../com/vaadin/plugin/hotswapagent/JdkUtil.kt | 17 +++++++++ 4 files changed, 76 insertions(+), 12 deletions(-) create mode 100644 src/main/kotlin/com/vaadin/plugin/hotswapagent/BadJBRFoundDialog.kt diff --git a/src/main/kotlin/com/vaadin/plugin/hotswapagent/BadJBRFoundDialog.kt b/src/main/kotlin/com/vaadin/plugin/hotswapagent/BadJBRFoundDialog.kt new file mode 100644 index 0000000..556a0c5 --- /dev/null +++ b/src/main/kotlin/com/vaadin/plugin/hotswapagent/BadJBRFoundDialog.kt @@ -0,0 +1,37 @@ +package com.vaadin.plugin.hotswapagent + +import com.intellij.openapi.ui.DialogWrapper +import javax.swing.Action +import javax.swing.JComponent +import javax.swing.JPanel +import javax.swing.JTextArea + +class BadJBRFoundDialog() : DialogWrapper(true) { + + private val message: String + + override fun createActions(): Array { + return arrayOf(myOKAction) + } + + init { + message = buildString { + append( + "HotswapAgent requires running with a JetBrains Runtime JDK which implements the low level hotswap functionality.\n\n") + append( + "Your IntelliJ IDEA installation includes a bundled JetBrains Runtime JDK which is known not work for this purpose.\n\n") + append("You can resolve this by one of the following:\n") + append("- Downgrade IntelliJ IDEA to version 2024.2.2 or earlier which bundle a working version\n") + append("- Once released, upgrade IntelliJ IDEA to a version newer than 2024.2.3\n") + append( + "- Download a newer JetBrains runtime from https://github.com/JetBrains/JetBrainsRuntime/releases and set your run configuration to use it.") + append("\n") + } + title = "Unable to Find a Suitable JDK" + init() + } + + override fun createCenterPanel(): JComponent { + return JPanel().apply { add(JTextArea(message)) } + } +} diff --git a/src/main/kotlin/com/vaadin/plugin/hotswapagent/HotswapAgentProgramPatcher.kt b/src/main/kotlin/com/vaadin/plugin/hotswapagent/HotswapAgentProgramPatcher.kt index 0726061..4a4bb41 100644 --- a/src/main/kotlin/com/vaadin/plugin/hotswapagent/HotswapAgentProgramPatcher.kt +++ b/src/main/kotlin/com/vaadin/plugin/hotswapagent/HotswapAgentProgramPatcher.kt @@ -29,9 +29,13 @@ class HotswapAgentProgramPatcher : JavaProgramPatcher() { } if (!JdkUtil.isJetbrainsRuntime(javaParameters.jdk)) { // Use the bundled Jetbrains Runtime - javaParameters.jdk = - JdkUtil.getCompatibleJetbrainsJdk(module) - ?: throw IllegalArgumentException("The bundled JBR is not compatible with the project JDK") + try { + javaParameters.jdk = + JdkUtil.getCompatibleJetbrainsJdk(module) + ?: throw IllegalArgumentException("The bundled JBR is not compatible with the project JDK") + } catch (e: BrokenJbrException) { + throw IllegalArgumentException("The bundled JBR is known to be broken") + } } val agentInHome = VaadinHomeUtil.getHotSwapAgentJar() diff --git a/src/main/kotlin/com/vaadin/plugin/hotswapagent/HotswapAgentRunner.kt b/src/main/kotlin/com/vaadin/plugin/hotswapagent/HotswapAgentRunner.kt index e7cedc5..52bde65 100644 --- a/src/main/kotlin/com/vaadin/plugin/hotswapagent/HotswapAgentRunner.kt +++ b/src/main/kotlin/com/vaadin/plugin/hotswapagent/HotswapAgentRunner.kt @@ -26,16 +26,22 @@ class HotswapAgentRunner : GenericDebuggerRunner() { val module = runProfile.configurationModule?.module ?: throw IllegalStateException("$runnerId needs a module") val javaParameters = javaCommandLine.javaParameters - val jdkOk = JdkUtil.isJetbrainsRuntime(javaParameters.jdk) || JdkUtil.getCompatibleJetbrainsJdk(module) != null - if (jdkOk) { - super.execute(environment) - } else { - val bundledJetbrainsJdk = JdkUtil.getSdkMajorVersion(JdkUtil.getBundledJetbrainsJdk()) - val projectSdkMajor = JdkUtil.getProjectSdkVersion(module) - - ApplicationManager.getApplication().invokeLater { - NoJBRFoundDialog(bundledJetbrainsJdk, projectSdkMajor).show() + try { + val jdkOk = + JdkUtil.isJetbrainsRuntime(javaParameters.jdk) || JdkUtil.getCompatibleJetbrainsJdk(module) != null + + if (jdkOk) { + super.execute(environment) + } else { + val bundledJetbrainsJdk = JdkUtil.getSdkMajorVersion(JdkUtil.getBundledJetbrainsJdk()) + val projectSdkMajor = JdkUtil.getProjectSdkVersion(module) + + ApplicationManager.getApplication().invokeLater { + NoJBRFoundDialog(bundledJetbrainsJdk, projectSdkMajor).show() + } } + } catch (e: BrokenJbrException) { + ApplicationManager.getApplication().invokeLater { BadJBRFoundDialog().show() } } } } diff --git a/src/main/kotlin/com/vaadin/plugin/hotswapagent/JdkUtil.kt b/src/main/kotlin/com/vaadin/plugin/hotswapagent/JdkUtil.kt index 21e7f3c..2b85a2a 100644 --- a/src/main/kotlin/com/vaadin/plugin/hotswapagent/JdkUtil.kt +++ b/src/main/kotlin/com/vaadin/plugin/hotswapagent/JdkUtil.kt @@ -8,6 +8,7 @@ import com.intellij.openapi.module.Module import com.intellij.openapi.projectRoots.JavaSdk import com.intellij.openapi.projectRoots.Sdk import com.intellij.openapi.roots.ProjectRootManager +import java.io.File import org.jetbrains.idea.maven.project.MavenProjectsManager import org.jetbrains.jps.model.java.JdkVersionDetector import org.jetbrains.plugins.gradle.util.GradleUtil @@ -33,6 +34,10 @@ class JdkUtil { JavaSdk.getInstance().getVersion(jbrSdk) ?: throw IllegalArgumentException("Unable to detect bundled sdk version") + if (isBrokenJbr(jbrSdk)) { + throw BrokenJbrException() + } + if (projectJavaVersion == null || bundledSdkVersion.maxLanguageLevel.toJavaVersion().isAtLeast(projectJavaVersion)) { return jbrSdk @@ -40,6 +45,16 @@ class JdkUtil { return null } + private fun isBrokenJbr(sdk: Sdk): Boolean { + val release = File(sdk.homePath, "release") + val version = sdk.versionString + if (version != null && version.contains("21.0.4") && release.exists()) { + return release.readText().contains("JAVA_RUNTIME_VERSION=\"21.0.4+13-b509.17\"") + } + + return false + } + private fun getProjectJavaVersion(module: Module): Int? { // If a target version is specified in Maven or Gradle, that defines the version needed // to run the app. @@ -76,3 +91,5 @@ class JdkUtil { } } } + +class BrokenJbrException : Exception() {}