aspose file tools*
The moose likes Java in General and the fly likes Would like to get the Type of a member of a class, I'm boxing with Generic, java erasure Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Would like to get the Type of a member of a class, I Watch "Would like to get the Type of a member of a class, I New topic
Author

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

Stephane Clinckart
Ranch Hand

Joined: Oct 21, 2003
Posts: 89
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

Joined: Oct 27, 2005
Posts: 19543
    
  16

How about this:


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Stephane Clinckart
Ranch Hand

Joined: Oct 21, 2003
Posts: 89
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

Joined: Oct 26, 2010
Posts: 98
This?


Stephane Clinckart
Ranch Hand

Joined: Oct 21, 2003
Posts: 89
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

Joined: Aug 22, 2010
Posts: 3436
    
  47

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

Joined: Oct 21, 2003
Posts: 89
I advance a little on this problem:

I found this solution:


What you think about ???
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

Yes. Sorry I'm late, I was going to say that.
http://www.coderanch.com/t/518731/java/java/Generics-collections-reflection
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Would like to get the Type of a member of a class, I'm boxing with Generic, java erasure
 
Similar Threads
Spring 3
stuck up in the problem
Generic DAO Pattern - Subclass Entities
How to Use Spring AOP for Auditing?
EasyMock'ing JPA