Java debugger cannot call some default method implementations

I am encoding IntelliJ IDEA. When debugging my application, I cannot use some implementations of the default methods in the clock.

Here is a condensed example:

public class Friendship { interface Friend { default void sayHiTo(Friend friend) { System.out.println("Hi, " + friend.hashCode()); } default int amountOfHands() { return 2; } } public static class BasicFriend implements Friend { int numberOfFaces() { return 1; } } public static void main(String[] args) { System.out.println("Put a breakpoint here"); } } 

In the main() method, I set a breakpoint and set three hours:

 // Default interface method with dependency new BasicFriend().sayHiTo(new BasicFriend()) // Default interface method without dependency new BasicFriend().amountOfHands() // Class method new BasicFriend().numberOfFaces() 

The first hours throw a NoSuchMethodException , complaining that the Friendship$BasicFriend.sayHiTo() method does not exist.

The second hour is successful, but, strangely enough, it reports the boxed object { java.lang.Integer@537 } "2" instead of a simple primitive 2 .

The third chat reports primitive 1, as expected.

Why don't the first hours work? This is mistake? Is this related to the IDE? Is it because of some kind of conceptual error of the default methods? Should it work as I want in the first place? Is the strange result of the second shift somehow related to the problem in the first hours?

+6
source share
1 answer

Prior to JDK 8u40, the default methods and static interfaces were not supported by JDI (Java Debugger Interface), JDWP (Java Debugging Protocol), and JDB (standard Java debugger). This is a JDK-8042123 error , which is written as fixed in 8u40, and the corresponding inscription appears in the 8u40 release notes .

Upgrade to 8u40 or later to fix this problem, at least on the JDK side.

From the error description, it also seems to require changes on the debugger side to avoid casting com.sun.jdi.InterfaceType objects to com.sun.jdi.ClassType, but instead call the InterfaceType.invokeMethod () interface directly.

In a specific case, IntelliJ Suseika confirmed in a comment that 14.1.2 basically fixed the problem (with the exception of an unexpected box), although Mike Kobit still experiences this problem in this version with a ClassCastException suggesting an incorrect cast above.

+6
source

Source: https://habr.com/ru/post/986522/


All Articles