Search in sources :

Example 1 with LocalVariableProxyImpl

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

the class ContextUtil method getContextElement.

@Nullable
public static PsiElement getContextElement(final StackFrameContext context, final SourcePosition position) {
    if (LOG.isDebugEnabled()) {
        final SourcePosition sourcePosition = getSourcePosition(context);
        LOG.assertTrue(Comparing.equal(sourcePosition, position));
    }
    return ReadAction.compute(() -> {
        final PsiElement element = getContextElement(position);
        if (element == null) {
            return null;
        }
        // further code is java specific, actually
        if (element.getLanguage().getAssociatedFileType() != DefaultCodeFragmentFactory.getInstance().getFileType()) {
            return element;
        }
        final StackFrameProxyImpl frameProxy = (StackFrameProxyImpl) context.getFrameProxy();
        if (frameProxy == null) {
            return element;
        }
        try {
            List<LocalVariableProxyImpl> list = frameProxy.visibleVariables();
            PsiResolveHelper resolveHelper = JavaPsiFacade.getInstance(element.getProject()).getResolveHelper();
            StringBuilder buf = null;
            for (LocalVariableProxyImpl localVariable : list) {
                final String varName = localVariable.name();
                if (resolveHelper.resolveReferencedVariable(varName, element) == null) {
                    if (buf == null) {
                        buf = new StringBuilder("{");
                    }
                    buf.append(localVariable.getVariable().typeName()).append(" ").append(varName).append(";");
                }
            }
            if (buf == null) {
                return element;
            }
            buf.append('}');
            final PsiElementFactory elementFactory = JavaPsiFacade.getInstance(element.getProject()).getElementFactory();
            final PsiCodeBlock codeBlockFromText = elementFactory.createCodeBlockFromText(buf.toString(), element);
            final PsiStatement[] statements = codeBlockFromText.getStatements();
            for (PsiStatement statement : statements) {
                if (statement instanceof PsiDeclarationStatement) {
                    PsiDeclarationStatement declStatement = (PsiDeclarationStatement) statement;
                    PsiElement[] declaredElements = declStatement.getDeclaredElements();
                    for (PsiElement declaredElement : declaredElements) {
                        declaredElement.putUserData(IS_JSP_IMPLICIT, Boolean.TRUE);
                    }
                }
            }
            return codeBlockFromText;
        } catch (IncorrectOperationException | EvaluateException ignored) {
            return element;
        }
    });
}
Also used : StackFrameProxyImpl(com.intellij.debugger.jdi.StackFrameProxyImpl) LocalVariableProxyImpl(com.intellij.debugger.jdi.LocalVariableProxyImpl) EvaluateException(com.intellij.debugger.engine.evaluation.EvaluateException) SourcePosition(com.intellij.debugger.SourcePosition) IncorrectOperationException(com.intellij.util.IncorrectOperationException) Nullable(org.jetbrains.annotations.Nullable)

Example 2 with LocalVariableProxyImpl

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

the class JavaStackFrame method buildVariables.

// copied from FrameVariablesTree
private void buildVariables(DebuggerContextImpl debuggerContext, final EvaluationContextImpl evaluationContext, @NotNull DebugProcessImpl debugProcess, XValueChildrenList children, ObjectReference thisObjectReference, Location location) throws EvaluateException {
    final Set<String> visibleLocals = new HashSet<>();
    if (NodeRendererSettings.getInstance().getClassRenderer().SHOW_VAL_FIELDS_AS_LOCAL_VARIABLES) {
        if (thisObjectReference != null && debugProcess.getVirtualMachineProxy().canGetSyntheticAttribute()) {
            final ReferenceType thisRefType = thisObjectReference.referenceType();
            if (thisRefType instanceof ClassType && location != null && thisRefType.equals(location.declaringType()) && thisRefType.name().contains("$")) {
                // makes sense for nested classes only
                for (Field field : thisRefType.fields()) {
                    if (DebuggerUtils.isSynthetic(field) && StringUtil.startsWith(field.name(), FieldDescriptorImpl.OUTER_LOCAL_VAR_FIELD_PREFIX)) {
                        final FieldDescriptorImpl fieldDescriptor = myNodeManager.getFieldDescriptor(myDescriptor, thisObjectReference, field);
                        children.add(JavaValue.create(fieldDescriptor, evaluationContext, myNodeManager));
                        visibleLocals.add(fieldDescriptor.calcValueName());
                    }
                }
            }
        }
    }
    boolean myAutoWatchMode = DebuggerSettings.getInstance().AUTO_VARIABLES_MODE;
    if (evaluationContext == null) {
        return;
    }
    try {
        if (!XDebuggerSettingsManager.getInstance().getDataViewSettings().isAutoExpressions() && !myAutoWatchMode) {
            // optimization
            superBuildVariables(evaluationContext, children);
        } else {
            final SourcePosition sourcePosition = debuggerContext.getSourcePosition();
            final Map<String, LocalVariableProxyImpl> visibleVariables = ContainerUtil.map2Map(getVisibleVariables(), var -> Pair.create(var.name(), var));
            Pair<Set<String>, Set<TextWithImports>> usedVars = EMPTY_USED_VARS;
            if (sourcePosition != null) {
                usedVars = ApplicationManager.getApplication().runReadAction(new Computable<Pair<Set<String>, Set<TextWithImports>>>() {

                    @Override
                    public Pair<Set<String>, Set<TextWithImports>> compute() {
                        return findReferencedVars(ContainerUtil.union(visibleVariables.keySet(), visibleLocals), sourcePosition);
                    }
                });
            }
            // add locals
            if (myAutoWatchMode) {
                for (String var : usedVars.first) {
                    LocalVariableProxyImpl local = visibleVariables.get(var);
                    if (local != null) {
                        children.add(JavaValue.create(myNodeManager.getLocalVariableDescriptor(null, local), evaluationContext, myNodeManager));
                    }
                }
            } else {
                superBuildVariables(evaluationContext, children);
            }
            final EvaluationContextImpl evalContextCopy = evaluationContext.createEvaluationContext(evaluationContext.getThisObject());
            evalContextCopy.setAutoLoadClasses(false);
            if (sourcePosition != null) {
                Set<TextWithImports> extraVars = computeExtraVars(usedVars, sourcePosition, evaluationContext);
                // add extra vars
                addToChildrenFrom(extraVars, children, evaluationContext);
            }
            // add expressions
            addToChildrenFrom(usedVars.second, children, evalContextCopy);
        }
    } catch (EvaluateException e) {
        if (e.getCause() instanceof AbsentInformationException) {
            children.add(LOCAL_VARIABLES_INFO_UNAVAILABLE_MESSAGE_NODE);
            // trying to collect values from variable slots
            try {
                for (Map.Entry<DecompiledLocalVariable, Value> entry : LocalVariablesUtil.fetchValues(getStackFrameProxy(), debugProcess, true).entrySet()) {
                    children.add(JavaValue.create(myNodeManager.getArgumentValueDescriptor(null, entry.getKey(), entry.getValue()), evaluationContext, myNodeManager));
                }
            } catch (Exception ex) {
                LOG.info(ex);
            }
        } else {
            throw e;
        }
    }
}
Also used : LocalVariableProxyImpl(com.intellij.debugger.jdi.LocalVariableProxyImpl) EvaluateException(com.intellij.debugger.engine.evaluation.EvaluateException) EvaluateException(com.intellij.debugger.engine.evaluation.EvaluateException) EvaluationContextImpl(com.intellij.debugger.engine.evaluation.EvaluationContextImpl) XSourcePosition(com.intellij.xdebugger.XSourcePosition) SourcePosition(com.intellij.debugger.SourcePosition) TextWithImports(com.intellij.debugger.engine.evaluation.TextWithImports)

Example 3 with LocalVariableProxyImpl

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

the class LocalVariableDescriptorImpl method getModifier.

@Override
public XValueModifier getModifier(JavaValue value) {
    return new JavaValueModifier(value) {

        @Override
        protected void setValueImpl(@NotNull String expression, @NotNull XModificationCallback callback) {
            final LocalVariableProxyImpl local = LocalVariableDescriptorImpl.this.getLocalVariable();
            if (local != null) {
                final DebuggerContextImpl debuggerContext = DebuggerManagerEx.getInstanceEx(getProject()).getContext();
                set(expression, callback, debuggerContext, new SetValueRunnable() {

                    public void setValue(EvaluationContextImpl evaluationContext, Value newValue) throws ClassNotLoadedException, InvalidTypeException, EvaluateException {
                        debuggerContext.getFrameProxy().setValue(local, preprocessValue(evaluationContext, newValue, local.getType()));
                        update(debuggerContext);
                    }

                    public ReferenceType loadClass(EvaluationContextImpl evaluationContext, String className) throws InvocationException, ClassNotLoadedException, IncompatibleThreadStateException, InvalidTypeException, EvaluateException {
                        return evaluationContext.getDebugProcess().loadClass(evaluationContext, className, evaluationContext.getClassLoader());
                    }
                });
            }
        }
    };
}
Also used : LocalVariableProxyImpl(com.intellij.debugger.jdi.LocalVariableProxyImpl) NotNull(org.jetbrains.annotations.NotNull) EvaluateException(com.intellij.debugger.engine.evaluation.EvaluateException) JavaValueModifier(com.intellij.debugger.engine.JavaValueModifier) EvaluationContextImpl(com.intellij.debugger.engine.evaluation.EvaluationContextImpl) JavaValue(com.intellij.debugger.engine.JavaValue) DebuggerContextImpl(com.intellij.debugger.impl.DebuggerContextImpl)

Aggregations

EvaluateException (com.intellij.debugger.engine.evaluation.EvaluateException)3 LocalVariableProxyImpl (com.intellij.debugger.jdi.LocalVariableProxyImpl)3 SourcePosition (com.intellij.debugger.SourcePosition)2 EvaluationContextImpl (com.intellij.debugger.engine.evaluation.EvaluationContextImpl)2 JavaValue (com.intellij.debugger.engine.JavaValue)1 JavaValueModifier (com.intellij.debugger.engine.JavaValueModifier)1 TextWithImports (com.intellij.debugger.engine.evaluation.TextWithImports)1 DebuggerContextImpl (com.intellij.debugger.impl.DebuggerContextImpl)1 StackFrameProxyImpl (com.intellij.debugger.jdi.StackFrameProxyImpl)1 IncorrectOperationException (com.intellij.util.IncorrectOperationException)1 XSourcePosition (com.intellij.xdebugger.XSourcePosition)1 NotNull (org.jetbrains.annotations.NotNull)1 Nullable (org.jetbrains.annotations.Nullable)1