TypeListener for @InjectLogger

51 views
Skip to first unread message

Jonas

unread,
Nov 18, 2014, 7:56:04 AM11/18/14
to google...@googlegroups.com
Hello,

I've been using the CustomInjections example to do Logger injection as described on https://github.com/google/guice/wiki/CustomInjections

However when using abstract classes / super classes the fields are not visible when typeLiteral.getRawType().getDeclaredFields() is called.

This bit me and I changed the code to the following:

Class<?> clazz = typeLiteral.getRawType();
while (clazz != null) {
    for (Field field : clazz.getDeclaredFields()) {
        if (field.getType() == Logger.class &&
            field.isAnnotationPresent(InjectLogger.class)) {
            encounter.register(new Log4JMembersInjector<T>(field));
        }
    }
    clazz = clazz.getSuperclass();
}

What do you think about updating the example with the code above? Is this the correct way of doing it if you have super classes or is it bad performance wise?

Kind regards
Jonas

Sam Berlin

unread,
Nov 18, 2014, 8:30:10 AM11/18/14
to google...@googlegroups.com
Yup, that'll work for basic (non-generic) types.  For potentially generic types,you'd want to iterate using something similar to this, and then later retrieve the types through the TypeLiteral (using TypeLiteral.getFieldType) too.  Otherwise you'll lose the generics info on the fields.

sam



--
You received this message because you are subscribed to the Google Groups "google-guice" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-guice...@googlegroups.com.
To post to this group, send email to google...@googlegroups.com.
Visit this group at http://groups.google.com/group/google-guice.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-guice/22d471fc-a59b-45cd-b519-741eb72611d8%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Sam Berlin

unread,
Nov 18, 2014, 8:34:54 AM11/18/14
to google...@googlegroups.com
(Updated the example in the wiki, thanks!)

On Tue Nov 18 2014 at 8:30:05 AM Sam Berlin <sbe...@gmail.com> wrote:
Yup, that'll work for basic (non-generic) types.  For potentially generic types,you'd want to iterate using something similar to this, and then later retrieve the types through the TypeLiteral (using TypeLiteral.getFieldType) too.  Otherwise you'll lose the generics info on the fields.

sam



On Tue Nov 18 2014 at 7:56:06 AM Jonas <jo...@weightpoint.se> wrote:
Hello,

I've been using the CustomInjections example to do Logger injection as described on https://github.com/google/guice/wiki/CustomInjections

However when using abstract classes / super classes the fields are not visible when typeLiteral.getRawType().getDeclaredFields() is called.

This bit me and I changed the code to the following:

Class<?> clazz = typeLiteral.getRawType();
while (clazz != null) {
    for (Field field : clazz.getDeclaredFields()) {
        if (field.getType() == Logger.class &&
            field.isAnnotationPresent(InjectLogger.class)) {
            encounter.register(new Log4JMembersInjector<T>(field));
        }
    }
    clazz = clazz.getSuperclass();
}

What do you think about updating the example with the code above? Is this the correct way of doing it if you have super classes or is it bad performance wise?

Kind regards
Jonas

--
You received this message because you are subscribed to the Google Groups "google-guice" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-guice+unsubscribe@googlegroups.com.

Jonas

unread,
Nov 18, 2014, 8:55:01 AM11/18/14
to google...@googlegroups.com
Thanks!


On Tuesday, November 18, 2014 2:34:54 PM UTC+1, Sam Berlin wrote:
(Updated the example in the wiki, thanks!)

On Tue Nov 18 2014 at 8:30:05 AM Sam Berlin <sbe...@gmail.com> wrote:
Yup, that'll work for basic (non-generic) types.  For potentially generic types,you'd want to iterate using something similar to this, and then later retrieve the types through the TypeLiteral (using TypeLiteral.getFieldType) too.  Otherwise you'll lose the generics info on the fields.

sam



On Tue Nov 18 2014 at 7:56:06 AM Jonas <jo...@weightpoint.se> wrote:
Hello,

I've been using the CustomInjections example to do Logger injection as described on https://github.com/google/guice/wiki/CustomInjections

However when using abstract classes / super classes the fields are not visible when typeLiteral.getRawType().getDeclaredFields() is called.

This bit me and I changed the code to the following:

Class<?> clazz = typeLiteral.getRawType();
while (clazz != null) {
    for (Field field : clazz.getDeclaredFields()) {
        if (field.getType() == Logger.class &&
            field.isAnnotationPresent(InjectLogger.class)) {
            encounter.register(new Log4JMembersInjector<T>(field));
        }
    }
    clazz = clazz.getSuperclass();
}

What do you think about updating the example with the code above? Is this the correct way of doing it if you have super classes or is it bad performance wise?

Kind regards
Jonas

--
You received this message because you are subscribed to the Google Groups "google-guice" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-guice...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages