Type of template if the template is the return value (Java)?

Apparently this special code for using template makes the return of getFromSessionMap a variable type and hence no need for a cast Fundamentally, there has to be a typecast somewhere between getting the result of session. GetAttribute(sessionKey) and the assignment to MyClassType The Java language (and the JVM) will not allow some object that is not a MyClassType instance (or a subtype thereof) to be assigned to a MyClassType variable No matter how you write the code a typecast has to occur And since the attribute (apparently) is not a MyClassType (or subtype), you are getting a ClassCastException So the real question is why aren't you getting a compilation error? And the answer is the SuppressWarnings("unchecked")!

If you removed that warning, you would get an "unsafe type conversion" error message for this line: return (T) session. GetAttribute(sessionKey) In truth, Java cannot do a (real) type cast to a generic type. And that is what the warning / error message is there to point out.In fact, once the code has been compiled, this code public T getFromSessionMap(String sessionKey) { return (T)session.

GetAttribute(sessionKey); } is actually no different in meaning from this: public Object getFromSessionMap(String sessionKey) { return session. GetAttribute(sessionKey); } Technically speaking this is called "type erasure So where is the type checking / typecast actually occurring? The answer is in this line: MyClassType type = request.

GetFromSessionMap("abc") Even though you haven't written a typecast here, the code generated by the Java compiler does a typecast before assigning the value to type It has to. Because as far as it knows, the instance it is assigning could be any object type Other posters have suggested adding a Class argument to getFromSessionMap By itself this does absolutely nothing If you also replace the body of the method with: return clazz. Cast(session.

GetAttribute(sessionKey)) you will cause the method to actually do a real type check. But this only cause ClassCastException to be thrown at a different place. And the assignment statement will still do a hidden type cast!

Apparently this special code for using template makes the return of getFromSessionMap a variable type and hence no need for a cast. Fundamentally, there has to be a typecast somewhere between getting the result of session. GetAttribute(sessionKey) and the assignment to MyClassType.

The Java language (and the JVM) will not allow some object that is not a MyClassType instance (or a subtype thereof) to be assigned to a MyClassType variable. No matter how you write the code, a typecast has to occur. And since the attribute (apparently) is not a MyClassType (or subtype), you are getting a ClassCastException.So the real question is why aren't you getting a compilation error?

And the answer is the @SuppressWarnings("unchecked")! If you removed that warning, you would get an "unsafe type conversion" error message for this line: return (T) session. GetAttribute(sessionKey); In truth, Java cannot do a (real) type cast to a generic type.

And that is what the warning / error message is there to point out. In fact, once the code has been compiled, this code public T getFromSessionMap(String sessionKey) { return (T)session. GetAttribute(sessionKey); } is actually no different in meaning from this: public Object getFromSessionMap(String sessionKey) { return session.

GetAttribute(sessionKey); } Technically speaking this is called "type erasure". So where is the type checking / typecast actually occurring? The answer is in this line: MyClassType type = request.

GetFromSessionMap("abc"); Even though you haven't written a typecast here, the code generated by the Java compiler does a typecast before assigning the value to type. It has to. Because as far as it knows, the instance it is assigning could be any object type.

Other posters have suggested adding a Class argument to getFromSessionMap. By itself this does absolutely nothing. If you also replace the body of the method with: return clazz.

Cast(session. GetAttribute(sessionKey)); you will cause the method to actually do a real type check. But this only cause ClassCastException to be thrown at a different place.

And the assignment statement will still do a hidden type cast!

– Nassign Nov 12 '09 at 10:36 1 T is not a type, T is a parameterized type. You cannot query what type T is at runtime (ala, type erasure). The code you have for putting in the attribute "abc" is not putting in a type of MyClassType (or it is being changed by something after being put into the sessionmap).

– Chii Nov 12 '09 at 10:47 @Nassign - read my answer carefully. There is no real typecast occurring when you write (T). What you are doing is an unsafe type conversion ... a no-op in this case.

– Stephen C Nov 12 '09 at 11:22 @Stephen C, thanks for the detailed explanation. I am wondering why the Java compiler even allowed making this construct even the class has no template class definition for the class. – Nassign Nov 12 '09 at 23:55 1 @Nassign - In Java, you don't need to declare a generic type parameter at the class level to be able to declare one at the method level.

– Stephen C Nov 12 '097 at 0:19.

In the question's example, the return type is T. The erased type will be Object as, implicitly, T extends Object. The actual cast is performed in the bytecode of the calling method (you can use javap -c to see that).

Generally, you should keep the number of "top-level" objects in sessions as small as possible. One of the benefits of doing that, is there is no longer a need for hacky methods like these.

Any help would be appreciated because my code encounters ClassCastException. If you're getting a ClassCastException, that means you're trying to cast something into something it isn't, like in the following code: Map session = new HashMap(); session. Put("date", "2009-11-12"); Date today = (Date) session.

Get("date"); // tries to convert String to Date The ClassCastException should have an enlightening detail message, like "java.lang. String cannot be cast to java.util. Date".

The thing is when I add to watch session. GetAttribute("abc"), it shows that the type is MyClassType. Yes, that would be the initial reaction to the class exception.

That was what I thought too. – Nassign Nov 12 '09 at 10:25 And the ClassCastException detail message is... what? – gustafc Nov 12 '09 at 11:51.

You use type cast within the method body ('(T)session. GetAttribute(sessionKey)'). That means that you explicitly say to compiler 'I'm absolutely sure that returned object IS-A T and ready to handle error if it is not'.

Here your assumption about attribute type was incorrect and you got an error. So, everything is correct and runtime already provides you with the real attribute object type that is not IS-A MyClassType.

Fundamentally, there has to be a typecast somewhere between getting the result of session. GetAttribute(sessionKey) and the assignment to MyClassType. The Java language (and the JVM) will not allow some object that is not a MyClassType instance (or a subtype thereof) to be assigned to a MyClassType variable.

No matter how you write the code, a typecast has to occur. And since the attribute (apparently) is not a MyClassType (or subtype), you are getting a ClassCastException. So the real question is why aren't you getting a compilation error?

And the answer is the @SuppressWarnings("unchecked")! In truth, Java cannot do a (real) type cast to a generic type. And that is what the warning / error message is there to point out.

Technically speaking this is called "type erasure". So where is the type checking / typecast actually occurring? Even though you haven't written a typecast here, the code generated by the Java compiler does a typecast before assigning the value to type.

It has to. Because as far as it knows, the instance it is assigning could be any object type. Other posters have suggested adding a Class argument to getFromSessionMap.

By itself this does absolutely nothing. You will cause the method to actually do a real type check. But this only cause ClassCastException to be thrown at a different place.

And the assignment statement will still do a hidden type cast!

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions