1 package com.fasterxml.jackson.databind.util;
2 
3 import com.fasterxml.jackson.databind.JavaType;
4 import com.fasterxml.jackson.databind.type.TypeFactory;
5 
6 /**
7  * Standard implementation of {@link Converter} that supports explicit
8  * type access, instead of relying type detection of generic type
9  * parameters.
10  *
11  * @since 2.2
12  */
13 public abstract class StdConverter<IN,OUT>
14     implements Converter<IN,OUT>
15 {
16     /*
17     /**********************************************************
18     /* Partial Converter API implementation
19     /**********************************************************
20      */
21 
22     @Override
convert(IN value)23     public abstract OUT convert(IN value);
24 
25     @Override
getInputType(TypeFactory typeFactory)26     public JavaType getInputType(TypeFactory typeFactory) {
27         return _findConverterType(typeFactory).containedType(0);
28     }
29 
30     @Override
getOutputType(TypeFactory typeFactory)31     public JavaType getOutputType(TypeFactory typeFactory) {
32         return _findConverterType(typeFactory).containedType(1);
33     }
34 
_findConverterType(TypeFactory tf)35     protected JavaType _findConverterType(TypeFactory tf) {
36         JavaType thisType = tf.constructType(getClass());
37         JavaType convType = thisType.findSuperType(Converter.class);
38         if (convType == null || convType.containedTypeCount() < 2) {
39             throw new IllegalStateException("Cannot find OUT type parameter for Converter of type "+getClass().getName());
40         }
41         return convType;
42     }
43 }
44