Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #30594 > unrolled thread

Class<T> get generic type at runtime CDI

Started byLeonan Luppi <leonan.luppi@gmail.com>
First post2014-12-19 04:23 -0800
Last post2014-12-20 18:28 +0000
Articles 5 — 4 participants

Back to article view | Back to comp.lang.java.programmer


Contents

  Class<T> get generic type at runtime CDI Leonan Luppi <leonan.luppi@gmail.com> - 2014-12-19 04:23 -0800
    Re: Class<T> get generic type at runtime CDI Robert Klemme <shortcutter@googlemail.com> - 2014-12-19 15:29 +0100
      Re: Class<T> get generic type at runtime CDI Leonan Luppi <leonan.luppi@gmail.com> - 2014-12-19 07:13 -0800
        Re: Class<T> get generic type at runtime CDI Sebastian <news@seyweiler.dyndns.org> - 2014-12-20 15:20 +0100
    Re: Class<T> get generic type at runtime CDI Andreas Leitgeb <avl@auth.logic.tuwien.ac.at> - 2014-12-20 18:28 +0000

#30594 — Class<T> get generic type at runtime CDI

FromLeonan Luppi <leonan.luppi@gmail.com>
Date2014-12-19 04:23 -0800
SubjectClass<T> get generic type at runtime CDI
Message-ID<be034564-30a4-4be6-82fa-2dc7717b70c9@googlegroups.com>
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.

[toc] | [next] | [standalone]


#30595

FromRobert Klemme <shortcutter@googlemail.com>
Date2014-12-19 15:29 +0100
Message-ID<cfir38Fg52fU1@mid.individual.net>
In reply to#30594
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:
>
>
> @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.

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/

[toc] | [prev] | [next] | [standalone]


#30597

FromLeonan Luppi <leonan.luppi@gmail.com>
Date2014-12-19 07:13 -0800
Message-ID<fb6aebf7-3cc3-4957-9283-3bd96cf0848a@googlegroups.com>
In reply to#30595
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:
> >
> >
> > @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.
> 
> 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/


Robert,

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

[toc] | [prev] | [next] | [standalone]


#30601

FromSebastian <news@seyweiler.dyndns.org>
Date2014-12-20 15:20 +0100
Message-ID<m740j3$55o$1@news.albasani.net>
In reply to#30597
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

[toc] | [prev] | [next] | [standalone]


#30602

FromAndreas Leitgeb <avl@auth.logic.tuwien.ac.at>
Date2014-12-20 18:28 +0000
Message-ID<slrnm9bfub.uoi.avl@login.logic.tuwien.ac.at>
In reply to#30594
Leonan Luppi <leonan.luppi@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.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.java.programmer


csiph-web