use of java.beans.PropertyEditor in project Smartcity-Smarthouse by TechnionYP5777.
the class StringConverter method convert.
/**
* Convert a string into any class
*
* @param targetType
* @param text
* @return the new converted Object
*/
public static Object convert(final Class<?> targetType, final String text) {
if (targetType.isPrimitive()) {
if (text == null)
return targetType.equals(Character.TYPE) ? '\0' : targetType.equals(Boolean.TYPE) ? false : 0;
if (targetType.equals(Character.TYPE))
return text.charAt(0);
}
final PropertyEditor editor = PropertyEditorManager.findEditor(targetType);
if (editor == null) {
log.warn("Converting an uknown Object to null: [targetType: " + targetType.getCanonicalName() + "]");
return null;
}
editor.setAsText(text);
return editor.getValue();
}
use of java.beans.PropertyEditor in project wildfly by wildfly.
the class Configurator method convertValue.
/**
* Convert a value
*
* @param clazz the class
* @param value the value
* @param replaceProperties whether to replace system properties
* @param trim whether to trim string value
* @return the value or null if there is no editor
* @throws Throwable for any error
*/
@SuppressWarnings("unchecked")
public static Object convertValue(Class<?> clazz, Object value, boolean replaceProperties, boolean trim) throws Throwable {
if (clazz == null)
return value;
if (value == null)
return null;
Class<?> valueClass = value.getClass();
// If we have a string, trim and replace any system properties when requested
if (valueClass == String.class) {
String string = (String) value;
if (trim)
string = string.trim();
if (replaceProperties)
value = PropertiesValueResolver.replaceProperties(string);
}
if (clazz.isAssignableFrom(valueClass))
return value;
// First see if this is an Enum
if (clazz.isEnum()) {
Class<? extends Enum> eclazz = clazz.asSubclass(Enum.class);
return Enum.valueOf(eclazz, value.toString());
}
// Next look for a property editor
if (valueClass == String.class) {
PropertyEditor editor = PropertyEditorManager.findEditor(clazz);
if (editor != null) {
editor.setAsText((String) value);
return editor.getValue();
}
}
// Try a static clazz.valueOf(value)
try {
Method method = clazz.getMethod("valueOf", valueClass);
int modifiers = method.getModifiers();
if (Modifier.isPublic(modifiers) && Modifier.isStatic(modifiers) && clazz.isAssignableFrom(method.getReturnType()))
return method.invoke(null, value);
} catch (Exception ignored) {
}
if (valueClass == String.class) {
try {
Constructor constructor = clazz.getConstructor(valueClass);
if (Modifier.isPublic(constructor.getModifiers()))
return constructor.newInstance(value);
} catch (Exception ignored) {
}
}
return value;
}
use of java.beans.PropertyEditor in project spring-framework by spring-projects.
the class URLEditorTests method testSetAsTextWithNull.
@Test
public void testSetAsTextWithNull() throws Exception {
PropertyEditor urlEditor = new URLEditor();
urlEditor.setAsText(null);
assertNull(urlEditor.getValue());
assertEquals("", urlEditor.getAsText());
}
use of java.beans.PropertyEditor in project spring-framework by spring-projects.
the class CustomEditorTests method testFileEditorWithAbsolutePath.
@Test
public void testFileEditorWithAbsolutePath() {
PropertyEditor fileEditor = new FileEditor();
// testing on Windows
if (new File("C:/myfile.txt").isAbsolute()) {
fileEditor.setAsText("C:/myfile.txt");
assertEquals(new File("C:/myfile.txt"), fileEditor.getValue());
}
// testing on Unix
if (new File("/myfile.txt").isAbsolute()) {
fileEditor.setAsText("/myfile.txt");
assertEquals(new File("/myfile.txt"), fileEditor.getValue());
}
}
use of java.beans.PropertyEditor in project spring-framework by spring-projects.
the class CustomEditorTests method testLocaleEditor.
@Test
public void testLocaleEditor() {
PropertyEditor localeEditor = new LocaleEditor();
localeEditor.setAsText("en_CA");
assertEquals(Locale.CANADA, localeEditor.getValue());
assertEquals("en_CA", localeEditor.getAsText());
localeEditor = new LocaleEditor();
assertEquals("", localeEditor.getAsText());
}
Aggregations