Re: Unchecked Cast
KDawg44 wrote:
Hi,
I am getting an error when I compile my app that i hoping someone can
shed some light on. I am working with OpenChord and am getting a
Serialized object. I am trying to cast that to a RegistrationInfo
Object in my program. I did this by:
try {
return
(RegistrationInfo)chord.retrieve(retrieveKey);
} catch (ServiceException e) {
throw new RuntimeException(e);
}
A few points:
1. Include a SSCCE; here, the problem is actually very easy to tell, but
the remedy is impossible.
2. Are you really sure that this is what is going on? Since the advent
of Java 5 and generics, the use cases of explicit casting has gone down
dramatically. OOP discourages such casting as part of its framework.
PresenceServer.java:102: warning: [unchecked] unchecked cast
found : java.util.Set<java.io.Serializable>
required: ClientPackage.RegistrationInfo
return
(RegistrationInfo)chord.retrieve(retrieveKey);
^
1 warning
>
What am I doing wrong?
An unchecked cast is, to be brief, a cast that throws away generics
information and, as such, destroys some compile-time checking opportunities.
Here, the output is a Set of Serializable objects, which you are trying
to force into a RegistrationInfo object. Without having any access to
the code you are having problems with, I cannot say how to fix this, but
I can guess (especially given your later runtime exception):
You are using an incorrect API. Go to the documentation for the retrieve
method and find out what the proper calls should be.
--
Beware of bugs in the above code; I have only proved it correct, not
tried it. -- Donald E. Knuth