Skip to content

Commit 200edc9

Browse files
authored
Add test asserting display name setter is accessible on current IntelliJ build (#8809)
Follow-up to #8796. Extracts the reflection lookup into a `@VisibleForTesting getDisplaySetter()` method and adds a test asserting it returns non-null. This serves as a regression guard: if JetBrains removes or renames `setDisplayName` in a future build, the test will fail loudly rather than the feature silently breaking for users (which was the original failure mode in #8795). Verified locally by running the test against the real SDK (passes), then temporarily corrupting the method name to `setDisplayName_doesNotExist` (fails with the expected assertion message). I've reviewed the contributor guide and applied the relevant portions to this PR.
1 parent be8bcce commit 200edc9

2 files changed

Lines changed: 53 additions & 8 deletions

File tree

src/io/flutter/run/LaunchState.java

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@
6060
import java.util.ArrayList;
6161
import java.util.Arrays;
6262
import java.util.List;
63+
import java.util.function.BiConsumer;
64+
import org.jetbrains.annotations.VisibleForTesting;
6365

6466
/**
6567
* Launches a flutter app, showing it in the console.
@@ -168,20 +170,42 @@ protected RunContentDescriptor launch(@NotNull ExecutionEnvironment env) throws
168170
// The descriptor shows the run configuration name (e.g., `main.dart`) by default;
169171
// adding the device name will help users identify the instance when trying to operate a specific one.
170172
final String nameWithDeviceName = descriptor.getDisplayName() + " (" + device.deviceName() + ")";
171-
172-
try {
173-
// RunContentDescriptor.setDisplayName() is protected, so we use reflection to call it.
174-
final Method setDisplayName = RunContentDescriptor.class.getDeclaredMethod("setDisplayName", String.class);
175-
setDisplayName.setAccessible(true);
176-
setDisplayName.invoke(descriptor, nameWithDeviceName);
173+
final BiConsumer<RunContentDescriptor, String> setter = getDisplaySetter();
174+
if (setter != null) {
175+
setter.accept(descriptor, nameWithDeviceName);
177176
}
178-
catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
179-
FlutterUtils.info(LOG, "Error setting display name", e, true);
177+
else {
178+
FlutterUtils.info(LOG, "Could not find a way to set run tab display name", null, true);
180179
}
181180

182181
return descriptor;
183182
}
184183

184+
/**
185+
* Returns a function that sets the display name on a {@link RunContentDescriptor}, or null if unavailable.
186+
* Uses reflection to call the protected {@code setDisplayName()} method.
187+
* Exposed for testing to verify that the method is accessible on the current IntelliJ build.
188+
*/
189+
@VisibleForTesting
190+
@Nullable
191+
static BiConsumer<RunContentDescriptor, String> getDisplaySetter() {
192+
try {
193+
final Method m = RunContentDescriptor.class.getDeclaredMethod("setDisplayName", String.class);
194+
m.setAccessible(true);
195+
return (descriptor, name) -> {
196+
try {
197+
m.invoke(descriptor, name);
198+
}
199+
catch (IllegalAccessException | InvocationTargetException e) {
200+
FlutterUtils.info(LOG, "Error setting display name", e, true);
201+
}
202+
};
203+
}
204+
catch (NoSuchMethodException e) {
205+
return null;
206+
}
207+
}
208+
185209
private static Class classForName(String className) {
186210
try {
187211
return Class.forName(className);
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/*
2+
* Copyright 2026 The Chromium Authors. All rights reserved.
3+
* Use of this source code is governed by a BSD-style license that can be
4+
* found in the LICENSE file.
5+
*/
6+
package io.flutter.run;
7+
8+
import org.junit.Test;
9+
10+
import static org.junit.Assert.assertNotNull;
11+
12+
public class LaunchStateTest {
13+
14+
@Test
15+
public void displaySetterIsAvailable() {
16+
assertNotNull(
17+
"setDisplayName not found on RunContentDescriptor — JetBrains may have removed or renamed it",
18+
LaunchState.getDisplaySetter()
19+
);
20+
}
21+
}

0 commit comments

Comments
 (0)