First: I couldn't find any better sub-forum - if one of the mods thinks this
thread is better placed else - please move =D
As one who knows how to ask questions correctly - let me split this up in a few specific sections:
What I tried:
So I'm playing around with a few crypto stuff - and had to deal with all those obscure factories (as this fits my personal style - I really like how de-coupled all this is =D) - and obvious had to down-cast some by myself.
Here's a small snippet:
"encoded" is a byte-array holding a PKCS8 RSA private key - so while I myself know the data I'm reading in from file indeed really is a RSAPrivateCrtKey - the compiler simple can't. As an experienced programmer I know - risking RuntimeException to be thrown is bad coding style - I try to avoid them by checking.
What I expected:
So as I wrote my lines - I simple missed such check once - but noticed the compiler doesn't show me any warnings about any potential issues wich I expected.
What's my question:
Is the compiler simple somhow so smart to somehow check this cast somehow (AFAIK, a PKCS8 can contain more than just an RSA private key - at least DH, DSA and RSA - according to
doc about what a KeyPairGenerator is required to be able to return) so it knows this is safe - or is such a manual down-cast by itself defined as "nah, the programmer surely knows what s/he's doin - the compiler doesn't need to check this at all"?
My environment:
OS: Windows 7 x64
JDK: Oracle 8u144
editor: notepad
compiler: javac on cmd-terminal
The main reason why I post this question: I've asked google about "java unsafe cast" - but all showing up is only Generics related. So I expected to get any warning w/o the if-block - but somehow the compiler just seems to think: "ok - manual down-cast - programmer knows - my job here's done - nothing to do for me - kthxbai"?
I guess using an
IDE like Netbeans, Eclipse or others maybe would advert some warning about this "unsage" down-cast w/o any checking beforehand - but standard oracle compiler seems not to care at all.
Any hints?