Java record debug

已回答

How can I set a breakpoint on a property (getter and setter) in a Java record?

0

Hello, it's not possible because a record class is just like an enum, it's a Java base type, eg below code:

public record Person (String name, String address) {
}

is compiled to something like this:

public final class Person extends Record {
    private final String name;
    
    private final String address;
    
    public Person(String name, String address) {
        this.name = name;
        this.address = address;
    }
    
    public final String toString() {
      ..
    }
    
    public final int hashCode() {
     ...
    }
    
    public final boolean equals(Object o) {
     ...
    }
    
    public String name() {
        return this.name;
    }
    
    public String address() {
        return this.address;
    }
}

However, it's not line to line corresponding to the source code, these extra code is created by the javac tool, so you can't put a breakpoint at the method inside it at the source code.

But if you still want need a get method and debug it, you can define it like this and put a break point in the getAddress() method.

public record Person (String name, String address) {
    public String getAddress() {
        return address;// Put a breakpoint here will work
    }
}

Hope this helps.

1

Thnx - That's how I did it so far.

0

请先登录再写评论。