下面列出了com.fasterxml.jackson.databind.JavaType#findSuperType ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Method similar to {@link #constructSpecializedType}, but that creates a
* less-specific type of given type. Usually this is as simple as simply
* finding super-type with type erasure of <code>superClass</code>, but
* there may be need for some additional work-arounds.
*
* @param superClass
*
* @since 2.7
*/
public JavaType constructGeneralizedType(JavaType baseType, Class<?> superClass)
{
// simple optimization to avoid costly introspection if type-erased type does NOT differ
final Class<?> rawBase = baseType.getRawClass();
if (rawBase == superClass) {
return baseType;
}
JavaType superType = baseType.findSuperType(superClass);
if (superType == null) {
// Most likely, caller did not verify sub/super-type relationship
if (!superClass.isAssignableFrom(rawBase)) {
throw new IllegalArgumentException(String.format(
"Class %s not a super-type of %s", superClass.getName(), baseType));
}
// 01-Nov-2015, tatu: Should never happen, but ch
throw new IllegalArgumentException(String.format(
"Internal error: class %s not included as super-type for %s",
superClass.getName(), baseType));
}
return superType;
}
protected JavaType _findConverterType(TypeFactory tf) {
JavaType thisType = tf.constructType(getClass());
JavaType convType = thisType.findSuperType(Converter.class);
if (convType == null || convType.containedTypeCount() < 2) {
throw new IllegalStateException("Cannot find OUT type parameter for Converter of type "+getClass().getName());
}
return convType;
}
private TypeBindings _bindingsForSubtype(JavaType baseType, int typeParamCount, Class<?> subclass)
{
PlaceholderForType[] placeholders = new PlaceholderForType[typeParamCount];
for (int i = 0; i < typeParamCount; ++i) {
placeholders[i] = new PlaceholderForType(i);
}
TypeBindings b = TypeBindings.create(subclass, placeholders);
// First: pseudo-resolve to get placeholders in place:
JavaType tmpSub = _fromClass(null, subclass, b);
// Then find super-type
JavaType baseWithPlaceholders = tmpSub.findSuperType(baseType.getRawClass());
if (baseWithPlaceholders == null) { // should be found but...
throw new IllegalArgumentException(String.format(
"Internal error: unable to locate supertype (%s) from resolved subtype %s", baseType.getRawClass().getName(),
subclass.getName()));
}
// and traverse type hierarchies to both verify and to resolve placeholders
String error = _resolveTypePlaceholders(baseType, baseWithPlaceholders);
if (error != null) {
throw new IllegalArgumentException("Failed to specialize base type "+baseType.toCanonical()+" as "
+subclass.getName()+", problem: "+error);
}
final JavaType[] typeParams = new JavaType[typeParamCount];
for (int i = 0; i < typeParamCount; ++i) {
JavaType t = placeholders[i].actualType();
// 18-Oct-2017, tatu: Looks like sometimes we have incomplete bindings (even if not
// common, it is possible if subtype is type-erased class with added type
// variable -- see test(s) with "bogus" type(s)).
if (t == null) {
t = unknownType();
}
typeParams[i] = t;
}
return TypeBindings.create(subclass, typeParams);
}
@Override
public @Nullable JsonSerializer<?> findSerializer(
SerializationConfig config, JavaType type, BeanDescription beanDesc) {
if (RangeSet.class.isAssignableFrom(type.getRawClass())) {
return new RangeSetSerializer(type.findSuperType(RangeSet.class));
}
return null;
}
static JavaUtilCollectionsConverter converter(int kind,
JavaType concreteType, Class<?> rawSuper)
{
return new JavaUtilCollectionsConverter(kind, concreteType.findSuperType(rawSuper));
}
/**
* Method that is to figure out actual type parameters that given
* class binds to generic types defined by given (generic)
* interface or class.
* This could mean, for example, trying to figure out
* key and value types for Map implementations.
*
* @param type Sub-type (leaf type) that implements <code>expType</code>
*/
public JavaType[] findTypeParameters(JavaType type, Class<?> expType)
{
JavaType match = type.findSuperType(expType);
if (match == null) {
return NO_TYPES;
}
return match.getBindings().typeParameterArray();
}