• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Lost with Generics and Runtime Annotations

 
Jared Krull
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jared Krull
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21024
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Jared Krull
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4181
21
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah, I doubt there is a nice workaround either. That is how code in the API does similar things: for example java.util.EnumMap
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20514
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
 
Jared Krull
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic