Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Class<T> get generic type at runtime CDI

594 views
Skip to first unread message

Leonan Luppi

unread,
Dec 19, 2014, 7:23:21 AM12/19/14
to
Hello,

I'm try to get generic class type but isn't work. Someone can help me? I'm using CDI Weld to Inject my DAO class with a generic type:


@Inject
private DAO<Agent> dao;

And my DAO Class:

public class DAO<T> {

public T findAll() {
CriteriaQuery<T> query = em.getCriteriaBuilder()
.createQuery(HERE_MY_GENERIC_CLASS);
query.select(query.from(HERE_MY_GENERIC_CLASS));
return (T) em.createQuery(query).getResultList();
}

}


How can i get at runtime? To make a query.

Robert Klemme

unread,
Dec 19, 2014, 9:30:14 AM12/19/14
to
Judging from the code you presented you need at least to return a
collection of T (e.g. Iterable<T>) and not a T.

Cheers

robert


--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/

Leonan Luppi

unread,
Dec 19, 2014, 10:13:14 AM12/19/14
to
Robert,

Sure i saw this after i've posted here. So How can I get my current T class in Runtime?

Sebastian

unread,
Dec 20, 2014, 9:20:38 AM12/20/14
to
Am 19.12.2014 16:13, schrieb Leonan Luppi:
> Em sexta-feira, 19 de dezembro de 2014 12h30min14s UTC-2, Robert Klemme escreveu:
>> On 19.12.2014 13:23, Leonan Luppi wrote:
>>> Hello,
>>>
>>> I'm try to get generic class type but isn't work. Someone can help
>>> me? I'm using CDI Weld to Inject my DAO class with a generic type:
>>>
[snip]
>>
>> Judging from the code you presented you need at least to return a
>> collection of T (e.g. Iterable<T>) and not a T.
>>
>> Cheers
>>
>> robert
>>
[snip]

Hava a look at https://github.com/jhalterman/typetools
I believe this library will solve your problem.

-- Sebastian

Andreas Leitgeb

unread,
Dec 20, 2014, 1:28:43 PM12/20/14
to
Leonan Luppi <leonan...@gmail.com> wrote:
> I'm try to get generic class type but isn't work.

That's not how generics work in Java.

A generic class (like your DAO) just doesn't know what it
got as its type parameter. To use this type at runtime you
must specify the information explicitly, either as a Class
typed parameter/property, or by use of a factory class (like
AgentFactory) that will implement all type-specific stuff.

A factory instance would then take the place of your "em", or
maybe that of what you obtain from .getCriteriaBuilder(),
except that it won't need the "HERE_MY_GENERIC_CLASS" argument
to it's methods.

> Someone can help me? I'm using CDI Weld to Inject my DAO class with a generic type:
> @Inject
> private DAO<Agent> dao;
>
> And my DAO Class:
> public class DAO<T> {
>
> public T findAll() {
> CriteriaQuery<T> query = em.getCriteriaBuilder()
> .createQuery(HERE_MY_GENERIC_CLASS);
> query.select(query.from(HERE_MY_GENERIC_CLASS));
> return (T) em.createQuery(query).getResultList();
> }
> }
> How can i get at runtime? To make a query.

return type of findAll has already been discussed in other subthread.

0 new messages