private String getDescription(UnsatisfiedDependencyException ex) { InjectionPoint injectionPoint = ex.getInjectionPoint(); if (injectionPoint != null) { if (injectionPoint.getField() != null) { return String.format("Field %s in %s", injectionPoint.getField().getName(), injectionPoint.getField().getDeclaringClass().getName()); } if (injectionPoint.getMethodParameter() != null) { if (injectionPoint.getMethodParameter().getConstructor() != null) { return String.format("Parameter %d of constructor in %s", injectionPoint.getMethodParameter().getParameterIndex(), injectionPoint.getMethodParameter().getDeclaringClass() .getName()); } return String.format("Parameter %d of method %s in %s", injectionPoint.getMethodParameter().getParameterIndex(), injectionPoint.getMethodParameter().getMethod().getName(), injectionPoint.getMethodParameter().getDeclaringClass() .getName()); } } return ex.getResourceDescription(); }
@Test public void testMethodInjectionWithMapAndMultipleMatches() { bf.registerBeanDefinition("annotatedBean", new RootBeanDefinition(MapMethodInjectionBean.class)); bf.registerBeanDefinition("testBean1", new RootBeanDefinition(TestBean.class)); bf.registerBeanDefinition("testBean2", new RootBeanDefinition(TestBean.class)); try { bf.getBean("annotatedBean"); fail("should have failed, more than one bean of type"); } catch (UnsatisfiedDependencyException ex) { // expected assertSame(MapMethodInjectionBean.class, ex.getInjectionPoint().getMethodParameter().getDeclaringClass()); } }
@Test public void testConstructorResourceInjectionWithNoCandidatesAndNoFallback() { bf.registerBeanDefinition("annotatedBean", new RootBeanDefinition(ConstructorWithoutFallbackBean.class)); try { bf.getBean("annotatedBean"); fail("Should have thrown UnsatisfiedDependencyException"); } catch (UnsatisfiedDependencyException ex) { // expected assertSame(ConstructorWithoutFallbackBean.class, ex.getInjectionPoint().getMethodParameter().getDeclaringClass()); } }
@Test public void testCustomAnnotationRequiredMethodResourceInjectionFailsWhenNoDependencyFound() { bpp.setAutowiredAnnotationType(MyAutowired.class); bpp.setRequiredParameterName("optional"); bpp.setRequiredParameterValue(false); bf.registerBeanDefinition("customBean", new RootBeanDefinition( CustomAnnotationRequiredMethodResourceInjectionBean.class)); try { bf.getBean("customBean"); fail("Should have thrown UnsatisfiedDependencyException"); } catch (UnsatisfiedDependencyException ex) { // expected assertSame(CustomAnnotationRequiredMethodResourceInjectionBean.class, ex.getInjectionPoint().getMethodParameter().getDeclaringClass()); } }
@Test public void testCustomAnnotationRequiredMethodResourceInjectionFailsWhenMultipleDependenciesFound() { bpp.setAutowiredAnnotationType(MyAutowired.class); bpp.setRequiredParameterName("optional"); bpp.setRequiredParameterValue(false); bf.registerBeanDefinition("customBean", new RootBeanDefinition( CustomAnnotationRequiredMethodResourceInjectionBean.class)); TestBean tb1 = new TestBean(); bf.registerSingleton("testBean1", tb1); TestBean tb2 = new TestBean(); bf.registerSingleton("testBean2", tb2); try { bf.getBean("customBean"); fail("Should have thrown UnsatisfiedDependencyException"); } catch (UnsatisfiedDependencyException ex) { // expected assertSame(CustomAnnotationRequiredMethodResourceInjectionBean.class, ex.getInjectionPoint().getMethodParameter().getDeclaringClass()); } }
@Test public void testCustomAnnotationOptionalMethodResourceInjectionWhenMultipleDependenciesFound() { bpp.setAutowiredAnnotationType(MyAutowired.class); bpp.setRequiredParameterName("optional"); bpp.setRequiredParameterValue(false); bf.registerBeanDefinition("customBean", new RootBeanDefinition( CustomAnnotationOptionalMethodResourceInjectionBean.class)); TestBean tb1 = new TestBean(); bf.registerSingleton("testBean1", tb1); TestBean tb2 = new TestBean(); bf.registerSingleton("testBean2", tb2); try { bf.getBean("customBean"); fail("Should have thrown UnsatisfiedDependencyException"); } catch (UnsatisfiedDependencyException ex) { // expected assertSame(CustomAnnotationOptionalMethodResourceInjectionBean.class, ex.getInjectionPoint().getMethodParameter().getDeclaringClass()); } }
public static Class<?> getGenericType(InjectionPoint ip) { if (ip.getField() != null) { return resolve(ip.getField().getGenericType(), ip); } if (ip.getMethodParameter() != null) { return resolve(ip.getMethodParameter().getGenericParameterType(), ip); } throw new IllegalArgumentException("Cannot derive generic type from InjectionPoint"); }