Search in sources :

Example 6 with Method

use of com.sun.jdi.Method in project intellij-community by JetBrains.

the class PositionManagerImpl method getSourcePosition.

@Nullable
public SourcePosition getSourcePosition(final Location location) throws NoDataException {
    DebuggerManagerThreadImpl.assertIsManagerThread();
    if (location == null) {
        return null;
    }
    Project project = getDebugProcess().getProject();
    PsiFile psiFile = getPsiFileByLocation(project, location);
    if (psiFile == null) {
        return null;
    }
    LOG.assertTrue(myDebugProcess != null);
    int lineNumber = DebuggerUtilsEx.getLineNumber(location, true);
    String qName = location.declaringType().name();
    // replace file with alternative
    String altFileUrl = DebuggerUtilsEx.getAlternativeSourceUrl(qName, project);
    if (altFileUrl != null) {
        VirtualFile altFile = VirtualFileManager.getInstance().findFileByUrl(altFileUrl);
        if (altFile != null) {
            PsiFile altPsiFile = psiFile.getManager().findFile(altFile);
            if (altPsiFile != null) {
                psiFile = altPsiFile;
            }
        }
    }
    SourcePosition sourcePosition = null;
    if (lineNumber > -1) {
        sourcePosition = calcLineMappedSourcePosition(psiFile, lineNumber);
    }
    final Method method = DebuggerUtilsEx.getMethod(location);
    if (sourcePosition == null && (psiFile instanceof PsiCompiledElement || lineNumber < 0)) {
        if (method != null && method.name() != null && method.signature() != null) {
            PsiClass psiClass = findPsiClassByName(qName, null);
            PsiMethod compiledMethod = findMethod(psiClass != null ? psiClass : psiFile, qName, method.name(), method.signature());
            if (compiledMethod != null) {
                sourcePosition = SourcePosition.createFromElement(compiledMethod);
                if (lineNumber >= 0) {
                    sourcePosition = new ClsSourcePosition(sourcePosition, lineNumber);
                }
            }
        } else {
            return SourcePosition.createFromLine(psiFile, -1);
        }
    }
    if (sourcePosition == null) {
        sourcePosition = SourcePosition.createFromLine(psiFile, lineNumber);
    }
    int lambdaOrdinal = -1;
    if (DebuggerUtilsEx.isLambda(method)) {
        Set<Method> lambdas = ContainerUtil.map2SetNotNull(locationsOfLine(location.declaringType(), sourcePosition), location1 -> {
            Method method1 = location1.method();
            if (DebuggerUtilsEx.isLambda(method1)) {
                return method1;
            }
            return null;
        });
        if (lambdas.size() > 1) {
            ArrayList<Method> lambdasList = new ArrayList<>(lambdas);
            lambdasList.sort(DebuggerUtilsEx.LAMBDA_ORDINAL_COMPARATOR);
            lambdaOrdinal = lambdasList.indexOf(method);
        }
    }
    return new JavaSourcePosition(sourcePosition, location.declaringType(), method, lambdaOrdinal);
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) Method(com.sun.jdi.Method) Project(com.intellij.openapi.project.Project) SourcePosition(com.intellij.debugger.SourcePosition) Nullable(org.jetbrains.annotations.Nullable)

Example 7 with Method

use of com.sun.jdi.Method in project intellij-community by JetBrains.

the class PositionManagerImpl method findNested.

@Nullable
private ReferenceType findNested(final ReferenceType fromClass, final int currentDepth, final PsiClass classToFind, final int requiredDepth, final SourcePosition position) {
    ApplicationManager.getApplication().assertReadAccessAllowed();
    final VirtualMachineProxyImpl vmProxy = myDebugProcess.getVirtualMachineProxy();
    if (fromClass.isPrepared()) {
        if (currentDepth < requiredDepth) {
            final List<ReferenceType> nestedTypes = vmProxy.nestedTypes(fromClass);
            for (ReferenceType nested : nestedTypes) {
                final ReferenceType found = findNested(nested, currentDepth + 1, classToFind, requiredDepth, position);
                if (found != null) {
                    return found;
                }
            }
            return null;
        }
        int rangeBegin = Integer.MAX_VALUE;
        int rangeEnd = Integer.MIN_VALUE;
        for (Location location : DebuggerUtilsEx.allLineLocations(fromClass)) {
            final int lnumber = DebuggerUtilsEx.getLineNumber(location, false);
            if (lnumber <= 1) {
                // such locations are hardly correspond to real lines in code, so skipping them too
                continue;
            }
            final Method method = DebuggerUtilsEx.getMethod(location);
            if (method == null || DebuggerUtils.isSynthetic(method) || method.isBridge()) {
                // do not take into account synthetic stuff
                continue;
            }
            int locationLine = lnumber - 1;
            PsiFile psiFile = position.getFile().getOriginalFile();
            if (psiFile instanceof PsiCompiledFile) {
                locationLine = DebuggerUtilsEx.bytecodeToSourceLine(psiFile, locationLine);
                if (locationLine < 0)
                    continue;
            }
            rangeBegin = Math.min(rangeBegin, locationLine);
            rangeEnd = Math.max(rangeEnd, locationLine);
        }
        final int positionLine = position.getLine();
        if (positionLine >= rangeBegin && positionLine <= rangeEnd) {
            // First offsets belong to parent class, and offsets inside te substring "new Runnable(){" belong to anonymous runnable.
            if (!classToFind.isValid()) {
                return null;
            }
            Set<PsiClass> lineClasses = getLineClasses(position.getFile(), rangeEnd);
            if (lineClasses.size() > 1) {
                // if there's more than one class on the line - try to match by name
                for (PsiClass aClass : lineClasses) {
                    if (classToFind.equals(aClass)) {
                        return fromClass;
                    }
                }
            } else if (!lineClasses.isEmpty()) {
                return classToFind.equals(lineClasses.iterator().next()) ? fromClass : null;
            }
            return null;
        }
    }
    return null;
}
Also used : VirtualMachineProxyImpl(com.intellij.debugger.jdi.VirtualMachineProxyImpl) Method(com.sun.jdi.Method) ReferenceType(com.sun.jdi.ReferenceType) Location(com.sun.jdi.Location) Nullable(org.jetbrains.annotations.Nullable)

Example 8 with Method

use of com.sun.jdi.Method in project intellij-community by JetBrains.

the class RequestHint method getNextStepDepth.

public int getNextStepDepth(final SuspendContextImpl context) {
    try {
        final StackFrameProxyImpl frameProxy = context.getFrameProxy();
        // smart step feature stop check
        if (myMethodFilter != null && frameProxy != null && !(myMethodFilter instanceof BreakpointStepMethodFilter) && myMethodFilter.locationMatches(context.getDebugProcess(), frameProxy.location()) && !isTheSameFrame(context)) {
            myTargetMethodMatched = true;
            return myMethodFilter.onReached(context, this);
        }
        if ((myDepth == StepRequest.STEP_OVER || myDepth == StepRequest.STEP_INTO) && myPosition != null) {
            SourcePosition locationPosition = ContextUtil.getSourcePosition(context);
            if (locationPosition != null) {
                Integer resultDepth = ApplicationManager.getApplication().runReadAction(new Computable<Integer>() {

                    public Integer compute() {
                        if (myPosition.getFile().equals(locationPosition.getFile()) && isTheSameFrame(context) && !mySteppedOut) {
                            return isOnTheSameLine(locationPosition) ? myDepth : STOP;
                        }
                        return null;
                    }
                });
                if (resultDepth != null) {
                    return resultDepth.intValue();
                }
            }
        }
        // Now check filters
        final DebuggerSettings settings = DebuggerSettings.getInstance();
        if ((myMethodFilter != null || (settings.SKIP_SYNTHETIC_METHODS && !myIgnoreFilters)) && frameProxy != null) {
            final Location location = frameProxy.location();
            if (location != null) {
                if (DebuggerUtils.isSynthetic(location.method())) {
                    return myDepth;
                }
            }
        }
        if (!myIgnoreFilters) {
            if (settings.SKIP_GETTERS) {
                boolean isGetter = ApplicationManager.getApplication().runReadAction(new Computable<Boolean>() {

                    public Boolean compute() {
                        PsiElement contextElement = ContextUtil.getContextElement(context);
                        return contextElement != null && DebuggerUtils.isInsideSimpleGetter(contextElement);
                    }
                }).booleanValue();
                if (isGetter) {
                    return StepRequest.STEP_OUT;
                }
            }
            if (frameProxy != null) {
                if (settings.SKIP_CONSTRUCTORS) {
                    final Location location = frameProxy.location();
                    if (location != null) {
                        final Method method = location.method();
                        if (method != null && method.isConstructor()) {
                            return StepRequest.STEP_OUT;
                        }
                    }
                }
                if (settings.SKIP_CLASSLOADERS) {
                    final Location location = frameProxy.location();
                    if (location != null && DebuggerUtilsEx.isAssignableFrom("java.lang.ClassLoader", location.declaringType())) {
                        return StepRequest.STEP_OUT;
                    }
                }
            }
            for (ExtraSteppingFilter filter : ExtraSteppingFilter.EP_NAME.getExtensions()) {
                try {
                    if (filter.isApplicable(context))
                        return filter.getStepRequestDepth(context);
                } catch (Exception | AssertionError e) {
                    LOG.error(e);
                }
            }
        }
        // smart step feature
        if (myMethodFilter != null && !mySteppedOut) {
            return StepRequest.STEP_OUT;
        }
    } catch (VMDisconnectedException ignored) {
    } catch (EvaluateException e) {
        LOG.error(e);
    }
    return STOP;
}
Also used : StackFrameProxyImpl(com.intellij.debugger.jdi.StackFrameProxyImpl) DebuggerSettings(com.intellij.debugger.settings.DebuggerSettings) Method(com.sun.jdi.Method) EvaluateException(com.intellij.debugger.engine.evaluation.EvaluateException) VMDisconnectedException(com.sun.jdi.VMDisconnectedException) VMDisconnectedException(com.sun.jdi.VMDisconnectedException) EvaluateException(com.intellij.debugger.engine.evaluation.EvaluateException) SourcePosition(com.intellij.debugger.SourcePosition) Computable(com.intellij.openapi.util.Computable) PsiElement(com.intellij.psi.PsiElement) Location(com.sun.jdi.Location)

Example 9 with Method

use of com.sun.jdi.Method in project intellij-community by JetBrains.

the class UnBoxingEvaluator method convertToPrimitive.

private static Value convertToPrimitive(EvaluationContextImpl context, ObjectReference value, final String conversionMethodName, String conversionMethodSignature) throws EvaluateException {
    final DebugProcessImpl process = context.getDebugProcess();
    final ClassType wrapperClass = (ClassType) value.referenceType();
    Method method = wrapperClass.concreteMethodByName(conversionMethodName, conversionMethodSignature);
    if (method == null) {
        throw new EvaluateException("Cannot convert to primitive value of type " + value.type() + ": Unable to find method " + conversionMethodName + conversionMethodSignature);
    }
    return process.invokeMethod(context, value, method, Collections.emptyList());
}
Also used : EvaluateException(com.intellij.debugger.engine.evaluation.EvaluateException) DebugProcessImpl(com.intellij.debugger.engine.DebugProcessImpl) Method(com.sun.jdi.Method) ClassType(com.sun.jdi.ClassType)

Example 10 with Method

use of com.sun.jdi.Method in project intellij-community by JetBrains.

the class ForceEarlyReturnAction method actionPerformed.

public void actionPerformed(@NotNull AnActionEvent e) {
    final Project project = e.getProject();
    final JavaStackFrame stackFrame = PopFrameAction.getStackFrame(e);
    if (stackFrame == null || project == null) {
        return;
    }
    final DebuggerContextImpl debuggerContext = DebuggerAction.getDebuggerContext(e.getDataContext());
    final DebugProcessImpl debugProcess = debuggerContext.getDebugProcess();
    if (debugProcess == null) {
        return;
    }
    final StackFrameProxyImpl proxy = stackFrame.getStackFrameProxy();
    final ThreadReferenceProxyImpl thread = proxy.threadProxy();
    debugProcess.getManagerThread().schedule(new DebuggerContextCommandImpl(debuggerContext, thread) {

        @Override
        public void threadAction() {
            Method method;
            try {
                method = proxy.location().method();
            } catch (EvaluateException e) {
                showError(project, DebuggerBundle.message("error.early.return", e.getLocalizedMessage()));
                return;
            }
            if ("void".equals(method.returnTypeName())) {
                forceEarlyReturnWithFinally(thread.getVirtualMachine().mirrorOfVoid(), stackFrame, debugProcess, null);
            } else {
                ApplicationManager.getApplication().invokeLater(() -> new ReturnExpressionDialog(project, debugProcess.getXdebugProcess().getEditorsProvider(), debugProcess, stackFrame).show());
            }
        }
    });
}
Also used : Project(com.intellij.openapi.project.Project) StackFrameProxyImpl(com.intellij.debugger.jdi.StackFrameProxyImpl) EvaluateException(com.intellij.debugger.engine.evaluation.EvaluateException) DebugProcessImpl(com.intellij.debugger.engine.DebugProcessImpl) JavaStackFrame(com.intellij.debugger.engine.JavaStackFrame) DebuggerContextImpl(com.intellij.debugger.impl.DebuggerContextImpl) Method(com.sun.jdi.Method) ThreadReferenceProxyImpl(com.intellij.debugger.jdi.ThreadReferenceProxyImpl) DebuggerContextCommandImpl(com.intellij.debugger.engine.events.DebuggerContextCommandImpl)

Aggregations

Method (com.sun.jdi.Method)19 EvaluateException (com.intellij.debugger.engine.evaluation.EvaluateException)5 Location (com.sun.jdi.Location)5 ReferenceType (com.sun.jdi.ReferenceType)5 ClassType (com.sun.jdi.ClassType)4 SourcePosition (com.intellij.debugger.SourcePosition)3 Nullable (org.jetbrains.annotations.Nullable)3 DebugProcessImpl (com.intellij.debugger.engine.DebugProcessImpl)2 StackFrameProxyImpl (com.intellij.debugger.jdi.StackFrameProxyImpl)2 VirtualMachineProxyImpl (com.intellij.debugger.jdi.VirtualMachineProxyImpl)2 Project (com.intellij.openapi.project.Project)2 AbsentInformationException (com.sun.jdi.AbsentInformationException)2 ClassNotLoadedException (com.sun.jdi.ClassNotLoadedException)2 IncompatibleThreadStateException (com.sun.jdi.IncompatibleThreadStateException)2 InterfaceType (com.sun.jdi.InterfaceType)2 InvocationException (com.sun.jdi.InvocationException)2 ObjectReference (com.sun.jdi.ObjectReference)2 Value (com.sun.jdi.Value)2 BreakpointRequest (com.sun.jdi.request.BreakpointRequest)2 JVMName (com.intellij.debugger.engine.JVMName)1