Search in sources :

Example 11 with ReferenceType

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

the class TrackInstancesToggleAction method isSelected.

@Override
public boolean isSelected(AnActionEvent e) {
    ReferenceType selectedClass = getSelectedClass(e);
    final Project project = e.getProject();
    if (project != null && selectedClass != null && !project.isDisposed()) {
        InstancesTracker tracker = InstancesTracker.getInstance(project);
        return tracker.isTracked(selectedClass.name());
    }
    return false;
}
Also used : Project(com.intellij.openapi.project.Project) InstancesTracker(com.intellij.debugger.memory.component.InstancesTracker) ReferenceType(com.sun.jdi.ReferenceType)

Example 12 with ReferenceType

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

the class DefaultSourcePositionProvider method getSourcePositionForField.

@Nullable
private static SourcePosition getSourcePositionForField(@NotNull FieldDescriptor descriptor, @NotNull Project project, @NotNull DebuggerContextImpl context, boolean nearest) {
    final ReferenceType type = descriptor.getField().declaringType();
    final JavaPsiFacade facade = JavaPsiFacade.getInstance(project);
    final String fieldName = descriptor.getField().name();
    if (fieldName.startsWith(FieldDescriptorImpl.OUTER_LOCAL_VAR_FIELD_PREFIX)) {
        // this field actually mirrors a local variable in the outer class
        String varName = fieldName.substring(fieldName.lastIndexOf('$') + 1);
        PsiElement element = PositionUtil.getContextElement(context);
        if (element == null) {
            return null;
        }
        PsiClass aClass = PsiTreeUtil.getParentOfType(element, PsiClass.class, false);
        if (aClass == null) {
            return null;
        }
        PsiElement navigationElement = aClass.getNavigationElement();
        if (!(navigationElement instanceof PsiClass)) {
            return null;
        }
        aClass = (PsiClass) navigationElement;
        PsiVariable psiVariable = facade.getResolveHelper().resolveReferencedVariable(varName, aClass);
        if (psiVariable == null) {
            return null;
        }
        if (nearest) {
            return DebuggerContextUtil.findNearest(context, psiVariable, aClass.getContainingFile());
        }
        return SourcePosition.createFromElement(psiVariable);
    } else {
        final DebuggerSession session = context.getDebuggerSession();
        final GlobalSearchScope scope = session != null ? session.getSearchScope() : GlobalSearchScope.allScope(project);
        PsiClass aClass = facade.findClass(type.name().replace('$', '.'), scope);
        if (aClass == null) {
            // trying to search, assuming declaring class is an anonymous class
            final DebugProcessImpl debugProcess = context.getDebugProcess();
            if (debugProcess != null) {
                try {
                    final List<Location> locations = type.allLineLocations();
                    if (!locations.isEmpty()) {
                        // important: use the last location to be sure the position will be within the anonymous class
                        final Location lastLocation = locations.get(locations.size() - 1);
                        final SourcePosition position = debugProcess.getPositionManager().getSourcePosition(lastLocation);
                        aClass = JVMNameUtil.getClassAt(position);
                    }
                } catch (AbsentInformationException | ClassNotPreparedException ignored) {
                }
            }
        }
        if (aClass != null) {
            PsiField field = aClass.findFieldByName(fieldName, false);
            if (field == null)
                return null;
            if (nearest) {
                return DebuggerContextUtil.findNearest(context, field.getNavigationElement(), aClass.getContainingFile());
            }
            return SourcePosition.createFromElement(field);
        }
        return null;
    }
}
Also used : AbsentInformationException(com.sun.jdi.AbsentInformationException) ClassNotPreparedException(com.sun.jdi.ClassNotPreparedException) ReferenceType(com.sun.jdi.ReferenceType) DebuggerSession(com.intellij.debugger.impl.DebuggerSession) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) SourcePosition(com.intellij.debugger.SourcePosition) Location(com.sun.jdi.Location) Nullable(org.jetbrains.annotations.Nullable)

Example 13 with ReferenceType

use of com.sun.jdi.ReferenceType 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 14 with ReferenceType

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

the class JumpToTypeSourceAction method getPsiClass.

@Nullable
private PsiClass getPsiClass(AnActionEvent e) {
    final ReferenceType selectedClass = getSelectedClass(e);
    final Project project = e.getProject();
    if (selectedClass == null || project == null) {
        return null;
    }
    final ReferenceType targetClass = getObjectType(selectedClass);
    if (targetClass != null) {
        return DebuggerUtils.findClass(targetClass.name(), project, GlobalSearchScope.allScope(project));
    }
    return null;
}
Also used : Project(com.intellij.openapi.project.Project) ReferenceType(com.sun.jdi.ReferenceType) Nullable(org.jetbrains.annotations.Nullable)

Example 15 with ReferenceType

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

the class TypeEvaluator method evaluate.

/**
   * @return ReferenceType in the target VM, with the given fully qualified name
   */
public Object evaluate(EvaluationContextImpl context) throws EvaluateException {
    ClassLoaderReference classLoader = context.getClassLoader();
    ReferenceType lastRes = SoftReference.dereference(myLastResult);
    if (lastRes != null && classLoader == SoftReference.dereference(myLastClassLoader)) {
        // if class loader is null, check that vms match
        if (classLoader != null || lastRes.virtualMachine().equals(context.getDebugProcess().getVirtualMachineProxy().getVirtualMachine())) {
            return lastRes;
        }
    }
    DebugProcessImpl debugProcess = context.getDebugProcess();
    String typeName = myTypeName.getName(debugProcess);
    ReferenceType type = debugProcess.findClass(context, typeName, classLoader);
    if (type == null) {
        throw EvaluateExceptionUtil.createEvaluateException(DebuggerBundle.message("error.class.not.loaded", typeName));
    }
    myLastClassLoader = new WeakReference<>(classLoader);
    myLastResult = new WeakReference<>(type);
    return type;
}
Also used : DebugProcessImpl(com.intellij.debugger.engine.DebugProcessImpl) ClassLoaderReference(com.sun.jdi.ClassLoaderReference) ReferenceType(com.sun.jdi.ReferenceType)

Aggregations

ReferenceType (com.sun.jdi.ReferenceType)40 Nullable (org.jetbrains.annotations.Nullable)9 Project (com.intellij.openapi.project.Project)8 Location (com.sun.jdi.Location)5 ObjectReference (com.sun.jdi.ObjectReference)5 AbsentInformationException (com.sun.jdi.AbsentInformationException)4 Method (com.sun.jdi.Method)4 EvaluateException (com.intellij.debugger.engine.evaluation.EvaluateException)3 ClassesTable (com.intellij.debugger.memory.ui.ClassesTable)3 InstancesWindow (com.intellij.debugger.memory.ui.InstancesWindow)3 GlobalSearchScope (com.intellij.psi.search.GlobalSearchScope)3 XDebugSession (com.intellij.xdebugger.XDebugSession)3 SourcePosition (com.intellij.debugger.SourcePosition)2 DebugProcess (com.intellij.debugger.engine.DebugProcess)2 DebugProcessImpl (com.intellij.debugger.engine.DebugProcessImpl)2 InstancesTracker (com.intellij.debugger.memory.component.InstancesTracker)2 InstancesProvider (com.intellij.debugger.memory.utils.InstancesProvider)2 ClassPrepareRequestor (com.intellij.debugger.requests.ClassPrepareRequestor)2 AnActionEvent (com.intellij.openapi.actionSystem.AnActionEvent)2 PsiClass (com.intellij.psi.PsiClass)2