This Bugzilla instance is a read-only archive of historic NetBeans bug reports. To report a bug in NetBeans please follow the project's instructions for reporting issues.

Bug 132511

Summary: Mark dead "instanceof" branch
Product: java Reporter: _ gtzabari <gtzabari>
Component: HintsAssignee: Svata Dedic <sdedic>
Status: NEW ---    
Severity: blocker CC: markiewb
Priority: P3    
Version: 6.x   
Hardware: All   
OS: All   
Issue Type: ENHANCEMENT Exception Reporter:

Description _ gtzabari 2008-04-10 12:25:02 UTC

if (Expression instanceof SomeType)

Netbeans should mark this branch if there is no way Expression will ever be of type SomeType. I would suggest either
marking the entire branch in gray to denote it is unused or flag it with a warning-hint.
Comment 1 Jan Lahoda 2008-04-24 06:14:23 UTC
Could you please be more specific about when the branch should be considered dead? AFAIK, a compile-time error occurs if
it is provable at compile-time that the instanceof condition can never be true.
Comment 2 _ gtzabari 2008-04-24 13:51:46 UTC
It is true that this is the case if Expression and SomeType are both concrete classes, but if one is an interface you
will not get a compile-time error because it is conceivably possible that in some distant future it might be true. Here
is a specific example:

ArrayList foo = null;
if (foo instanceof Map)
  System.out.println("No compile-time error");

There are two possible scenarios as far as I can tell:

1) Some project contains the above code. The branch will *never* execute because we know the runtime value of "foo" and
it can never be an instanceof Map (javac applies a very superficial check so it misses this fact). Netbeans should mark
this branch as gray to indicate it can be removed (or is a programming mistake).

2) Project A contains the following code:

public SomeMethod(ArrayList foo)
  if (foo instanceof Map)
    System.out.println("No compile-time error");

Project B depends on Project A passes in an implementation of ArrayList that implements Map. The code branch will then
be exercised. In such a case Netbeans can't mark the branch gray because it has no way of knowing what value is passed in.
Comment 3 markiewb 2015-01-02 13:27:11 UTC
@_gtzabari: Fixed in my own plugin.
Give it a try and feedback.

@Svata: Or do you like to have this integrated natively in NB? I only adapted some old code from Jan Lahoda to make it possible