Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Debugger cannot see local variable in a Lambda

I noticed that when I hover my mouse over a local variable when my debugger is stopped inside a lambda it will report Cannot find local variable 'variable_name' even if it's visible inside the lambda and it's used.

Example code

public class Main {
    public static void main(String[] args) {
        String a = "hello_world";
        m1(a);
    }

    private static void m1(String a) {
        AccessController.doPrivileged((PrivilegedAction<String>) () -> {
            System.out.println("blala " + a);
            return "abc";
        });
    }
}

Try with a breakpoint in System.out.println("blala " + a); and after return "abc" and it always report the same error.

enter image description here

I used AccessController.doPrivileged because it's what I used in my original code and of course i'm using Java 8.

It says the same thing in Watchers and Evaluate Expression.

I tried using the "anonymous class" version and the debugger sees the value of a correctly

enter image description here

private static void m1(String a) {
    AccessController.doPrivileged(new PrivilegedAction<String>() {
        @Override
        public String run() {
            System.out.println("blala " + a);
            return "abc";
        }
    });
}

I'm missing something about lambda expressions or it's an IntellIJ IDEA 14 bug?

I don't want to report the bug right now because I already reported a bug that was caused by my code instead of IntellIJ IDEA, so I want to be sure before do something (and because I don't use Java 8 so often, so I could be wrong).

like image 276
Marco Acierno Avatar asked Nov 12 '14 20:11

Marco Acierno


People also ask

Can you pass local variables to lambda expressions?

A lambda expression can't define any new scope as an anonymous inner class does, so we can't declare a local variable with the same which is already declared in the enclosing scope of a lambda expression. Inside lambda expression, we can't assign any value to some local variable declared outside the lambda expression.

Why can't we manipulate a local variable inside a lambda?

3. Local Variables in Capturing Lambdas. start is a local variable, and we are trying to modify it inside of a lambda expression. The basic reason this won't compile is that the lambda is capturing the value of start, meaning making a copy of it.


2 Answers

This appears to be a know issue. According to JetBrains the root causes of this behavior is with the JDK. For more info see: IDEA-126257

like image 123
Mike Rylander Avatar answered Oct 20 '22 16:10

Mike Rylander


I can confirm what is written in IDEA bug report linked by Mike Rylander: this is a JDK bug and update to version 8u60_25 of the JDK solves it.

like image 38
Nicolas Nobelis Avatar answered Oct 20 '22 17:10

Nicolas Nobelis