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 - Mark dead "instanceof" branch
Summary: Mark dead "instanceof" branch
Status: NEW
Alias: None
Product: java
Classification: Unclassified
Component: Hints (show other bugs)
Version: 6.x
Hardware: All All
: P3 blocker (vote)
Assignee: Svata Dedic
Depends on:
Reported: 2008-04-10 12:25 UTC by _ gtzabari
Modified: 2015-01-02 13:27 UTC (History)
1 user (show)

See Also:
Exception Reporter:


Note You need to log in before you can comment on or make changes to this bug.
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