hypfvieh / dbus-java

Improved version of java DBus library provided by freedesktop.org (https://dbus.freedesktop.org/doc/dbus-java/)
https://hypfvieh.github.io/dbus-java/
MIT License
185 stars 73 forks source link

SpotBugs analysis: MethodType.equals(Object) does not check for null #204

Closed ghost closed 1 year ago

ghost commented 1 year ago

From SpotBugs:

dbus-npe-04

The offending method:

    @Override
    public boolean equals(Object _o) {
        return _o.getClass().equals(MethodTuple.class) && ((MethodTuple) _o).name.equals(this.name) && ((MethodTuple) _o).sig.equals(this.sig);
    }

SpotBugs is correct, _o needs a null guard (by convention). The IDE generates the following code for hashCode, equals, and the constructor:

    public MethodTuple(final String _name, final String _sig) {
        name = _name;
        sig = Objects.requireNonNullElse(_sig, "");
        logger.trace("new MethodTuple({}, {})", name, sig);
    }

    @Override
    public boolean equals(final Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        final MethodTuple that = (MethodTuple) o;
        return Objects.equals(name, that.name) && Objects.equals(sig, that.sig);
    }

    @Override
    public int hashCode() {
        return Objects.hash(name, sig);
    }