Added conversion support for Java 8's ZoneId class and the 'of' method convention
Issue: SPR-1528
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2012 the original author or authors.
|
||||
* Copyright 2002-2013 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.
|
||||
@@ -29,13 +29,13 @@ import org.springframework.util.ClassUtils;
|
||||
import org.springframework.util.ReflectionUtils;
|
||||
|
||||
/**
|
||||
* Generic Converter that attempts to convert a source Object to a target type
|
||||
* Generic converter that attempts to convert a source Object to a target type
|
||||
* by delegating to methods on the target type.
|
||||
*
|
||||
* <p>Calls the static {@code valueOf(sourceType)} method on the target type
|
||||
* to perform the conversion, if such a method exists. Else calls the target type's
|
||||
* Constructor that accepts a single sourceType argument, if such a Constructor exists.
|
||||
* Else throws a ConversionFailedException.
|
||||
* <p>Calls a static {@code valueOf(sourceType)} or Java 8 style {@code of(sourceType)} method
|
||||
* on the target type to perform the conversion, if such a method exists. Otherwise, it calls
|
||||
* the target type's constructor that accepts a single {@code sourceType} argument, if such
|
||||
* a constructor exists. If neither strategy works, it throws a ConversionFailedException.
|
||||
*
|
||||
* @author Keith Donald
|
||||
* @author Juergen Hoeller
|
||||
@@ -92,7 +92,11 @@ final class ObjectToObjectConverter implements ConditionalGenericConverter {
|
||||
}
|
||||
|
||||
private static Method getValueOfMethodOn(Class<?> clazz, Class<?> sourceParameterType) {
|
||||
return ClassUtils.getStaticMethod(clazz, "valueOf", sourceParameterType);
|
||||
Method method = ClassUtils.getStaticMethod(clazz, "valueOf", sourceParameterType);
|
||||
if (method == null) {
|
||||
method = ClassUtils.getStaticMethod(clazz, "of", sourceParameterType);
|
||||
}
|
||||
return method;
|
||||
}
|
||||
|
||||
private static Constructor<?> getConstructor(Class<?> clazz, Class<?> sourceParameterType) {
|
||||
|
||||
@@ -16,19 +16,10 @@
|
||||
|
||||
package org.springframework.core.convert.support;
|
||||
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.junit.Assert.*;
|
||||
import static org.junit.Assert.assertArrayEquals;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertNull;
|
||||
import static org.junit.Assert.assertSame;
|
||||
import static org.junit.Assert.assertThat;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.awt.Color;
|
||||
import java.math.BigDecimal;
|
||||
import java.math.BigInteger;
|
||||
import java.time.ZoneId;
|
||||
import java.util.AbstractList;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
@@ -45,6 +36,7 @@ import java.util.Properties;
|
||||
import java.util.Set;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.core.MethodParameter;
|
||||
import org.springframework.core.convert.ConversionFailedException;
|
||||
import org.springframework.core.convert.ConverterNotFoundException;
|
||||
@@ -52,6 +44,9 @@ import org.springframework.core.convert.TypeDescriptor;
|
||||
import org.springframework.core.convert.converter.Converter;
|
||||
import org.springframework.core.convert.converter.ConverterRegistry;
|
||||
|
||||
import static org.hamcrest.Matchers.*;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
/**
|
||||
* @author Keith Donald
|
||||
* @author Juergen Hoeller
|
||||
@@ -709,6 +704,11 @@ public class DefaultConversionTests {
|
||||
assertEquals("123456789", conversionService.convert(new SSN("123456789"), String.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void convertObjectToStringWithJavaTimeOfMethodPresent() {
|
||||
assertTrue(conversionService.convert(ZoneId.of("GMT+1"), String.class).startsWith("GMT+"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void convertObjectToStringNotSupported() {
|
||||
assertFalse(conversionService.canConvert(TestEntity.class, String.class));
|
||||
@@ -725,73 +725,16 @@ public class DefaultConversionTests {
|
||||
assertEquals("123456789", conversionService.convert(new SSN("123456789"), String.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void convertObjectToObjectWithJavaTimeOfMethod() {
|
||||
assertEquals(ZoneId.of("GMT+1"), conversionService.convert("GMT+1", ZoneId.class));
|
||||
}
|
||||
|
||||
@Test(expected=ConverterNotFoundException.class)
|
||||
public void convertObjectToObjectNoValueOFMethodOrConstructor() {
|
||||
conversionService.convert(new Long(3), SSN.class);
|
||||
}
|
||||
|
||||
public Object assignableTarget;
|
||||
|
||||
private static class SSN {
|
||||
|
||||
private String value;
|
||||
|
||||
public SSN(String value) {
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (!(o instanceof SSN)) {
|
||||
return false;
|
||||
}
|
||||
SSN ssn = (SSN) o;
|
||||
return this.value.equals(ssn.value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return value.hashCode();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return value;
|
||||
}
|
||||
}
|
||||
|
||||
private static class ISBN {
|
||||
|
||||
private String value;
|
||||
|
||||
private ISBN(String value) {
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (!(o instanceof ISBN)) {
|
||||
return false;
|
||||
}
|
||||
ISBN isbn = (ISBN) o;
|
||||
return this.value.equals(isbn.value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return value.hashCode();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return value;
|
||||
}
|
||||
|
||||
public static ISBN valueOf(String value) {
|
||||
return new ISBN(value);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void convertObjectToObjectFinderMethod() {
|
||||
TestEntity e = conversionService.convert(1L, TestEntity.class);
|
||||
@@ -864,4 +807,66 @@ public class DefaultConversionTests {
|
||||
}
|
||||
}
|
||||
|
||||
public Object assignableTarget;
|
||||
|
||||
private static class SSN {
|
||||
|
||||
private String value;
|
||||
|
||||
public SSN(String value) {
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (!(o instanceof SSN)) {
|
||||
return false;
|
||||
}
|
||||
SSN ssn = (SSN) o;
|
||||
return this.value.equals(ssn.value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return value.hashCode();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return value;
|
||||
}
|
||||
}
|
||||
|
||||
private static class ISBN {
|
||||
|
||||
private String value;
|
||||
|
||||
private ISBN(String value) {
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (!(o instanceof ISBN)) {
|
||||
return false;
|
||||
}
|
||||
ISBN isbn = (ISBN) o;
|
||||
return this.value.equals(isbn.value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return value.hashCode();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return value;
|
||||
}
|
||||
|
||||
public static ISBN valueOf(String value) {
|
||||
return new ISBN(value);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user