Skip to content

Commit 40b6325

Browse files
authored
Refator IDebugSession & IProvider (microsoft#91)
1 parent f612bdf commit 40b6325

File tree

12 files changed

+63
-64
lines changed

12 files changed

+63
-64
lines changed

com.microsoft.java.debug.core/src/main/java/com/microsoft/java/debug/core/DebugSession.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,12 +78,12 @@ public void terminate() {
7878

7979
@Override
8080
public IBreakpoint createBreakpoint(String className, int lineNumber) {
81-
return new Breakpoint(vm, this.eventHub(), className, lineNumber);
81+
return new Breakpoint(vm, this.getEventHub(), className, lineNumber);
8282
}
8383

8484
@Override
8585
public IBreakpoint createBreakpoint(String className, int lineNumber, int hitCount) {
86-
return new Breakpoint(vm, this.eventHub(), className, lineNumber, hitCount);
86+
return new Breakpoint(vm, this.getEventHub(), className, lineNumber, hitCount);
8787
}
8888

8989
@Override
@@ -102,12 +102,17 @@ public Process process() {
102102
}
103103

104104
@Override
105-
public List<ThreadReference> allThreads() {
105+
public List<ThreadReference> getAllThreads() {
106106
return vm.allThreads();
107107
}
108108

109109
@Override
110-
public IEventHub eventHub() {
110+
public IEventHub getEventHub() {
111111
return eventHub;
112112
}
113+
114+
@Override
115+
public VirtualMachine getVM() {
116+
return vm;
117+
}
113118
}

com.microsoft.java.debug.core/src/main/java/com/microsoft/java/debug/core/DebugUtility.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ public static ThreadReference getThread(IDebugSession debugSession, long threadI
228228
public static List<ThreadReference> getAllThreadsSafely(IDebugSession debugSession) {
229229
if (debugSession != null) {
230230
try {
231-
return debugSession.allThreads();
231+
return debugSession.getAllThreads();
232232
} catch (VMDisconnectedException ex) {
233233
// do nothing.
234234
}

com.microsoft.java.debug.core/src/main/java/com/microsoft/java/debug/core/IDebugSession.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import java.util.List;
1515

1616
import com.sun.jdi.ThreadReference;
17+
import com.sun.jdi.VirtualMachine;
1718

1819
public interface IDebugSession {
1920
void start();
@@ -37,8 +38,9 @@ public interface IDebugSession {
3738

3839
Process process();
3940

40-
List<ThreadReference> allThreads();
41+
List<ThreadReference> getAllThreads();
4142

42-
IEventHub eventHub();
43-
}
43+
IEventHub getEventHub();
4444

45+
VirtualMachine getVM();
46+
}

com.microsoft.java.debug.core/src/main/java/com/microsoft/java/debug/core/adapter/IProvider.java

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,24 +11,18 @@
1111

1212
package com.microsoft.java.debug.core.adapter;
1313

14-
import java.util.HashMap;
1514
import java.util.Map;
1615

16+
import com.microsoft.java.debug.core.IDebugSession;
17+
1718
public interface IProvider {
1819
/**
1920
* Initialize this provider.
20-
* @param options the options
21-
*/
22-
default void initialize(Map<String, Object> options) {
23-
24-
}
25-
26-
/**
27-
* Get the default options for this provider.
28-
*
29-
* @return The default options.
21+
* @param debugSession
22+
* The associated debug session
23+
* @param options
24+
* the options
3025
*/
31-
default Map<String, Object> getDefaultOptions() {
32-
return new HashMap<>();
26+
default void initialize(IDebugSession debugSession, Map<String, Object> options) {
3327
}
3428
}

com.microsoft.java.debug.core/src/main/java/com/microsoft/java/debug/core/adapter/handler/AttachRequestHandler.java

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import java.io.IOException;
1515
import java.nio.charset.StandardCharsets;
1616
import java.util.Arrays;
17+
import java.util.HashMap;
1718
import java.util.List;
1819
import java.util.Map;
1920
import java.util.logging.Logger;
@@ -50,25 +51,25 @@ public void handle(Command command, Arguments arguments, Response response, IDeb
5051
context.setDebuggeeEncoding(StandardCharsets.UTF_8); // Use UTF-8 as debuggee's default encoding format.
5152

5253
IVirtualMachineManagerProvider vmProvider = context.getProvider(IVirtualMachineManagerProvider.class);
53-
ISourceLookUpProvider sourceProvider = context.getProvider(ISourceLookUpProvider.class);
54-
Map<String, Object> options = sourceProvider.getDefaultOptions();
55-
options.put(Constants.DEBUGGEE_ENCODING, context.getDebuggeeEncoding());
56-
if (attachArguments.projectName != null) {
57-
options.put(Constants.PROJECTNAME, attachArguments.projectName);
58-
}
59-
sourceProvider.initialize(options);
6054

6155
try {
62-
logger.info(String.format("Trying to attach to remote debuggee VM %s:%d .",
63-
attachArguments.hostName, attachArguments.port));
64-
IDebugSession debugSession = DebugUtility.attach(vmProvider.getVirtualMachineManager(),
65-
attachArguments.hostName, attachArguments.port, attachArguments.timeout);
56+
logger.info(String.format("Trying to attach to remote debuggee VM %s:%d .", attachArguments.hostName, attachArguments.port));
57+
IDebugSession debugSession = DebugUtility.attach(vmProvider.getVirtualMachineManager(), attachArguments.hostName, attachArguments.port,
58+
attachArguments.timeout);
6659
context.setDebugSession(debugSession);
6760
logger.info("Attaching to debuggee VM succeeded.");
6861
} catch (IOException | IllegalConnectorArgumentsException e) {
6962
AdapterUtils.setErrorResponse(response, ErrorCode.ATTACH_FAILURE,
7063
String.format("Failed to attach to remote debuggee VM. Reason: %s", e.toString()));
7164
}
65+
66+
Map<String, Object> options = new HashMap<>();
67+
options.put(Constants.DEBUGGEE_ENCODING, context.getDebuggeeEncoding());
68+
if (attachArguments.projectName != null) {
69+
options.put(Constants.PROJECTNAME, attachArguments.projectName);
70+
}
71+
ISourceLookUpProvider sourceProvider = context.getProvider(ISourceLookUpProvider.class);
72+
sourceProvider.initialize(context.getDebugSession(), options);
7273
}
7374

7475
}

com.microsoft.java.debug.core/src/main/java/com/microsoft/java/debug/core/adapter/handler/ConfigurationDoneRequestHandler.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public void handle(Command command, Arguments arguments, Response response, IDeb
4848
IDebugSession debugSession = context.getDebugSession();
4949
if (debugSession != null) {
5050
// This is a global event handler to handle the JDI Event from Virtual Machine.
51-
debugSession.eventHub().events().subscribe(debugEvent -> {
51+
debugSession.getEventHub().events().subscribe(debugEvent -> {
5252
handleDebugEvent(debugEvent, debugSession, context);
5353
});
5454
// configuration is done, and start debug session.
@@ -72,7 +72,7 @@ private void handleDebugEvent(DebugEvent debugEvent, IDebugSession debugSession,
7272
context.sendEventAsync(new Events.TerminatedEvent());
7373
// Terminate eventHub thread.
7474
try {
75-
debugSession.eventHub().close();
75+
debugSession.getEventHub().close();
7676
} catch (Exception e) {
7777
// do nothing.
7878
}

com.microsoft.java.debug.core/src/main/java/com/microsoft/java/debug/core/adapter/handler/LaunchRequestHandler.java

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -87,13 +87,7 @@ public void handle(Command command, Arguments arguments, Response response, IDeb
8787

8888

8989
IVirtualMachineManagerProvider vmProvider = context.getProvider(IVirtualMachineManagerProvider.class);
90-
ISourceLookUpProvider sourceProvider = context.getProvider(ISourceLookUpProvider.class);
91-
Map<String, Object> options = sourceProvider.getDefaultOptions();
92-
options.put(Constants.DEBUGGEE_ENCODING, context.getDebuggeeEncoding());
93-
if (launchArguments.projectName != null) {
94-
options.put(Constants.PROJECTNAME, launchArguments.projectName);
95-
}
96-
sourceProvider.initialize(options);
90+
9791

9892
// Append environment to native environment.
9993
String[] envVars = null;
@@ -142,5 +136,13 @@ public void handle(Command command, Arguments arguments, Response response, IDeb
142136
AdapterUtils.setErrorResponse(response, ErrorCode.LAUNCH_FAILURE,
143137
String.format("Failed to launch debuggee VM. Reason: %s", e.toString()));
144138
}
139+
140+
ISourceLookUpProvider sourceProvider = context.getProvider(ISourceLookUpProvider.class);
141+
Map<String, Object> options = new HashMap<>();
142+
options.put(Constants.DEBUGGEE_ENCODING, context.getDebuggeeEncoding());
143+
if (launchArguments.projectName != null) {
144+
options.put(Constants.PROJECTNAME, launchArguments.projectName);
145+
}
146+
sourceProvider.initialize(context.getDebugSession(), options);
145147
}
146148
}

com.microsoft.java.debug.core/src/main/java/com/microsoft/java/debug/core/adapter/handler/ThreadsRequestHandler.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ public void handle(Command command, Arguments arguments, Response response, IDeb
7777
private void threads(Requests.ThreadsArguments arguments, Response response, IDebugAdapterContext context) {
7878
ArrayList<Types.Thread> threads = new ArrayList<>();
7979
try {
80-
for (ThreadReference thread : context.getDebugSession().allThreads()) {
80+
for (ThreadReference thread : context.getDebugSession().getAllThreads()) {
8181
if (thread.isCollected()) {
8282
continue;
8383
}
@@ -94,23 +94,23 @@ private void threads(Requests.ThreadsArguments arguments, Response response, IDe
9494
private void stepIn(Requests.StepInArguments arguments, Response response, IDebugAdapterContext context) {
9595
ThreadReference thread = DebugUtility.getThread(context.getDebugSession(), arguments.threadId);
9696
if (thread != null) {
97-
DebugUtility.stepInto(thread, context.getDebugSession().eventHub());
97+
DebugUtility.stepInto(thread, context.getDebugSession().getEventHub());
9898
checkThreadRunningAndRecycleIds(thread, context);
9999
}
100100
}
101101

102102
private void stepOut(Requests.StepOutArguments arguments, Response response, IDebugAdapterContext context) {
103103
ThreadReference thread = DebugUtility.getThread(context.getDebugSession(), arguments.threadId);
104104
if (thread != null) {
105-
DebugUtility.stepOut(thread, context.getDebugSession().eventHub());
105+
DebugUtility.stepOut(thread, context.getDebugSession().getEventHub());
106106
checkThreadRunningAndRecycleIds(thread, context);
107107
}
108108
}
109109

110110
private void next(Requests.NextArguments arguments, Response response, IDebugAdapterContext context) {
111111
ThreadReference thread = DebugUtility.getThread(context.getDebugSession(), arguments.threadId);
112112
if (thread != null) {
113-
DebugUtility.stepOver(thread, context.getDebugSession().eventHub());
113+
DebugUtility.stepOver(thread, context.getDebugSession().getEventHub());
114114
checkThreadRunningAndRecycleIds(thread, context);
115115
}
116116
}

com.microsoft.java.debug.core/src/test/java/com/microsoft/java/debug/core/AbstractJdiTestCase.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ protected BreakpointEvent waitForBreakPointEvent(String breakpointAtClass, int l
4343
System.out.println("Breakpoint is accepted.");
4444
});
4545
debugSession.start();
46-
debugSession.eventHub().breakpointEvents().subscribe(breakpoint -> {
46+
debugSession.getEventHub().breakpointEvents().subscribe(breakpoint -> {
4747
System.out.println("Breakpoint is hit.");
4848
breakpoint.shouldResume = false;
4949
staticBreakpointEvent = (BreakpointEvent) breakpoint.event;

com.microsoft.java.debug.core/src/test/java/com/microsoft/java/debug/core/DebugSessionFactory.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,10 @@ public static IDebugSession getDebugSession(String projectName, String mainClass
5353
try {
5454
final IDebugSession debugSession = DebugUtility.launch(Bootstrap.virtualMachineManager(), mainClass, "", "",
5555
new File(projectRoot, "bin").getAbsolutePath(), null, null);
56-
debugSession.eventHub().events().subscribe(debugEvent -> {
56+
debugSession.getEventHub().events().subscribe(debugEvent -> {
5757
if (debugEvent.event instanceof VMDisconnectEvent) {
5858
try {
59-
debugSession.eventHub().close();
59+
debugSession.getEventHub().close();
6060
} catch (Exception e) {
6161
// do nothing.
6262
}

0 commit comments

Comments
 (0)