-1

Can anybody explain to me why the following block of code is highlighted as a mistake in Android Studio?

The IDE is saying elem instanceof SubtypeB is always false - is this just a mistake by the inspector or is this a real language detail I need to learn about?. Can I safely suppress the warning and keep the current code?

Example code:

for (Parcelable elem : list) {
    try {
        listOfSubtypeA.add( (SubtypeA) elem );
    } catch (ClassCastException cce) {
        //Line below is highlighted as always false
        if (elem instanceof SubtypeB) {
            ... //just logging
        }
    }
}
Nick Cardoso
  • 20,807
  • 14
  • 73
  • 124
  • What relationship is there between `SubtypeB` and `Parcelable` ? – k5_ Sep 29 '19 at 19:59
  • SubtypeA/B implement Parcelable here – Nick Cardoso Sep 29 '19 at 20:05
  • I can't reproduce the problem. Do you get the error with your paraphrased version? Can you provide a minimal example with all relevant code? – k5_ Sep 29 '19 at 20:08
  • 1
    What is the relation between `SubtypeA`and `SubtypeB`? – Willem Sep 29 '19 at 20:08
  • @k5 it's trivial to reproduce, I've already given the minimal example but I added a picture just for you – Nick Cardoso Sep 29 '19 at 20:13
  • 1
    Please supply a complete program, as text not a picture. – Patricia Shanahan Sep 29 '19 at 20:15
  • If list actually contained a `SubtypeB` the if is reached an true. So the check is thrown off by something. As your picture seems to contain compile errors without compile error markings. It isn't that trustworthy. – k5_ Sep 29 '19 at 20:19
  • There's no compile errors, it's just marking the items as unused since it's just a demo block – Nick Cardoso Sep 29 '19 at 20:20
  • @Willem that's even more confusing then, but may be the answer to my question - the IDE is probably making a mistake (I've already tried invalidating caches) – Nick Cardoso Sep 29 '19 at 20:25
  • @Temporary I did not downvote. I prefer making suggestions for improving the question in comments. If you post a demo program as text, I can copy-paste it into an IDE window and compile exactly what you are compiling, with no risk of typos. If you post a partial program and I try to expand it to a compilable program there is a risk I'll make choices that prevent the problem. If you post a complete demo program, that risk is also eliminated. – Patricia Shanahan Sep 29 '19 at 20:26

1 Answers1

1

It is incorrect from your IDE. Just run it:

 interface Test123  { }
static class SubtypeA implements Test123{ }
static class SubtypeB implements Test123 { }

public static void main(String[] args) {
    List<Test123> list = new ArrayList<>();
    list.add(new SubtypeB());
    List<SubtypeA> subList = new ArrayList<>();

    for (Test123 elem : list) {
        try {
            subList.add( (SubtypeA) elem );
        } catch (ClassCastException cce) {
            //Line below is highlighted as always false
            if (elem instanceof SubtypeB) {
                System.out.println("cce = " + cce);
        //just logging
            }
        }
    }
}

You will see that the statement if (elem instanceof SubtypeB) turns out to be true so your IDE doesn't detect it correct

Willem
  • 992
  • 6
  • 13
  • 1
    This is what I'd expect, my own code also compiles, I just don't want to risk that it crashes in production. I tested your code out of curiosity and it is also highlighted in my IDE. I think I'm just going to suppress it, thanks – Nick Cardoso Sep 29 '19 at 20:31
  • I get that, maybe you can discuss it with your colleagues – Willem Sep 29 '19 at 20:47