diff --git a/com.microsoft.java.debug.core/src/main/java/com/microsoft/java/debug/core/DebugSession.java b/com.microsoft.java.debug.core/src/main/java/com/microsoft/java/debug/core/DebugSession.java index 986d4704e..7d5b5cc56 100644 --- a/com.microsoft.java.debug.core/src/main/java/com/microsoft/java/debug/core/DebugSession.java +++ b/com.microsoft.java.debug.core/src/main/java/com/microsoft/java/debug/core/DebugSession.java @@ -78,12 +78,12 @@ public void terminate() { @Override public IBreakpoint createBreakpoint(String className, int lineNumber) { - return new Breakpoint(vm, this.eventHub(), className, lineNumber); + return new Breakpoint(vm, this.getEventHub(), className, lineNumber); } @Override public IBreakpoint createBreakpoint(String className, int lineNumber, int hitCount) { - return new Breakpoint(vm, this.eventHub(), className, lineNumber, hitCount); + return new Breakpoint(vm, this.getEventHub(), className, lineNumber, hitCount); } @Override @@ -102,12 +102,17 @@ public Process process() { } @Override - public List allThreads() { + public List getAllThreads() { return vm.allThreads(); } @Override - public IEventHub eventHub() { + public IEventHub getEventHub() { return eventHub; } + + @Override + public VirtualMachine getVM() { + return vm; + } } diff --git a/com.microsoft.java.debug.core/src/main/java/com/microsoft/java/debug/core/DebugUtility.java b/com.microsoft.java.debug.core/src/main/java/com/microsoft/java/debug/core/DebugUtility.java index 8f86ef8a1..a324dc732 100644 --- a/com.microsoft.java.debug.core/src/main/java/com/microsoft/java/debug/core/DebugUtility.java +++ b/com.microsoft.java.debug.core/src/main/java/com/microsoft/java/debug/core/DebugUtility.java @@ -228,7 +228,7 @@ public static ThreadReference getThread(IDebugSession debugSession, long threadI public static List getAllThreadsSafely(IDebugSession debugSession) { if (debugSession != null) { try { - return debugSession.allThreads(); + return debugSession.getAllThreads(); } catch (VMDisconnectedException ex) { // do nothing. } diff --git a/com.microsoft.java.debug.core/src/main/java/com/microsoft/java/debug/core/IDebugSession.java b/com.microsoft.java.debug.core/src/main/java/com/microsoft/java/debug/core/IDebugSession.java index 03e2f98ec..c1ed7d848 100644 --- a/com.microsoft.java.debug.core/src/main/java/com/microsoft/java/debug/core/IDebugSession.java +++ b/com.microsoft.java.debug.core/src/main/java/com/microsoft/java/debug/core/IDebugSession.java @@ -14,6 +14,7 @@ import java.util.List; import com.sun.jdi.ThreadReference; +import com.sun.jdi.VirtualMachine; public interface IDebugSession { void start(); @@ -37,8 +38,9 @@ public interface IDebugSession { Process process(); - List allThreads(); + List getAllThreads(); - IEventHub eventHub(); -} + IEventHub getEventHub(); + VirtualMachine getVM(); +} diff --git a/com.microsoft.java.debug.core/src/main/java/com/microsoft/java/debug/core/adapter/IProvider.java b/com.microsoft.java.debug.core/src/main/java/com/microsoft/java/debug/core/adapter/IProvider.java index 7be0f400d..17b137b2e 100644 --- a/com.microsoft.java.debug.core/src/main/java/com/microsoft/java/debug/core/adapter/IProvider.java +++ b/com.microsoft.java.debug.core/src/main/java/com/microsoft/java/debug/core/adapter/IProvider.java @@ -11,24 +11,18 @@ package com.microsoft.java.debug.core.adapter; -import java.util.HashMap; import java.util.Map; +import com.microsoft.java.debug.core.IDebugSession; + public interface IProvider { /** * Initialize this provider. - * @param options the options - */ - default void initialize(Map options) { - - } - - /** - * Get the default options for this provider. - * - * @return The default options. + * @param debugSession + * The associated debug session + * @param options + * the options */ - default Map getDefaultOptions() { - return new HashMap<>(); + default void initialize(IDebugSession debugSession, Map options) { } } diff --git a/com.microsoft.java.debug.core/src/main/java/com/microsoft/java/debug/core/adapter/handler/AttachRequestHandler.java b/com.microsoft.java.debug.core/src/main/java/com/microsoft/java/debug/core/adapter/handler/AttachRequestHandler.java index bcb62ee96..4d0dc5779 100644 --- a/com.microsoft.java.debug.core/src/main/java/com/microsoft/java/debug/core/adapter/handler/AttachRequestHandler.java +++ b/com.microsoft.java.debug.core/src/main/java/com/microsoft/java/debug/core/adapter/handler/AttachRequestHandler.java @@ -14,6 +14,7 @@ import java.io.IOException; import java.nio.charset.StandardCharsets; import java.util.Arrays; +import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.logging.Logger; @@ -50,25 +51,25 @@ public void handle(Command command, Arguments arguments, Response response, IDeb context.setDebuggeeEncoding(StandardCharsets.UTF_8); // Use UTF-8 as debuggee's default encoding format. IVirtualMachineManagerProvider vmProvider = context.getProvider(IVirtualMachineManagerProvider.class); - ISourceLookUpProvider sourceProvider = context.getProvider(ISourceLookUpProvider.class); - Map options = sourceProvider.getDefaultOptions(); - options.put(Constants.DEBUGGEE_ENCODING, context.getDebuggeeEncoding()); - if (attachArguments.projectName != null) { - options.put(Constants.PROJECTNAME, attachArguments.projectName); - } - sourceProvider.initialize(options); try { - logger.info(String.format("Trying to attach to remote debuggee VM %s:%d .", - attachArguments.hostName, attachArguments.port)); - IDebugSession debugSession = DebugUtility.attach(vmProvider.getVirtualMachineManager(), - attachArguments.hostName, attachArguments.port, attachArguments.timeout); + logger.info(String.format("Trying to attach to remote debuggee VM %s:%d .", attachArguments.hostName, attachArguments.port)); + IDebugSession debugSession = DebugUtility.attach(vmProvider.getVirtualMachineManager(), attachArguments.hostName, attachArguments.port, + attachArguments.timeout); context.setDebugSession(debugSession); logger.info("Attaching to debuggee VM succeeded."); } catch (IOException | IllegalConnectorArgumentsException e) { AdapterUtils.setErrorResponse(response, ErrorCode.ATTACH_FAILURE, String.format("Failed to attach to remote debuggee VM. Reason: %s", e.toString())); } + + Map options = new HashMap<>(); + options.put(Constants.DEBUGGEE_ENCODING, context.getDebuggeeEncoding()); + if (attachArguments.projectName != null) { + options.put(Constants.PROJECTNAME, attachArguments.projectName); + } + ISourceLookUpProvider sourceProvider = context.getProvider(ISourceLookUpProvider.class); + sourceProvider.initialize(context.getDebugSession(), options); } } diff --git a/com.microsoft.java.debug.core/src/main/java/com/microsoft/java/debug/core/adapter/handler/ConfigurationDoneRequestHandler.java b/com.microsoft.java.debug.core/src/main/java/com/microsoft/java/debug/core/adapter/handler/ConfigurationDoneRequestHandler.java index a7329a906..1736e75df 100644 --- a/com.microsoft.java.debug.core/src/main/java/com/microsoft/java/debug/core/adapter/handler/ConfigurationDoneRequestHandler.java +++ b/com.microsoft.java.debug.core/src/main/java/com/microsoft/java/debug/core/adapter/handler/ConfigurationDoneRequestHandler.java @@ -48,7 +48,7 @@ public void handle(Command command, Arguments arguments, Response response, IDeb IDebugSession debugSession = context.getDebugSession(); if (debugSession != null) { // This is a global event handler to handle the JDI Event from Virtual Machine. - debugSession.eventHub().events().subscribe(debugEvent -> { + debugSession.getEventHub().events().subscribe(debugEvent -> { handleDebugEvent(debugEvent, debugSession, context); }); // configuration is done, and start debug session. @@ -72,7 +72,7 @@ private void handleDebugEvent(DebugEvent debugEvent, IDebugSession debugSession, context.sendEventAsync(new Events.TerminatedEvent()); // Terminate eventHub thread. try { - debugSession.eventHub().close(); + debugSession.getEventHub().close(); } catch (Exception e) { // do nothing. } diff --git a/com.microsoft.java.debug.core/src/main/java/com/microsoft/java/debug/core/adapter/handler/LaunchRequestHandler.java b/com.microsoft.java.debug.core/src/main/java/com/microsoft/java/debug/core/adapter/handler/LaunchRequestHandler.java index bcbf105d5..d1e2eeaa4 100644 --- a/com.microsoft.java.debug.core/src/main/java/com/microsoft/java/debug/core/adapter/handler/LaunchRequestHandler.java +++ b/com.microsoft.java.debug.core/src/main/java/com/microsoft/java/debug/core/adapter/handler/LaunchRequestHandler.java @@ -87,13 +87,7 @@ public void handle(Command command, Arguments arguments, Response response, IDeb IVirtualMachineManagerProvider vmProvider = context.getProvider(IVirtualMachineManagerProvider.class); - ISourceLookUpProvider sourceProvider = context.getProvider(ISourceLookUpProvider.class); - Map options = sourceProvider.getDefaultOptions(); - options.put(Constants.DEBUGGEE_ENCODING, context.getDebuggeeEncoding()); - if (launchArguments.projectName != null) { - options.put(Constants.PROJECTNAME, launchArguments.projectName); - } - sourceProvider.initialize(options); + // Append environment to native environment. String[] envVars = null; @@ -142,5 +136,13 @@ public void handle(Command command, Arguments arguments, Response response, IDeb AdapterUtils.setErrorResponse(response, ErrorCode.LAUNCH_FAILURE, String.format("Failed to launch debuggee VM. Reason: %s", e.toString())); } + + ISourceLookUpProvider sourceProvider = context.getProvider(ISourceLookUpProvider.class); + Map options = new HashMap<>(); + options.put(Constants.DEBUGGEE_ENCODING, context.getDebuggeeEncoding()); + if (launchArguments.projectName != null) { + options.put(Constants.PROJECTNAME, launchArguments.projectName); + } + sourceProvider.initialize(context.getDebugSession(), options); } } diff --git a/com.microsoft.java.debug.core/src/main/java/com/microsoft/java/debug/core/adapter/handler/ThreadsRequestHandler.java b/com.microsoft.java.debug.core/src/main/java/com/microsoft/java/debug/core/adapter/handler/ThreadsRequestHandler.java index a95c29fb1..3c46d7d46 100644 --- a/com.microsoft.java.debug.core/src/main/java/com/microsoft/java/debug/core/adapter/handler/ThreadsRequestHandler.java +++ b/com.microsoft.java.debug.core/src/main/java/com/microsoft/java/debug/core/adapter/handler/ThreadsRequestHandler.java @@ -77,7 +77,7 @@ public void handle(Command command, Arguments arguments, Response response, IDeb private void threads(Requests.ThreadsArguments arguments, Response response, IDebugAdapterContext context) { ArrayList threads = new ArrayList<>(); try { - for (ThreadReference thread : context.getDebugSession().allThreads()) { + for (ThreadReference thread : context.getDebugSession().getAllThreads()) { if (thread.isCollected()) { continue; } @@ -94,7 +94,7 @@ private void threads(Requests.ThreadsArguments arguments, Response response, IDe private void stepIn(Requests.StepInArguments arguments, Response response, IDebugAdapterContext context) { ThreadReference thread = DebugUtility.getThread(context.getDebugSession(), arguments.threadId); if (thread != null) { - DebugUtility.stepInto(thread, context.getDebugSession().eventHub()); + DebugUtility.stepInto(thread, context.getDebugSession().getEventHub()); checkThreadRunningAndRecycleIds(thread, context); } } @@ -102,7 +102,7 @@ private void stepIn(Requests.StepInArguments arguments, Response response, IDebu private void stepOut(Requests.StepOutArguments arguments, Response response, IDebugAdapterContext context) { ThreadReference thread = DebugUtility.getThread(context.getDebugSession(), arguments.threadId); if (thread != null) { - DebugUtility.stepOut(thread, context.getDebugSession().eventHub()); + DebugUtility.stepOut(thread, context.getDebugSession().getEventHub()); checkThreadRunningAndRecycleIds(thread, context); } } @@ -110,7 +110,7 @@ private void stepOut(Requests.StepOutArguments arguments, Response response, IDe private void next(Requests.NextArguments arguments, Response response, IDebugAdapterContext context) { ThreadReference thread = DebugUtility.getThread(context.getDebugSession(), arguments.threadId); if (thread != null) { - DebugUtility.stepOver(thread, context.getDebugSession().eventHub()); + DebugUtility.stepOver(thread, context.getDebugSession().getEventHub()); checkThreadRunningAndRecycleIds(thread, context); } } diff --git a/com.microsoft.java.debug.core/src/test/java/com/microsoft/java/debug/core/AbstractJdiTestCase.java b/com.microsoft.java.debug.core/src/test/java/com/microsoft/java/debug/core/AbstractJdiTestCase.java index 21b104732..a69c3c92d 100644 --- a/com.microsoft.java.debug.core/src/test/java/com/microsoft/java/debug/core/AbstractJdiTestCase.java +++ b/com.microsoft.java.debug.core/src/test/java/com/microsoft/java/debug/core/AbstractJdiTestCase.java @@ -43,7 +43,7 @@ protected BreakpointEvent waitForBreakPointEvent(String breakpointAtClass, int l System.out.println("Breakpoint is accepted."); }); debugSession.start(); - debugSession.eventHub().breakpointEvents().subscribe(breakpoint -> { + debugSession.getEventHub().breakpointEvents().subscribe(breakpoint -> { System.out.println("Breakpoint is hit."); breakpoint.shouldResume = false; staticBreakpointEvent = (BreakpointEvent) breakpoint.event; diff --git a/com.microsoft.java.debug.core/src/test/java/com/microsoft/java/debug/core/DebugSessionFactory.java b/com.microsoft.java.debug.core/src/test/java/com/microsoft/java/debug/core/DebugSessionFactory.java index 5d6da3f39..8baac6694 100644 --- a/com.microsoft.java.debug.core/src/test/java/com/microsoft/java/debug/core/DebugSessionFactory.java +++ b/com.microsoft.java.debug.core/src/test/java/com/microsoft/java/debug/core/DebugSessionFactory.java @@ -53,10 +53,10 @@ public static IDebugSession getDebugSession(String projectName, String mainClass try { final IDebugSession debugSession = DebugUtility.launch(Bootstrap.virtualMachineManager(), mainClass, "", "", new File(projectRoot, "bin").getAbsolutePath(), null, null); - debugSession.eventHub().events().subscribe(debugEvent -> { + debugSession.getEventHub().events().subscribe(debugEvent -> { if (debugEvent.event instanceof VMDisconnectEvent) { try { - debugSession.eventHub().close(); + debugSession.getEventHub().close(); } catch (Exception e) { // do nothing. } diff --git a/com.microsoft.java.debug.plugin/src/main/java/com/microsoft/java/debug/plugin/internal/JdtSourceLookUpProvider.java b/com.microsoft.java.debug.plugin/src/main/java/com/microsoft/java/debug/plugin/internal/JdtSourceLookUpProvider.java index 002bc2c4f..f3b45690f 100644 --- a/com.microsoft.java.debug.plugin/src/main/java/com/microsoft/java/debug/plugin/internal/JdtSourceLookUpProvider.java +++ b/com.microsoft.java.debug.plugin/src/main/java/com/microsoft/java/debug/plugin/internal/JdtSourceLookUpProvider.java @@ -55,6 +55,7 @@ import com.microsoft.java.debug.core.Configuration; import com.microsoft.java.debug.core.DebugException; +import com.microsoft.java.debug.core.IDebugSession; import com.microsoft.java.debug.core.adapter.AdapterUtils; import com.microsoft.java.debug.core.adapter.Constants; import com.microsoft.java.debug.core.adapter.ISourceLookUpProvider; @@ -64,14 +65,14 @@ public class JdtSourceLookUpProvider implements ISourceLookUpProvider { private static final String JDT_SCHEME = "jdt"; private static final String PATH_SEPARATOR = "/"; - private HashMap context = new HashMap(); + private HashMap options = new HashMap(); @Override - public void initialize(Map props) { + public void initialize(IDebugSession debugSession, Map props) { if (props == null) { throw new IllegalArgumentException("argument is null"); } - context.putAll(props); + options.putAll(props); } @Override @@ -110,7 +111,7 @@ public String[] getFullyQualifiedName(String uri, int[] lines, int[] columns) th String filePath = AdapterUtils.toPath(uri); // For file uri, read the file contents directly and pass them to the ast parser. if (filePath != null && Files.isRegularFile(Paths.get(filePath))) { - Charset cs = (Charset) this.context.get(Constants.DEBUGGEE_ENCODING); + Charset cs = (Charset) this.options.get(Constants.DEBUGGEE_ENCODING); if (cs == null) { cs = Charset.defaultCharset(); } @@ -130,11 +131,11 @@ public String[] getFullyQualifiedName(String uri, int[] lines, int[] columns) th * See the java doc for { @link ASTParser#setSource(char[]) }, * the user need specify the compiler options explicitly. */ - Map options = JavaCore.getOptions(); - options.put(JavaCore.COMPILER_SOURCE, JavaCore.VERSION_1_8); - options.put(JavaCore.COMPILER_CODEGEN_TARGET_PLATFORM, JavaCore.VERSION_1_8); - options.put(JavaCore.COMPILER_COMPLIANCE, JavaCore.VERSION_1_8); - parser.setCompilerOptions(options); + Map javaOptions = JavaCore.getOptions(); + javaOptions.put(JavaCore.COMPILER_SOURCE, JavaCore.VERSION_1_8); + javaOptions.put(JavaCore.COMPILER_CODEGEN_TARGET_PLATFORM, JavaCore.VERSION_1_8); + javaOptions.put(JavaCore.COMPILER_COMPLIANCE, JavaCore.VERSION_1_8); + parser.setCompilerOptions(javaOptions); astUnit = (CompilationUnit) parser.createAST(null); } else { // For non-file uri (e.g. jdt://contents/rt.jar/java.io/PrintStream.class), @@ -223,7 +224,7 @@ private IJavaProject getJavaProjectFromName(String projectName) throws CoreExcep } private String searchDeclarationFileByFqn(String fullyQualifiedName) { - String projectName = (String) context.get(Constants.PROJECTNAME); + String projectName = (String) options.get(Constants.PROJECTNAME); try { IJavaSearchScope searchScope = projectName != null ? createSearchScope(getJavaProjectFromName(projectName)) diff --git a/com.microsoft.java.debug.plugin/src/main/java/com/microsoft/java/debug/plugin/internal/JdtVirtualMachineManagerProvider.java b/com.microsoft.java.debug.plugin/src/main/java/com/microsoft/java/debug/plugin/internal/JdtVirtualMachineManagerProvider.java index 170043d99..513f49f6b 100644 --- a/com.microsoft.java.debug.plugin/src/main/java/com/microsoft/java/debug/plugin/internal/JdtVirtualMachineManagerProvider.java +++ b/com.microsoft.java.debug.plugin/src/main/java/com/microsoft/java/debug/plugin/internal/JdtVirtualMachineManagerProvider.java @@ -11,8 +11,6 @@ package com.microsoft.java.debug.plugin.internal; -import java.util.Map; - import com.microsoft.java.debug.core.adapter.IVirtualMachineManagerProvider; import com.sun.jdi.VirtualMachineManager; @@ -26,8 +24,4 @@ public synchronized VirtualMachineManager getVirtualMachineManager() { } return vmManager; } - - @Override - public void initialize(Map props) { - } }