Backwards-compatible handling of generic and raw collection converters

Issue: SPR-11369
This commit is contained in:
Juergen Hoeller
2014-02-01 19:24:40 +01:00
parent e6f4796779
commit 874a2a9ca2
3 changed files with 88 additions and 25 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2012 the original author or authors.
* Copyright 2002-2014 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -19,9 +19,9 @@ package org.springframework.core.convert.converter;
import org.springframework.core.convert.TypeDescriptor;
/**
* A {@link GenericConverter} that may conditionally execute based on attributes of the
* {@code source} and {@code target} {@link TypeDescriptor}. See
* {@link ConditionalConverter} for details.
* A {@link GenericConverter} that may conditionally execute based on attributes
* of the {@code source} and {@code target} {@link TypeDescriptor}.
* See {@link ConditionalConverter} for details.
*
* @author Keith Donald
* @author Phillip Webb
@@ -29,7 +29,6 @@ import org.springframework.core.convert.TypeDescriptor;
* @see GenericConverter
* @see ConditionalConverter
*/
public interface ConditionalGenericConverter extends GenericConverter,
ConditionalConverter {
public interface ConditionalGenericConverter extends GenericConverter, ConditionalConverter {
}

View File

@@ -331,18 +331,18 @@ public class GenericConversionService implements ConfigurableConversionService {
@Override
public boolean matches(TypeDescriptor sourceType, TypeDescriptor targetType) {
// Check raw type first...
if (!this.typeInfo.getTargetType().equals(targetType.getObjectType())) {
return false;
}
// Full check for complex generic type match required?
ResolvableType rt = targetType.getResolvableType();
if (!rt.isAssignableFrom(this.targetType)) {
// Generic type structure not fully assignable -> try lenient fallback if
// unresolvable generics remain, just requiring the raw type to match then
if (!rt.hasUnresolvableGenerics() || !this.typeInfo.getTargetType().equals(targetType.getObjectType())) {
return false;
}
if (!(rt.getType() instanceof Class) && !rt.isAssignableFrom(this.targetType) &&
!this.targetType.hasUnresolvableGenerics()) {
return false;
}
if (this.converter instanceof ConditionalConverter) {
return ((ConditionalConverter) this.converter).matches(sourceType, targetType);
}
return true;
return !(this.converter instanceof ConditionalConverter) ||
((ConditionalConverter) this.converter).matches(sourceType, targetType);
}
@Override