@Test public void testPropertySetting() throws Exception { IMocksControl control = createControl(); TypeEncounter<SomeInjectableBean> encounter = control.createMock(TypeEncounter.class); Provider<Injector> injectorProvider = control.createMock(Provider.class); Injector injector = control.createMock(Injector.class); expect(encounter.getProvider(Injector.class)).andReturn(injectorProvider); expect(injectorProvider.get()).andReturn(injector).anyTimes(); Capture<MembersInjector<SomeInjectableBean>> capture = Capture.newInstance(); encounter.register(and(anyObject(MembersInjector.class), capture(capture))); SecurityManager securityManager = control.createMock(SecurityManager.class); String property = "myPropertyValue"; expect(injector.getInstance(Key.get(SecurityManager.class))).andReturn(securityManager); expect(injector.getInstance(Key.get(String.class, Names.named("shiro.myProperty")))).andReturn(property); expect(injector.getInstance(Key.get(String.class, Names.named("shiro.unavailableProperty")))) .andThrow(new ConfigurationException(Collections.singleton(new Message("Not Available!")))); expect((Map)injector.getInstance(BeanTypeListener.MAP_KEY)).andReturn(Collections.EMPTY_MAP).anyTimes(); control.replay(); BeanTypeListener underTest = new BeanTypeListener(); underTest.hear(TypeLiteral.get(SomeInjectableBean.class), encounter); SomeInjectableBean bean = new SomeInjectableBean(); capture.getValue().injectMembers(bean); assertSame(securityManager, bean.securityManager); assertSame(property, bean.myProperty); assertNull(bean.unavailableProperty); control.verify(); }
public <I> void hear(TypeLiteral<I> type, final TypeEncounter<I> encounter) { PropertyDescriptor propertyDescriptors[] = beanUtilsBean.getPropertyUtils().getPropertyDescriptors(type.getRawType()); final Map<PropertyDescriptor, Key<?>> propertyDependencies = new HashMap<PropertyDescriptor, Key<?>>(propertyDescriptors.length); final Provider<Injector> injectorProvider = encounter.getProvider(Injector.class); for (PropertyDescriptor propertyDescriptor : propertyDescriptors) { if (propertyDescriptor.getWriteMethod() != null && Modifier.isPublic(propertyDescriptor.getWriteMethod().getModifiers())) { Type propertyType = propertyDescriptor.getWriteMethod().getGenericParameterTypes()[0]; propertyDependencies.put(propertyDescriptor, createDependencyKey(propertyDescriptor, propertyType)); } } encounter.register(new MembersInjector<I>() { public void injectMembers(I instance) { for (Map.Entry<PropertyDescriptor, Key<?>> dependency : propertyDependencies.entrySet()) { try { final Injector injector = injectorProvider.get(); Object value = injector.getInstance(getMappedKey(injector, dependency.getValue())); dependency.getKey().getWriteMethod().invoke(instance, value); } catch (ConfigurationException e) { } catch (InvocationTargetException e) { throw new RuntimeException("Couldn't set property " + dependency.getKey().getDisplayName(), e); } catch (IllegalAccessException e) { throw new RuntimeException("We shouldn't have ever reached this point, we don't try to inject to non-accessible methods.", e); } } } }); }
BeanTypeListener implements TypeListener { public <I> void hear(TypeLiteral<I> type, final TypeEncounter<I> encounter) { PropertyDescriptor propertyDescriptors[] = beanUtilsBean.getPropertyUtils().getPropertyDescriptors(type.getRawType()); final Map<PropertyDescriptor, Key<?>> propertyDependencies = new HashMap<PropertyDescriptor, Key<?>>(propertyDescriptors.length); final Provider<Injector> injectorProvider = encounter.getProvider(Injector.class); for (PropertyDescriptor propertyDescriptor : propertyDescriptors) { if (propertyDescriptor.getWriteMethod() != null && Modifier.isPublic(propertyDescriptor.getWriteMethod().getModifiers())) { Type propertyType = propertyDescriptor.getWriteMethod().getGenericParameterTypes()[0]; propertyDependencies.put(propertyDescriptor, createDependencyKey(propertyDescriptor, propertyType)); } } encounter.register(new MembersInjector<I>() { public void injectMembers(I instance) { for (Map.Entry<PropertyDescriptor, Key<?>> dependency : propertyDependencies.entrySet()) { try { final Injector injector = injectorProvider.get(); Object value = injector.getInstance(getMappedKey(injector, dependency.getValue())); dependency.getKey().getWriteMethod().invoke(instance, value); } catch (ConfigurationException e) { } catch (InvocationTargetException e) { throw new RuntimeException("Couldn't set property " + dependency.getKey().getDisplayName(), e); } catch (IllegalAccessException e) { throw new RuntimeException("We shouldn't have ever reached this point, we don't try to inject to non-accessible methods.", e); } } } }); } }
BeanTypeListener implements TypeListener { public <I> void hear(TypeLiteral<I> type, final TypeEncounter<I> encounter) { PropertyDescriptor propertyDescriptors[] = beanUtilsBean.getPropertyUtils().getPropertyDescriptors(type.getRawType()); final Map<PropertyDescriptor, Key<?>> propertyDependencies = new HashMap<PropertyDescriptor, Key<?>>(propertyDescriptors.length); final Provider<Injector> injectorProvider = encounter.getProvider(Injector.class); for (PropertyDescriptor propertyDescriptor : propertyDescriptors) { if (propertyDescriptor.getWriteMethod() != null && Modifier.isPublic(propertyDescriptor.getWriteMethod().getModifiers())) { Type propertyType = propertyDescriptor.getWriteMethod().getGenericParameterTypes()[0]; propertyDependencies.put(propertyDescriptor, createDependencyKey(propertyDescriptor, propertyType)); } } encounter.register(new MembersInjector<I>() { public void injectMembers(I instance) { for (Map.Entry<PropertyDescriptor, Key<?>> dependency : propertyDependencies.entrySet()) { try { final Injector injector = injectorProvider.get(); Object value = injector.getInstance(getMappedKey(injector, dependency.getValue())); dependency.getKey().getWriteMethod().invoke(instance, value); } catch (ConfigurationException e) { } catch (InvocationTargetException e) { throw new RuntimeException("Couldn't set property " + dependency.getKey().getDisplayName(), e); } catch (IllegalAccessException e) { throw new RuntimeException("We shouldn't have ever reached this point, we don't try to inject to non-accessible methods.", e); } } } }); } BeanTypeListener(); }
BeanTypeListener implements TypeListener { public <I> void hear(TypeLiteral<I> type, final TypeEncounter<I> encounter) { PropertyDescriptor propertyDescriptors[] = beanUtilsBean.getPropertyUtils().getPropertyDescriptors(type.getRawType()); final Map<PropertyDescriptor, Key<?>> propertyDependencies = new HashMap<PropertyDescriptor, Key<?>>(propertyDescriptors.length); final Provider<Injector> injectorProvider = encounter.getProvider(Injector.class); for (PropertyDescriptor propertyDescriptor : propertyDescriptors) { if (propertyDescriptor.getWriteMethod() != null && Modifier.isPublic(propertyDescriptor.getWriteMethod().getModifiers())) { Type propertyType = propertyDescriptor.getWriteMethod().getGenericParameterTypes()[0]; propertyDependencies.put(propertyDescriptor, createDependencyKey(propertyDescriptor, propertyType)); } } encounter.register(new MembersInjector<I>() { public void injectMembers(I instance) { for (Map.Entry<PropertyDescriptor, Key<?>> dependency : propertyDependencies.entrySet()) { try { final Injector injector = injectorProvider.get(); Object value = injector.getInstance(getMappedKey(injector, dependency.getValue())); dependency.getKey().getWriteMethod().invoke(instance, value); } catch (ConfigurationException e) { } catch (InvocationTargetException e) { throw new RuntimeException("Couldn't set property " + dependency.getKey().getDisplayName(), e); } catch (IllegalAccessException e) { throw new RuntimeException("We shouldn't have ever reached this point, we don't try to inject to non-accessible methods.", e); } } } }); } BeanTypeListener(); void hear(TypeLiteral<I> type, final TypeEncounter<I> encounter); }
BeanTypeListener implements TypeListener { public <I> void hear(TypeLiteral<I> type, final TypeEncounter<I> encounter) { PropertyDescriptor propertyDescriptors[] = beanUtilsBean.getPropertyUtils().getPropertyDescriptors(type.getRawType()); final Map<PropertyDescriptor, Key<?>> propertyDependencies = new HashMap<PropertyDescriptor, Key<?>>(propertyDescriptors.length); final Provider<Injector> injectorProvider = encounter.getProvider(Injector.class); for (PropertyDescriptor propertyDescriptor : propertyDescriptors) { if (propertyDescriptor.getWriteMethod() != null && Modifier.isPublic(propertyDescriptor.getWriteMethod().getModifiers())) { Type propertyType = propertyDescriptor.getWriteMethod().getGenericParameterTypes()[0]; propertyDependencies.put(propertyDescriptor, createDependencyKey(propertyDescriptor, propertyType)); } } encounter.register(new MembersInjector<I>() { public void injectMembers(I instance) { for (Map.Entry<PropertyDescriptor, Key<?>> dependency : propertyDependencies.entrySet()) { try { final Injector injector = injectorProvider.get(); Object value = injector.getInstance(getMappedKey(injector, dependency.getValue())); dependency.getKey().getWriteMethod().invoke(instance, value); } catch (ConfigurationException e) { } catch (InvocationTargetException e) { throw new RuntimeException("Couldn't set property " + dependency.getKey().getDisplayName(), e); } catch (IllegalAccessException e) { throw new RuntimeException("We shouldn't have ever reached this point, we don't try to inject to non-accessible methods.", e); } } } }); } BeanTypeListener(); void hear(TypeLiteral<I> type, final TypeEncounter<I> encounter); static final Package SHIRO_GUICE_PACKAGE; static final Package SHIRO_PACKAGE; static final Matcher<TypeLiteral> MATCHER; }