diff --git a/flutter-idea/src/io/flutter/run/LaunchState.java b/flutter-idea/src/io/flutter/run/LaunchState.java index d3fe9a66bb..1a3727e8ea 100644 --- a/flutter-idea/src/io/flutter/run/LaunchState.java +++ b/flutter-idea/src/io/flutter/run/LaunchState.java @@ -50,6 +50,8 @@ import org.jetbrains.annotations.Nullable; import java.lang.reflect.Field; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; import java.util.ArrayList; import java.util.Arrays; import java.util.List; @@ -156,14 +158,40 @@ protected RunContentDescriptor launch(@NotNull ExecutionEnvironment env) throws else { descriptor = new RunContentBuilder(result, env).showRunContent(env.getContentToReuse()); } + + // Add the device name for the run descriptor. + // The descriptor shows the run configuration name (e.g., `main.dart`) by default; + // adding the device name will help users identify the instance when trying to operate a specific one. + final String nameWithDeviceName = descriptor.getDisplayName() + " (" + device.deviceName() + ")"; + boolean displayNameUpdated = false; try { - final Field f = descriptor.getClass().getDeclaredField("myDisplayName"); + // Find "myDisplayNameView" for 2024+ builds. + // https://github.com/JetBrains/intellij-community/blob/idea/241.14494.240/platform/execution/src/com/intellij/execution/ui/RunContentDescriptor.java#L33 + final Field f = descriptor.getClass().getDeclaredField("myDisplayNameView"); f.setAccessible(true); - f.set(descriptor, descriptor.getDisplayName() + " (" + device.deviceName() + ")"); + Object viewInstance = f.get(descriptor); + if (viewInstance != null) { + final Method setValueMethod = viewInstance.getClass().getMethod("setValue", Object.class); + setValueMethod.invoke(viewInstance, nameWithDeviceName); + displayNameUpdated = true; + } } - catch (IllegalAccessException | NoSuchFieldException e) { + catch (IllegalAccessException | InvocationTargetException | NoSuchFieldException | NoSuchMethodException e) { LOG.info(e); } + if (!displayNameUpdated) { + try { + // Find "myDisplayName" for 2023 builds. + // https://github.com/JetBrains/intellij-community/blob/idea/231.8109.175/platform/execution/src/com/intellij/execution/ui/RunContentDescriptor.java#L30 + final Field f = descriptor.getClass().getDeclaredField("myDisplayName"); + f.setAccessible(true); + f.set(descriptor, nameWithDeviceName); + } + catch (IllegalAccessException | NoSuchFieldException e) { + LOG.info(e); + } + } + return descriptor; }