[code snippet] How to cast types without warnings?

When we want to convert one type to another we usually cast it using round brackets:

 Object result = calculateResults();
 Foo foo = (Foo)result;
 // ...

This will cause IntelliJ to show Warning about casting. We could help it by adding condition with instanceof operator:

if (result instanceof Foo) {
   Foo foo = (Foo)result;
   // ...
}

Another way of casting can be done using cast() method:

Foo foo = Foo.class.cast(result);

And thisĀ  an be combined with isInstance() method:

if (Foo.class.isInstance(result)) {
   Foo foo = Foo.class.cast(result);
   //...
}