Added conversion support for Java 8's ZoneId class and the 'of' method convention

Issue: SPR-1528
This commit is contained in:
Juergen Hoeller
2013-08-29 19:15:22 +02:00
parent 0c00b0d902
commit c664010001
5 changed files with 154 additions and 86 deletions

View File

@@ -65,6 +65,7 @@ import org.springframework.beans.propertyeditors.TimeZoneEditor;
import org.springframework.beans.propertyeditors.URIEditor;
import org.springframework.beans.propertyeditors.URLEditor;
import org.springframework.beans.propertyeditors.UUIDEditor;
import org.springframework.beans.propertyeditors.ZoneIdEditor;
import org.springframework.core.convert.ConversionService;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.ResourceArrayPropertyEditor;
@@ -84,6 +85,19 @@ import org.springframework.util.ClassUtils;
*/
public class PropertyEditorRegistrySupport implements PropertyEditorRegistry {
private static Class<?> zoneIdClass;
static {
try {
zoneIdClass = PropertyEditorRegistrySupport.class.getClassLoader().loadClass("java.time.ZoneId");
}
catch (ClassNotFoundException ex) {
// Java 8 ZoneId class not available
zoneIdClass = null;
}
}
private ConversionService conversionService;
private boolean defaultEditorsActive = false;
@@ -202,6 +216,9 @@ public class PropertyEditorRegistrySupport implements PropertyEditorRegistry {
this.defaultEditors.put(URI.class, new URIEditor());
this.defaultEditors.put(URL.class, new URLEditor());
this.defaultEditors.put(UUID.class, new UUIDEditor());
if (zoneIdClass != null) {
this.defaultEditors.put(zoneIdClass, new ZoneIdEditor());
}
// Default instances of collection editors.
// Can be overridden by registering custom instances of those as custom editors.

View File

@@ -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.
@@ -21,11 +21,12 @@ import java.util.TimeZone;
/**
* Editor for {@code java.util.TimeZone}, translating timezone IDs into
* TimeZone objects. Does not expose a text representation for TimeZone objects.
* TimeZone objects. Exposed the TimeZone ID as a text representation.
*
* @author Juergen Hoeller
* @since 3.0
* @see java.util.TimeZone
* @see ZoneIdEditor
*/
public class TimeZoneEditor extends PropertyEditorSupport {
@@ -34,13 +35,10 @@ public class TimeZoneEditor extends PropertyEditorSupport {
setValue(TimeZone.getTimeZone(text));
}
/**
* This implementation returns {@code null} to indicate that
* there is no appropriate text representation.
*/
@Override
public String getAsText() {
return null;
TimeZone value = (TimeZone) getValue();
return (value != null ? value.getID() : "");
}
}

View File

@@ -0,0 +1,44 @@
/*
* 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.beans.propertyeditors;
import java.beans.PropertyEditorSupport;
import java.time.ZoneId;
/**
* Editor for {@code java.time.ZoneId}, translating zone ID Strings into
* ZoneId objects. Exposed the TimeZone ID as a text representation.
*
* @author Juergen Hoeller
* @since 4.0
* @see java.time.ZoneId
* @see TimeZoneEditor
*/
public class ZoneIdEditor extends PropertyEditorSupport {
@Override
public void setAsText(String text) throws IllegalArgumentException {
setValue(ZoneId.of(text));
}
@Override
public String getAsText() {
ZoneId value = (ZoneId) getValue();
return (value != null ? value.getId() : "");
}
}