T.class on template class

N

nr

Hi,

I'm new to java's template/generic programming.

I'm using JPA and e.g. the find method of EntityManager:

// regular
Entity ent = em.find (Entity.class, key)

How can I make this when using find inside a template/generic ?
How to get the ".class" work ?

class Access<T>
{
public T findIt (int key)
{
// T.class does not work
return em.find (T.class, key) ;
}
}

Thanx a lot for help

NR
 
D

Daniel Pitts

nr said:
Hi,

I'm new to java's template/generic programming.
I don't know about "java", but "Java" doesn't have template programming,
that's primarily c++.
I'm using JPA and e.g. the find method of EntityManager:

// regular
Entity ent = em.find (Entity.class, key)

How can I make this when using find inside a template/generic ?
How to get the ".class" work ?

class Access<T>
{
public T findIt (int key)
{
// T.class does not work
return em.find (T.class, key) ;
}
}

Thanx a lot for help

NR

The problem here is with type erasure. T is not a "real" type, so
T.class is not valid. your Access<T> object needs to own a class object,
try this approach instead:

class Access<T> {
private Class<T> type;
public Access<T>(Class<T> type) {
this.type = type;
}

public T findIt(int key) {
return em.find(type, key);
}
}

Access<Entity> access = new Access<Entity>(Entity.class);

Entity e = access.findIt(3);
 
J

John B. Matthews

[...]
I'm new to java's template/generic programming.

Same here. I'm getting used to using generic types, but I'm still
learning how to write generic methods. I refer regularly to Joshua
Bloch's _Effective_Java_, especially chapter five on generics:

<http://java.sun.com/docs/books/effective/>

In particular, I see that "You must use raw types in class literals."
I'm using JPA and e.g. the find method of EntityManager:

// regular
Entity ent = em.find (Entity.class, key)

How can I make this when using find inside a template/generic ?
How to get the ".class" work ?

class Access<T>
{
public T findIt (int key)
{
// T.class does not work
return em.find (T.class, key) ;
}
}

I don't know JPA and EntityManager, but isn't the find() method already
generic? IIUC, it returns a value whose type is that of the class
literal named entityClass. I think you can pass the class literal as a
type token, mentioned by Bloch (item 29) and discussed here (in a
slightly different context):

<http://groups.google.com/group/comp.lang.java.help/browse_frm/thread/f23
08015239df83d>
 
L

Lew

Daniel said:
The problem here is with type erasure. T is not a "real" type, so
T.class is not valid. your Access<T> object needs to own a class object,
try this approach instead:

class Access<T> {
    private Class<T> type;
    public Access<T>(Class<T> type) {
       this.type = type;
    }
....
Access<Entity> access = new Access<Entity>(Entity.class);

Entity e = access.findIt(3);

See also:
<http://java.sun.com/docs/books/effective/generics.pdf>
from Joshua Bloch's /Effective Java/.
 
L

Lew

John said:
I don't know JPA and EntityManager, but isn't the find() method already generic?
Checking
<http://java.sun.com/javaee/5/docs/api/javax/persistence/
EntityManager.html#find(java.lang.Class,%20java.lang.Object)>

Yep, it sure is.
IIUC, it returns a value whose type is that of the class
literal named entityClass.

The 'Class <T>' method argument doesn't always need to be a class
literal, though that is one common way to pass the value for that
parameter. Any expression of the correct type for the parameter will
do.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,772
Messages
2,569,593
Members
45,111
Latest member
KetoBurn
Top