|
50 | 50 | import org.jetbrains.annotations.Nullable;
|
51 | 51 |
|
52 | 52 | import java.lang.reflect.Field;
|
| 53 | +import java.lang.reflect.InvocationTargetException; |
| 54 | +import java.lang.reflect.Method; |
53 | 55 | import java.util.ArrayList;
|
54 | 56 | import java.util.Arrays;
|
55 | 57 | import java.util.List;
|
@@ -156,14 +158,40 @@ protected RunContentDescriptor launch(@NotNull ExecutionEnvironment env) throws
|
156 | 158 | else {
|
157 | 159 | descriptor = new RunContentBuilder(result, env).showRunContent(env.getContentToReuse());
|
158 | 160 | }
|
| 161 | + |
| 162 | + // Add the device name for the run descriptor. |
| 163 | + // The descriptor shows the run configuration name (e.g., `main.dart`) by default; |
| 164 | + // adding the device name will help users identify the instance when trying to operate a specific one. |
| 165 | + final String nameWithDeviceName = descriptor.getDisplayName() + " (" + device.deviceName() + ")"; |
| 166 | + boolean displayNameUpdated = false; |
159 | 167 | try {
|
160 |
| - final Field f = descriptor.getClass().getDeclaredField("myDisplayName"); |
| 168 | + // Find "myDisplayNameView" for 2024+ builds. |
| 169 | + // https://github.com/JetBrains/intellij-community/blob/idea/241.14494.240/platform/execution/src/com/intellij/execution/ui/RunContentDescriptor.java#L33 |
| 170 | + final Field f = descriptor.getClass().getDeclaredField("myDisplayNameView"); |
161 | 171 | f.setAccessible(true);
|
162 |
| - f.set(descriptor, descriptor.getDisplayName() + " (" + device.deviceName() + ")"); |
| 172 | + Object viewInstance = f.get(descriptor); |
| 173 | + if (viewInstance != null) { |
| 174 | + final Method setValueMethod = viewInstance.getClass().getMethod("setValue", Object.class); |
| 175 | + setValueMethod.invoke(viewInstance, nameWithDeviceName); |
| 176 | + displayNameUpdated = true; |
| 177 | + } |
163 | 178 | }
|
164 |
| - catch (IllegalAccessException | NoSuchFieldException e) { |
| 179 | + catch (IllegalAccessException | InvocationTargetException | NoSuchFieldException | NoSuchMethodException e) { |
165 | 180 | LOG.info(e);
|
166 | 181 | }
|
| 182 | + if (!displayNameUpdated) { |
| 183 | + try { |
| 184 | + // Find "myDisplayName" for 2023 builds. |
| 185 | + // https://github.com/JetBrains/intellij-community/blob/idea/231.8109.175/platform/execution/src/com/intellij/execution/ui/RunContentDescriptor.java#L30 |
| 186 | + final Field f = descriptor.getClass().getDeclaredField("myDisplayName"); |
| 187 | + f.setAccessible(true); |
| 188 | + f.set(descriptor, nameWithDeviceName); |
| 189 | + } |
| 190 | + catch (IllegalAccessException | NoSuchFieldException e) { |
| 191 | + LOG.info(e); |
| 192 | + } |
| 193 | + } |
| 194 | + |
167 | 195 | return descriptor;
|
168 | 196 | }
|
169 | 197 |
|
|
0 commit comments