use of java.beans.PropertyEditor in project spring-framework by spring-projects.
the class ResourceArrayPropertyEditorTests method testSystemPropertyReplacement.
@Test
public void testSystemPropertyReplacement() {
PropertyEditor editor = new ResourceArrayPropertyEditor();
System.setProperty("test.prop", "foo");
try {
editor.setAsText("${test.prop}-${bar}");
Resource[] resources = (Resource[]) editor.getValue();
assertEquals("foo-${bar}", resources[0].getFilename());
} finally {
System.getProperties().remove("test.prop");
}
}
use of java.beans.PropertyEditor in project jdk8u_jdk by JetBrains.
the class PropertyEditorFinder method find.
@Override
public PropertyEditor find(Class<?> type) {
Class<?> predefined;
synchronized (this.registry) {
predefined = this.registry.get(type);
}
PropertyEditor editor = instantiate(predefined, null);
if (editor == null) {
editor = super.find(type);
if ((editor == null) && (null != type.getEnumConstants())) {
editor = new EnumEditor(type);
}
}
return editor;
}
use of java.beans.PropertyEditor in project tomee by apache.
the class Converter method convert.
public static Object convert(final Object value, Class<?> targetType, final String name) {
if (value == null) {
if (targetType.equals(Boolean.TYPE)) {
return false;
}
return value;
}
final Class<? extends Object> actualType = value.getClass();
if (targetType.isPrimitive()) {
targetType = PrimitiveTypes.valueOf(targetType.toString().toUpperCase()).getWraper();
}
if (targetType.isAssignableFrom(actualType)) {
return value;
}
if (Number.class.isAssignableFrom(actualType) && Number.class.isAssignableFrom(targetType)) {
return value;
}
if (!(value instanceof String)) {
final String message = String.format("Expected type '%s' for '%s'. Found '%s'", targetType.getName(), name, actualType.getName());
throw new IllegalArgumentException(message);
}
final String stringValue = (String) value;
if (Enum.class.isAssignableFrom(targetType)) {
final Class<? extends Enum> enumType = (Class<? extends Enum>) targetType;
try {
return Enum.valueOf(enumType, stringValue);
} catch (final IllegalArgumentException e) {
try {
return Enum.valueOf(enumType, stringValue.toUpperCase());
} catch (final IllegalArgumentException e1) {
return Enum.valueOf(enumType, stringValue.toLowerCase());
}
}
}
try {
// Force static initializers to run
Class.forName(targetType.getName(), true, targetType.getClassLoader());
} catch (final ClassNotFoundException e) {
e.printStackTrace();
}
final PropertyEditor editor = Editors.get(targetType);
if (editor == null) {
final Object result = create(targetType, stringValue);
if (result != null) {
return result;
}
}
if (editor == null) {
final String message = String.format("Cannot convert to '%s' for '%s'. No PropertyEditor", targetType.getName(), name);
throw new IllegalArgumentException(message);
}
editor.setAsText(stringValue);
return editor.getValue();
}
use of java.beans.PropertyEditor in project tomee by apache.
the class Editors method get.
public static PropertyEditor get(final Class<?> type) {
final PropertyEditor editor = PropertyEditorManager.findEditor(type);
if (editor != null) {
return editor;
}
final Class<Editors> c = Editors.class;
try {
final Class<?> editorClass = c.getClassLoader().loadClass(c.getName().replace("Editors", type.getSimpleName() + "Editor"));
PropertyEditorManager.registerEditor(type, editorClass);
return PropertyEditorManager.findEditor(type);
} catch (final ClassNotFoundException e) {
return null;
}
}
use of java.beans.PropertyEditor in project tomee by apache.
the class ClientInjectionProcessor method convert.
private Object convert(final Class type, Object value) {
if (type == Object.class || !(value instanceof String)) {
return value;
}
final String stringValue = (String) value;
final PropertyEditor editor = findEditor(type);
if (editor != null) {
editor.setAsText(stringValue);
value = editor.getValue();
}
return value;
}
Aggregations