Skip to content

Commit 5e0cd9f

Browse files
committed
Merge branch '5.1.x'
2 parents 9967ece + ac0e5d8 commit 5e0cd9f

File tree

2 files changed

+61
-52
lines changed

2 files changed

+61
-52
lines changed

spring-core/src/main/java/org/springframework/core/convert/support/ConversionUtils.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -22,6 +22,7 @@
2222
import org.springframework.core.convert.converter.GenericConverter;
2323
import org.springframework.lang.Nullable;
2424
import org.springframework.util.Assert;
25+
import org.springframework.util.ClassUtils;
2526

2627
/**
2728
* Internal utilities for the conversion package.
@@ -62,7 +63,7 @@ public static boolean canConvertElements(@Nullable TypeDescriptor sourceElementT
6263
// yes
6364
return true;
6465
}
65-
if (sourceElementType.getType().isAssignableFrom(targetElementType.getType())) {
66+
if (ClassUtils.isAssignable(sourceElementType.getType(), targetElementType.getType())) {
6667
// maybe
6768
return true;
6869
}

spring-core/src/test/java/org/springframework/core/convert/converter/DefaultConversionServiceTests.java

+58-50
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -134,7 +134,7 @@ public void testBooleanToString() {
134134
}
135135

136136
@Test
137-
public void testStringToByte() throws Exception {
137+
public void testStringToByte() {
138138
assertEquals(Byte.valueOf("1"), conversionService.convert("1", Byte.class));
139139
}
140140

@@ -225,12 +225,12 @@ public void testStringToNumberEmptyString() {
225225
}
226226

227227
@Test
228-
public void testStringToEnum() throws Exception {
228+
public void testStringToEnum() {
229229
assertEquals(Foo.BAR, conversionService.convert("BAR", Foo.class));
230230
}
231231

232232
@Test
233-
public void testStringToEnumWithSubclass() throws Exception {
233+
public void testStringToEnumWithSubclass() {
234234
assertEquals(SubFoo.BAZ, conversionService.convert("BAZ", SubFoo.BAR.getClass()));
235235
}
236236

@@ -245,12 +245,12 @@ public void testEnumToString() {
245245
}
246246

247247
@Test
248-
public void testIntegerToEnum() throws Exception {
248+
public void testIntegerToEnum() {
249249
assertEquals(Foo.BAR, conversionService.convert(0, Foo.class));
250250
}
251251

252252
@Test
253-
public void testIntegerToEnumWithSubclass() throws Exception {
253+
public void testIntegerToEnumWithSubclass() {
254254
assertEquals(SubFoo.BAZ, conversionService.convert(1, SubFoo.BAR.getClass()));
255255
}
256256

@@ -395,10 +395,6 @@ public void convertArrayToAbstractCollection() {
395395
conversionService.convert(new String[]{"1", "2", "3"}, AbstractList.class);
396396
}
397397

398-
public static enum FooEnum {
399-
BAR, BAZ
400-
}
401-
402398
@Test
403399
public void convertArrayToString() {
404400
String result = conversionService.convert(new String[] {"1", "2", "3"}, String.class);
@@ -525,19 +521,17 @@ public void convertCollectionToStringWithElementConversion() throws Exception {
525521
}
526522

527523
@Test
528-
@SuppressWarnings("rawtypes")
529524
public void convertStringToCollection() {
530-
List result = conversionService.convert("1,2,3", List.class);
525+
List<?> result = conversionService.convert("1,2,3", List.class);
531526
assertEquals(3, result.size());
532527
assertEquals("1", result.get(0));
533528
assertEquals("2", result.get(1));
534529
assertEquals("3", result.get(2));
535530
}
536531

537532
@Test
538-
@SuppressWarnings("rawtypes")
539533
public void convertStringToCollectionWithElementConversion() throws Exception {
540-
List result = (List) conversionService.convert("1,2,3", TypeDescriptor.valueOf(String.class),
534+
List<?> result = (List) conversionService.convert("1,2,3", TypeDescriptor.valueOf(String.class),
541535
new TypeDescriptor(getClass().getField("genericList")));
542536
assertEquals(3, result.size());
543537
assertEquals(1, result.get(0));
@@ -546,9 +540,8 @@ public void convertStringToCollectionWithElementConversion() throws Exception {
546540
}
547541

548542
@Test
549-
@SuppressWarnings("rawtypes")
550543
public void convertEmptyStringToCollection() {
551-
Collection result = conversionService.convert("", Collection.class);
544+
Collection<?> result = conversionService.convert("", Collection.class);
552545
assertEquals(0, result.size());
553546
}
554547

@@ -575,25 +568,18 @@ public void convertCollectionToObjectAssignableTarget() throws Exception {
575568
}
576569

577570
@Test
578-
@SuppressWarnings("rawtypes")
579-
public void convertCollectionToObjectWithCustomConverter() throws Exception {
571+
public void convertCollectionToObjectWithCustomConverter() {
580572
List<String> source = new ArrayList<>();
581573
source.add("A");
582574
source.add("B");
583-
conversionService.addConverter(new Converter<List, ListWrapper>() {
584-
@Override
585-
public ListWrapper convert(List source) {
586-
return new ListWrapper(source);
587-
}
588-
});
575+
conversionService.addConverter(List.class, ListWrapper.class, ListWrapper::new);
589576
ListWrapper result = conversionService.convert(source, ListWrapper.class);
590577
assertSame(source, result.getList());
591578
}
592579

593580
@Test
594-
@SuppressWarnings("rawtypes")
595581
public void convertObjectToCollection() {
596-
List result = conversionService.convert(3L, List.class);
582+
List<?> result = conversionService.convert(3L, List.class);
597583
assertEquals(1, result.size());
598584
assertEquals(3L, result.get(0));
599585
}
@@ -608,23 +594,55 @@ public void convertObjectToCollectionWithElementConversion() throws Exception {
608594
}
609595

610596
@Test
611-
public void convertArrayToArray() {
597+
public void convertStringArrayToIntegerArray() {
612598
Integer[] result = conversionService.convert(new String[] {"1", "2", "3"}, Integer[].class);
613599
assertEquals(Integer.valueOf(1), result[0]);
614600
assertEquals(Integer.valueOf(2), result[1]);
615601
assertEquals(Integer.valueOf(3), result[2]);
616602
}
617603

618604
@Test
619-
public void convertArrayToPrimitiveArray() {
605+
public void convertStringArrayToIntArray() {
620606
int[] result = conversionService.convert(new String[] {"1", "2", "3"}, int[].class);
621607
assertEquals(1, result[0]);
622608
assertEquals(2, result[1]);
623609
assertEquals(3, result[2]);
624610
}
625611

626612
@Test
627-
public void convertArrayToWrapperArray() {
613+
public void convertIntegerArrayToIntegerArray() {
614+
Integer[] result = conversionService.convert(new Integer[] {1, 2, 3}, Integer[].class);
615+
assertEquals(Integer.valueOf(1), result[0]);
616+
assertEquals(Integer.valueOf(2), result[1]);
617+
assertEquals(Integer.valueOf(3), result[2]);
618+
}
619+
620+
@Test
621+
public void convertIntegerArrayToIntArray() {
622+
int[] result = conversionService.convert(new Integer[] {1, 2, 3}, int[].class);
623+
assertEquals(1, result[0]);
624+
assertEquals(2, result[1]);
625+
assertEquals(3, result[2]);
626+
}
627+
628+
@Test
629+
public void convertObjectArrayToIntegerArray() {
630+
Integer[] result = conversionService.convert(new Object[] {1, 2, 3}, Integer[].class);
631+
assertEquals(Integer.valueOf(1), result[0]);
632+
assertEquals(Integer.valueOf(2), result[1]);
633+
assertEquals(Integer.valueOf(3), result[2]);
634+
}
635+
636+
@Test
637+
public void convertObjectArrayToIntArray() {
638+
int[] result = conversionService.convert(new Object[] {1, 2, 3}, int[].class);
639+
assertEquals(1, result[0]);
640+
assertEquals(2, result[1]);
641+
assertEquals(3, result[2]);
642+
}
643+
644+
@Test
645+
public void convertByteArrayToWrapperArray() {
628646
byte[] byteArray = new byte[] {1, 2, 3};
629647
Byte[] converted = conversionService.convert(byteArray, Byte[].class);
630648
assertThat(converted, equalTo(new Byte[]{1, 2, 3}));
@@ -694,7 +712,7 @@ public void convertCollectionToCollectionNull() throws Exception {
694712

695713
@Test
696714
@SuppressWarnings("rawtypes")
697-
public void convertCollectionToCollectionNotGeneric() throws Exception {
715+
public void convertCollectionToCollectionNotGeneric() {
698716
Set<String> foo = new LinkedHashSet<>();
699717
foo.add("1");
700718
foo.add("2");
@@ -740,10 +758,10 @@ public void convertMapToMap() throws Exception {
740758
foo.put("1", "BAR");
741759
foo.put("2", "BAZ");
742760
@SuppressWarnings("unchecked")
743-
Map<Integer, FooEnum> map = (Map<Integer, FooEnum>) conversionService.convert(foo,
761+
Map<Integer, Foo> map = (Map<Integer, Foo>) conversionService.convert(foo,
744762
TypeDescriptor.forObject(foo), new TypeDescriptor(getClass().getField("genericMap")));
745-
assertEquals(FooEnum.BAR, map.get(1));
746-
assertEquals(FooEnum.BAZ, map.get(2));
763+
assertEquals(Foo.BAR, map.get(1));
764+
assertEquals(Foo.BAZ, map.get(2));
747765
}
748766

749767
@Test
@@ -881,25 +899,20 @@ public void convertObjectToObjectFinderMethodWithIdConversion() {
881899
}
882900

883901
@Test
884-
public void convertCharArrayToString() throws Exception {
902+
public void convertCharArrayToString() {
885903
String converted = conversionService.convert(new char[] {'a', 'b', 'c'}, String.class);
886904
assertThat(converted, equalTo("a,b,c"));
887905
}
888906

889907
@Test
890-
public void convertStringToCharArray() throws Exception {
908+
public void convertStringToCharArray() {
891909
char[] converted = conversionService.convert("a,b,c", char[].class);
892910
assertThat(converted, equalTo(new char[]{'a', 'b', 'c'}));
893911
}
894912

895913
@Test
896-
public void convertStringToCustomCharArray() throws Exception {
897-
conversionService.addConverter(new Converter<String, char[]>() {
898-
@Override
899-
public char[] convert(String source) {
900-
return source.toCharArray();
901-
}
902-
});
914+
public void convertStringToCustomCharArray() {
915+
conversionService.addConverter(String.class, char[].class, String::toCharArray);
903916
char[] converted = conversionService.convert("abc", char[].class);
904917
assertThat(converted, equalTo(new char[] {'a', 'b', 'c'}));
905918
}
@@ -916,16 +929,11 @@ public void multidimensionalArrayToListConversionShouldConvertEntriesCorrectly()
916929

917930
@Test
918931
public void convertCannotOptimizeArray() {
919-
conversionService.addConverter(new Converter<Byte, Byte>() {
920-
@Override
921-
public Byte convert(Byte source) {
922-
return (byte) (source + 1);
923-
}
924-
});
932+
conversionService.addConverter(Byte.class, Byte.class, source -> (byte) (source + 1));
925933
byte[] byteArray = new byte[] {1, 2, 3};
926934
byte[] converted = conversionService.convert(byteArray, byte[].class);
927935
assertNotSame(byteArray, converted);
928-
assertTrue(Arrays.equals(new byte[] {2, 3, 4}, converted));
936+
assertArrayEquals(new byte[]{2, 3, 4}, converted);
929937
}
930938

931939
@Test
@@ -977,7 +985,7 @@ public void testPerformance1() {
977985

978986
public Stream<Integer> genericStream;
979987

980-
public Map<Integer, FooEnum> genericMap = new HashMap<>();
988+
public Map<Integer, Foo> genericMap = new HashMap<>();
981989

982990
public EnumSet<Foo> enumSet;
983991

0 commit comments

Comments
 (0)