assert type != null : "property type is null";
String text = reader.getElementText();
// degenerate case where we are returning a String
if (String.class.equals(type)) {
return new SingletonObjectFactory(text);
}
// special handler to convert hexBinary to a byte[]
if (byte[].class.equals(type)) {
byte[] instance = new byte[text.length() >> 1];
for (int i = 0; i < instance.length; i++) {
instance[i] = (byte) (Character.digit(text.charAt(i << 1), 16) << 4 | Character.digit(text.charAt((i << 1) + 1), 16));
}
return new SingletonObjectFactory(instance);
}
// does this type have a static valueOf(String) method?
try {
Method valueOf = type.getMethod("valueOf", String.class);
if (Modifier.isStatic(valueOf.getModifiers())) {
try {
return new SingletonObjectFactory(valueOf.invoke(null, text));
} catch (IllegalAccessException e) {
throw new AssertionError("getMethod returned an inaccessible method");
} catch (InvocationTargetException e) {
// FIXME we should throw something better
throw new ConfigurationLoadException(e.getCause());
}
}
} catch (NoSuchMethodException e) {
// try something else
}
// does this type have a constructor that takes a String?
try {
Constructor<?> ctr = type.getConstructor(String.class);
return new SingletonObjectFactory(ctr.newInstance(text));
} catch (NoSuchMethodException e) {
// try something else
} catch (IllegalAccessException e) {
throw new AssertionError("getConstructor returned an inaccessible method");
} catch (InstantiationException e) {
throw new ConfigurationLoadException("Property type cannot be instantiated: " + type.getName());
} catch (InvocationTargetException e) {
// FIXME we should throw something better
throw new ConfigurationLoadException(e.getCause());
}
// do we have a property editor for it?
PropertyEditor editor = PropertyEditorManager.findEditor(type);
if (editor != null) {
try {
editor.setAsText(text);
return new SingletonObjectFactory(editor.getValue());
} catch (IllegalArgumentException e) {
// FIXME we should throw something better
throw new ConfigurationLoadException(e);
}