Unchecked Cast

K

KDawg44

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);
}

When I go to compile, I first get this:

Note: PresenceServer.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.

(PresenceServer.java is the class the above code is in).

So I compile with the -Xlint option and I get this:


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?

Thanks for any suggestions.
 
K

KDawg44

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);
}

When I go to compile, I first get this:

Note: PresenceServer.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.

(PresenceServer.java is the class the above code is in).

So I compile with the -Xlint option and I get this:

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?

Thanks for any suggestions.

This appears to be just a warning so can I ignore it for the time
being? What causes this?

Thanks.
 
K

KDawg44

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);
}
When I go to compile, I first get this:
Note: PresenceServer.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
(PresenceServer.java is the class the above code is in).
So I compile with the -Xlint option and I get this:
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?
Thanks for any suggestions.

This appears to be just a warning so can I ignore it for the time
being? What causes this?

Thanks.

Okay... so there must be a reason for a warning.... This is now
causing my application to break....


public RegistrationInfo lookup(String username) {
StringKey retrieveKey = new StringKey(username);
try {
return (RegistrationInfo)
chord.retrieve(retrieveKey);
} catch (ServiceException e) {
throw new RuntimeException(e);
}

}


Exception:
java.lang.ClassCastException: java.util.HashSet
at ClientPackage.PresenceServer.lookup(PresenceServer.java:
107)
at ClientPackage.ChatClient.talk(ChatClient.java:47)
at ClientPackage.SendMessage.run(SendMessage.java:55)
at java.lang.Thread.run(Thread.java:595)


What is the correct way to cast this object back to the
RegistrationInfo Object?

Thanks.
 
J

Joshua Cranmer

KDawg44 said:
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.
 
N

Nigel Wade

KDawg44 said:
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);
}
When I go to compile, I first get this:
Note: PresenceServer.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
(PresenceServer.java is the class the above code is in).
So I compile with the -Xlint option and I get this:
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?
Thanks for any suggestions.

This appears to be just a warning so can I ignore it for the time
being? What causes this?

Thanks.

Okay... so there must be a reason for a warning.... This is now
causing my application to break....


public RegistrationInfo lookup(String username) {
StringKey retrieveKey = new StringKey(username);
try {
return (RegistrationInfo)
chord.retrieve(retrieveKey);
} catch (ServiceException e) {
throw new RuntimeException(e);
}

}


Exception:
java.lang.ClassCastException: java.util.HashSet
at ClientPackage.PresenceServer.lookup(PresenceServer.java:
107)
at ClientPackage.ChatClient.talk(ChatClient.java:47)
at ClientPackage.SendMessage.run(SendMessage.java:55)
at java.lang.Thread.run(Thread.java:595)

The reason for the warning is to cover exactly this eventuality, attempting to
cast to RegistrationInfo an object which isn't of the class RegistrationInfo.
The methodology your code is using has taken responsibility for ensuring this
cast is valid out of the hands of the compiler (via generics) and specifically
into your hands, hence the warning.
What is the correct way to cast this object back to the
RegistrationInfo Object?

If it's not a RegistrationInfo object there is no way to cast it to one. The
error is caused because the object which chord.retrieve(retrieveKey) returned
was not of class RegistrationInfo but you attempted to cast it into one. You
are not using generics so the compiler can't help. You need to check your code
(the code which you haven't shown us) and see what is being stored and
retrieved.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Staff online

Members online

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top