FieldInjectorImpl implements FieldInjector { @Override public void inject(Object resource, ApplicationContext context) { try { Object value = null; if (annotation != null) { value = parameterResolverFactory.createParameterResolver(annotation).resolve(this, context); } else { DependencySupplier dependencies = context.getDependencySupplier(); if (dependencies != null) { value = dependencies.getInstance(this); } } if (value != null) { if (setter != null) { setter.invoke(resource, value); } else { if (!Modifier.isPublic(field.getModifiers())) { field.setAccessible(true); } field.set(resource, value); } } } catch (Exception e) { if (annotation != null) { Class<?> annotationType = annotation.annotationType(); if (annotationType == PathParam.class || annotationType == QueryParam.class || annotationType == MatrixParam.class) { throw new WebApplicationException(e, Response.status(NOT_FOUND).build()); } throw new WebApplicationException(e, Response.status(BAD_REQUEST).build()); } throw new WebApplicationException(e, Response.status(INTERNAL_SERVER_ERROR).build()); } } FieldInjectorImpl(Field field, ParameterResolverFactory parameterResolverFactory); @Override Annotation getAnnotation(); @Override Annotation[] getAnnotations(); @Override String getDefaultValue(); @Override Class<?> getParameterClass(); @Override Type getGenericType(); @Override boolean isEncoded(); @Override String getName(); @Override void inject(Object resource, ApplicationContext context); @Override String toString();  }
@Test public void injectsField() throws Exception { when(pathParameterResolver.resolve(isA(Parameter.class), eq(applicationContext))).thenReturn("path parameter"); FieldInjectorImpl fieldInjector = new FieldInjectorImpl(Resource.class.getDeclaredField("pathParam"), parameterResolverFactory); Resource instance = new Resource(); fieldInjector.inject(instance, applicationContext); assertEquals("path parameter", instance.pathParam); }
@Test public void usesSetterForSettingField() throws Exception { when(headerParameterResolver.resolve(isA(Parameter.class), eq(applicationContext))).thenReturn("header parameter"); FieldInjectorImpl fieldInjector = new FieldInjectorImpl(Resource.class.getDeclaredField("headerParam"), parameterResolverFactory); Resource instance = spy(new Resource()); fieldInjector.inject(instance, applicationContext); assertEquals("header parameter", instance.headerParam); verify(instance).setHeaderParam("header parameter"); }
@Test public void throwsWebApplicationExceptionWithStatus_NOT_FOUND_WhenFieldAnnotatedWithPathParamAnnotationCanNotBeResolved() throws Exception { when(pathParameterResolver.resolve(isA(Parameter.class), eq(applicationContext))).thenThrow(new Exception()); FieldInjectorImpl fieldInjector = new FieldInjectorImpl(Resource.class.getDeclaredField("pathParam"), parameterResolverFactory); Resource instance = new Resource(); thrown.expect(webApplicationExceptionWithStatusMatcher(NOT_FOUND)); fieldInjector.inject(instance, applicationContext); }
@Test public void throwsWebApplicationExceptionWithStatus_NOT_FOUND_WhenFieldAnnotatedWithQueryParamAnnotationCanNotBeResolved() throws Exception { when(queryParameterResolver.resolve(isA(Parameter.class), eq(applicationContext))).thenThrow(new Exception()); FieldInjectorImpl fieldInjector = new FieldInjectorImpl(Resource.class.getDeclaredField("queryParam"), parameterResolverFactory); Resource instance = new Resource(); thrown.expect(webApplicationExceptionWithStatusMatcher(NOT_FOUND)); fieldInjector.inject(instance, applicationContext); }
@Test public void throwsWebApplicationExceptionWithStatus_NOT_FOUND_WhenFieldAnnotatedWithMatrixParamAnnotationCanNotBeResolved() throws Exception { when(matrixParameterResolver.resolve(isA(Parameter.class), eq(applicationContext))).thenThrow(new Exception()); FieldInjectorImpl fieldInjector = new FieldInjectorImpl(Resource.class.getDeclaredField("matrixParam"), parameterResolverFactory); Resource instance = new Resource(); thrown.expect(webApplicationExceptionWithStatusMatcher(NOT_FOUND)); fieldInjector.inject(instance, applicationContext); }
@Test public void throwsWebApplicationExceptionWithStatus_BAD_REQUEST_WhenFieldAnnotatedWithHeaderParamAnnotationCanNotBeResolved() throws Exception { when(headerParameterResolver.resolve(isA(Parameter.class), eq(applicationContext))).thenThrow(new Exception()); FieldInjectorImpl fieldInjector = new FieldInjectorImpl(Resource.class.getDeclaredField("headerParam"), parameterResolverFactory); Resource instance = new Resource(); thrown.expect(webApplicationExceptionWithStatusMatcher(BAD_REQUEST)); fieldInjector.inject(instance, applicationContext); }
@Test public void throwsWebApplicationExceptionWithStatus_BAD_REQUEST_WhenFieldAnnotatedWithCookieParamAnnotationCanNotBeResolved() throws Exception { when(cookieParameterResolver.resolve(isA(Parameter.class), eq(applicationContext))).thenThrow(new Exception()); FieldInjectorImpl fieldInjector = new FieldInjectorImpl(Resource.class.getDeclaredField("cookieParam"), parameterResolverFactory); Resource instance = new Resource(); thrown.expect(webApplicationExceptionWithStatusMatcher(BAD_REQUEST)); fieldInjector.inject(instance, applicationContext); }