Re: Problems with class hierarchy on Guice object

187 views
Skip to first unread message

Stuart McCulloch

unread,
Jun 26, 2012, 9:25:44 AM6/26/12
to google...@googlegroups.com

On 26 Jun 2012, at 13:53, André Salvati wrote:

I'm trying to use Guice to instantiate my Objectify DAOs. My hierarchy is the following:

public class EmpresaDao extends ObjectifyDao<Empresa> { ... }

public class ObjectifyDao<T> extends DAOBase { ... }

When I use "new EmpresaDao()", getClass().getGenericSuperclass() gives me:

    [INFO] superclass -> br.com.xxxxx.server.service.ObjectifyDao<br.com.xxxxx.domain.Empresa>

When I use "injector.getInstance(EmpresaDao.class)", getClass().getGenericSuperclass() gives me:

    [INFO] superclass -> class br.com.xxxx.server.service.EmpresaDao

Obviously, I want to let Guice instantiate my objects with DI.

Can someone explain why this is happen?

What does instance.getClass() show when using the injector?  If the name contains "ByGuice" then Guice has created an intermediate proxy, which is why the superclass differs.

Guice creates proxies to handle method interception (AOP) or to break circular dependencies - you could try turning off the circular dependency proxy feature in your module:


or alternatively if you don't need AOP then you can use the "no_aop" flavour of Guice. 

Is there any way (instantiating with Guice) to get the same superclass as with new().

Thanks.


--
You received this message because you are subscribed to the Google Groups "google-guice" group.
To view this discussion on the web visit https://groups.google.com/d/msg/google-guice/-/nD9OpdD__GEJ.
To post to this group, send email to google...@googlegroups.com.
To unsubscribe from this group, send email to google-guice...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/google-guice?hl=en.

Stuart McCulloch

unread,
Jun 26, 2012, 12:45:58 PM6/26/12
to google...@googlegroups.com
On 26 Jun 2012, at 17:31, André Salvati wrote:

Thanks Stuart.

I've tried to disable circular proxies this way and got same results.

Well circular proxies are only one of the possibilities I mentioned - the other is using AOP (such as method interception).

Based on the instance class name below (which has EnhancerByGuice) it looks like you, or something you're using, has enabled method interception for that class.
I'm guessing that it's related to guice-persist, which uses method interception to add transactional behaviour to classes or methods annotated with @Transactional.

If so then this is working-as-designed... in order to add the aspect behaviour the class must be extended, which will then add an extra class to the class hierarchy.

Is there a reason why you depend on a specific getGenericSuperclass? Usually such code can be improved to handle proxies by searching further up the hierarchy. 

Is this the right way??

new ServletModule() {

@Override
protected void configureServlets() {
binder().disableCircularProxies();
...
 
bind(EmpresaDao.class).in(RequestScoped.class)


[INFO] new()
[INFO] class -> class br.com.noxxonsat.server.service.EmpresaDao
[INFO] superclass -> br.com.noxxonsat.server.service.ObjectifyDao<br.com.noxxonsat.domain.Empresa>

[INFO] Guice
[INFO] class -> class br.com.noxxonsat.server.service.EmpresaDao$$EnhancerByGuice$$12c0765f
[INFO] superclass -> class br.com.noxxonsat.server.service.EmpresaDao


To unsubscribe from this group, send email to google-guice+unsubscribe@googlegroups.com.

For more options, visit this group at http://groups.google.com/group/google-guice?hl=en.
--
You received this message because you are subscribed to the Google Groups "google-guice" group.
To view this discussion on the web visit https://groups.google.com/d/msg/google-guice/-/c9TFlVtmfI0J.

Stuart McCulloch

unread,
Jun 26, 2012, 2:23:20 PM6/26/12
to google...@googlegroups.com
On 26 Jun 2012, at 18:48, André Salvati wrote:

This is related to an example posted by David Chandler using RequestFactory (GWT) and Objectify.


getGenericSuperclass() is used here ir order to get the business object which the DAO manipulates. I noticed in debugger that inside guiceObj.getGenericSuperclass() there is a "genericInfo" field which contains the business object. Do you know how can I access it?

There are various third-party libraries available for accessing generic types, or you can take advantage of Guice's TypeLiteral:

   Type genericSuperclass = TypeLiteral.get( instance.getClass() ).getSupertype( ObjectifyDao.class ).getType();

This will give you the generic ObjectifyDao type with the appropriate type arguments filled in based on the implementation type. 

public class ObjectifyDao<T> extends DAOBase
{

static final int BAD_MODIFIERS = Modifier.FINAL | Modifier.STATIC
| Modifier.TRANSIENT;

static
{
ObjectifyService.register(NamedList.class);
ObjectifyService.register(AppUser.class);
}

protected Class<T> clazz;

public ObjectifyDao()
{
 Type genericSuperclass = getClass().getGenericSuperclass();
 // Allow this class to be safely instantiated with or without a parameterized type
 if (genericSuperclass instanceof ParameterizedType)
   clazz = (Class<T>) ((ParameterizedType) genericSuperclass).getActualTypeArguments()[0];
To view this discussion on the web visit https://groups.google.com/d/msg/google-guice/-/NOphc8tGH8IJ.

André Salvati

unread,
Jun 26, 2012, 3:10:43 PM6/26/12
to google...@googlegroups.com
Thanks Stuart.

It's working as related here.
Reply all
Reply to author
Forward
0 new messages