formatted annotation to apply to formatted value object classes
This commit is contained in:
@@ -21,6 +21,7 @@ import org.springframework.ui.format.Formatter;
|
||||
|
||||
/**
|
||||
* A centralized registry of Formatters indexed by property types.
|
||||
* TODO - consider moving to ui.format
|
||||
* @author Keith Donald
|
||||
* @since 3.0
|
||||
*/
|
||||
|
||||
@@ -21,10 +21,13 @@ import java.lang.reflect.Type;
|
||||
import java.lang.reflect.TypeVariable;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
import org.springframework.core.GenericTypeResolver;
|
||||
import org.springframework.core.annotation.AnnotationUtils;
|
||||
import org.springframework.core.convert.TypeDescriptor;
|
||||
import org.springframework.ui.format.AnnotationFormatterFactory;
|
||||
import org.springframework.ui.format.Formatted;
|
||||
import org.springframework.ui.format.Formatter;
|
||||
|
||||
/**
|
||||
@@ -37,7 +40,7 @@ import org.springframework.ui.format.Formatter;
|
||||
@SuppressWarnings("unchecked")
|
||||
public class GenericFormatterRegistry implements FormatterRegistry {
|
||||
|
||||
private Map<Class, Formatter> typeFormatters = new HashMap<Class, Formatter>();
|
||||
private Map<Class, Formatter> typeFormatters = new ConcurrentHashMap<Class, Formatter>();
|
||||
|
||||
private Map<Class, AnnotationFormatterFactory> annotationFormatters = new HashMap<Class, AnnotationFormatterFactory>();
|
||||
|
||||
@@ -53,8 +56,22 @@ public class GenericFormatterRegistry implements FormatterRegistry {
|
||||
if (formatter != null) {
|
||||
return formatter;
|
||||
} else {
|
||||
// TODO check class-level @Formatted annotation
|
||||
return null;
|
||||
Formatted formatted = AnnotationUtils.findAnnotation(propertyType.getType(), Formatted.class);
|
||||
if (formatted != null) {
|
||||
Class formatterClass = formatted.value();
|
||||
try {
|
||||
formatter = (Formatter) formatterClass.newInstance();
|
||||
} catch (InstantiationException e) {
|
||||
// TODO better runtime exception
|
||||
throw new IllegalStateException(e);
|
||||
} catch (IllegalAccessException e) {
|
||||
throw new IllegalStateException(e);
|
||||
}
|
||||
typeFormatters.put(propertyType.getType(), formatter);
|
||||
return formatter;
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,36 @@
|
||||
/*
|
||||
* Copyright 2004-2009 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.ui.format;
|
||||
|
||||
import java.lang.annotation.Documented;
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
/**
|
||||
* A type that can be formatted as a String for display in a UI.
|
||||
*/
|
||||
@Target({ElementType.TYPE})
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Documented
|
||||
public @interface Formatted {
|
||||
|
||||
/**
|
||||
* The Formatter that handles the formatting.
|
||||
*/
|
||||
Class<?> value();
|
||||
}
|
||||
Reference in New Issue
Block a user