Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Would like to get the Type of a member of a class, I'm boxing with Generic, java erasure

 
Stephane Clinckart
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I would like to write java code that is binded to my Entity declaration... and if I change it later, I don't have to refactor all my code.

Exemple: I have on Class Entity where the id is type "Integer".
If I change it to "Long", I don't want to rewrite all code binded.

Here is the code I want to make more "Generic":

AbstractDao


AbstractEntity


Entity


AbstractService


Now... the code I woul like to be more generic:


I would like to make "Long" generic in AbstractService<Entity, Long>

Thanks a lot for help.
 
Rob Spoor
Sheriff
Pie
Posts: 20511
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How about this:
 
Stephane Clinckart
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Prime wrote:How about this:


Yes... this works... but... you are just moving the problem a little bit more far.

The goal I try to achieve is to define only in one place the Type (Long) of the id inside the Entity.
--> So if I change it to Integer or something else... I need to do only in one place ad don't have to refactor all my code.

I showed the code with the "EntityService"... but other layers of my application will have the same problem (DO, DTO, DO2DTOMapper, ...)

Anyway... thank for help.

Hope someone hass a good idea to fix this problem.
 
Alex Hurtt
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This?


 
Stephane Clinckart
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alex Hurtt wrote:This?




May be I explain badly ? (i'm not english native).

My target is to have something like:



This way... the day the type of id change... all code binded to this type is automaticly changed without having to refactor.

Who know how to achieve this ???

Thanks a lot

 
Martin Vajsar
Sheriff
Posts: 3752
62
Chrome Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What about this:
?

Of course, you have to define fields and methods in the AbstractEntityService class, where you can use the PK type parameter.
 
Stephane Clinckart
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I advance a little on this problem:

I found this solution:


What you think about ???
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes. Sorry I'm late, I was going to say that.
http://www.coderanch.com/t/518731/java/java/Generics-collections-reflection
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic