Skip to content

Commit 41befe0

Browse files
authored
[CQ] remove dead GRADLE_SYNC_TOPIC subscriptions (flutter#8218)
Back In 2019, we added some logic to reflectively access a not-yet public `"GRADLE_SYNC_TOPIC"` on `GradleSyncState` subscribing a listener that called `GradleUtils.checkDartSupport()` on all sync events. Sometime since then, the field has gone away entirely and we are not getting these notifications. What makes this interesting is how our use of reflection masked the failure. Luckily, an inspection flagged the unsafe check, spamming our logs at runtime: ![image](https://github.com/user-attachments/assets/0b42c065-0abc-4c43-8b5f-de3663096877) My first attempt was to fix the cast but I'm glad I embraced the yak shave. "Safe" reflective access in this case would still have obscured the unnecessariness of this code. --- - [x] I’ve reviewed the contributor guide and applied the relevant portions to this PR. <details> <summary>Contribution guidelines:</summary><br> - See our [contributor guide]([https://github.com/dart-lang/sdk/blob/main/CONTRIBUTING.md](https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#overview) for general expectations for PRs. - Larger or significant changes should be discussed in an issue before creating a PR. - Dart contributions to our repos should follow the [Dart style guide](https://dart.dev/guides/language/effective-dart) and use `dart format`. - Java and Kotlin contributions should strive to follow Java and Kotlin best practices ([discussion](flutter#8098)). </details>
1 parent 597a921 commit 41befe0

File tree

1 file changed

+5
-60
lines changed

1 file changed

+5
-60
lines changed

flutter-studio/src/io/flutter/utils/AddToAppUtils.java

Lines changed: 5 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55
*/
66
package io.flutter.utils;
77

8-
import com.android.tools.idea.gradle.project.sync.GradleSyncListener;
9-
import com.android.tools.idea.gradle.project.sync.GradleSyncState;
108
import com.intellij.debugger.engine.DebugProcess;
119
import com.intellij.debugger.engine.DebugProcessListener;
1210
import com.intellij.debugger.impl.DebuggerManagerListener;
@@ -23,23 +21,18 @@
2321
import com.intellij.util.ThreeState;
2422
import com.intellij.util.concurrency.AppExecutorUtil;
2523
import com.intellij.util.messages.MessageBusConnection;
26-
import com.intellij.util.messages.Topic;
2724
import io.flutter.FlutterUtils;
2825
import io.flutter.actions.AttachDebuggerAction;
2926
import io.flutter.dart.FlutterDartAnalysisServer;
3027
import io.flutter.pub.PubRoot;
3128
import io.flutter.run.SdkAttachConfig;
3229
import io.flutter.sdk.FlutterSdk;
33-
import org.jetbrains.annotations.NonNls;
3430
import org.jetbrains.annotations.NotNull;
3531
import org.jetbrains.annotations.Nullable;
3632

37-
import java.lang.reflect.Field;
38-
import java.lang.reflect.Modifier;
3933
import java.util.Collection;
4034
import java.util.List;
4135

42-
import static com.intellij.util.ReflectionUtil.findAssignableField;
4336
import static io.flutter.actions.AttachDebuggerAction.ATTACH_IS_ACTIVE;
4437
import static io.flutter.actions.AttachDebuggerAction.findRunConfig;
4538

@@ -49,13 +42,12 @@ private AddToAppUtils() {
4942
}
5043

5144
public static boolean initializeAndDetectFlutter(@NotNull Project project) {
45+
// In 2019, we added some logic to reflectively access a not-yet public GRADLE_SYNC_TOPIC on `GradleSyncState` subscribing
46+
// a listener that called `GradleUtils.checkDartSupport()` on all sync events.
47+
// Sometime since then, the field has gone away entirely and we are not getting these notifications.
48+
// TODO(pq): 5/23/25 Asses whether we want to try and restore this logic
49+
5250
MessageBusConnection connection = project.getMessageBus().connect(FlutterDartAnalysisServer.getInstance(project));
53-
// GRADLE_SYNC_TOPIC is not public in Android Studio 3.5. It is in 3.6. It isn't defined in 3.4.
54-
//noinspection unchecked
55-
Topic<GradleSyncListener> topic = getStaticFieldValue(GradleSyncState.class, Topic.class, "GRADLE_SYNC_TOPIC");
56-
if (topic != null) {
57-
connection.subscribe(topic, makeSyncListener());
58-
}
5951

6052
if (!FlutterModuleUtils.hasFlutterModule(project)) {
6153
connection.subscribe(ModuleListener.TOPIC, new ModuleListener() {
@@ -89,53 +81,6 @@ public void modulesAdded(@NotNull Project proj, @NotNull List<? extends Module>
8981
return true;
9082
}
9183

92-
// Derived from the method in ReflectionUtil, with the addition of setAccessible().
93-
@Nullable
94-
public static <T> T getStaticFieldValue(@NotNull Class<?> objectClass,
95-
@Nullable("null means any type") Class<T> fieldType,
96-
@NotNull @NonNls String fieldName) {
97-
try {
98-
final Field field = findAssignableField(objectClass, fieldType, fieldName);
99-
if (!Modifier.isStatic(field.getModifiers())) {
100-
throw new IllegalArgumentException("Field " + objectClass + "." + fieldName + " is not static");
101-
}
102-
field.setAccessible(true);
103-
104-
var value = field.get(null);
105-
if (value == null) return null;
106-
107-
if (fieldType.isInstance(value)) {
108-
return fieldType.cast(value); // Type-safe cast
109-
}
110-
111-
// If `fieldType` is too broad, we might get here.
112-
return null;
113-
}
114-
catch (NoSuchFieldException | IllegalAccessException e) {
115-
return null;
116-
}
117-
}
118-
119-
@NotNull
120-
private static GradleSyncListener makeSyncListener() {
121-
return new GradleSyncListener() {
122-
@Override
123-
public void syncSucceeded(@NotNull Project project) {
124-
GradleUtils.checkDartSupport(project);
125-
}
126-
127-
@Override
128-
public void syncFailed(@NotNull Project project, @NotNull String errorMessage) {
129-
GradleUtils.checkDartSupport(project);
130-
}
131-
132-
@Override
133-
public void syncSkipped(@NotNull Project project) {
134-
GradleUtils.checkDartSupport(project);
135-
}
136-
};
137-
}
138-
13984
@NotNull
14085
private static DebuggerManagerListener makeAddToAppAttachListener(@NotNull Project project) {
14186
return new DebuggerManagerListener() {

0 commit comments

Comments
 (0)