Skip to content

Commit

Permalink
chore: Do not use broken JBR in IntelliJ 2024.2.3 (#123)
Browse files Browse the repository at this point in the history
* chore: Do not use broken JBR in IntelliJ 2024.2.3

* Add missing file

* formatter

---------

Co-authored-by: marcin <[email protected]>
  • Loading branch information
Artur- and MarcinVaadin authored Oct 8, 2024
1 parent 099b184 commit 86ddfe2
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -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<Action> {
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)) }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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() }
}
}
}
17 changes: 17 additions & 0 deletions src/main/kotlin/com/vaadin/plugin/hotswapagent/JdkUtil.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -33,13 +34,27 @@ 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
}
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.
Expand Down Expand Up @@ -76,3 +91,5 @@ class JdkUtil {
}
}
}

class BrokenJbrException : Exception() {}

0 comments on commit 86ddfe2

Please sign in to comment.