This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Java in General and the fly likes Lost with Generics and Runtime Annotations Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Lost with Generics and Runtime Annotations" Watch "Lost with Generics and Runtime Annotations" New topic
Author

Lost with Generics and Runtime Annotations

Jared Krull
Ranch Hand

Joined: Sep 28, 2001
Posts: 32
I have a library that creates an manipulates lots of javabean based objects based on a mysql database (using IBatis).

Each type of object is created and has annotations that are accessible at runtime to give various string commands used to search, update, delete and add objects


I want to generalize manipulation of these objects to a specific class that will take the command annotations at runtime and provide typesafe methods for these commands.


Ideally, this Accessor class would harvest the command annotations at runtime and use them with the framework to manipulate objects. It uses generics to provide typesafe return types that I dont have to recast

The problem I have is that I cannot for the life of me work out how to access these annotations based on the "T" generic type. It seems that I cannot instantiate a class of type "T" or query its properties at runtime. The Generic benefits are locked at compile time and the annotations benefits are locked at runtime and trying to get them to work together is rather hard.

I'm more used to C++ Templates, so please forgive me if there's a simple answer to this.


C programmer gone astray
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

http://java.sun.com/docs/books/tutorial/java/generics/erasure.html

(And http://java.sun.com/j2se/1.5.0/docs/guide/language/generics.html)
Jared Krull
Ranch Hand

Joined: Sep 28, 2001
Posts: 32
There, name changed.

David Newton wrote:http://java.sun.com/docs/books/tutorial/java/generics/erasure.html

(And http://java.sun.com/j2se/1.5.0/docs/guide/language/generics.html)


Yeah... I know about type erasure, but does anyone have any suggestions on methods or patterns that work in Java to help deal with my problem?
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18538
    
  40

Hector Floyd wrote:
That screen name was fine in 2001 when I joined...ah well


I'm not surprised. It has been quite a while since your last post... regardless, many thanks for changing your screen name.

Hector Floyd wrote:
Yeah... I know about type erasure, but does anyone have any suggestions on methods or patterns that work in Java to help deal with my problem?


Sometime last year, I looked into this. Trying to get info on T into runtime. And failed miserably.

You probably noticed that you can't instantiate based on T. You can't type check T. So, there is no way to figure T out. I couldn't even get info from reflection. At one point, I thought I got it as reflection did return generic type data, but that was for the upper and lower bounds.

Sorry,
Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Jared Krull
Ranch Hand

Joined: Sep 28, 2001
Posts: 32
Hector Floyd wrote:

Yeah... I know about type erasure, but does anyone have any suggestions on methods or patterns that work in Java to help deal with my problem?


Sometime last year, I looked into this. Trying to get info on T into runtime. And failed miserably.

You probably noticed that you can't instantiate based on T. You can't type check T. So, there is no way to figure T out. I couldn't even get info from reflection. At one point, I thought I got it as reflection did return generic type data, but that was for the upper and lower bounds.

Sorry,
Henry


Hmm...seems like to make my original idea work I might have to pass the type twice: one for compile time one for runtime, ala



Ugly.
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4167
    
  21

Yeah, I doubt there is a nice workaround either. That is how code in the API does similar things: for example java.util.EnumMap


Steve
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

I vaguely remember something about Guice 2.0 and the ability to reify type erased doodads--see Guice20. May not be what you want or need, but it's interesting.
Jared Krull
Ranch Hand

Joined: Sep 28, 2001
Posts: 32
David Newton wrote:I vaguely remember something about Guice 2.0 and the ability to reify type erased doodads--see Guice20. May not be what you want or need, but it's interesting.


Oooh..now that looks promising. I'll have a good play around with that!

Thanks guys.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19654
    
  18

Hector Floyd wrote:

Ugly.

Ugly and error prone. The constructor signature should be changed to this, should you still want to use this:
Possibly that class can be <? extends T>, but you definitely want to make it bound by T. Otherwise, the following would be allowed:


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Jared Krull
Ranch Hand

Joined: Sep 28, 2001
Posts: 32
Rob Prime wrote:
Hector Floyd wrote:

Ugly.

Ugly and error prone. The constructor signature should be changed to this, should you still want to use this:
Possibly that class can be <? extends T>, but you definitely want to make it bound by T. Otherwise, the following would be allowed:


Noted, though the example I gave was trying to show the inelegance of the method not how best it should be employed.

I've had a play with Guice and it seems to do just what I wanted. Thanks for that David Newton, your a lifesaver

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Lost with Generics and Runtime Annotations
 
Similar Threads
Generics
<Generics> Is this a good use of Java Generics?! UPDATED: Another question included.
Generics
generics...
generics