Search in sources :

Example 31 with SourcePosition

use of com.intellij.debugger.SourcePosition in project intellij-community by JetBrains.

the class JSR45PositionManager method getSourcePosition.

@Override
public SourcePosition getSourcePosition(final Location location) throws NoDataException {
    SourcePosition sourcePosition = null;
    try {
        String sourcePath = getRelativeSourcePathByLocation(location);
        PsiFile file = mySourcesFinder.findSourceFile(sourcePath, myDebugProcess.getProject(), myScope);
        if (file != null) {
            int lineNumber = getLineNumber(location);
            sourcePosition = SourcePosition.createFromLine(file, lineNumber - 1);
        }
    } catch (AbsentInformationException ignored) {
    // ignored
    } catch (Throwable e) {
        LOG.info(e);
    }
    if (sourcePosition == null) {
        throw NoDataException.INSTANCE;
    }
    return sourcePosition;
}
Also used : SourcePosition(com.intellij.debugger.SourcePosition) PsiFile(com.intellij.psi.PsiFile)

Example 32 with SourcePosition

use of com.intellij.debugger.SourcePosition in project intellij-community by JetBrains.

the class ExceptionBreakpoint method createRequest.

public void createRequest(final DebugProcessImpl debugProcess) {
    DebuggerManagerThreadImpl.assertIsManagerThread();
    if (!shouldCreateRequest(debugProcess)) {
        return;
    }
    SourcePosition classPosition = ApplicationManager.getApplication().runReadAction(new Computable<SourcePosition>() {

        public SourcePosition compute() {
            PsiClass psiClass = DebuggerUtils.findClass(getQualifiedName(), myProject, debugProcess.getSearchScope());
            return psiClass != null ? SourcePosition.createFromElement(psiClass) : null;
        }
    });
    if (classPosition == null) {
        createOrWaitPrepare(debugProcess, getQualifiedName());
    } else {
        createOrWaitPrepare(debugProcess, classPosition);
    }
}
Also used : SourcePosition(com.intellij.debugger.SourcePosition) PsiClass(com.intellij.psi.PsiClass)

Example 33 with SourcePosition

use of com.intellij.debugger.SourcePosition in project intellij-community by JetBrains.

the class LineBreakpoint method isInScopeOf.

private boolean isInScopeOf(DebugProcessImpl debugProcess, String className) {
    final SourcePosition position = getSourcePosition();
    if (position != null) {
        final VirtualFile breakpointFile = position.getFile().getVirtualFile();
        final ProjectFileIndex fileIndex = ProjectRootManager.getInstance(myProject).getFileIndex();
        if (breakpointFile != null && fileIndex.isUnderSourceRootOfType(breakpointFile, JavaModuleSourceRootTypes.SOURCES)) {
            if (debugProcess.getSearchScope().contains(breakpointFile)) {
                return true;
            }
            // apply filtering to breakpoints from content sources only, not for sources attached to libraries
            final Collection<VirtualFile> candidates = findClassCandidatesInSourceContent(className, debugProcess.getSearchScope(), fileIndex);
            if (LOG.isDebugEnabled()) {
                LOG.debug("Found " + (candidates == null ? "null" : candidates.size()) + " candidate containing files for class " + className);
            }
            if (candidates == null) {
                // If no candidates are found in scope then assume that class is loaded dynamically and allow breakpoint
                return true;
            }
            //}
            if (LOG.isDebugEnabled()) {
                final GlobalSearchScope scope = debugProcess.getSearchScope();
                final boolean contains = scope.contains(breakpointFile);
                List<VirtualFile> files = ContainerUtil.map(JavaFullClassNameIndex.getInstance().get(className.hashCode(), myProject, scope), aClass -> aClass.getContainingFile().getVirtualFile());
                List<VirtualFile> allFiles = ContainerUtil.map(JavaFullClassNameIndex.getInstance().get(className.hashCode(), myProject, new EverythingGlobalScope(myProject)), aClass -> aClass.getContainingFile().getVirtualFile());
                final VirtualFile contentRoot = fileIndex.getContentRootForFile(breakpointFile);
                final Module module = fileIndex.getModuleForFile(breakpointFile);
                LOG.debug("Did not find '" + className + "' in " + scope + "; contains=" + contains + "; contentRoot=" + contentRoot + "; module = " + module + "; all files in index are: " + files + "; all possible files are: " + allFiles);
            }
            return false;
        }
    }
    return true;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) ProjectFileIndex(com.intellij.openapi.roots.ProjectFileIndex) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) XSourcePosition(com.intellij.xdebugger.XSourcePosition) SourcePosition(com.intellij.debugger.SourcePosition) EverythingGlobalScope(com.intellij.psi.search.EverythingGlobalScope) Module(com.intellij.openapi.module.Module)

Example 34 with SourcePosition

use of com.intellij.debugger.SourcePosition in project intellij-community by JetBrains.

the class SourceCodeChecker method checkAllClasses.

@TestOnly
@SuppressWarnings("UseOfSystemOutOrSystemErr")
private static void checkAllClasses(DebuggerContextImpl debuggerContext) {
    DebugProcessImpl process = debuggerContext.getDebugProcess();
    @SuppressWarnings("ConstantConditions") VirtualMachine machine = process.getVirtualMachineProxy().getVirtualMachine();
    // only default position manager for now
    PositionManagerImpl positionManager = new PositionManagerImpl(process);
    List<ReferenceType> types = machine.allClasses();
    System.out.println("Checking " + types.size() + " classes");
    int i = 0;
    for (ReferenceType type : types) {
        i++;
        try {
            for (Location loc : type.allLineLocations()) {
                SourcePosition position = ReadAction.compute(() -> {
                    try {
                        return positionManager.getSourcePosition(loc);
                    } catch (NoDataException ignore) {
                        return null;
                    }
                });
                if (position == null) {
                    continue;
                }
                if (position.getFile() instanceof PsiCompiledFile) {
                    VirtualFile file = position.getFile().getVirtualFile();
                    if (file == null || file.getUserData(LineNumbersMapping.LINE_NUMBERS_MAPPING_KEY) == null) {
                        // no mapping - skip the whole file
                        break;
                    }
                    if (DebuggerUtilsEx.bytecodeToSourceLine(position.getFile(), loc.lineNumber()) == -1) {
                        continue;
                    }
                }
                if (check(loc, position, process.getProject()) == ThreeState.NO) {
                    System.out.println("failed " + type);
                    break;
                }
            }
        } catch (AbsentInformationException ignored) {
        }
    }
    System.out.println("Done checking");
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) PositionManagerImpl(com.intellij.debugger.engine.PositionManagerImpl) DebugProcessImpl(com.intellij.debugger.engine.DebugProcessImpl) SourcePosition(com.intellij.debugger.SourcePosition) NoDataException(com.intellij.debugger.NoDataException) TestOnly(org.jetbrains.annotations.TestOnly)

Example 35 with SourcePosition

use of com.intellij.debugger.SourcePosition in project intellij-community by JetBrains.

the class SourceCodeChecker method check.

private static ThreeState check(Location location, SourcePosition position, Project project) {
    Method method = DebuggerUtilsEx.getMethod(location);
    // for now skip constructors, bridges, lambdas etc.
    if (method == null || method.isConstructor() || method.isSynthetic() || method.isBridge() || method.isStaticInitializer() || (method.declaringType() instanceof ClassType && ((ClassType) method.declaringType()).isEnum()) || DebuggerUtilsEx.isLambda(method)) {
        return ThreeState.UNSURE;
    }
    List<Location> locations = DebuggerUtilsEx.allLineLocations(method);
    if (ContainerUtil.isEmpty(locations)) {
        return ThreeState.UNSURE;
    }
    if (position != null) {
        return ReadAction.compute(() -> {
            PsiFile psiFile = position.getFile();
            if (!psiFile.getLanguage().isKindOf(JavaLanguage.INSTANCE)) {
                // only for java for now
                return ThreeState.UNSURE;
            }
            Document document = PsiDocumentManager.getInstance(project).getDocument(psiFile);
            if (document == null) {
                return ThreeState.UNSURE;
            }
            boolean res = false;
            PsiElement psiMethod = DebuggerUtilsEx.getContainingMethod(position);
            if (psiMethod != null) {
                TextRange range = psiMethod.getTextRange();
                if (psiMethod instanceof PsiDocCommentOwner) {
                    PsiDocComment comment = ((PsiDocCommentOwner) psiMethod).getDocComment();
                    if (comment != null) {
                        range = new TextRange(comment.getTextRange().getEndOffset() + 1, range.getEndOffset());
                    }
                }
                int startLine = document.getLineNumber(range.getStartOffset()) + 1;
                int endLine = document.getLineNumber(range.getEndOffset()) + 1;
                res = getLinesStream(locations, psiFile).allMatch(line -> startLine <= line && line <= endLine);
                if (!res) {
                    LOG.debug("Source check failed: Method " + method.name() + ", source: " + ((NavigationItem) psiMethod).getName() + "\nLines: " + getLinesStream(locations, psiFile).joining(", ") + "\nExpected range: " + startLine + "-" + endLine);
                }
            } else {
                LOG.debug("Source check failed: method " + method.name() + " not found in sources");
            }
            if (!res) {
                FileEditor editor = FileEditorManager.getInstance(project).getSelectedEditor(position.getFile().getVirtualFile());
                if (editor instanceof TextEditor) {
                    AppUIUtil.invokeOnEdt(() -> HintManager.getInstance().showErrorHint(((TextEditor) editor).getEditor(), DebuggerBundle.message("warning.source.code.not.match")));
                } else {
                    XDebugSessionImpl.NOTIFICATION_GROUP.createNotification(DebuggerBundle.message("warning.source.code.not.match"), NotificationType.WARNING).notify(project);
                }
                return ThreeState.NO;
            }
            return ThreeState.YES;
        });
    }
    return ThreeState.YES;
}
Also used : PsiDocComment(com.intellij.psi.javadoc.PsiDocComment) NavigationItem(com.intellij.navigation.NavigationItem) VirtualFile(com.intellij.openapi.vfs.VirtualFile) Document(com.intellij.openapi.editor.Document) ContainerUtil(com.intellij.util.containers.ContainerUtil) XDebugSessionImpl(com.intellij.xdebugger.impl.XDebugSessionImpl) ReadAction(com.intellij.openapi.application.ReadAction) FileEditorManager(com.intellij.openapi.fileEditor.FileEditorManager) PositionManagerImpl(com.intellij.debugger.engine.PositionManagerImpl) SuspendContextImpl(com.intellij.debugger.engine.SuspendContextImpl) Project(com.intellij.openapi.project.Project) DebuggerBundle(com.intellij.debugger.DebuggerBundle) Logger(com.intellij.openapi.diagnostic.Logger) JavaLanguage(com.intellij.lang.java.JavaLanguage) TextEditor(com.intellij.openapi.fileEditor.TextEditor) SuspendContextCommandImpl(com.intellij.debugger.engine.events.SuspendContextCommandImpl) StackFrameProxyImpl(com.intellij.debugger.jdi.StackFrameProxyImpl) EvaluateException(com.intellij.debugger.engine.evaluation.EvaluateException) NoDataException(com.intellij.debugger.NoDataException) AppUIUtil(com.intellij.ui.AppUIUtil) ThreeState(com.intellij.util.ThreeState) DebugProcessImpl(com.intellij.debugger.engine.DebugProcessImpl) TextRange(com.intellij.openapi.util.TextRange) FileEditor(com.intellij.openapi.fileEditor.FileEditor) NotificationType(com.intellij.notification.NotificationType) TestOnly(org.jetbrains.annotations.TestOnly) PsiDocComment(com.intellij.psi.javadoc.PsiDocComment) List(java.util.List) LineNumbersMapping(com.intellij.execution.filters.LineNumbersMapping) StreamEx(one.util.streamex.StreamEx) Registry(com.intellij.openapi.util.registry.Registry) com.sun.jdi(com.sun.jdi) IntStreamEx(one.util.streamex.IntStreamEx) com.intellij.psi(com.intellij.psi) HintManager(com.intellij.codeInsight.hint.HintManager) SourcePosition(com.intellij.debugger.SourcePosition) FileEditor(com.intellij.openapi.fileEditor.FileEditor) TextRange(com.intellij.openapi.util.TextRange) Document(com.intellij.openapi.editor.Document) NavigationItem(com.intellij.navigation.NavigationItem) TextEditor(com.intellij.openapi.fileEditor.TextEditor)

Aggregations

SourcePosition (com.intellij.debugger.SourcePosition)36 Project (com.intellij.openapi.project.Project)13 Nullable (org.jetbrains.annotations.Nullable)11 EvaluateException (com.intellij.debugger.engine.evaluation.EvaluateException)9 VirtualFile (com.intellij.openapi.vfs.VirtualFile)8 XSourcePosition (com.intellij.xdebugger.XSourcePosition)7 DebuggerContextImpl (com.intellij.debugger.impl.DebuggerContextImpl)6 StackFrameProxyImpl (com.intellij.debugger.jdi.StackFrameProxyImpl)6 Document (com.intellij.openapi.editor.Document)6 DebuggerContextCommandImpl (com.intellij.debugger.engine.events.DebuggerContextCommandImpl)5 DebugProcessImpl (com.intellij.debugger.engine.DebugProcessImpl)4 ExpressionEvaluator (com.intellij.debugger.engine.evaluation.expression.ExpressionEvaluator)4 DebuggerSession (com.intellij.debugger.impl.DebuggerSession)4 DebuggerTreeNodeImpl (com.intellij.debugger.ui.impl.watch.DebuggerTreeNodeImpl)4 Location (com.sun.jdi.Location)4 NotNull (org.jetbrains.annotations.NotNull)4 PsiElement (com.intellij.psi.PsiElement)3 DebuggerBundle (com.intellij.debugger.DebuggerBundle)2 DebuggerManagerEx (com.intellij.debugger.DebuggerManagerEx)2 NoDataException (com.intellij.debugger.NoDataException)2