0

I'd like to know how to avoid this unchecked cast warning in eclipse even though it might not be necessary since I know what types i'm putting into the object. My CriteriaComboBox extends a RenderedComboBox which extends a JComboBox. My class is of type TEST.Criteria.

Can someone advise if this is just a bug in eclipse? Should I just add a suppress warning? Or am I not doing this correctly?

enter image description here

Here is the code snippet:

                        // identify the source dropdown component
                        Object source = evt.getSource();
                        CriteriaComboBox<TEST.Criteria> cBox = null;
                        if (source instanceof CriteriaComboBox) {
                            cBox = (CriteriaComboBox<TEST.Criteria>) source;
                        }

This also give me the same warning:

                        if (source instanceof CriteriaComboBox) {
                            cBox = (CriteriaComboBox<TEST.Criteria>) source;
                        }

My version of eclipse is 2021-12 (4.22.0) running on ubuntu.

simgineer
  • 1,754
  • 2
  • 22
  • 49
  • "since I know what types I'm putting", yes, you do. Your compiler, however, doesn't. evt.getSource() returns an Object, not (necessarily) one of type CriteriaComboBox. There is quite a bit of related code missing. But, indeed, just because the compiler checked that your object is of type CriteriaComboBox, it doesn't mean it checked the types added to it. – Stultuske Jan 04 '22 at 06:41
  • @Stultuske could you give me a hint at the related code I am missing? – simgineer Jan 04 '22 at 06:43
  • https://stackoverflow.com/questions/509076/how-do-i-address-unchecked-cast-warnings – 윤현구 Jan 04 '22 at 06:55
  • @simgineer we have no idea what "evt" is, we can only guess, yet you want us to know everything it does/return on a method call? – Stultuske Jan 04 '22 at 07:05
  • @Stultuske evt is an ActionEvent from an overridden actionPerformed method. Sorry guess that was specified in the attached image. – simgineer Jan 04 '22 at 07:10
  • You will just have to suppress this warning, there is no way to convince Java it is safe. – greg-449 Jan 04 '22 at 08:01

0 Answers0