下面列出了org.springframework.core.convert.ConverterNotFoundException#org.springframework.core.convert.ConversionException 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Decode the a message into an object.
* @see javax.websocket.Decoder.Text#decode(String)
* @see javax.websocket.Decoder.Binary#decode(ByteBuffer)
*/
@SuppressWarnings("unchecked")
@Nullable
public T decode(M message) throws DecodeException {
try {
return (T) getConversionService().convert(message, getMessageType(), getType());
}
catch (ConversionException ex) {
if (message instanceof String) {
throw new DecodeException((String) message,
"Unable to decode websocket message using ConversionService", ex);
}
if (message instanceof ByteBuffer) {
throw new DecodeException((ByteBuffer) message,
"Unable to decode websocket message using ConversionService", ex);
}
throw ex;
}
}
/**
* Decode the a message into an object.
* @see javax.websocket.Decoder.Text#decode(String)
* @see javax.websocket.Decoder.Binary#decode(ByteBuffer)
*/
@SuppressWarnings("unchecked")
@Nullable
public T decode(M message) throws DecodeException {
try {
return (T) getConversionService().convert(message, getMessageType(), getType());
}
catch (ConversionException ex) {
if (message instanceof String) {
throw new DecodeException((String) message,
"Unable to decode websocket message using ConversionService", ex);
}
if (message instanceof ByteBuffer) {
throw new DecodeException((ByteBuffer) message,
"Unable to decode websocket message using ConversionService", ex);
}
throw ex;
}
}
@Override
public Object fromMessage(Message<?> message, Class<?> targetClass) {
Object payload = message.getPayload();
if (targetClass == null) {
return payload;
}
if (payload != null && this.conversionService.canConvert(payload.getClass(), targetClass)) {
try {
return this.conversionService.convert(payload, targetClass);
}
catch (ConversionException ex) {
throw new MessageConversionException(message, "Failed to convert message payload '" +
payload + "' to '" + targetClass.getName() + "'", ex);
}
}
return (ClassUtils.isAssignableValue(targetClass, payload) ? payload : null);
}
/**
* @see javax.websocket.Decoder.Text#decode(String)
* @see javax.websocket.Decoder.Binary#decode(ByteBuffer)
*/
@SuppressWarnings("unchecked")
public T decode(M message) throws DecodeException {
try {
return (T) getConversionService().convert(message, getMessageType(), getType());
}
catch (ConversionException ex) {
if (message instanceof String) {
throw new DecodeException((String) message,
"Unable to decode websocket message using ConversionService", ex);
}
if (message instanceof ByteBuffer) {
throw new DecodeException((ByteBuffer) message,
"Unable to decode websocket message using ConversionService", ex);
}
throw ex;
}
}
/** @throws ConversionException if convertion failed or no converter was found */
@SuppressWarnings("unchecked")
@Nullable
@CheckForNull
private static <T> T convert(@Nullable @CheckForNull Object source, Class<T> targetType) {
if (source == null) {
return null;
}
if (targetType.isAssignableFrom(source.getClass())) {
return (T) source;
}
try {
return getConversionService().convert(source, targetType);
} catch (ConversionException e) {
throw new DataConversionException(e);
}
}
/**
* Check if a string is valid for a type <br/>
* If conversion service is not provided try to check by apache commons
* utilities. <b>TODO</b> in this (no-conversionService) case just
* implemented for numerics
*
* @param string
* @param typeDescriptor
* @param conversionService (optional)
* @return
*/
private static boolean isValidValueFor(String string,
TypeDescriptor typeDescriptor, ConversionService conversionService) {
if (conversionService != null) {
try {
conversionService.convert(string, STRING_TYPE_DESCRIPTOR,
typeDescriptor);
}
catch (ConversionException e) {
return false;
}
return true;
}
else {
Class<?> fieldType = typeDescriptor.getType();
if (Number.class.isAssignableFrom(fieldType)
|| NUMBER_PRIMITIVES.contains(fieldType)) {
return NumberUtils.isNumber(string);
}
// TODO implement other types
return true;
}
}
/**
* Check if a string is valid for a type <br/>
* If conversion service is not provided try to check by apache commons
* utilities. <b>TODO</b> in this (no-conversionService) case just
* implemented for numerics
*
* @param string
* @param typeDescriptor
* @param conversionService (optional)
* @return
*/
private static boolean isValidValueFor(String string,
TypeDescriptor typeDescriptor, ConversionService conversionService) {
if (conversionService != null) {
try {
conversionService.convert(string, STRING_TYPE_DESCRIPTOR,
typeDescriptor);
}
catch (ConversionException e) {
return false;
}
return true;
}
else {
Class<?> fieldType = typeDescriptor.getType();
if (Number.class.isAssignableFrom(fieldType)
|| NUMBER_PRIMITIVES.contains(fieldType)) {
return NumberUtils.isNumber(string);
}
// TODO implement other types
return true;
}
}
@Override
public void extract(PageParameters sourceParameters, ILinkParameterConversionService conversionService)
throws LinkParameterExtractionException {
Args.notNull(sourceParameters, "sourceParameters");
Args.notNull(conversionService, "conversionService");
String groupId = sourceParameters.get(GROUP_ID_PARAMETER).toString();
String artifactId = sourceParameters.get(ARTIFACT_ID_PARAMETER).toString();
Artifact artifact = null;
if (groupId != null && artifactId != null) {
ArtifactKey artifactKey = new ArtifactKey(groupId, artifactId);
try {
artifact = conversionService.convert(artifactKey, Artifact.class);
} catch (ConversionException e) {
throw new LinkParameterExtractionException(e);
}
}
artifactModel.setObject(artifact);
}
@Override
@Nullable
public Object fromMessage(Message<?> message, Class<?> targetClass) {
Object payload = message.getPayload();
if (this.conversionService.canConvert(payload.getClass(), targetClass)) {
try {
return this.conversionService.convert(payload, targetClass);
}
catch (ConversionException ex) {
throw new MessageConversionException(message, "Failed to convert message payload '" +
payload + "' to '" + targetClass.getName() + "'", ex);
}
}
return (ClassUtils.isAssignableValue(targetClass, payload) ? payload : null);
}
@Test
public void fromMessageWithFailedConversion() {
Message<String> content = MessageBuilder.withPayload("test not a number").build();
assertThatExceptionOfType(MessageConversionException.class).isThrownBy(() ->
converter.fromMessage(content, Integer.class))
.withCauseInstanceOf(ConversionException.class);
}
@Override
@Nullable
public Object convertValue(@Nullable Object value, @Nullable TypeDescriptor sourceType, TypeDescriptor targetType) {
try {
return this.conversionService.convert(value, sourceType, targetType);
}
catch (ConversionException ex) {
throw new SpelEvaluationException(ex, SpelMessage.TYPE_CONVERSION_ERROR,
(sourceType != null ? sourceType.toString() : (value != null ? value.getClass().getName() : "null")),
targetType.toString());
}
}
/**
* Encode an object to a message.
* @see javax.websocket.Encoder.Text#encode(Object)
* @see javax.websocket.Encoder.Binary#encode(Object)
*/
@SuppressWarnings("unchecked")
@Nullable
public M encode(T object) throws EncodeException {
try {
return (M) getConversionService().convert(object, getType(), getMessageType());
}
catch (ConversionException ex) {
throw new EncodeException(object, "Unable to encode websocket message using ConversionService", ex);
}
}
@Override
public <T> T convert(Object source, Class<T> targetType) {
for (int i = 0; i < this.delegates.size() - 1; i++) {
try {
ConversionService delegate = this.delegates.get(i);
if (delegate.canConvert(source.getClass(), targetType)) {
return delegate.convert(source, targetType);
}
} catch (ConversionException e) {
// ignored
}
}
return this.delegates.get(this.delegates.size() - 1).convert(source, targetType);
}
@Override
public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
for (int i = 0; i < this.delegates.size() - 1; i++) {
try {
ConversionService delegate = this.delegates.get(i);
if (delegate.canConvert(sourceType, targetType)) {
return delegate.convert(source, sourceType, targetType);
}
} catch (ConversionException e) {
// ignored
}
}
return this.delegates.get(this.delegates.size() - 1).convert(source, sourceType, targetType);
}
@Override
@Nullable
public Object fromMessage(Message<?> message, Class<?> targetClass) {
Object payload = message.getPayload();
if (this.conversionService.canConvert(payload.getClass(), targetClass)) {
try {
return this.conversionService.convert(payload, targetClass);
}
catch (ConversionException ex) {
throw new MessageConversionException(message, "Failed to convert message payload '" +
payload + "' to '" + targetClass.getName() + "'", ex);
}
}
return (ClassUtils.isAssignableValue(targetClass, payload) ? payload : null);
}
@Test
public void fromMessageWithFailedConversion() {
Message<String> content = MessageBuilder.withPayload("test not a number").build();
thrown.expect(MessageConversionException.class);
thrown.expectCause(isA(ConversionException.class));
converter.fromMessage(content, Integer.class);
}
@Override
@Nullable
public Object convertValue(@Nullable Object value, @Nullable TypeDescriptor sourceType, TypeDescriptor targetType) {
try {
return this.conversionService.convert(value, sourceType, targetType);
}
catch (ConversionException ex) {
throw new SpelEvaluationException(ex, SpelMessage.TYPE_CONVERSION_ERROR,
(sourceType != null ? sourceType.toString() : (value != null ? value.getClass().getName() : "null")),
targetType.toString());
}
}
/**
* Encode an object to a message.
* @see javax.websocket.Encoder.Text#encode(Object)
* @see javax.websocket.Encoder.Binary#encode(Object)
*/
@SuppressWarnings("unchecked")
@Nullable
public M encode(T object) throws EncodeException {
try {
return (M) getConversionService().convert(object, getType(), getMessageType());
}
catch (ConversionException ex) {
throw new EncodeException(object, "Unable to encode websocket message using ConversionService", ex);
}
}
@Override
public Object convert(Object source, TypeDescriptor sourceType,
TypeDescriptor targetType) {
for (int i = 0; i < this.delegates.size() - 1; i++) {
try {
ConversionService delegate = this.delegates.get(i);
if (delegate.canConvert(sourceType, targetType)) {
return delegate.convert(source, sourceType, targetType);
}
} catch (ConversionException ex) {
}
}
return this.delegates.get(this.delegates.size() - 1).convert(source,
sourceType, targetType);
}
@Override
public Object convertValue(Object value, TypeDescriptor sourceType, TypeDescriptor targetType) {
try {
return this.conversionService.convert(value, sourceType, targetType);
}
catch (ConversionException ex) {
throw new SpelEvaluationException(ex, SpelMessage.TYPE_CONVERSION_ERROR,
(sourceType != null ? sourceType.toString() : (value != null ? value.getClass().getName() : "null")),
targetType.toString());
}
}
@SuppressWarnings("unchecked")
public <T> T getPropertyValue(String fieldName, EmbeddedType embeddedType, TypeInformation targetTypeInformation) {
if (!this.entity.contains(fieldName)) {
return null;
}
try {
return this.conversion.convertOnRead(this.entity.getValue(fieldName).get(),
embeddedType, targetTypeInformation);
}
catch (ConversionException | DatastoreDataException ex) {
throw new DatastoreDataException("Unable to read property " + fieldName, ex);
}
}
@Test
public void fromMessageWithFailedConversion() {
Message<String> content = MessageBuilder.withPayload("test not a number").build();
thrown.expect(MessageConversionException.class);
thrown.expectCause(isA(ConversionException.class));
converter.fromMessage(content, Integer.class);
}
@Test(expected=ConversionException.class)
public void getPropertyAsClass_withMismatchedTypeForValue() {
MutablePropertySources propertySources = new MutablePropertySources();
propertySources.addFirst(new MockPropertySource().withProperty("some.class", "java.lang.String"));
PropertyResolver resolver = new PropertySourcesPropertyResolver(propertySources);
resolver.getPropertyAsClass("some.class", SomeType.class);
}
@Test(expected=ConversionException.class)
public void getPropertyAsClass_withNonExistentClassForValue() {
MutablePropertySources propertySources = new MutablePropertySources();
propertySources.addFirst(new MockPropertySource().withProperty("some.class", "some.bogus.Class"));
PropertyResolver resolver = new PropertySourcesPropertyResolver(propertySources);
resolver.getPropertyAsClass("some.class", SomeType.class);
}
@Test(expected=ConversionException.class)
public void getPropertyAsClass_withMismatchedObjectForValue() {
MutablePropertySources propertySources = new MutablePropertySources();
propertySources.addFirst(new MockPropertySource().withProperty("some.class", new Integer(42)));
PropertyResolver resolver = new PropertySourcesPropertyResolver(propertySources);
resolver.getPropertyAsClass("some.class", SomeType.class);
}
@Test(expected=ConversionException.class)
public void getPropertyAsClass_withMismatchedRealClassForValue() {
MutablePropertySources propertySources = new MutablePropertySources();
propertySources.addFirst(new MockPropertySource().withProperty("some.class", Integer.class));
PropertyResolver resolver = new PropertySourcesPropertyResolver(propertySources);
resolver.getPropertyAsClass("some.class", SomeType.class);
}
@Override
public Object convertValue(Object value, TypeDescriptor sourceType, TypeDescriptor targetType) {
try {
return this.conversionService.convert(value, sourceType, targetType);
}
catch (ConversionException ex) {
throw new SpelEvaluationException(
ex, SpelMessage.TYPE_CONVERSION_ERROR, sourceType.toString(), targetType.toString());
}
}
/**
* @see javax.websocket.Encoder.Text#encode(Object)
* @see javax.websocket.Encoder.Binary#encode(Object)
*/
@SuppressWarnings("unchecked")
public M encode(T object) throws EncodeException {
try {
return (M) getConversionService().convert(object, getType(), getMessageType());
}
catch (ConversionException ex) {
throw new EncodeException(object, "Unable to encode websocket message using ConversionService", ex);
}
}
private String convertToString(Object invalidValue) {
if (invalidValue == null) {
return null;
}
try {
return conversionService.convert(invalidValue, String.class);
} catch (ConversionException ex) {
return invalidValue.toString();
}
}
/**
* Return where clause expression for number properties by casting it to
* string before check its value.
* <p/>
* Querydsl Expr:
* {@code entityPath.fieldName.stringValue() eq searchStr
* Database operation:
* {@code str(entity.fieldName) = searchStr
* <p/>
* Like operation is case sensitive.
*
* @param entityPath Full path to entity and associations. For example:
* {@code Pet} , {@code Pet.owner}
* @param fieldName Property name in the given entity path. For example:
* {@code weight} in {@code Pet} entity, {@code age} in
* {@code Pet.owner} entity.
* @param searchStr the value to find, may be null
* @return PredicateOperation
*/
@SuppressWarnings("unchecked")
public static <T, N extends java.lang.Number & java.lang.Comparable<?>> BooleanExpression createNumberExpressionEqual(
PathBuilder<T> entityPath, String fieldName, Class<N> fieldType,
TypeDescriptor descriptor, String searchStr,
ConversionService conversionService) {
if (StringUtils.isEmpty(searchStr)) {
return null;
}
NumberPath<N> numberExpression = entityPath.getNumber(fieldName,
fieldType);
TypeDescriptor strDesc = STRING_TYPE_DESCRIPTOR;
if (conversionService != null) {
try {
return numberExpression.eq((N) conversionService.convert(
searchStr, strDesc, descriptor));
}
catch (ConversionException ex) {
return numberExpression.stringValue().like(
"%".concat(searchStr).concat("%"));
}
}
else {
return numberExpression.stringValue().like(
"%".concat(searchStr).concat("%"));
}
}