Search in sources :

Example 26 with SourcePosition

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

the class RuntimeTypeEvaluator method evaluate.

@Nullable
protected PsiType evaluate(final EvaluationContextImpl evaluationContext) throws EvaluateException {
    Project project = evaluationContext.getProject();
    SourcePosition position = ContextUtil.getSourcePosition(evaluationContext);
    ExpressionEvaluator evaluator = DebuggerInvocationUtil.commitAndRunReadAction(project, new EvaluatingComputable<ExpressionEvaluator>() {

        public ExpressionEvaluator compute() throws EvaluateException {
            return EvaluatorBuilderImpl.getInstance().build(myElement, position);
        }
    });
    final Value value = evaluator.evaluate(evaluationContext);
    if (value != null) {
        return getCastableRuntimeType(project, value);
    }
    throw EvaluateExceptionUtil.createEvaluateException(DebuggerBundle.message("evaluation.error.surrounded.expression.null"));
}
Also used : Project(com.intellij.openapi.project.Project) EvaluateException(com.intellij.debugger.engine.evaluation.EvaluateException) SourcePosition(com.intellij.debugger.SourcePosition) Value(com.sun.jdi.Value) ExpressionEvaluator(com.intellij.debugger.engine.evaluation.expression.ExpressionEvaluator) Nullable(org.jetbrains.annotations.Nullable)

Example 27 with SourcePosition

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

the class BasicStepMethodFilter method locationMatches.

public boolean locationMatches(final DebugProcessImpl process, final Location location) throws EvaluateException {
    Method method = location.method();
    String name = method.name();
    if (!myTargetMethodName.equals(name)) {
        if (DebuggerUtilsEx.isLambdaName(name)) {
            SourcePosition position = process.getPositionManager().getSourcePosition(location);
            return ReadAction.compute(() -> {
                PsiElement psiMethod = DebuggerUtilsEx.getContainingMethod(position);
                if (psiMethod instanceof PsiLambdaExpression) {
                    PsiType type = ((PsiLambdaExpression) psiMethod).getFunctionalInterfaceType();
                    PsiMethod interfaceMethod = LambdaUtil.getFunctionalInterfaceMethod(type);
                    if (type != null && interfaceMethod != null && myTargetMethodName.equals(interfaceMethod.getName())) {
                        try {
                            return InheritanceUtil.isInheritor(type, myDeclaringClassName.getName(process).replace('$', '.'));
                        } catch (EvaluateException e) {
                            LOG.info(e);
                        }
                    }
                }
                return false;
            });
        }
        return false;
    }
    if (myTargetMethodSignature != null && !signatureMatches(method, myTargetMethodSignature.getName(process))) {
        return false;
    }
    if (method.isBridge()) {
        // skip bridge methods
        return false;
    }
    return DebuggerUtilsEx.isAssignableFrom(myDeclaringClassName.getName(process), location.declaringType());
}
Also used : EvaluateException(com.intellij.debugger.engine.evaluation.EvaluateException) SourcePosition(com.intellij.debugger.SourcePosition) Method(com.sun.jdi.Method)

Example 28 with SourcePosition

use of com.intellij.debugger.SourcePosition 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 29 with SourcePosition

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

the class JavaValueModifier method set.

protected void set(@NotNull final String expression, final XModificationCallback callback, final DebuggerContextImpl debuggerContext, final SetValueRunnable setValueRunnable) {
    final ProgressWindowWithNotification progressWindow = new ProgressWindowWithNotification(true, debuggerContext.getProject());
    final EvaluationContextImpl evaluationContext = myJavaValue.getEvaluationContext();
    SuspendContextCommandImpl askSetAction = new DebuggerContextCommandImpl(debuggerContext) {

        public Priority getPriority() {
            return Priority.HIGH;
        }

        public void threadAction(@NotNull SuspendContextImpl suspendContext) {
            ExpressionEvaluator evaluator;
            try {
                Project project = evaluationContext.getProject();
                SourcePosition position = ContextUtil.getSourcePosition(evaluationContext);
                PsiElement context = ContextUtil.getContextElement(evaluationContext, position);
                evaluator = DebuggerInvocationUtil.commitAndRunReadAction(project, new EvaluatingComputable<ExpressionEvaluator>() {

                    public ExpressionEvaluator compute() throws EvaluateException {
                        return EvaluatorBuilderImpl.build(new TextWithImportsImpl(CodeFragmentKind.EXPRESSION, expression), context, position, project);
                    }
                });
                setValue(expression, evaluator, evaluationContext, new SetValueRunnable() {

                    public void setValue(EvaluationContextImpl evaluationContext, Value newValue) throws ClassNotLoadedException, InvalidTypeException, EvaluateException, IncompatibleThreadStateException {
                        if (!progressWindow.isCanceled()) {
                            setValueRunnable.setValue(evaluationContext, newValue);
                        //node.calcValue();
                        }
                    }

                    public ReferenceType loadClass(EvaluationContextImpl evaluationContext, String className) throws InvocationException, ClassNotLoadedException, EvaluateException, IncompatibleThreadStateException, InvalidTypeException {
                        return setValueRunnable.loadClass(evaluationContext, className);
                    }
                });
                callback.valueModified();
            } catch (EvaluateException e) {
                callback.errorOccurred(e.getMessage());
            }
        //String initialString = "";
        //if (descriptor instanceof ValueDescriptorImpl) {
        //  Value currentValue = ((ValueDescriptorImpl) descriptor).getValue();
        //  if (currentValue instanceof StringReference) {
        //    initialString = DebuggerUtilsEx.getValueOrErrorAsString(debuggerContext.createEvaluationContext(), currentValue);
        //    initialString = initialString == null ? "" : "\"" + DebuggerUtilsEx.translateStringValue(initialString) + "\"";
        //  }
        //  else if (currentValue instanceof PrimitiveValue) {
        //    ValueLabelRenderer renderer = ((ValueDescriptorImpl) descriptor).getRenderer(debuggerContext.getDebugProcess());
        //    initialString = getDisplayableString((PrimitiveValue) currentValue, renderer instanceof NodeRenderer && HexRenderer.UNIQUE_ID.equals(renderer.getUniqueId()));
        //  }
        //
        //  final String initialString1 = initialString;
        //  final Project project = debuggerContext.getProject();
        //  DebuggerInvocationUtil.swingInvokeLater(project, new Runnable() {
        //    public void run() {
        //      showEditor(new TextWithImportsImpl(CodeFragmentKind.EXPRESSION, initialString1), node, debuggerContext, setValueRunnable);
        //    }
        //  });
        //}
        }
    };
    progressWindow.setTitle(DebuggerBundle.message("title.evaluating"));
    evaluationContext.getDebugProcess().getManagerThread().startProgress(askSetAction, progressWindow);
}
Also used : ProgressWindowWithNotification(com.intellij.openapi.progress.util.ProgressWindowWithNotification) SuspendContextCommandImpl(com.intellij.debugger.engine.events.SuspendContextCommandImpl) NotNull(org.jetbrains.annotations.NotNull) EvaluatingComputable(com.intellij.debugger.EvaluatingComputable) Project(com.intellij.openapi.project.Project) SourcePosition(com.intellij.debugger.SourcePosition) DebuggerContextCommandImpl(com.intellij.debugger.engine.events.DebuggerContextCommandImpl) PsiElement(com.intellij.psi.PsiElement)

Example 30 with SourcePosition

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

the class CompilingEvaluator method evaluate.

@Override
public Value evaluate(final EvaluationContext evaluationContext) throws EvaluateException {
    DebugProcess process = evaluationContext.getDebugProcess();
    EvaluationContextImpl autoLoadContext = ((EvaluationContextImpl) evaluationContext).createEvaluationContext(evaluationContext.getThisObject());
    autoLoadContext.setAutoLoadClasses(true);
    ClassLoaderReference classLoader = ClassLoadingUtils.getClassLoader(autoLoadContext, process);
    autoLoadContext.setClassLoader(classLoader);
    String version = ((VirtualMachineProxyImpl) process.getVirtualMachineProxy()).version();
    Collection<ClassObject> classes = compile(JdkVersionUtil.getVersion(version));
    defineClasses(classes, autoLoadContext, process, classLoader);
    try {
        // invoke base evaluator on call code
        SourcePosition position = ContextUtil.getSourcePosition(evaluationContext);
        ExpressionEvaluator evaluator = DebuggerInvocationUtil.commitAndRunReadAction(myProject, new EvaluatingComputable<ExpressionEvaluator>() {

            @Override
            public ExpressionEvaluator compute() throws EvaluateException {
                TextWithImports callCode = getCallCode();
                PsiElement copyContext = myData.getAnchor();
                CodeFragmentFactory factory = DebuggerUtilsEx.findAppropriateCodeFragmentFactory(callCode, copyContext);
                return factory.getEvaluatorBuilder().build(factory.createCodeFragment(callCode, copyContext, myProject), position);
            }
        });
        return evaluator.evaluate(autoLoadContext);
    } catch (Exception e) {
        throw new EvaluateException("Error during generated code invocation " + e, e);
    }
}
Also used : VirtualMachineProxyImpl(com.intellij.debugger.jdi.VirtualMachineProxyImpl) ClassObject(com.intellij.openapi.compiler.ClassObject) ClassLoaderReference(com.sun.jdi.ClassLoaderReference) ExpressionEvaluator(com.intellij.debugger.engine.evaluation.expression.ExpressionEvaluator) DebugProcess(com.intellij.debugger.engine.DebugProcess) SourcePosition(com.intellij.debugger.SourcePosition) PsiElement(com.intellij.psi.PsiElement)

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