c = CONVERT_UTILS.get(type);
if (c != null) {
return c;
}
- // fall back to compatibility behavior
- c = ConvertUtils.lookup(type);
- if (c != null) {
- return c;
+
+ // EnumSet conversion is handled by JSON binding, don't convert from string
+ if (EnumSet.class.isAssignableFrom(type)) {
+ return null;
+ }
+
+ // check if Spring's conversion service can handle it
+ if (CONVERSION_SERVICE.canConvert(String.class, type)) {
+ return source -> CONVERSION_SERVICE.convert(source, type);
}
// look for the associated converter
@@ -1175,7 +1182,7 @@ public static Converter lookupConverter(Class type) {
}
Class> cl = type.getClassLoader().loadClass(type.getName() + "$StaplerConverterImpl");
c = (Converter) cl.getDeclaredConstructor().newInstance();
- CONVERT_UTILS.register(c, type);
+ CONVERT_UTILS.put(type, c);
return c;
} catch (ClassNotFoundException e) {
// fall through
@@ -1206,77 +1213,35 @@ public static Converter lookupConverter(Class type) {
}
}
- // bean utils doesn't check the super type, so converters that apply to multiple types
- // need to be handled outside its semantics
- if (Enum.class.isAssignableFrom(type)) { // enum
- return ENUM_CONVERTER;
- }
-
return null;
}
static {
- CONVERT_UTILS.register(
- new Converter() {
- @Override
- public Object convert(Class type, Object value) {
- if (value == null) {
- return null;
- }
- try {
- return new URL(value.toString());
- } catch (MalformedURLException e) {
- throw new ConversionException(e);
- }
- }
- },
- URL.class);
-
- CONVERT_UTILS.register(
- new Converter() {
- @Override
- public FileItem convert(Class type, Object value) {
- if (value == null) {
- return null;
- }
- try {
- return Stapler.getCurrentRequest2().getFileItem2(value.toString());
- } catch (ServletException | IOException e) {
- throw new ConversionException(e);
- }
- }
- },
- FileItem.class);
-
- CONVERT_UTILS.register(
- new Converter() {
- @Override
- public org.apache.commons.fileupload.FileItem convert(Class type, Object value) {
- if (value == null) {
- return null;
- }
- try {
- return org.apache.commons.fileupload.FileItem.fromFileUpload2FileItem(
- Stapler.getCurrentRequest2().getFileItem2(value.toString()));
- } catch (ServletException | IOException e) {
- throw new ConversionException(e);
- }
- }
- },
- org.apache.commons.fileupload.FileItem.class);
+ CONVERT_UTILS.put(URL.class, source -> {
+ try {
+ return new URL(source);
+ } catch (MalformedURLException e) {
+ throw new RuntimeException(e);
+ }
+ });
- // mapping for boxed types should map null to null, instead of null to zero.
- CONVERT_UTILS.register(new IntegerConverter(null), Integer.class);
- CONVERT_UTILS.register(new FloatConverter(null), Float.class);
- CONVERT_UTILS.register(new DoubleConverter(null), Double.class);
- }
+ CONVERT_UTILS.put(FileItem.class, source -> {
+ try {
+ return Stapler.getCurrentRequest2().getFileItem2(source);
+ } catch (ServletException | IOException e) {
+ throw new RuntimeException(e);
+ }
+ });
- private static final Converter ENUM_CONVERTER = new Converter() {
- @Override
- public Object convert(Class type, Object value) {
- return Enum.valueOf(type, value.toString());
- }
- };
+ CONVERT_UTILS.put(org.apache.commons.fileupload.FileItem.class, source -> {
+ try {
+ return org.apache.commons.fileupload.FileItem.fromFileUpload2FileItem(
+ Stapler.getCurrentRequest2().getFileItem2(source));
+ } catch (ServletException | IOException e) {
+ throw new RuntimeException(e);
+ }
+ });
+ }
/**
* Escapes HTML/XML unsafe characters for the PCDATA section.
diff --git a/core/src/main/java/org/kohsuke/stapler/StaplerRequest.java b/core/src/main/java/org/kohsuke/stapler/StaplerRequest.java
index 0c0ea358b..86e7cb636 100644
--- a/core/src/main/java/org/kohsuke/stapler/StaplerRequest.java
+++ b/core/src/main/java/org/kohsuke/stapler/StaplerRequest.java
@@ -72,12 +72,11 @@
import javax.servlet.http.PushBuilder;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
-import org.apache.commons.beanutils.BeanUtils;
-import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.fileupload2.core.FileItem;
import org.kohsuke.stapler.bind.BoundObjectTable;
import org.kohsuke.stapler.json.SubmittedForm;
import org.kohsuke.stapler.lang.Klass;
+import org.springframework.beans.BeanUtils;
/**
* Defines additional parameters/operations made available by Stapler.
@@ -307,9 +306,9 @@ public interface StaplerRequest extends HttpServletRequest {
* be simply ignored.
*
*
- * Values are converted into the right type. See {@link ConvertUtils#convert(String, Class)}.
+ * Values are converted into the right type using Spring's conversion service.
*
- * @see BeanUtils#setProperty(Object, String, Object)
+ * @see BeanUtils#copyProperties(Object, Object)
*
* @param bean
* The object which will be filled out.
diff --git a/core/src/main/java/org/kohsuke/stapler/StaplerRequest2.java b/core/src/main/java/org/kohsuke/stapler/StaplerRequest2.java
index 91b4eb996..442948fbc 100644
--- a/core/src/main/java/org/kohsuke/stapler/StaplerRequest2.java
+++ b/core/src/main/java/org/kohsuke/stapler/StaplerRequest2.java
@@ -36,12 +36,11 @@
import java.util.Set;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
-import org.apache.commons.beanutils.BeanUtils;
-import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.fileupload2.core.FileItem;
import org.kohsuke.stapler.bind.BoundObjectTable;
import org.kohsuke.stapler.json.SubmittedForm;
import org.kohsuke.stapler.lang.Klass;
+import org.springframework.beans.BeanUtils;
/**
* Defines additional parameters/operations made available by Stapler.
@@ -269,9 +268,9 @@ public interface StaplerRequest2 extends HttpServletRequest {
* be simply ignored.
*
*
- * Values are converted into the right type. See {@link ConvertUtils#convert(String, Class)}.
+ * Values are converted into the right type using Spring's conversion service.
*
- * @see BeanUtils#setProperty(Object, String, Object)
+ * @see BeanUtils#copyProperties(Object, Object)
*
* @param bean
* The object which will be filled out.
diff --git a/core/src/main/java/org/kohsuke/stapler/config/ConfigurationLoader.java b/core/src/main/java/org/kohsuke/stapler/config/ConfigurationLoader.java
index 4c1a181c1..d570f0c6f 100644
--- a/core/src/main/java/org/kohsuke/stapler/config/ConfigurationLoader.java
+++ b/core/src/main/java/org/kohsuke/stapler/config/ConfigurationLoader.java
@@ -13,7 +13,7 @@
import java.util.Properties;
import java.util.TreeMap;
import java.util.function.Function;
-import org.apache.commons.beanutils.ConvertUtils;
+import org.springframework.core.convert.support.DefaultConversionService;
/**
* Provides a type-safe access to the configuration of the application.
@@ -48,6 +48,7 @@
*/
public class ConfigurationLoader {
private final Function source;
+ private static final DefaultConversionService CONVERSION_SERVICE = new DefaultConversionService();
/**
* The caller should use one of the fromXyz methods.
@@ -127,7 +128,7 @@ public Object invoke(Object proxy, Method method, Object[] args) throws Throwabl
return null; // TODO: check how the primitive types are handled here
}
- return ConvertUtils.convert(v, r);
+ return CONVERSION_SERVICE.convert(v, r);
}
private String getKey(Method method, Configuration c) {
diff --git a/core/src/test/java/org/kohsuke/stapler/ObjectWithCustomConverter.java b/core/src/test/java/org/kohsuke/stapler/ObjectWithCustomConverter.java
index 43bd09ff8..c4ba374c2 100644
--- a/core/src/test/java/org/kohsuke/stapler/ObjectWithCustomConverter.java
+++ b/core/src/test/java/org/kohsuke/stapler/ObjectWithCustomConverter.java
@@ -1,6 +1,6 @@
package org.kohsuke.stapler;
-import org.apache.commons.beanutils.Converter;
+import org.springframework.core.convert.converter.Converter;
/**
* @author Kohsuke Kawaguchi
@@ -13,10 +13,10 @@ public ObjectWithCustomConverter(int x, int y) {
this.y = y;
}
- public static class StaplerConverterImpl implements Converter {
+ public static class StaplerConverterImpl implements Converter {
@Override
- public Object convert(Class type, Object value) {
- String[] tokens = value.toString().split(",");
+ public Object convert(String source) {
+ String[] tokens = source.split(",");
return new ObjectWithCustomConverter(Integer.parseInt(tokens[0]), Integer.parseInt(tokens[1]));
}
}