fb-contrib icon indicating copy to clipboard operation
fb-contrib copied to clipboard

false-positive for ITC_INHERITANCE_TYPE_CHECKING in overridden method

Open Vampire opened this issue 5 years ago • 5 comments

I implement a method in an interface that is not under my control. This method needs to do instanceof checks on the parameter to decide how to handle the event. I don't see any chance besides reflection to do this in another way. If the instanceof checks are done on the parameter of an @Override-annotated method, this rule should not report a violation.

Vampire avatar Oct 10 '19 14:10 Vampire

Can you give a code example?

ThrawnCA avatar Oct 11 '19 00:10 ThrawnCA

@Override isn't really the key point, it's that you don't own the package of the interface. I agree with your assessment, tho, but won't be all that easy to fix. In the interim, you can use the SuppressFBWarnings annotation.

mebigfatguy avatar Oct 11 '19 01:10 mebigfatguy

Sure @ThrawnCA:

@Override
public void onEvent(GenericEvent event) {
    if (event instanceof MessageReceivedEvent) {
        onMessageReceived((MessageReceivedEvent) event);
    } else if (event instanceof PrivateMessageReceivedEvent) {
        onPrivateMessageReceived((PrivateMessageReceivedEvent) event);
    }
}

You're right of course Dave, that if you control the interface, you could change the signature there.

Vampire avatar Oct 11 '19 10:10 Vampire

i'm assuming that MessageReceivedEvent and PrivateMessageReceiveEvent are not yours, if they were, you could just have them implement an interface like

interface VampireEvent {
    public void processEvent();
}

and just then do

public void onEvent(GenericEvent event) {
    VampireEvent ve = (VampireEvent) event;
    ve.processEvent();
}

mebigfatguy avatar Oct 12 '19 21:10 mebigfatguy

Yes, of course not, they are from the same lib as the interface. In this case it is JDA.

Vampire avatar Oct 12 '19 22:10 Vampire