2424import  org .apache .commons .logging .Log ;
2525import  org .apache .commons .logging .LogFactory ;
2626
27- import  org .springframework .core .MethodParameter ;
2827import  org .springframework .messaging .Message ;
2928import  org .springframework .messaging .MessageHeaders ;
3029import  org .springframework .messaging .support .MessageBuilder ;
4039 * 
4140 * @author Rossen Stoyanchev 
4241 * @author Sebastien Deleuze 
42+  * @author Juergen Hoeller 
4343 * @since 4.0 
4444 */ 
4545public  abstract  class  AbstractMessageConverter  implements  MessageConverter  {
4646
47- 	/** 
48- 	 * Name of the header that can be set to provide further information 
49- 	 * ({@link MethodParameter} instance) about the origin of the payload (for 
50- 	 * {@link #toMessage(Object, MessageHeaders)}) or about the target of the payload 
51- 	 * ({@link #fromMessage(Message, Class)}). 
52- 	 * @since 4.2 
53- 	 */ 
54- 	public  static  final  String  METHOD_PARAMETER_HINT_HEADER  = "methodParameterHint" ;
55- 
56- 
5747	protected  final  Log  logger  = LogFactory .getLog (getClass ());
5848
5949	private  final  List <MimeType > supportedMimeTypes ;
@@ -174,10 +164,27 @@ protected MimeType getDefaultContentType(Object payload) {
174164
175165	@ Override 
176166	public  final  Object  fromMessage (Message <?> message , Class <?> targetClass ) {
167+ 		return  fromMessage (message , targetClass , null );
168+ 	}
169+ 
170+ 	/** 
171+ 	 * A variant of {@link #fromMessage(Message, Class)} which takes an extra 
172+ 	 * conversion context as an argument, allowing to take e.g. annotations 
173+ 	 * on a payload parameter into account. 
174+ 	 * @param message the input message 
175+ 	 * @param targetClass the target class for the conversion 
176+ 	 * @param conversionHint an extra object passed to the {@link MessageConverter}, 
177+ 	 * e.g. the associated {@code MethodParameter} (may be {@code null}} 
178+ 	 * @return the result of the conversion, or {@code null} if the converter cannot 
179+ 	 * perform the conversion 
180+ 	 * @since 4.2 
181+ 	 * @see #fromMessage(Message, Class) 
182+ 	 */ 
183+ 	public  final  Object  fromMessage (Message <?> message , Class <?> targetClass , Object  conversionHint ) {
177184		if  (!canConvertFrom (message , targetClass )) {
178185			return  null ;
179186		}
180- 		return  convertFromInternal (message , targetClass );
187+ 		return  convertFromInternal (message , targetClass ,  conversionHint );
181188	}
182189
183190	protected  boolean  canConvertFrom (Message <?> message , Class <?> targetClass ) {
@@ -186,13 +193,33 @@ protected boolean canConvertFrom(Message<?> message, Class<?> targetClass) {
186193
187194	@ Override 
188195	public  final  Message <?> toMessage (Object  payload , MessageHeaders  headers ) {
196+ 		return  toMessage (payload , headers , null );
197+ 	}
198+ 
199+ 	/** 
200+ 	 * A variant of {@link #toMessage(Object, MessageHeaders)} which takes an extra 
201+ 	 * conversion context as an argument, allowing to take e.g. annotations 
202+ 	 * on a return type into account. 
203+ 	 * @param payload the Object to convert 
204+ 	 * @param headers optional headers for the message (may be {@code null}) 
205+ 	 * @param conversionHint an extra object passed to the {@link MessageConverter}, 
206+ 	 * e.g. the associated {@code MethodParameter} (may be {@code null}} 
207+ 	 * @return the new message, or {@code null} if the converter does not support the 
208+ 	 * Object type or the target media type 
209+ 	 * @since 4.2 
210+ 	 * @see #toMessage(Object, MessageHeaders) 
211+ 	 */ 
212+ 	public  final  Message <?> toMessage (Object  payload , MessageHeaders  headers , Object  conversionHint ) {
189213		if  (!canConvertTo (payload , headers )) {
190214			return  null ;
191215		}
192216
193- 		payload  = convertToInternal (payload , headers );
194- 		MimeType  mimeType  = getDefaultContentType (payload );
217+ 		payload  = convertToInternal (payload , headers , conversionHint );
218+ 		if  (payload  == null ) {
219+ 			return  null ;
220+ 		}
195221
222+ 		MimeType  mimeType  = getDefaultContentType (payload );
196223		if  (headers  != null ) {
197224			MessageHeaderAccessor  accessor  = MessageHeaderAccessor .getAccessor (headers , MessageHeaderAccessor .class );
198225			if  (accessor  != null  && accessor .isMutable ()) {
@@ -244,13 +271,52 @@ protected MimeType getMimeType(MessageHeaders headers) {
244271
245272	/** 
246273	 * Convert the message payload from serialized form to an Object. 
274+ 	 * @param message the input message 
275+ 	 * @param targetClass the target class for the conversion 
276+ 	 * @param conversionHint an extra object passed to the {@link MessageConverter}, 
277+ 	 * e.g. the associated {@code MethodParameter} (may be {@code null}} 
278+ 	 * @return the result of the conversion, or {@code null} if the converter cannot 
279+ 	 * perform the conversion 
280+ 	 * @since 4.2 
247281	 */ 
248- 	public  abstract  Object  convertFromInternal (Message <?> message , Class <?> targetClass );
282+ 	@ SuppressWarnings ("deprecation" )
283+ 	protected  Object  convertFromInternal (Message <?> message , Class <?> targetClass , Object  conversionHint ) {
284+ 		return  convertFromInternal (message , targetClass );
285+ 	}
249286
287+ 	/** 
288+ 	 * Convert the payload object to serialized form. 
289+ 	 * @param payload the Object to convert 
290+ 	 * @param headers optional headers for the message (may be {@code null}) 
291+ 	 * @param conversionHint an extra object passed to the {@link MessageConverter}, 
292+ 	 * e.g. the associated {@code MethodParameter} (may be {@code null}} 
293+ 	 * @return the resulting payload for the message, or {@code null} if the converter 
294+ 	 * cannot perform the conversion 
295+ 	 * @since 4.2 
296+ 	 */ 
297+ 	@ SuppressWarnings ("deprecation" )
298+ 	protected  Object  convertToInternal (Object  payload , MessageHeaders  headers , Object  conversionHint ) {
299+ 		return  convertToInternal (payload , headers );
300+ 	}
301+ 
302+ 	/** 
303+ 	 * Convert the message payload from serialized form to an Object. 
304+ 	 * @deprecated as of Spring 4.2, in favor of {@link #convertFromInternal(Message, Class, Object)} 
305+ 	 * (which is also protected instead of public) 
306+ 	 */ 
307+ 	@ Deprecated 
308+ 	public  Object  convertFromInternal (Message <?> message , Class <?> targetClass ) {
309+ 		return  null ;
310+ 	}
250311
251312	/** 
252313	 * Convert the payload object to serialized form. 
314+ 	 * @deprecated as of Spring 4.2, in favor of {@link #convertFromInternal(Message, Class, Object)} 
315+ 	 * (which is also protected instead of public) 
253316	 */ 
254- 	public  abstract  Object  convertToInternal (Object  payload , MessageHeaders  headers );
317+ 	@ Deprecated 
318+ 	public  Object  convertToInternal (Object  payload , MessageHeaders  headers ) {
319+ 		return  null ;
320+ 	}
255321
256322}
0 commit comments