frostwire / frostwire-jlibtorrent

A swig Java interface for libtorrent by the makers of FrostWire. Develop libtorrent based apps with the joy of coding in Java.
http://www.frostwire.com
MIT License
444 stars 137 forks source link

[1.2.0.18-RC10] Strange behaviour of overridden PeerInfo::init() method #205

Closed proninyaroslav closed 5 years ago

proninyaroslav commented 5 years ago

Hi. I needed to extend PeerInfo class and add some fields (port, pieces). I inherited this class, and also override init() method to fill in my own fields. I saw a warning comment. As a result, if I override init() method and call it in constructor, after this all fields of the superclass become null or zero (ip, client, etc). So I deleted the overridden method and fill fields in constructor, and it works great. Why is this happening?

aldenml commented 5 years ago

did you call super.init() in your override? and you should not need to call it in your constructor. In any case, I have seconds thoughts about this design, since it's an anti-pattern to call a virtual method in a constructor, more so if it's the "actual constructor" such a method.

proninyaroslav commented 5 years ago

I thought that calling super.init() in an overridden method is superfluous, as it is already done in the superclass constructor.

aldenml commented 5 years ago

actually, it's not :)

proninyaroslav commented 5 years ago

This is an example with problem:

public AdvancedPeerInfo(peer_info p) {
    super(p);
    init(p);
}

@Override
protected void init(peer_info p) {
    //init fields
}

An example without problem:

public AdvancedPeerInfo(peer_info p) {
    super(p);
    //init fields
}
aldenml commented 5 years ago

Make it like this

public AdvancedPeeeInfo(peer_info p) {
    super(p);
}

@Override
protected void init(peer_info p) {
    super.init(p);
    //your extra logic here
}
proninyaroslav commented 5 years ago

Thanks, every time I open new things in Java)