• Post Reply Bookmark Topic Watch Topic
  • New Topic

return type with generic.class  RSS feed

 
Ellen Bee
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is a method (from apache camel) I want to call with the following signature

I need a FtpEndpoint that is a generic class:

So I make a variable of the desired type and call the method:

But now the returned value is of type FtpEndpoint instead of type FtpEndpoint<FTPFile> (Warning: Type safety: The expression of type FtpEndpoint needs unchecked conversion to conform to FtpEndpoint<FTPFile>).
I thought I had to do something like

but this is causing a compile error: "FtpEndpoint cannot be resolved to a variable"
How can I correctly call a method like this with generics?
 
Brian Burress
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For the code that gives you a warning, is the returned object what you expect? If so, then either try casting it to the type needed -or- ignore the warning (I am taking your wording literally - if it is a warning and you have verified the behavior is ok then you are good).
 
Wendy L Gibbons
Bartender
Posts: 1111
Eclipse IDE Oracle VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
it is very unlikely to be returning a FtpEndpoint<FTPFile>, as generics don't exist at runtime.
Have you tried looking at the source for camel?
 
Ellen Bee
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Brian Burress wrote:For the code that gives you a warning, is the returned object what you expect? If so, then either try casting it to the type needed -or- ignore the warning (I am taking your wording literally - if it is a warning and you have verified the behavior is ok then you are good).


Yes, the returned object is what I expect, I could cast it or ignore the warning. But I thought a advantage of using generics was to not having to cast in cases like this?

Wendy Gibbons wrote: it is very unlikely to be returning a FtpEndpoint<FTPFile>, as generics don't exist at runtime.
Have you tried looking at the source for camel?


I can remove the generic from the variable type like this:

But then I get the Warning: FtpEndpoint is a raw type. References to generic type FtpEndpoint<T> should be parameterized

The source for the method I like to call is:

But I don't see how this brings me any further.
 
Wendy L Gibbons
Bartender
Posts: 1111
Eclipse IDE Oracle VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ellen I just forward this thread to a friend of mine who is my generics guy and I am afraid here is his response

"Basically the guys is screwed in Java generics unless he casts or not uses the generics.

He wants to use a “class” parameter that itself is generic and existing java does not allow that.

You can do things like assign to non generic variable and then use that to assign to what he needs or possibly use a <?> wild card.
"

So sorry.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!