Java Code Examples for org.springframework.util.ClassUtils#getDescriptiveType()

The following examples show how to use org.springframework.util.ClassUtils#getDescriptiveType() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MessageMethodArgumentResolver.java    From spring-analysis-note with MIT License 6 votes vote down vote up
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;
}
 
Example 2
Source File: MessageMethodArgumentResolver.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@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());
}
 
Example 3
Source File: SmartMessageMethodArgumentResolver.java    From spring-cloud-stream with Apache License 2.0 6 votes vote down vote up
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;
}
 
Example 4
Source File: TypeMismatchException.java    From spring-analysis-note with MIT License 6 votes vote down vote up
/**
 * 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;
}
 
Example 5
Source File: MessageMethodArgumentResolver.java    From java-technology-stack with MIT License 6 votes vote down vote up
@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());
}
 
Example 6
Source File: MessageMethodArgumentResolver.java    From java-technology-stack with MIT License 6 votes vote down vote up
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;
}
 
Example 7
Source File: TypeMismatchException.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
/**
 * 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;
}
 
Example 8
Source File: TypeMismatchException.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
/**
 * 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;
}
 
Example 9
Source File: TypeMismatchException.java    From blog_demos with Apache License 2.0 5 votes vote down vote up
/**
 * 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;
}
 
Example 10
Source File: TypeMismatchException.java    From blog_demos with Apache License 2.0 5 votes vote down vote up
/**
 * 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;
}
 
Example 11
Source File: TypeMismatchException.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * 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;
}
 
Example 12
Source File: TypeMismatchException.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * 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;
}
 
Example 13
Source File: TypeMismatchException.java    From java-technology-stack with MIT License 5 votes vote down vote up
/**
 * 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;
}
 
Example 14
Source File: SmartMessageMethodArgumentResolver.java    From spring-cloud-stream with Apache License 2.0 5 votes vote down vote up
@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());
}
 
Example 15
Source File: TypeMismatchException.java    From spring-analysis-note with MIT License 5 votes vote down vote up
/**
 * 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;
}
 
Example 16
Source File: RemoteInvocationSerializingExporter.java    From lams with GNU General Public License v2.0 3 votes vote down vote up
/**
 * 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;
}
 
Example 17
Source File: AbstractHttpInvokerRequestExecutor.java    From lams with GNU General Public License v2.0 3 votes vote down vote up
/**
 * 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;
}
 
Example 18
Source File: AbstractHttpInvokerRequestExecutor.java    From java-technology-stack with MIT License 3 votes vote down vote up
/**
 * 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;
}
 
Example 19
Source File: RemoteInvocationSerializingExporter.java    From java-technology-stack with MIT License 3 votes vote down vote up
/**
 * 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;
}
 
Example 20
Source File: RemoteInvocationSerializingExporter.java    From spring-analysis-note with MIT License 3 votes vote down vote up
/**
 * 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;
}