下面列出了org.springframework.util.ClassUtils#getDescriptiveType ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
public Object resolveArgument(MethodParameter parameter, Message<?> message) throws Exception {
Class<?> targetMessageType = parameter.getParameterType();
Class<?> targetPayloadType = getPayloadType(parameter, message);
if (!targetMessageType.isAssignableFrom(message.getClass())) {
throw new MethodArgumentTypeMismatchException(message, parameter, "Actual message type '" +
ClassUtils.getDescriptiveType(message) + "' does not match expected type '" +
ClassUtils.getQualifiedName(targetMessageType) + "'");
}
Object payload = message.getPayload();
if (targetPayloadType.isInstance(payload)) {
return message;
}
if (isEmptyPayload(payload)) {
throw new MessageConversionException(message, "Cannot convert from actual payload type '" +
ClassUtils.getDescriptiveType(payload) + "' to expected payload type '" +
ClassUtils.getQualifiedName(targetPayloadType) + "' when payload is empty");
}
payload = convertPayload(message, parameter, targetPayloadType);
return MessageBuilder.createMessage(payload, message.getHeaders());
}
private Object convertPayload(Message<?> message, MethodParameter parameter, Class<?> targetPayloadType) {
Object result = null;
if (this.converter instanceof SmartMessageConverter) {
SmartMessageConverter smartConverter = (SmartMessageConverter) this.converter;
result = smartConverter.fromMessage(message, targetPayloadType, parameter);
}
else if (this.converter != null) {
result = this.converter.fromMessage(message, targetPayloadType);
}
if (result == null) {
throw new MessageConversionException(message, "No converter found from actual payload type '" +
ClassUtils.getDescriptiveType(message.getPayload()) + "' to expected payload type '" +
ClassUtils.getQualifiedName(targetPayloadType) + "'");
}
return result;
}
private Object convertPayload(Message<?> message, MethodParameter parameter,
Class<?> targetPayloadType) {
Object result = null;
if (this.messageConverter instanceof SmartMessageConverter) {
SmartMessageConverter smartConverter = (SmartMessageConverter) this.messageConverter;
result = smartConverter.fromMessage(message, targetPayloadType, parameter);
}
else if (this.messageConverter != null) {
result = this.messageConverter.fromMessage(message, targetPayloadType);
}
if (result == null) {
throw new MessageConversionException(message,
"No converter found from actual payload type '"
+ ClassUtils.getDescriptiveType(message.getPayload())
+ "' to expected payload type '"
+ ClassUtils.getQualifiedName(targetPayloadType) + "'");
}
return result;
}
/**
* Create a new {@code TypeMismatchException}.
* @param propertyChangeEvent the PropertyChangeEvent that resulted in the problem
* @param requiredType the required target type (or {@code null} if not known)
* @param cause the root cause (may be {@code null})
*/
public TypeMismatchException(PropertyChangeEvent propertyChangeEvent, @Nullable Class<?> requiredType,
@Nullable Throwable cause) {
super(propertyChangeEvent,
"Failed to convert property value of type '" +
ClassUtils.getDescriptiveType(propertyChangeEvent.getNewValue()) + "'" +
(requiredType != null ?
" to required type '" + ClassUtils.getQualifiedName(requiredType) + "'" : "") +
(propertyChangeEvent.getPropertyName() != null ?
" for property '" + propertyChangeEvent.getPropertyName() + "'" : ""),
cause);
this.propertyName = propertyChangeEvent.getPropertyName();
this.value = propertyChangeEvent.getNewValue();
this.requiredType = requiredType;
}
@Override
public Object resolveArgument(MethodParameter parameter, Message<?> message) throws Exception {
Class<?> targetMessageType = parameter.getParameterType();
Class<?> targetPayloadType = getPayloadType(parameter);
if (!targetMessageType.isAssignableFrom(message.getClass())) {
throw new MethodArgumentTypeMismatchException(message, parameter, "Actual message type '" +
ClassUtils.getDescriptiveType(message) + "' does not match expected type '" +
ClassUtils.getQualifiedName(targetMessageType) + "'");
}
Object payload = message.getPayload();
if (targetPayloadType.isInstance(payload)) {
return message;
}
if (isEmptyPayload(payload)) {
throw new MessageConversionException(message, "Cannot convert from actual payload type '" +
ClassUtils.getDescriptiveType(payload) + "' to expected payload type '" +
ClassUtils.getQualifiedName(targetPayloadType) + "' when payload is empty");
}
payload = convertPayload(message, parameter, targetPayloadType);
return MessageBuilder.createMessage(payload, message.getHeaders());
}
private Object convertPayload(Message<?> message, MethodParameter parameter, Class<?> targetPayloadType) {
Object result = null;
if (this.converter instanceof SmartMessageConverter) {
SmartMessageConverter smartConverter = (SmartMessageConverter) this.converter;
result = smartConverter.fromMessage(message, targetPayloadType, parameter);
}
else if (this.converter != null) {
result = this.converter.fromMessage(message, targetPayloadType);
}
if (result == null) {
throw new MessageConversionException(message, "No converter found from actual payload type '" +
ClassUtils.getDescriptiveType(message.getPayload()) + "' to expected payload type '" +
ClassUtils.getQualifiedName(targetPayloadType) + "'");
}
return result;
}
/**
* Create a new {@code TypeMismatchException} without a {@code PropertyChangeEvent}.
* @param value the offending value that couldn't be converted (may be {@code null})
* @param requiredType the required target type (or {@code null} if not known)
* @param cause the root cause (may be {@code null})
* @see #initPropertyName
*/
public TypeMismatchException(@Nullable Object value, @Nullable Class<?> requiredType, @Nullable Throwable cause) {
super("Failed to convert value of type '" + ClassUtils.getDescriptiveType(value) + "'" +
(requiredType != null ? " to required type '" + ClassUtils.getQualifiedName(requiredType) + "'" : ""),
cause);
this.value = value;
this.requiredType = requiredType;
}
@Override
public Object resolveArgument(MethodParameter parameter, Message<?> message)
throws Exception {
Class<?> targetMessageType = parameter.getParameterType();
Class<?> targetPayloadType = getPayloadType(parameter);
if (!targetMessageType.isAssignableFrom(message.getClass())) {
throw new MethodArgumentTypeMismatchException(message, parameter,
"Actual message type '" + ClassUtils.getDescriptiveType(message)
+ "' does not match expected type '"
+ ClassUtils.getQualifiedName(targetMessageType) + "'");
}
Class<?> payloadClass = message.getPayload().getClass();
if (message instanceof ErrorMessage
|| conversionNotRequired(payloadClass, targetPayloadType)) {
return message;
}
Object payload = message.getPayload();
if (isEmptyPayload(payload)) {
throw new MessageConversionException(message,
"Cannot convert from actual payload type '"
+ ClassUtils.getDescriptiveType(payload)
+ "' to expected payload type '"
+ ClassUtils.getQualifiedName(targetPayloadType)
+ "' when payload is empty");
}
payload = convertPayload(message, parameter, targetPayloadType);
return MessageBuilder.createMessage(payload, message.getHeaders());
}
/**
* Create a new {@code TypeMismatchException} without a {@code PropertyChangeEvent}.
* @param value the offending value that couldn't be converted (may be {@code null})
* @param requiredType the required target type (or {@code null} if not known)
* @param cause the root cause (may be {@code null})
* @see #initPropertyName
*/
public TypeMismatchException(@Nullable Object value, @Nullable Class<?> requiredType, @Nullable Throwable cause) {
super("Failed to convert value of type '" + ClassUtils.getDescriptiveType(value) + "'" +
(requiredType != null ? " to required type '" + ClassUtils.getQualifiedName(requiredType) + "'" : ""),
cause);
this.value = value;
this.requiredType = requiredType;
}
/**
* Create a new TypeMismatchException.
* @param propertyChangeEvent the PropertyChangeEvent that resulted in the problem
* @param requiredType the required target type (or {@code null} if not known)
* @param cause the root cause (may be {@code null})
*/
public TypeMismatchException(PropertyChangeEvent propertyChangeEvent, Class<?> requiredType, Throwable cause) {
super(propertyChangeEvent,
"Failed to convert property value of type '" +
ClassUtils.getDescriptiveType(propertyChangeEvent.getNewValue()) + "'" +
(requiredType != null ?
" to required type '" + ClassUtils.getQualifiedName(requiredType) + "'" : "") +
(propertyChangeEvent.getPropertyName() != null ?
" for property '" + propertyChangeEvent.getPropertyName() + "'" : ""),
cause);
this.value = propertyChangeEvent.getNewValue();
this.requiredType = requiredType;
}
/**
* Create a new TypeMismatchException without PropertyChangeEvent.
* @param value the offending value that couldn't be converted (may be {@code null})
* @param requiredType the required target type (or {@code null} if not known)
* @param cause the root cause (may be {@code null})
*/
public TypeMismatchException(Object value, Class<?> requiredType, Throwable cause) {
super("Failed to convert value of type '" + ClassUtils.getDescriptiveType(value) + "'" +
(requiredType != null ? " to required type '" + ClassUtils.getQualifiedName(requiredType) + "'" : ""),
cause);
this.value = value;
this.requiredType = requiredType;
}
/**
* Create a new TypeMismatchException.
* @param propertyChangeEvent the PropertyChangeEvent that resulted in the problem
* @param requiredType the required target type (or {@code null} if not known)
* @param cause the root cause (may be {@code null})
*/
public TypeMismatchException(PropertyChangeEvent propertyChangeEvent, Class<?> requiredType, Throwable cause) {
super(propertyChangeEvent,
"Failed to convert property value of type '" +
ClassUtils.getDescriptiveType(propertyChangeEvent.getNewValue()) + "'" +
(requiredType != null ?
" to required type '" + ClassUtils.getQualifiedName(requiredType) + "'" : "") +
(propertyChangeEvent.getPropertyName() != null ?
" for property '" + propertyChangeEvent.getPropertyName() + "'" : ""),
cause);
this.value = propertyChangeEvent.getNewValue();
this.requiredType = requiredType;
}
/**
* Create a new TypeMismatchException without PropertyChangeEvent.
* @param value the offending value that couldn't be converted (may be {@code null})
* @param requiredType the required target type (or {@code null} if not known)
* @param cause the root cause (may be {@code null})
*/
public TypeMismatchException(Object value, Class<?> requiredType, Throwable cause) {
super("Failed to convert value of type '" + ClassUtils.getDescriptiveType(value) + "'" +
(requiredType != null ? " to required type '" + ClassUtils.getQualifiedName(requiredType) + "'" : ""),
cause);
this.value = value;
this.requiredType = requiredType;
}
/**
* Create a new TypeMismatchException.
* @param propertyChangeEvent the PropertyChangeEvent that resulted in the problem
* @param requiredType the required target type (or {@code null} if not known)
* @param cause the root cause (may be {@code null})
*/
public TypeMismatchException(PropertyChangeEvent propertyChangeEvent, Class<?> requiredType, Throwable cause) {
super(propertyChangeEvent,
"Failed to convert property value of type [" +
ClassUtils.getDescriptiveType(propertyChangeEvent.getNewValue()) + "]" +
(requiredType != null ?
" to required type [" + ClassUtils.getQualifiedName(requiredType) + "]" : "") +
(propertyChangeEvent.getPropertyName() != null ?
" for property '" + propertyChangeEvent.getPropertyName() + "'" : ""),
cause);
this.value = propertyChangeEvent.getNewValue();
this.requiredType = requiredType;
}
/**
* Create a new TypeMismatchException without PropertyChangeEvent.
* @param value the offending value that couldn't be converted (may be {@code null})
* @param requiredType the required target type (or {@code null} if not known)
* @param cause the root cause (may be {@code null})
*/
public TypeMismatchException(Object value, Class<?> requiredType, Throwable cause) {
super("Failed to convert value of type [" + ClassUtils.getDescriptiveType(value) + "]" +
(requiredType != null ? " to required type [" + ClassUtils.getQualifiedName(requiredType) + "]" : ""),
cause);
this.value = value;
this.requiredType = requiredType;
}
/**
* Perform the actual reading of an invocation result object from the
* given ObjectInputStream.
* <p>The default implementation simply calls
* {@link java.io.ObjectInputStream#readObject()}.
* Can be overridden for deserialization of a custom wrapper object rather
* than the plain invocation, for example an encryption-aware holder.
* @param ois the ObjectInputStream to read from
* @return the RemoteInvocationResult object
* @throws java.io.IOException in case of I/O failure
* @throws ClassNotFoundException if case of a transferred class not
* being found in the local ClassLoader
*/
protected RemoteInvocation doReadRemoteInvocation(ObjectInputStream ois)
throws IOException, ClassNotFoundException {
Object obj = ois.readObject();
if (!(obj instanceof RemoteInvocation)) {
throw new RemoteException("Deserialized object needs to be assignable to type [" +
RemoteInvocation.class.getName() + "]: " + ClassUtils.getDescriptiveType(obj));
}
return (RemoteInvocation) obj;
}
/**
* Perform the actual reading of an invocation result object from the
* given ObjectInputStream.
* <p>The default implementation simply calls
* {@link java.io.ObjectInputStream#readObject()}.
* Can be overridden for deserialization of a custom wrapper object rather
* than the plain invocation, for example an encryption-aware holder.
* @param ois the ObjectInputStream to read from
* @return the RemoteInvocationResult object
* @throws java.io.IOException in case of I/O failure
* @throws ClassNotFoundException if case of a transferred class not
* being found in the local ClassLoader
*/
protected RemoteInvocation doReadRemoteInvocation(ObjectInputStream ois)
throws IOException, ClassNotFoundException {
Object obj = ois.readObject();
if (!(obj instanceof RemoteInvocation)) {
throw new RemoteException("Deserialized object needs to be assignable to type [" +
RemoteInvocation.class.getName() + "]: " + ClassUtils.getDescriptiveType(obj));
}
return (RemoteInvocation) obj;
}
/**
* Perform the actual reading of an invocation object from the
* given ObjectInputStream.
* <p>The default implementation simply calls {@code readObject}.
* Can be overridden for deserialization of a custom wrapper object rather
* than the plain invocation, for example an encryption-aware holder.
* @param ois the ObjectInputStream to read from
* @return the RemoteInvocationResult object
* @throws IOException if thrown by I/O methods
* @throws ClassNotFoundException if the class name of a serialized object
* couldn't get resolved
* @see java.io.ObjectOutputStream#writeObject
*/
protected RemoteInvocationResult doReadRemoteInvocationResult(ObjectInputStream ois)
throws IOException, ClassNotFoundException {
Object obj = ois.readObject();
if (!(obj instanceof RemoteInvocationResult)) {
throw new RemoteException("Deserialized object needs to be assignable to type [" +
RemoteInvocationResult.class.getName() + "]: " + ClassUtils.getDescriptiveType(obj));
}
return (RemoteInvocationResult) obj;
}
/**
* Perform the actual reading of an invocation result object from the
* given ObjectInputStream.
* <p>The default implementation simply calls
* {@link java.io.ObjectInputStream#readObject()}.
* Can be overridden for deserialization of a custom wrapper object rather
* than the plain invocation, for example an encryption-aware holder.
* @param ois the ObjectInputStream to read from
* @return the RemoteInvocationResult object
* @throws java.io.IOException in case of I/O failure
* @throws ClassNotFoundException if case of a transferred class not
* being found in the local ClassLoader
*/
protected RemoteInvocation doReadRemoteInvocation(ObjectInputStream ois)
throws IOException, ClassNotFoundException {
Object obj = ois.readObject();
if (!(obj instanceof RemoteInvocation)) {
throw new RemoteException("Deserialized object needs to be assignable to type [" +
RemoteInvocation.class.getName() + "]: " + ClassUtils.getDescriptiveType(obj));
}
return (RemoteInvocation) obj;
}
/**
* Perform the actual reading of an invocation object from the
* given ObjectInputStream.
* <p>The default implementation simply calls {@code readObject}.
* Can be overridden for deserialization of a custom wrapper object rather
* than the plain invocation, for example an encryption-aware holder.
* @param ois the ObjectInputStream to read from
* @return the RemoteInvocationResult object
* @throws IOException if thrown by I/O methods
* @throws ClassNotFoundException if the class name of a serialized object
* couldn't get resolved
* @see java.io.ObjectOutputStream#writeObject
*/
protected RemoteInvocationResult doReadRemoteInvocationResult(ObjectInputStream ois)
throws IOException, ClassNotFoundException {
Object obj = ois.readObject();
if (!(obj instanceof RemoteInvocationResult)) {
throw new RemoteException("Deserialized object needs to be assignable to type [" +
RemoteInvocationResult.class.getName() + "]: " + ClassUtils.getDescriptiveType(obj));
}
return (RemoteInvocationResult) obj;
}