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();}--
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.
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/CustomInjectionsHowever 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 regardsJonas
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.
(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/CustomInjectionsHowever 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 regardsJonas
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.