Search in sources :

Example 6 with SourceLocation

use of com.google.api.services.clouddebugger.v2.model.SourceLocation in project google-cloud-intellij by GoogleCloudPlatform.

the class CloudBreakpointHandler method registerBreakpoint.

/**
 * Called by IntelliJ when the user has enabled or created a new breakpoint. Creates the server
 * breakpoint.
 *
 * @param ideBreakpoint breakpoint to register
 */
@Override
public void registerBreakpoint(@NotNull final XLineBreakpoint<CloudLineBreakpointProperties> ideBreakpoint) {
    if (ideBreakpoint.getSourcePosition() == null || !ideBreakpoint.isEnabled() || !(ideBreakpoint.getType() instanceof CloudLineBreakpointType)) {
        return;
    }
    com.intellij.debugger.ui.breakpoints.Breakpoint cloudIdeBreakpoint = BreakpointManager.getJavaBreakpoint(ideBreakpoint);
    if (!(cloudIdeBreakpoint instanceof CloudLineBreakpointType.CloudLineBreakpoint)) {
        LOG.error("breakpoint was not of the correct type to create on the cloud.  It was not a " + "CloudLineBreakpoint");
        return;
    }
    final CloudLineBreakpointType.CloudLineBreakpoint cloudIdeLineBreakpoint = (CloudLineBreakpointType.CloudLineBreakpoint) cloudIdeBreakpoint;
    if (ideBreakpoint.getProperties().isCreatedByServer()) {
        // gets called during construction.
        return;
    }
    if (ideBreakpoint.getProperties().isAddedOnServer() && !ideBreakpoint.getProperties().isDisabledByServer()) {
        // disabled breakpoint
        return;
    }
    PsiFile javaFile = psiManager.findFile(ideBreakpoint.getSourcePosition().getFile());
    if (!(javaFile instanceof PsiJavaFile)) {
        return;
    }
    SourceLocation location = new SourceLocation();
    // Sending the file as com/package/example/Class.java to Cloud Debugger because it plays nice
    // with the CDB plugin. See ServerToIdeFileResolver.
    location.setPath(ServerToIdeFileResolver.getCloudPathFromJavaFile((PsiJavaFile) javaFile));
    location.setLine(ideBreakpoint.getSourcePosition().getLine() + 1);
    Breakpoint serverNewBreakpoint = new Breakpoint();
    serverNewBreakpoint.setLocation(location);
    if (ideBreakpoint.getConditionExpression() != null) {
        serverNewBreakpoint.setCondition(ideBreakpoint.getConditionExpression().getExpression());
    }
    List<String> watches = cloudIdeLineBreakpoint.getWatchExpressions();
    if (watches != null) {
        serverNewBreakpoint.setExpressions(watches);
    }
    // The breakpoint will enter error state asynchronously.  For now, we state that its verified.
    process.getStateController().setBreakpointAsync(serverNewBreakpoint, new SetBreakpointHandler() {

        @Override
        public void onSuccess(@NotNull final String id) {
            Runnable runnable = new Runnable() {

                @Override
                public void run() {
                    if (!Strings.isNullOrEmpty(id)) {
                        if (!cloudIdeLineBreakpoint.isEnabled()) {
                            process.getStateController().deleteBreakpointAsync(// race condition
                            id);
                        } else {
                            // Success.
                            // Mark as added so we don't add it again.
                            ideBreakpoint.getProperties().setAddedOnServer(true);
                            cloudIdeLineBreakpoint.setErrorMessage(null);
                            process.updateBreakpointPresentation(cloudIdeLineBreakpoint);
                        }
                    } else {
                        // TODO(joaomartins): Why couldn't the breakpoint be set? Improve this
                        // message.
                        cloudIdeLineBreakpoint.setErrorMessage(GctBundle.getString("clouddebug.errorset"));
                        process.updateBreakpointPresentation(cloudIdeLineBreakpoint);
                    }
                    if (!Strings.isNullOrEmpty(id)) {
                        ideBreakpoint.getProperties().setDisabledByServer(false);
                        String oldId = ideBreakpoint.getUserData(CLOUD_ID);
                        if (!Strings.isNullOrEmpty(oldId)) {
                            ideBreakpoints.remove(oldId);
                        }
                        ideBreakpoint.putUserData(CLOUD_ID, id);
                        ideBreakpoints.put(id, ideBreakpoint);
                    }
                }
            };
            if (ApplicationManager.getApplication().isUnitTestMode()) {
                runnable.run();
            } else {
                SwingUtilities.invokeLater(runnable);
            }
        }

        @Override
        public void onError(String errorMessage) {
            cloudIdeLineBreakpoint.setErrorMessage(errorMessage);
            process.updateBreakpointPresentation(cloudIdeLineBreakpoint);
        }
    });
}
Also used : SourceLocation(com.google.api.services.clouddebugger.v2.model.SourceLocation) Breakpoint(com.google.api.services.clouddebugger.v2.model.Breakpoint) XBreakpoint(com.intellij.xdebugger.breakpoints.XBreakpoint) CloudLineBreakpoint(com.google.cloud.tools.intellij.debugger.CloudLineBreakpointType.CloudLineBreakpoint) XLineBreakpoint(com.intellij.xdebugger.breakpoints.XLineBreakpoint) CloudLineBreakpoint(com.google.cloud.tools.intellij.debugger.CloudLineBreakpointType.CloudLineBreakpoint) CloudLineBreakpoint(com.google.cloud.tools.intellij.debugger.CloudLineBreakpointType.CloudLineBreakpoint) SetBreakpointHandler(com.google.cloud.tools.intellij.debugger.CloudDebugProcessStateController.SetBreakpointHandler) PsiJavaFile(com.intellij.psi.PsiJavaFile) PsiFile(com.intellij.psi.PsiFile)

Example 7 with SourceLocation

use of com.google.api.services.clouddebugger.v2.model.SourceLocation in project google-cloud-intellij by GoogleCloudPlatform.

the class CloudDebugProcessStateController method setBreakpointAsync.

/**
 * Called from the {@link CloudDebugProcessHandler} to set a breakpoint.
 */
@SuppressWarnings("FutureReturnValueIgnored")
void setBreakpointAsync(@NotNull final Breakpoint serverBreakpoint, @NotNull final SetBreakpointHandler handler) {
    if (state == null) {
        handler.onError(GctBundle.getString("clouddebug.invalid.state"));
        return;
    }
    final Debugger client = CloudDebuggerClient.getLongTimeoutClient(state);
    if (client == null) {
        LOG.warn("no client available attempting to setBreakpoint");
        handler.onError(GctBundle.getString("clouddebug.bad.login.message"));
        return;
    }
    final String debuggeeId = state.getDebuggeeId();
    assert debuggeeId != null;
    ApplicationManager.getApplication().executeOnPooledThread(() -> {
        try {
            // Delete old breakpoints at this location.
            List<Breakpoint> currentList = state.getCurrentServerBreakpointList();
            SourceLocation location = serverBreakpoint.getLocation();
            for (Breakpoint serverBp : currentList) {
                if (!Boolean.TRUE.equals(serverBp.getIsFinalState()) && serverBp.getLocation().getLine() != null && serverBp.getLocation().getLine().equals(location.getLine()) && !Strings.isNullOrEmpty(serverBp.getLocation().getPath()) && serverBp.getLocation().getPath().equals(location.getPath())) {
                    // should not be async here.
                    deleteBreakpoint(serverBp.getId());
                }
            }
            SetBreakpointResponse addResponse = client.debuggees().breakpoints().set(debuggeeId, serverBreakpoint).setClientVersion(ServiceManager.getService(PluginInfoService.class).getClientVersionForCloudDebugger()).execute();
            if (addResponse != null && addResponse.getBreakpoint() != null) {
                Breakpoint result = addResponse.getBreakpoint();
                if (result.getStatus() != null && Boolean.TRUE.equals(result.getStatus().getIsError()) && result.getStatus().getDescription() != null) {
                    handler.onError(BreakpointUtil.getUserErrorMessage(result.getStatus()));
                }
                handler.onSuccess(addResponse.getBreakpoint().getId());
            } else {
                handler.onError(GctBundle.getString("clouddebug.no.response"));
            }
        } catch (IOException ex) {
            LOG.error("exception setting a breakpoint", ex);
            handler.onError(ex.toString());
        }
    });
}
Also used : Debugger(com.google.api.services.clouddebugger.v2.Clouddebugger.Debugger) SourceLocation(com.google.api.services.clouddebugger.v2.model.SourceLocation) Breakpoint(com.google.api.services.clouddebugger.v2.model.Breakpoint) SetBreakpointResponse(com.google.api.services.clouddebugger.v2.model.SetBreakpointResponse) PluginInfoService(com.google.cloud.tools.intellij.service.PluginInfoService) IOException(java.io.IOException)

Example 8 with SourceLocation

use of com.google.api.services.clouddebugger.v2.model.SourceLocation in project google-cloud-intellij by GoogleCloudPlatform.

the class CloudExecutionStackTest method testGetTopFrame_nonNull.

@Test
public void testGetTopFrame_nonNull() {
    List<StackFrame> frames = new ArrayList<StackFrame>();
    StackFrame frame1 = new StackFrame();
    SourceLocation location1 = new SourceLocation();
    location1.setLine(1);
    frame1.setLocation(location1);
    List<Variable> variables = new ArrayList<Variable>();
    Variable variable = new Variable();
    variable.setName("foo");
    variables.add(variable);
    frame1.setLocals(variables);
    StackFrame frame2 = new StackFrame();
    SourceLocation location2 = new SourceLocation();
    location2.setLine(2);
    frame2.setLocation(location2);
    frames.add(frame1);
    frames.add(frame2);
    CloudExecutionStack stack = new CloudExecutionStack(project, "name", frames, null, null);
    CloudStackFrame localFrame = stack.getTopFrame();
    Assert.assertNotNull(localFrame);
    SpyNode node = new SpyNode();
    localFrame.computeChildren(node);
    Assert.assertEquals(1, node.seenChildren.size());
    Assert.assertEquals("foo", node.seenChildren.get(0));
}
Also used : SourceLocation(com.google.api.services.clouddebugger.v2.model.SourceLocation) Variable(com.google.api.services.clouddebugger.v2.model.Variable) StackFrame(com.google.api.services.clouddebugger.v2.model.StackFrame) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 9 with SourceLocation

use of com.google.api.services.clouddebugger.v2.model.SourceLocation in project google-cloud-intellij by GoogleCloudPlatform.

the class CloudDebugHistoricalSnapshotsTest method testOnBreakpointListChanged_twoBreakPoints.

@Test
public void testOnBreakpointListChanged_twoBreakPoints() throws InterruptedException {
    CloudDebugHistoricalSnapshots snapshots = new CloudDebugHistoricalSnapshots(handler);
    Breakpoint bp1 = new Breakpoint();
    bp1.setId("bp1");
    bp1.setFinalTime("2015-08-22T05:23:34.123Z");
    bp1.setIsFinalState(true);
    SourceLocation location = new SourceLocation();
    location.setPath("foo/bar/baz");
    location.setLine(12);
    bp1.setLocation(location);
    Breakpoint bp2 = new Breakpoint();
    bp2.setId("bp2");
    bp2.setFinalTime("2016-08-22T05:23:34.123Z");
    bp2.setIsFinalState(true);
    SourceLocation location2 = new SourceLocation();
    location2.setPath("foo/bar/baz");
    location2.setLine(14);
    bp2.setLocation(location);
    List<Breakpoint> breakpoints1 = new ArrayList<Breakpoint>();
    breakpoints1.add(bp1);
    List<Breakpoint> breakpoints2 = new ArrayList<Breakpoint>();
    breakpoints2.add(bp1);
    breakpoints2.add(bp2);
    CloudBreakpointHandler breakpointHandler = Mockito.mock(CloudBreakpointHandler.class);
    Mockito.when(mockProcess.getBreakpointHandler()).thenReturn(breakpointHandler);
    Assert.assertEquals(-1, snapshots.table.getSelectedRow());
    // BP1
    Mockito.when(mockProcess.getCurrentBreakpointList()).thenReturn(breakpoints1);
    Mockito.when(mockProcess.getCurrentSnapshot()).thenReturn(bp1);
    runModelSetter(snapshots);
    Assert.assertEquals(0, snapshots.table.getSelectedRow());
    // BP2
    Mockito.when(mockProcess.getCurrentBreakpointList()).thenReturn(breakpoints2);
    Mockito.when(mockProcess.getCurrentSnapshot()).thenReturn(bp2);
    runModelSetter(snapshots);
    Assert.assertEquals(1, snapshots.table.getSelectedRow());
}
Also used : SourceLocation(com.google.api.services.clouddebugger.v2.model.SourceLocation) Breakpoint(com.google.api.services.clouddebugger.v2.model.Breakpoint) CloudBreakpointHandler(com.google.cloud.tools.intellij.debugger.CloudBreakpointHandler) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Aggregations

SourceLocation (com.google.api.services.clouddebugger.v2.model.SourceLocation)9 Breakpoint (com.google.api.services.clouddebugger.v2.model.Breakpoint)7 CloudLineBreakpoint (com.google.cloud.tools.intellij.debugger.CloudLineBreakpointType.CloudLineBreakpoint)3 XBreakpoint (com.intellij.xdebugger.breakpoints.XBreakpoint)3 XLineBreakpoint (com.intellij.xdebugger.breakpoints.XLineBreakpoint)3 ArrayList (java.util.ArrayList)3 Test (org.junit.Test)3 CloudBreakpointHandler (com.google.cloud.tools.intellij.debugger.CloudBreakpointHandler)2 Debugger (com.google.api.services.clouddebugger.v2.Clouddebugger.Debugger)1 FormatMessage (com.google.api.services.clouddebugger.v2.model.FormatMessage)1 SetBreakpointResponse (com.google.api.services.clouddebugger.v2.model.SetBreakpointResponse)1 StackFrame (com.google.api.services.clouddebugger.v2.model.StackFrame)1 StatusMessage (com.google.api.services.clouddebugger.v2.model.StatusMessage)1 Variable (com.google.api.services.clouddebugger.v2.model.Variable)1 SetBreakpointHandler (com.google.cloud.tools.intellij.debugger.CloudDebugProcessStateController.SetBreakpointHandler)1 PluginInfoService (com.google.cloud.tools.intellij.service.PluginInfoService)1 Project (com.intellij.openapi.project.Project)1 VirtualFile (com.intellij.openapi.vfs.VirtualFile)1 PsiFile (com.intellij.psi.PsiFile)1 PsiJavaFile (com.intellij.psi.PsiJavaFile)1