|
| 1 | +/******************************************************************************* |
| 2 | +* Copyright (c) 2022 Microsoft Corporation and others. |
| 3 | +* All rights reserved. This program and the accompanying materials |
| 4 | +* are made available under the terms of the Eclipse Public License v1.0 |
| 5 | +* which accompanies this distribution, and is available at |
| 6 | +* http://www.eclipse.org/legal/epl-v10.html |
| 7 | +* |
| 8 | +* Contributors: |
| 9 | +* Gayan Perera - initial API and implementation |
| 10 | +*******************************************************************************/ |
| 11 | +package com.microsoft.java.debug.core.adapter.handler; |
| 12 | + |
| 13 | +import java.util.ArrayList; |
| 14 | +import java.util.Arrays; |
| 15 | +import java.util.Collections; |
| 16 | +import java.util.List; |
| 17 | +import java.util.Optional; |
| 18 | +import java.util.concurrent.CompletableFuture; |
| 19 | + |
| 20 | +import com.microsoft.java.debug.core.adapter.IDebugAdapterContext; |
| 21 | +import com.microsoft.java.debug.core.adapter.IDebugRequestHandler; |
| 22 | +import com.microsoft.java.debug.core.adapter.ISourceLookUpProvider; |
| 23 | +import com.microsoft.java.debug.core.adapter.ISourceLookUpProvider.MethodInvocation; |
| 24 | +import com.microsoft.java.debug.core.adapter.variables.StackFrameReference; |
| 25 | +import com.microsoft.java.debug.core.protocol.Messages.Response; |
| 26 | +import com.microsoft.java.debug.core.protocol.Requests.Arguments; |
| 27 | +import com.microsoft.java.debug.core.protocol.Requests.Command; |
| 28 | +import com.microsoft.java.debug.core.protocol.Requests.StepInTargetsArguments; |
| 29 | +import com.microsoft.java.debug.core.protocol.Responses.StepInTargetsResponse; |
| 30 | +import com.microsoft.java.debug.core.protocol.Types.StepInTarget; |
| 31 | +import com.sun.jdi.StackFrame; |
| 32 | + |
| 33 | +public class StepInTargetsRequestHandler implements IDebugRequestHandler { |
| 34 | + |
| 35 | + @Override |
| 36 | + public List<Command> getTargetCommands() { |
| 37 | + return Arrays.asList(Command.STEPIN_TARGETS); |
| 38 | + } |
| 39 | + |
| 40 | + @Override |
| 41 | + public CompletableFuture<Response> handle(Command command, Arguments arguments, Response response, |
| 42 | + IDebugAdapterContext context) { |
| 43 | + final StepInTargetsArguments stepInTargetsArguments = (StepInTargetsArguments) arguments; |
| 44 | + |
| 45 | + final int frameId = stepInTargetsArguments.frameId; |
| 46 | + return CompletableFuture.supplyAsync(() -> { |
| 47 | + response.body = new StepInTargetsResponse( |
| 48 | + findFrame(frameId, context).map(f -> findTargets(f.thread().uniqueID(), f, context)) |
| 49 | + .orElse(Collections.emptyList()).toArray(StepInTarget[]::new)); |
| 50 | + return response; |
| 51 | + }); |
| 52 | + } |
| 53 | + |
| 54 | + private Optional<StackFrame> findFrame(int frameId, IDebugAdapterContext context) { |
| 55 | + Object object = context.getRecyclableIdPool().getObjectById(frameId); |
| 56 | + if (object instanceof StackFrameReference) { |
| 57 | + return Optional.of(context.getStackFrameManager().getStackFrame((StackFrameReference) object)); |
| 58 | + } |
| 59 | + return Optional.empty(); |
| 60 | + } |
| 61 | + |
| 62 | + private List<StepInTarget> findTargets(long threadId, StackFrame stackframe, IDebugAdapterContext context) { |
| 63 | + ISourceLookUpProvider sourceLookUpProvider = context.getProvider(ISourceLookUpProvider.class); |
| 64 | + List<MethodInvocation> invocations = sourceLookUpProvider.findMethodInvocations(stackframe); |
| 65 | + if (invocations.isEmpty()) { |
| 66 | + return Collections.emptyList(); |
| 67 | + } |
| 68 | + |
| 69 | + List<StepInTarget> targets = new ArrayList<>(invocations.size()); |
| 70 | + for (MethodInvocation methodInvocation : invocations) { |
| 71 | + int id = context.getRecyclableIdPool().addObject(threadId, methodInvocation); |
| 72 | + StepInTarget target = new StepInTarget(id, methodInvocation.expression); |
| 73 | + target.column = methodInvocation.columnStart; |
| 74 | + target.endColumn = methodInvocation.columnEnd; |
| 75 | + target.line = methodInvocation.lineStart; |
| 76 | + target.endLine = methodInvocation.lineEnd; |
| 77 | + targets.add(target); |
| 78 | + } |
| 79 | + return targets; |
| 80 | + } |
| 81 | +} |
0 commit comments