Avoid undefined premature downcast to potentially wrong type

Change-Id: I53bf8b74edab720fccc829f9228f7d9c4a8c2dad