下面列出了com.fasterxml.jackson.databind.exc.InvalidDefinitionException#from ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Helper method called to indicate problem in POJO (serialization) definitions or settings
* regarding specific property (of a type), unrelated to actual JSON content to map.
* Default behavior is to construct and throw a {@link JsonMappingException}.
*
* @since 2.9
*/
public <T> T reportBadPropertyDefinition(BeanDescription bean, BeanPropertyDefinition prop,
String message, Object... msgArgs) throws JsonMappingException {
message = _format(message, msgArgs);
String propName = "N/A";
if (prop != null) {
propName = _quotedString(prop.getName());
}
String beanDesc = "N/A";
if (bean != null) {
beanDesc = ClassUtil.nameOf(bean.getBeanClass());
}
message = String.format("Invalid definition for property %s (of type %s): %s",
propName, beanDesc, message);
throw InvalidDefinitionException.from(getGenerator(), message, bean, prop);
}
private void _reportMissingSetter(JsonParser p, DeserializationContext ctxt) throws IOException
{
final String msg = "No fallback setter/field defined for creator property '"+getName()+"'";
// Hmmmh. Should we return quietly (NOP), or error?
// Perhaps better to throw an exception, since it's generally an error.
if (ctxt != null ) {
ctxt.reportBadDefinition(getType(), msg);
} else {
throw InvalidDefinitionException.from(p, msg, getType());
}
}
/**
* Helper method called to indicate problem in POJO (serialization) definitions or settings
* regarding specific Java type, unrelated to actual JSON content to map.
* Default behavior is to construct and throw a {@link JsonMappingException}.
*
* @since 2.9
*/
public <T> T reportBadTypeDefinition(BeanDescription bean,
String msg, Object... msgArgs) throws JsonMappingException {
String beanDesc = "N/A";
if (bean != null) {
beanDesc = ClassUtil.nameOf(bean.getBeanClass());
}
msg = String.format("Invalid type definition for type %s: %s",
beanDesc, _format(msg, msgArgs));
throw InvalidDefinitionException.from(getGenerator(), msg, bean, null);
}
/**
* @since 2.9
*/
public <T> T reportBadDefinition(JavaType type, String msg, Throwable cause)
throws JsonMappingException {
InvalidDefinitionException e = InvalidDefinitionException.from(getGenerator(), msg, type);
e.initCause(cause);
throw e;
}
/**
* @since 2.9
*/
public <T> T reportBadDefinition(Class<?> raw, String msg, Throwable cause)
throws JsonMappingException {
InvalidDefinitionException e = InvalidDefinitionException.from(getGenerator(), msg, constructType(raw));
e.initCause(cause);
throw e;
}
/**
* Helper method called to indicate problem in POJO (serialization) definitions or settings
* regarding specific Java type, unrelated to actual JSON content to map.
* Default behavior is to construct and throw a {@link JsonMappingException}.
*
* @since 2.9
*/
public <T> T reportBadTypeDefinition(BeanDescription bean,
String msg, Object... msgArgs) throws JsonMappingException {
msg = _format(msg, msgArgs);
String beanDesc = ClassUtil.nameOf(bean.getBeanClass());
msg = String.format("Invalid type definition for type %s: %s", beanDesc, msg);
throw InvalidDefinitionException.from(_parser, msg, bean, null);
}
/**
* Helper method called to indicate problem in POJO (serialization) definitions or settings
* regarding specific property (of a type), unrelated to actual JSON content to map.
* Default behavior is to construct and throw a {@link JsonMappingException}.
*
* @since 2.9
*/
public <T> T reportBadPropertyDefinition(BeanDescription bean, BeanPropertyDefinition prop,
String msg, Object... msgArgs) throws JsonMappingException {
msg = _format(msg, msgArgs);
String propName = ClassUtil.nameOf(prop);
String beanDesc = ClassUtil.nameOf(bean.getBeanClass());
msg = String.format("Invalid definition for property %s (of type %s): %s",
propName, beanDesc, msg);
throw InvalidDefinitionException.from(_parser, msg, bean, prop);
}
/**
* Helper method for constructing instantiation exception for specified type,
* to indicate problem with physically constructing instance of
* specified class (missing constructor, exception from constructor)
*<p>
* Note that most of the time this method should NOT be called; instead,
* {@link #handleInstantiationProblem} should be called which will call this method
* if necessary.
*/
public JsonMappingException instantiationException(Class<?> instClass, Throwable cause) {
// Most likely problem with Creator definition, right?
JavaType type = constructType(instClass);
String msg = String.format("Cannot construct instance of %s, problem: %s",
ClassUtil.nameOf(instClass), cause.getMessage());
InvalidDefinitionException e = InvalidDefinitionException.from(_parser, msg, type);
e.initCause(cause);
return e;
}
@Override
public <T> T reportBadDefinition(JavaType type, String msg) throws JsonMappingException {
throw InvalidDefinitionException.from(getGenerator(), msg, type);
}
@Override
public <T> T reportBadDefinition(JavaType type, String msg) throws JsonMappingException {
throw InvalidDefinitionException.from(_parser, msg, type);
}
/**
* Method that deserializer may call if it is called to do an update ("merge")
* but deserializer operates on a non-mergeable type. Although this should
* usually be caught earlier, sometimes it may only be caught during operation
* and if so this is the method to call.
* Note that if {@link MapperFeature#IGNORE_MERGE_FOR_UNMERGEABLE} is enabled,
* this method will simply return null; otherwise {@link InvalidDefinitionException}
* will be thrown.
*
* @since 2.9
*/
public <T> T reportBadMerge(JsonDeserializer<?> deser) throws JsonMappingException
{
if (isEnabled(MapperFeature.IGNORE_MERGE_FOR_UNMERGEABLE)) {
return null;
}
JavaType type = constructType(deser.handledType());
String msg = String.format("Invalid configuration: values of type %s cannot be merged", type);
throw InvalidDefinitionException.from(getParser(), msg, type);
}
/**
* Helper method for constructing instantiation exception for specified type,
* to indicate that instantiation failed due to missing instantiator
* (creator; constructor or factory method).
*<p>
* Note that most of the time this method should NOT be called; instead,
* {@link #handleMissingInstantiator} should be called which will call this method
* if necessary.
*/
public JsonMappingException instantiationException(Class<?> instClass, String msg0) {
// Most likely problem with Creator definition, right?
JavaType type = constructType(instClass);
String msg = String.format("Cannot construct instance of %s: %s",
ClassUtil.nameOf(instClass), msg0);
return InvalidDefinitionException.from(_parser, msg, type);
}