You can use ProvisonListener to intercept the provision (@Provides, Provider.get, or constructor) of any binding.
sam On Sun, Apr 24, 2016, 8:16 AM Pablo perez <politr...@gmail.com> wrote: > thanks for the answer. To make it works I create a Factory class and I > create the map instance using it, so then is guice which is creating the > instance therefore I can filter it already. > > install(new FactoryModuleBuilder().build(MapFactory.Factory.class)); > > > public static class MapFactory<K, V> extends HashMap<String, String> { > > public interface Factory { > MapFactory <String, String> create(); > } > } > > > MapFactory.Factory mapFactory > > > Map<String, String> map =mapFactory.create(); > > > What do you think?. Do you know if there´s a better or standard way to do > it? > > Regards. > > El sábado, 23 de abril de 2016, 23:46:48 (UTC+1), scl escribió: > >> Yes. Guice can only provide the filter if guice created the instance. It >> does not work for instances created by your own code or for instances >> created by providers you have written. >> >> Am 24. April 2016 00:30:47 MESZ, schrieb Pablo perez <poli...@gmail.com>: >>> >>> So, If I understand what you said, Guice can only provide in the filter >>> those instance created by itself, like through @injected or created through >>> Factories. >>> >>> >>> >>> >>> >>> >>> >>> >>> >>> >>> >>> >>> El sábado, 23 de abril de 2016, 21:52:59 (UTC+1), scl escribió: >>>> >>>> You canot intercept methods on instances created by a provider. What >>>> guice does is to create a subclass of the thing you want to to intercept. >>>> But when you use a provider to create this object you are calling new >>>> yourself which makes it impossible for guice to sneak in the subclass. >>>> >>>> Therefore interception of methods only works on instances created by >>>> guice (there is no 'new' in your code) >>>> >>>> Am 23. April 2016 19:44:05 MESZ, schrieb Pablo perez <poli...@gmail.com >>>> >: >>>>> >>>>> I´m using Guice TypeListener to intercept a map instance generated by >>>>> a provider. >>>>> >>>>> Here the config method where I bind my listener >>>>> >>>>> @Overridepublic void addConfig() { >>>>> MapListener mapListener = new MapListener(); >>>>> requestInjection(mapListener); >>>>> bindListener(Matchers.any(), mapListener);} >>>>> >>>>> This is the map instance created by this provider >>>>> >>>>> @Provides >>>>> Map<String, String> mapTest(){ >>>>> return new HashMap<>(); >>>>> } >>>>> >>>>> This is the listener class to detect the map instance >>>>> >>>>> public class MapListener implements TypeListener { >>>>> >>>>> @Override >>>>> public <I> void hear(final TypeLiteral<I> typeLiteral, >>>>> TypeEncounter<I> typeEncounter) { >>>>> typeEncounter.register((InjectionListener<I>) i -> { >>>>> if(i.getClass().isAssignableFrom(Map.class)){ >>>>> System.out.println("DI:"+ i.getClass() >>>>> .getName()); >>>>> //TODO:Logic to modify map >>>>> } >>>>> }); >>>>> }} >>>>> >>>>> The problem is the Map is never passed to the hear method. Am I doing >>>>> something wrong? >>>>> >>>>> Regards. >>>>> >>>>> -- > 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+unsubscr...@googlegroups.com. > To post to this group, send email to google-guice@googlegroups.com. > Visit this group at https://groups.google.com/group/google-guice. > To view this discussion on the web visit > https://groups.google.com/d/msgid/google-guice/717b6c94-658a-48ec-87c9-51ad9a488a7a%40googlegroups.com > <https://groups.google.com/d/msgid/google-guice/717b6c94-658a-48ec-87c9-51ad9a488a7a%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 google-guice+unsubscr...@googlegroups.com. To post to this group, send email to google-guice@googlegroups.com. Visit this group at https://groups.google.com/group/google-guice. To view this discussion on the web visit https://groups.google.com/d/msgid/google-guice/CAJEBNUefkNfEWDsF84vFj%3DdCAQkbe%3D2%3Dt0zUf2LNk60Ga37NWA%40mail.gmail.com. For more options, visit https://groups.google.com/d/optout.