Skip to content

Commit edf1df3

Browse files
committed
ObjectToObjectConverter properly handles constructors on non-public classes
Issue: SPR-14304 (cherry picked from commit 9659bc5)
1 parent bf0df54 commit edf1df3

File tree

3 files changed

+14
-16
lines changed

3 files changed

+14
-16
lines changed

Diff for: spring-core/src/main/java/org/springframework/core/convert/support/ObjectToObjectConverter.java

+1
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor t
102102
}
103103
else if (member instanceof Constructor) {
104104
Constructor<?> ctor = (Constructor<?>) member;
105+
ReflectionUtils.makeAccessible(ctor);
105106
return ctor.newInstance(source);
106107
}
107108
}

Diff for: spring-core/src/test/java/org/springframework/core/convert/support/DefaultConversionServiceTests.java renamed to spring-core/src/test/java/org/springframework/core/convert/converter/DefaultConversionServiceTests.java

+10-11
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17-
package org.springframework.core.convert.support;
17+
package org.springframework.core.convert.converter;
1818

1919
import java.awt.Color;
2020
import java.lang.reflect.Method;
@@ -49,8 +49,7 @@
4949
import org.springframework.core.convert.ConversionFailedException;
5050
import org.springframework.core.convert.ConverterNotFoundException;
5151
import org.springframework.core.convert.TypeDescriptor;
52-
import org.springframework.core.convert.converter.Converter;
53-
import org.springframework.core.convert.converter.ConverterRegistry;
52+
import org.springframework.core.convert.support.DefaultConversionService;
5453
import org.springframework.tests.Assume;
5554
import org.springframework.tests.TestGroup;
5655
import org.springframework.util.ClassUtils;
@@ -60,16 +59,16 @@
6059
import static org.junit.Assert.*;
6160

6261
/**
63-
* Unit tests for the {@link DefaultConversionService}.
62+
* Unit tests for {@link DefaultConversionService}.
6463
*
65-
* <p>For tests involving the {@link GenericConversionService}, see
66-
* {@link GenericConversionServiceTests}.
64+
* <p>In this package for enforcing accessibility checks to non-public classes outside
65+
* of the {@code org.springframework.core.convert.support} implementation package.
66+
* Only in such a scenario, {@code setAccessible(true)} is actually necessary.
6767
*
6868
* @author Keith Donald
6969
* @author Juergen Hoeller
7070
* @author Stephane Nicoll
7171
* @author Sam Brannen
72-
* @see GenericConversionServiceTests
7372
*/
7473
public class DefaultConversionServiceTests {
7574

@@ -243,22 +242,22 @@ public void testStringToEnumEmptyString() {
243242
public void testEnumToString() {
244243
assertEquals("BAR", conversionService.convert(Foo.BAR, String.class));
245244
}
246-
245+
247246
@Test
248247
public void testIntegerToEnum() throws Exception {
249248
assertEquals(Foo.BAR, conversionService.convert(0, Foo.class));
250249
}
251-
250+
252251
@Test
253252
public void testIntegerToEnumWithSubclass() throws Exception {
254253
assertEquals(SubFoo.BAZ, conversionService.convert(1, SubFoo.BAR.getClass()));
255254
}
256-
255+
257256
@Test
258257
public void testIntegerToEnumNull() {
259258
assertEquals(null, conversionService.convert(null, Foo.class));
260259
}
261-
260+
262261
@Test
263262
public void testEnumToInteger() {
264263
assertEquals(Integer.valueOf(0), conversionService.convert(Foo.BAR, Integer.class));

Diff for: spring-core/src/test/java/org/springframework/core/convert/support/GenericConversionServiceTests.java

+3-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2015 the original author or authors.
2+
* Copyright 2002-2016 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.
@@ -54,17 +54,15 @@
5454
import static org.junit.Assert.*;
5555

5656
/**
57-
* Unit tests for the {@link GenericConversionService}.
57+
* Unit tests for {@link GenericConversionService}.
5858
*
59-
* <p>For tests involving the {@link DefaultConversionService}, see
60-
* {@link DefaultConversionServiceTests}.
59+
* <p>In this package for access to package-local converter implementations.
6160
*
6261
* @author Keith Donald
6362
* @author Juergen Hoeller
6463
* @author Phillip Webb
6564
* @author David Haraburda
6665
* @author Sam Brannen
67-
* @see DefaultConversionServiceTests
6866
*/
6967
public class GenericConversionServiceTests {
7068

0 commit comments

Comments
 (0)