Skip to content

Commit 4dbbd18

Browse files
fix: use reflection for Scala plugin SBT versions API compatibility
The Scala plugin for IntelliJ 2026.1 changed the SBT versions API. Use reflection to try new API first, fall back to old API for cross-version compatibility.
1 parent e7c547c commit 4dbbd18

1 file changed

Lines changed: 31 additions & 2 deletions

File tree

  • PluginsAndFeatures/azure-toolkit-for-intellij/azure-intellij-plugin-hdinsight/src/main/java/com/microsoft/azure/hdinsight/projects

PluginsAndFeatures/azure-toolkit-for-intellij/azure-intellij-plugin-hdinsight/src/main/java/com/microsoft/azure/hdinsight/projects/SbtVersionOptionsPanel.java

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,12 @@
88
import com.intellij.openapi.progress.ProgressManager;
99
import com.intellij.openapi.ui.ComboBox;
1010
import com.microsoft.azure.hdinsight.common.logger.ILogger;
11-
import org.jetbrains.plugins.scala.project.Versions;
11+
import scala.collection.immutable.Seq;
1212
import scala.reflect.ClassTag;
1313

1414
import javax.swing.*;
1515
import java.awt.*;
16+
import java.lang.reflect.Method;
1617

1718
public class SbtVersionOptionsPanel extends JPanel implements ILogger {
1819
private ComboBox sbtVersionComboBox;
@@ -36,7 +37,35 @@ public SbtVersionOptionsPanel() {
3637
public void updateSbtVersions() {
3738
final String[][] versions = new String[1][1];
3839
ProgressManager.getInstance().runProcess(() -> {
39-
versions[0] = (String[]) Versions.SBT$.MODULE$.loadVersionsWithProgress(null).versions().toArray(ClassTag.apply(String.class));
40+
try {
41+
// In newer Scala plugin versions, the API changed. Use reflection for cross-version compatibility.
42+
// Old: Versions.SBT$.MODULE$.loadVersionsWithProgress(null).versions() -> Seq<String>
43+
// New: Versions.loadSbtVersions(false, null) -> Seq<SbtVersion>
44+
final Class<?> versionsClass = Class.forName("org.jetbrains.plugins.scala.project.Versions");
45+
try {
46+
// Try new API first
47+
final Method loadSbtVersionsMethod = versionsClass.getMethod("loadSbtVersions", boolean.class, com.intellij.openapi.progress.ProgressIndicator.class);
48+
final Seq<?> sbtVersions = (Seq<?>) loadSbtVersionsMethod.invoke(null, false, null);
49+
final int size = sbtVersions.size();
50+
final String[] result = new String[size];
51+
for (int i = 0; i < size; i++) {
52+
result[i] = sbtVersions.apply(i).toString();
53+
}
54+
versions[0] = result;
55+
} catch (final NoSuchMethodException e) {
56+
// Fallback to old API
57+
final Class<?> versionsSbtClass = Class.forName("org.jetbrains.plugins.scala.project.Versions$SBT$");
58+
final Object module = versionsSbtClass.getField("MODULE$").get(null);
59+
final Method loadMethod = module.getClass().getMethod("loadVersionsWithProgress", com.intellij.openapi.progress.ProgressIndicator.class);
60+
final Object loadedVersions = loadMethod.invoke(module, (Object) null);
61+
final Method versionsMethod = loadedVersions.getClass().getMethod("versions");
62+
final Seq<String> versionSeq = (Seq<String>) versionsMethod.invoke(loadedVersions);
63+
versions[0] = (String[]) versionSeq.toArray(ClassTag.apply(String.class));
64+
}
65+
} catch (final Exception e) {
66+
log().warn("Failed to get SBT versions from scala plugin.", e);
67+
versions[0] = new String[0];
68+
}
4069
}, null);
4170

4271
for (String version : versions[0]) {

0 commit comments

Comments
 (0)