@@ -67,17 +67,31 @@ public void process(FieldContext context, Convert annotation, HandlerChain chain
6767 "Found Binding which is not a BindingBuilder but: " + binding .getClass ().getSimpleName ());
6868 BindingBuilder bindingBuilder = (BindingBuilder ) binding ;
6969
70- // add the converter
71- Class <?> converterType = annotation .with ();
72- LazyConverterAdapter converter = new LazyConverterAdapter (converterType );
70+ Converter <?> converter = null ;
71+
72+ // identify converter by the type of the converter
73+ if (annotation .with () != Object .class ) {
74+ converter = LazyConverterAdapter .forConverterType (annotation .with ());
75+ }
76+
77+ // identify converter by some kind of unique id
78+ else if (annotation .id ().length () > 0 ) {
79+ converter = LazyConverterAdapter .forConverterId (annotation .id ());
80+ }
81+
82+ // default: identify converter by the target type
83+ else {
84+ converter = LazyConverterAdapter .forTargetType (field .getType ());
85+ }
86+
7387 bindingBuilder .convertedBy (converter );
7488
75- // some logging
7689 if (log .isTraceEnabled ()) {
77- log .trace ("Attached converter adapter for [{}] to field [{}] of class [{}]" , new Object [] {
78- converterType . getSimpleName (), field .getName (), field .getDeclaringClass ().getName ()
90+ log .trace ("Attached converter to field [{}] of class [{}]: " , new Object [] {
91+ field .getName (), field .getDeclaringClass ().getName (), converter
7992 });
8093 }
94+
8195 }
8296
8397 // continue with the chain
@@ -92,11 +106,30 @@ public void process(FieldContext context, Convert annotation, HandlerChain chain
92106 private static class LazyConverterAdapter implements Converter <Object >
93107 {
94108
95- private final Class <?> converterClass ;
109+ private final Class <?> targetType ;
110+ private final String converterId ;
111+ private final Class <?> converterType ;
112+
113+ private LazyConverterAdapter (Class <?> targetType , String converterId , Class <?> converterType )
114+ {
115+ this .targetType = targetType ;
116+ this .converterId = converterId ;
117+ this .converterType = converterType ;
118+ }
119+
120+ public static LazyConverterAdapter forConverterType (Class <?> converterType )
121+ {
122+ return new LazyConverterAdapter (null , null , converterType );
123+ }
124+
125+ public static LazyConverterAdapter forConverterId (String id )
126+ {
127+ return new LazyConverterAdapter (null , id , null );
128+ }
96129
97- public LazyConverterAdapter (Class <?> clazz )
130+ public static LazyConverterAdapter forTargetType (Class <?> targetType )
98131 {
99- this . converterClass = clazz ;
132+ return new LazyConverterAdapter ( targetType , null , null ) ;
100133 }
101134
102135 @ Override
@@ -110,15 +143,48 @@ public Object convert(Rewrite event, EvaluationContext context, Object value)
110143 Iterator <ConverterProvider > providers = ServiceLoader .load (ConverterProvider .class ).iterator ();
111144 while (providers .hasNext ()) {
112145 ConverterProvider provider = providers .next ();
113- converter = provider .getByType (converterClass );
146+
147+ if (targetType != null ) {
148+ converter = provider .getByTargetType (targetType );
149+ }
150+ else if (converterType != null ) {
151+ converter = provider .getByConverterType (converterType );
152+ }
153+ else {
154+ converter = provider .getByConverterId (converterId );
155+ }
156+
114157 if (converter != null ) {
115158 break ;
116159 }
160+
117161 }
118- Assert .notNull (converter , "Got no result from any ConverterProvider for type : " + converterClass . getName ());
162+ Assert .notNull (converter , "Got no converter from any ConverterProvider for: " + this . toString ());
119163
120164 return converter .convert (event , context , value );
121165
122166 }
167+
168+ @ Override
169+ public String toString ()
170+ {
171+ StringBuilder b = new StringBuilder ();
172+ b .append (this .getClass ().getSimpleName ());
173+ b .append (" for " );
174+ if (targetType != null ) {
175+ b .append (" target type " );
176+ b .append (targetType .getName ());
177+ }
178+ else if (converterType != null ) {
179+ b .append (" converter type " );
180+ b .append (converterType .getName ());
181+ }
182+ else {
183+ b .append (" id " );
184+ b .append (converterId );
185+ }
186+ return b .toString ();
187+ }
188+
123189 }
124190}
0 commit comments