Browse Source

ObjectToObjectConverter properly handles constructors on non-public classes

Issue: SPR-14304
(cherry picked from commit 9659bc5)
pull/1257/head
Juergen Hoeller 9 years ago
parent
commit
edf1df33cc
  1. 1
      spring-core/src/main/java/org/springframework/core/convert/support/ObjectToObjectConverter.java
  2. 21
      spring-core/src/test/java/org/springframework/core/convert/converter/DefaultConversionServiceTests.java
  3. 8
      spring-core/src/test/java/org/springframework/core/convert/support/GenericConversionServiceTests.java

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

@ -102,6 +102,7 @@ final class ObjectToObjectConverter implements ConditionalGenericConverter { @@ -102,6 +102,7 @@ final class ObjectToObjectConverter implements ConditionalGenericConverter {
}
else if (member instanceof Constructor) {
Constructor<?> ctor = (Constructor<?>) member;
ReflectionUtils.makeAccessible(ctor);
return ctor.newInstance(source);
}
}

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

@ -14,7 +14,7 @@ @@ -14,7 +14,7 @@
* limitations under the License.
*/
package org.springframework.core.convert.support;
package org.springframework.core.convert.converter;
import java.awt.Color;
import java.lang.reflect.Method;
@ -49,8 +49,7 @@ import org.springframework.core.MethodParameter; @@ -49,8 +49,7 @@ import org.springframework.core.MethodParameter;
import org.springframework.core.convert.ConversionFailedException;
import org.springframework.core.convert.ConverterNotFoundException;
import org.springframework.core.convert.TypeDescriptor;
import org.springframework.core.convert.converter.Converter;
import org.springframework.core.convert.converter.ConverterRegistry;
import org.springframework.core.convert.support.DefaultConversionService;
import org.springframework.tests.Assume;
import org.springframework.tests.TestGroup;
import org.springframework.util.ClassUtils;
@ -60,16 +59,16 @@ import static org.hamcrest.Matchers.*; @@ -60,16 +59,16 @@ import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;
/**
* Unit tests for the {@link DefaultConversionService}.
* Unit tests for {@link DefaultConversionService}.
*
* <p>For tests involving the {@link GenericConversionService}, see
* {@link GenericConversionServiceTests}.
* <p>In this package for enforcing accessibility checks to non-public classes outside
* of the {@code org.springframework.core.convert.support} implementation package.
* Only in such a scenario, {@code setAccessible(true)} is actually necessary.
*
* @author Keith Donald
* @author Juergen Hoeller
* @author Stephane Nicoll
* @author Sam Brannen
* @see GenericConversionServiceTests
*/
public class DefaultConversionServiceTests {
@ -243,22 +242,22 @@ public class DefaultConversionServiceTests { @@ -243,22 +242,22 @@ public class DefaultConversionServiceTests {
public void testEnumToString() {
assertEquals("BAR", conversionService.convert(Foo.BAR, String.class));
}
@Test
public void testIntegerToEnum() throws Exception {
assertEquals(Foo.BAR, conversionService.convert(0, Foo.class));
}
@Test
public void testIntegerToEnumWithSubclass() throws Exception {
assertEquals(SubFoo.BAZ, conversionService.convert(1, SubFoo.BAR.getClass()));
}
@Test
public void testIntegerToEnumNull() {
assertEquals(null, conversionService.convert(null, Foo.class));
}
@Test
public void testEnumToInteger() {
assertEquals(Integer.valueOf(0), conversionService.convert(Foo.BAR, Integer.class));

8
spring-core/src/test/java/org/springframework/core/convert/support/GenericConversionServiceTests.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2015 the original author or authors.
* Copyright 2002-2016 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.
@ -54,17 +54,15 @@ import static org.hamcrest.Matchers.*; @@ -54,17 +54,15 @@ import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;
/**
* Unit tests for the {@link GenericConversionService}.
* Unit tests for {@link GenericConversionService}.
*
* <p>For tests involving the {@link DefaultConversionService}, see
* {@link DefaultConversionServiceTests}.
* <p>In this package for access to package-local converter implementations.
*
* @author Keith Donald
* @author Juergen Hoeller
* @author Phillip Webb
* @author David Haraburda
* @author Sam Brannen
* @see DefaultConversionServiceTests
*/
public class GenericConversionServiceTests {

Loading…
Cancel
Save