throw new UnsatisfiedDependencyException(null, beanName, new InjectionPoint(field), ex);
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(); }
@Override public boolean equals(Object other) { if (this == other) { return true; } if (!super.equals(other)) { return false; } DependencyDescriptor otherDesc = (DependencyDescriptor) other; return (this.required == otherDesc.required && this.eager == otherDesc.eager && this.nestingLevel == otherDesc.nestingLevel && this.containingClass == otherDesc.containingClass); }
@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()); } }
/** * Return the type declared by the underlying field or method/constructor parameter, * indicating the injection type. */ public Class<?> getDeclaredType() { return (this.field != null ? this.field.getType() : obtainMethodParameter().getParameterType()); }
private String determineDescription(BeanCreationException ex) { if (StringUtils.hasText(ex.getResourceDescription())) { return String.format(" defined in %s", ex.getResourceDescription()); } InjectionPoint failedInjectionPoint = findFailedInjectionPoint(ex); if (failedInjectionPoint != null && failedInjectionPoint.getField() != null) { return String.format(" (field %s)", failedInjectionPoint.getField()); } return ""; }
@Override public int hashCode() { return 31 * super.hashCode() + ObjectUtils.nullSafeHashCode(this.containingClass); }
@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()); } }
/** * Returns the wrapped member, containing the injection point. * @return the Field / Method / Constructor as Member */ public Member getMember() { return (this.field != null ? this.field : obtainMethodParameter().getMember()); }
@Test public void testCustomAnnotationRequiredFieldResourceInjectionFailsWhenNoDependencyFound() { bpp.setAutowiredAnnotationType(MyAutowired.class); bpp.setRequiredParameterName("optional"); bpp.setRequiredParameterValue(false); bf.registerBeanDefinition("customBean", new RootBeanDefinition( CustomAnnotationRequiredFieldResourceInjectionBean.class)); try { bf.getBean("customBean"); fail("Should have thrown UnsatisfiedDependencyException"); } catch (UnsatisfiedDependencyException ex) { // expected assertSame(CustomAnnotationRequiredFieldResourceInjectionBean.class, ex.getInjectionPoint().getField().getDeclaringClass()); } }
@Override public int hashCode() { return 31 * super.hashCode() + ObjectUtils.nullSafeHashCode(this.containingClass); }
throw new UnsatisfiedDependencyException(null, beanName, new InjectionPoint(methodParam), ex);
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"); }
@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()); } }
/** * Retrieve a field/parameter annotation of the given type, if any. * @param annotationType the annotation type to retrieve * @return the annotation instance, or {@code null} if none found * @since 4.3.9 */ @Nullable public <A extends Annotation> A getAnnotation(Class<A> annotationType) { return (this.field != null ? this.field.getAnnotation(annotationType) : obtainMethodParameter().getParameterAnnotation(annotationType)); }
@Test public void testCustomAnnotationOptionalFieldResourceInjectionWhenMultipleDependenciesFound() { bpp.setAutowiredAnnotationType(MyAutowired.class); bpp.setRequiredParameterName("optional"); bpp.setRequiredParameterValue(false); bf.registerBeanDefinition("customBean", new RootBeanDefinition( CustomAnnotationOptionalFieldResourceInjectionBean.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(CustomAnnotationOptionalFieldResourceInjectionBean.class, ex.getInjectionPoint().getField().getDeclaringClass()); } }
@Override public boolean equals(Object other) { if (this == other) { return true; } if (!super.equals(other)) { return false; } DependencyDescriptor otherDesc = (DependencyDescriptor) other; return (this.required == otherDesc.required && this.eager == otherDesc.eager && this.nestingLevel == otherDesc.nestingLevel && this.containingClass == otherDesc.containingClass); }
throw new UnsatisfiedDependencyException(null, beanName, new InjectionPoint(field), ex);
@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()); } }
/** * Return the wrapped annotated element. * <p>Note: In case of a method/constructor parameter, this exposes * the annotations declared on the method or constructor itself * (i.e. at the method/constructor level, not at the parameter level). * Use {@link #getAnnotations()} to obtain parameter-level annotations in * such a scenario, transparently with corresponding field annotations. * @return the Field / Method / Constructor as AnnotatedElement */ public AnnotatedElement getAnnotatedElement() { return (this.field != null ? this.field : obtainMethodParameter().getAnnotatedElement()); }