Yup, that'll work for basic (non-generic) types.  For potentially generic
types,you'd want to iterate using something similar to this
<https://github.com/google/guice/blob/master/extensions/testlib/src/com/google/inject/testing/fieldbinder/BoundFieldModule.java#L363>,
and then later retrieve the types through the TypeLiteral (using
TypeLiteral.getFieldType
<TypeLiteral.getFieldTypehttp://google.github.io/guice/api-docs/latest/javadoc/com/google/inject/TypeLiteral.html#getFieldType-java.lang.reflect.Field->)
too.  Otherwise you'll lose the generics info on the fields.

sam



On Tue Nov 18 2014 at 7:56:06 AM Jonas <[email protected]> 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 [email protected].
> To post to this group, send email to [email protected].
> 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
> <https://groups.google.com/d/msgid/google-guice/22d471fc-a59b-45cd-b519-741eb72611d8%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
> For more options, visit https://groups.google.com/d/optout.
>

-- 
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 [email protected].
To post to this group, send email to [email protected].
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/CAJEBNUcp3906XdnYtKm8A7-T%2BxSdhLm6gXHp5Rag%2BnPsq7JbBg%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to