Skip to content

Commit

Permalink
fixed defaults for Java standard types
Browse files Browse the repository at this point in the history
  • Loading branch information
Artem Fedorov committed Mar 27, 2017
1 parent 28b1a0c commit 849c5dc
Showing 1 changed file with 19 additions and 2 deletions.
Expand Up @@ -9,7 +9,6 @@
import hudson.model.ParameterDefinition;
import hudson.model.ParameterValue;
import hudson.model.ParametersDefinitionProperty;
import hudson.util.ReflectionUtils;
import jenkins.model.Jenkins;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.ObjectUtils;
Expand Down Expand Up @@ -317,7 +316,7 @@ private Object[] buildArguments(Map<String,?> bag, Type[] types, String[] names,
if (a != null) {
args[i] = coerce(this.type.getName() + "." + name, type, a);
} else if (type instanceof Class && ((Class) type).isPrimitive()) {
args[i] = ReflectionUtils.getVmDefaultValueForPrimitiveType((Class)type);
args[i] = getVmDefaultValueForPrimitiveType((Class)type);
if (args[i]==null && callEvenIfNoArgs)
throw new UnsupportedOperationException("not yet handling @DataBoundConstructor default value of " + type + "; pass an explicit value for " + name);
} else {
Expand Down Expand Up @@ -707,4 +706,22 @@ private Object readResolve() {
private static final Logger LOGGER = Logger.getLogger(DescribableModel.class.getName());

private static final long serialVersionUID = 1L;

/**
* Given the primitive type, returns the VM default value for that type in a boxed form.
*/
public static Object getVmDefaultValueForPrimitiveType(Class<?> type) {
return defaultPrimitiveValue.get(type);
}

private static final Map<Class,Object> defaultPrimitiveValue = new HashMap<Class, Object>();
static {
defaultPrimitiveValue.put(boolean.class, false);
defaultPrimitiveValue.put(byte.class, (byte) 0);
defaultPrimitiveValue.put(short.class, (short) 0);
defaultPrimitiveValue.put(int.class, 0);
defaultPrimitiveValue.put(long.class, 0L);
defaultPrimitiveValue.put(float.class, 0.0f);
defaultPrimitiveValue.put(double.class, 0.0d);
}
}

0 comments on commit 849c5dc

Please sign in to comment.