下面列出了org.springframework.beans.NullValueInNestedPathException#org.springframework.beans.InvalidPropertyException 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Test
public void testAutoGrowBeyondDefaultLimit() {
TestBean testBean = new TestBean();
DataBinder binder = new DataBinder(testBean, "testBean");
MutablePropertyValues mpvs = new MutablePropertyValues();
mpvs.add("friends[256]", "");
try {
binder.bind(mpvs);
fail("Should have thrown InvalidPropertyException");
}
catch (InvalidPropertyException ex) {
// expected
assertTrue(ex.getRootCause() instanceof IndexOutOfBoundsException);
}
}
@Test
public void testAutoGrowBeyondCustomLimit() {
TestBean testBean = new TestBean();
DataBinder binder = new DataBinder(testBean, "testBean");
binder.setAutoGrowCollectionLimit(10);
MutablePropertyValues mpvs = new MutablePropertyValues();
mpvs.add("friends[16]", "");
try {
binder.bind(mpvs);
fail("Should have thrown InvalidPropertyException");
}
catch (InvalidPropertyException ex) {
// expected
assertTrue(ex.getRootCause() instanceof IndexOutOfBoundsException);
}
}
@Test
public void testAutoGrowBeyondDefaultLimit() {
TestBean testBean = new TestBean();
DataBinder binder = new DataBinder(testBean, "testBean");
MutablePropertyValues mpvs = new MutablePropertyValues();
mpvs.add("friends[256]", "");
try {
binder.bind(mpvs);
fail("Should have thrown InvalidPropertyException");
}
catch (InvalidPropertyException ex) {
// expected
assertTrue(ex.getRootCause() instanceof IndexOutOfBoundsException);
}
}
@Test
public void testAutoGrowBeyondCustomLimit() {
TestBean testBean = new TestBean();
DataBinder binder = new DataBinder(testBean, "testBean");
binder.setAutoGrowCollectionLimit(10);
MutablePropertyValues mpvs = new MutablePropertyValues();
mpvs.add("friends[16]", "");
try {
binder.bind(mpvs);
fail("Should have thrown InvalidPropertyException");
}
catch (InvalidPropertyException ex) {
// expected
assertTrue(ex.getRootCause() instanceof IndexOutOfBoundsException);
}
}
/**
* Creates the instance to be injected for the given member.
*
* @param <T> The type of the instance to be injected.
* @param name The name that was used to create the channel.
* @param injectionTarget The target member for the injection.
* @param injectionType The class that should injected.
* @param channel The channel that should be used to create the instance.
* @return The value that matches the type of the given field.
* @throws BeansException If the value of the field could not be created or the type of the field is unsupported.
*/
protected <T> T valueForMember(final String name, final Member injectionTarget,
final Class<T> injectionType,
final Channel channel) throws BeansException {
if (Channel.class.equals(injectionType)) {
return injectionType.cast(channel);
} else if (AbstractStub.class.isAssignableFrom(injectionType)) {
@SuppressWarnings("unchecked") // Eclipse incorrectly marks this as not required
AbstractStub<?> stub = createStub(injectionType.asSubclass(AbstractStub.class), channel);
for (final StubTransformer stubTransformer : getStubTransformers()) {
stub = stubTransformer.transform(name, stub);
}
return injectionType.cast(stub);
} else {
throw new InvalidPropertyException(injectionTarget.getDeclaringClass(), injectionTarget.getName(),
"Unsupported type " + injectionType.getName());
}
}
@Test
public void testAutoGrowBeyondDefaultLimit() throws Exception {
TestBean testBean = new TestBean();
DataBinder binder = new DataBinder(testBean, "testBean");
MutablePropertyValues mpvs = new MutablePropertyValues();
mpvs.add("friends[256]", "");
try {
binder.bind(mpvs);
fail("Should have thrown InvalidPropertyException");
}
catch (InvalidPropertyException ex) {
// expected
assertTrue(ex.getRootCause() instanceof IndexOutOfBoundsException);
}
}
@Test
public void testAutoGrowBeyondCustomLimit() throws Exception {
TestBean testBean = new TestBean();
DataBinder binder = new DataBinder(testBean, "testBean");
binder.setAutoGrowCollectionLimit(10);
MutablePropertyValues mpvs = new MutablePropertyValues();
mpvs.add("friends[16]", "");
try {
binder.bind(mpvs);
fail("Should have thrown InvalidPropertyException");
}
catch (InvalidPropertyException ex) {
// expected
assertTrue(ex.getRootCause() instanceof IndexOutOfBoundsException);
}
}
/**
* Creates the instance to be injected for the given member.
*
* @param <T> The type of the instance to be injected.
* @param name The name that was used to create the channel.
* @param injectionTarget The target member for the injection.
* @param injectionType The class that should injected.
* @param channel The channel that should be used to create the instance.
* @return The value that matches the type of the given field.
* @throws BeansException If the value of the field could not be created or the type of the field is unsupported.
*/
protected <T> T valueForMember(final String name, final Member injectionTarget,
final Class<T> injectionType,
final Channel channel) throws BeansException {
if (Channel.class.equals(injectionType)) {
return injectionType.cast(channel);
} else if (AbstractStub.class.isAssignableFrom(injectionType)) {
@SuppressWarnings("unchecked") // Eclipse incorrectly marks this as not required
AbstractStub<?> stub = createStub(injectionType.asSubclass(AbstractStub.class), channel);
for (final StubTransformer stubTransformer : getStubTransformers()) {
stub = stubTransformer.transform(name, stub);
}
return injectionType.cast(stub);
} else {
throw new InvalidPropertyException(injectionTarget.getDeclaringClass(), injectionTarget.getName(),
"Unsupported type " + injectionType.getName());
}
}
/**
* Returns the value for the given property growing nested paths depending on the parameter.
*
* @param propertyName name of the property to get value for
* @param autoGrowNestedPaths whether nested paths should be grown (initialized if null)
* @return value for property
*/
protected Object getPropertyValue(String propertyName, boolean autoGrowNestedPaths) {
setAutoGrowNestedPaths(autoGrowNestedPaths);
Object value = null;
try {
value = super.getPropertyValue(propertyName);
} catch (NullValueInNestedPathException e) {
// swallow null values in path and return null as the value
} catch (InvalidPropertyException e1) {
if (!(e1.getRootCause() instanceof NullValueInNestedPathException)) {
throw e1;
}
}
return value;
}
/**
* 参数转换出错
*
* @param e
*
* @return
*/
private ModelAndView getParamErrors(InvalidPropertyException e) {
Map<String, String> errorMap = new HashMap<String, String>();
errorMap.put(e.getPropertyName(), " parameter cannot find");
JsonObjectBase jsonObject = JsonObjectUtils.buildFieldError(errorMap, ErrorCode.TYPE_MIS_MATCH);
return JsonObjectUtils.JsonObjectError2ModelView((JsonObjectError) jsonObject);
}
@SuppressWarnings("unchecked")
@Override
public <X> X getValue(String attrName) throws AttributeValidationException {
X attributeValue = null;
Exception e = null;
try {
attributeValue = (X) beanWrapper.getPropertyValue(attrName);
} catch (IllegalArgumentException iae) {
e = iae;
} catch (InvalidPropertyException ipe) {
//just return null
}
if (e != null) {
throw new AttributeValidationException(
"Unable to lookup attribute value by name (" + attrName + ") using introspection", e);
}
// JLR : KS has code to handle dynamic attributes -- not sure whether this is really needed anymore if we're actually relying on types
// // Extract dynamic attributes
// if(DYNAMIC_ATTRIBUTE.equals(propName)) {
// dataMap.putAll((Map<String, String>)value);
// } else {
// dataMap.put(propName, value);
// }
return attributeValue;
}
/**
* 参数转换出错
*
* @param e
*
* @return
*/
private ModelAndView getParamErrors(InvalidPropertyException e) {
Map<String, String> errorMap = new HashMap<String, String>();
errorMap.put(e.getPropertyName(), " parameter cannot find");
JsonObjectBase jsonObject = JsonObjectUtils.buildFieldError(errorMap, ErrorCode.TYPE_MIS_MATCH);
return JsonObjectUtils.JsonObjectError2ModelView((JsonObjectError) jsonObject);
}
@Override
public Object getPropertyValue(String propertyName) throws BeansException {
Field field = this.fieldMap.get(propertyName);
if (field == null) {
throw new NotReadablePropertyException(
this.target.getClass(), propertyName, "Field '" + propertyName + "' does not exist");
}
try {
ReflectionUtils.makeAccessible(field);
return field.get(this.target);
}
catch (IllegalAccessException ex) {
throw new InvalidPropertyException(this.target.getClass(), propertyName, "Field is not accessible", ex);
}
}
/**
* Set property, without trowing exceptions on errors
* @param bean bean name
* @param name name
* @param value value
*/
public static void setProperty(Object bean, String name, Object value) {
try {
BeanWrapper wrapper = new BeanWrapperImpl(bean);
wrapper.setPropertyValue(new PropertyValue(name, value));
} catch (InvalidPropertyException ipe) {
log.debug("Bean has no property: " + name);
} catch (PropertyAccessException pae) {
log.debug("Access Error on property: " + name);
}
}
@Override
public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object o,
Exception e) {
LOG.warn(request.getRequestURI() + " ExceptionHandler FOUND. " + e.toString() + "\t" + e.getCause());
// PathVariable 出错
if (e instanceof TypeMismatchException) {
return getParamErrors((TypeMismatchException) e);
// Bean 参数无法映射错误
} else if (e instanceof InvalidPropertyException) {
return getParamErrors((InvalidPropertyException) e);
// @Valid 出错
} else if (e instanceof BindException) {
return ParamValidateUtils.getParamErrors((BindException) e);
// 业务校验处理
} else if (e instanceof FieldException) {
return getParamErrors((FieldException) e);
} else if (e instanceof DocumentNotFoundException) {
response.setStatus(HttpServletResponse.SC_NOT_FOUND);
try {
FileUtils.closeWriter(response.getWriter());
} catch (IOException e1) {
e1.printStackTrace();
}
return null;
// 用户没有请求方法的访问权限
} else if (e instanceof AccessDeniedException) {
LOG.warn("details: " + ((AccessDeniedException) e).getErrorMessage());
return buildError("auth.access.denied", ErrorCode.ACCESS_NOAUTH_ERROR);
} else if (e instanceof HttpRequestMethodNotSupportedException) {
return buildError("syserror.httpmethod", ErrorCode.HttpRequestMethodNotSupportedException);
} else if (e instanceof MissingServletRequestParameterException) {
return buildError("syserror.param.miss", ErrorCode.MissingServletRequestParameterException);
} else if (e instanceof GlobalExceptionAware) {
LOG.error("details: ", e);
GlobalExceptionAware g = (GlobalExceptionAware) e;
return buildError(g.getErrorMessage(), g.getErrorCode());
} else {
LOG.warn("details: ", e);
return buildError("syserror.inner", ErrorCode.GLOBAL_ERROR);
}
}
/**
* {@inheritDoc}
*/
@Override
public PropertyDescriptor getPropertyDescriptor(String propertyName) throws InvalidPropertyException {
return wrapper.getPropertyDescriptor(propertyName);
}
@Override
public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object o,
Exception e) {
LOG.warn(request.getRequestURI() + " ExceptionHandler FOUND. " + e.toString() + "\t" + e.getCause());
// PathVariable 出错
if (e instanceof TypeMismatchException) {
return getParamErrors((TypeMismatchException) e);
// Bean 参数无法映射错误
} else if (e instanceof InvalidPropertyException) {
return getParamErrors((InvalidPropertyException) e);
// @Valid 出错
} else if (e instanceof BindException) {
return ParamValidateUtils.getParamErrors((BindException) e);
// 业务校验处理
} else if (e instanceof FieldException) {
return getParamErrors((FieldException) e);
} else if (e instanceof DocumentNotFoundException) {
response.setStatus(HttpServletResponse.SC_NOT_FOUND);
try {
FileUtils.closeWriter(response.getWriter());
} catch (IOException e1) {
e1.printStackTrace();
}
return null;
// 用户没有请求方法的访问权限
} else if (e instanceof AccessDeniedException) {
LOG.warn("details: " + ((AccessDeniedException) e).getErrorMessage());
return buildError("auth.access.denied", ErrorCode.ACCESS_NOAUTH_ERROR);
} else if (e instanceof HttpRequestMethodNotSupportedException) {
return buildError("syserror.httpmethod", ErrorCode.HttpRequestMethodNotSupportedException);
} else if (e instanceof MissingServletRequestParameterException) {
return buildError("syserror.param.miss", ErrorCode.MissingServletRequestParameterException);
} else if (e instanceof GlobalExceptionAware) {
LOG.error("details: ", e);
GlobalExceptionAware g = (GlobalExceptionAware) e;
return buildError(g.getErrorMessage(), g.getErrorCode());
} else {
LOG.warn("details: ", e);
return buildError("syserror.inner", ErrorCode.GLOBAL_ERROR);
}
}