|
| 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 com.intellij.execution.ExecutionException; |
| 9 | +import com.intellij.execution.runners.ExecutionEnvironment; |
| 10 | +import com.intellij.execution.ui.RunContentDescriptor; |
| 11 | +import com.intellij.xdebugger.XDebugProcessStarter; |
| 12 | +import com.intellij.xdebugger.XDebugSession; |
| 13 | +import com.intellij.xdebugger.XDebuggerManager; |
| 14 | +import org.jetbrains.annotations.NotNull; |
| 15 | + |
| 16 | +import java.lang.reflect.InvocationTargetException; |
| 17 | +import java.lang.reflect.Method; |
| 18 | + |
| 19 | +// TODO(helin24): This class is using reflection to find experimental APIs that are only present in platform versions 2025.3+. We should be |
| 20 | +// able to use the APIs directly once we are only supporting versions past 2025.3. |
| 21 | +// See https://github.com/flutter/flutter-intellij/issues/8879. |
| 22 | +public class FlutterDebugSessionUtils { |
| 23 | + |
| 24 | + private static final Method newSessionBuilderMethod; |
| 25 | + private static final Method environmentMethod; |
| 26 | + private static final Method startSessionMethod; |
| 27 | + |
| 28 | + static { |
| 29 | + Method nsb = null; |
| 30 | + Method env = null; |
| 31 | + Method ss = null; |
| 32 | + try { |
| 33 | + nsb = XDebuggerManager.class.getMethod("newSessionBuilder", XDebugProcessStarter.class); |
| 34 | + Class<?> builderClass = nsb.getReturnType(); |
| 35 | + env = builderClass.getMethod("environment", ExecutionEnvironment.class); |
| 36 | + ss = builderClass.getMethod("startSession"); |
| 37 | + } catch (NoSuchMethodException e) { |
| 38 | + // Fallback for older platforms |
| 39 | + } |
| 40 | + newSessionBuilderMethod = nsb; |
| 41 | + environmentMethod = env; |
| 42 | + startSessionMethod = ss; |
| 43 | + } |
| 44 | + |
| 45 | + public static @NotNull RunContentDescriptor startSessionAndGetDescriptor( |
| 46 | + @NotNull XDebuggerManager manager, |
| 47 | + @NotNull ExecutionEnvironment env, |
| 48 | + @NotNull XDebugProcessStarter starter, |
| 49 | + boolean muteBreakpoints) throws ExecutionException { |
| 50 | + try { |
| 51 | + if (newSessionBuilderMethod == null) { |
| 52 | + throw new NoSuchMethodException("newSessionBuilder is not available"); |
| 53 | + } |
| 54 | + Object builder = newSessionBuilderMethod.invoke(manager, starter); |
| 55 | + builder = environmentMethod.invoke(builder, env); |
| 56 | + Object sessionResult = startSessionMethod.invoke(builder); |
| 57 | + |
| 58 | + if (muteBreakpoints) { |
| 59 | + Method getSessionMethod = sessionResult.getClass().getMethod("getSession"); |
| 60 | + XDebugSession session = (XDebugSession) getSessionMethod.invoke(sessionResult); |
| 61 | + session.setBreakpointMuted(true); |
| 62 | + } |
| 63 | + |
| 64 | + Method getDescriptorMethod = sessionResult.getClass().getMethod("getRunContentDescriptor"); |
| 65 | + return (RunContentDescriptor) getDescriptorMethod.invoke(sessionResult); |
| 66 | + |
| 67 | + } catch (NoSuchMethodException e) { |
| 68 | + // Fallback to old API for 2025.1 and older |
| 69 | + XDebugSession session = manager.startSession(env, starter); |
| 70 | + if (muteBreakpoints) { |
| 71 | + session.setBreakpointMuted(true); |
| 72 | + } |
| 73 | + return session.getRunContentDescriptor(); |
| 74 | + } catch (InvocationTargetException e) { |
| 75 | + Throwable cause = e.getCause(); |
| 76 | + if (cause instanceof ExecutionException) { |
| 77 | + throw (ExecutionException) cause; |
| 78 | + } |
| 79 | + throw new ExecutionException("Failed to start debug session via reflection", cause != null ? cause : e); |
| 80 | + } catch (Exception e) { |
| 81 | + throw new ExecutionException("Failed with unexpected reflection error", e); |
| 82 | + } |
| 83 | + } |
| 84 | +} |
0 commit comments