From 46618222ccd29474daefa920a5b06ebf5095b605 Mon Sep 17 00:00:00 2001 From: Ruben Dijkstra Date: Thu, 17 Apr 2014 18:53:10 +0200 Subject: [PATCH] Removed the try-catch for the varargsconstructortransformer. --- .../mysema/query/util/ConstructorUtils.java | 45 +++++++++++++------ 1 file changed, 31 insertions(+), 14 deletions(-) diff --git a/querydsl-core/src/main/java/com/mysema/query/util/ConstructorUtils.java b/querydsl-core/src/main/java/com/mysema/query/util/ConstructorUtils.java index 59ca0ccda..bcb02e572 100644 --- a/querydsl-core/src/main/java/com/mysema/query/util/ConstructorUtils.java +++ b/querydsl-core/src/main/java/com/mysema/query/util/ConstructorUtils.java @@ -128,8 +128,8 @@ public class ConstructorUtils { */ public static Collection> getTransformers(Constructor constructor) { ArrayList transformers = Lists.newArrayList( - new VarArgsTransformer(constructor), - new NullSafePrimitiveTransformer(constructor)); + new NullSafePrimitiveTransformer(constructor), + new VarArgsTransformer(constructor)); return copyOf(filter(transformers, applicableFilter)); } @@ -177,7 +177,6 @@ public class ConstructorUtils { paramTypes = constructor.getParameterTypes(); if (paramTypes.length > 0) { componentType = paramTypes[paramTypes.length - 1].getComponentType(); - } else { componentType = null; } @@ -211,13 +210,7 @@ public class ConstructorUtils { } private void set(Object array, int index, Object value) throws IllegalArgumentException, ArrayIndexOutOfBoundsException { - try { - Array.set(array, index, value); - } catch (IllegalArgumentException primitiveArrayCantHoldNull) { - // retry with default value - Object defaultValue = defaultPrimitives.getInstance(componentType); - Array.set(array, index, defaultValue); - } + Array.set(array, index, value); } } @@ -225,6 +218,7 @@ public class ConstructorUtils { private static class NullSafePrimitiveTransformer extends ConstructorArgumentTransformer { private final Map> primitiveLocations = Maps.newLinkedHashMap(); + private final boolean hasPrimitiveVarArgComponentType; private NullSafePrimitiveTransformer(Constructor constructor) { super(constructor); @@ -237,6 +231,18 @@ public class ConstructorUtils { primitiveLocations.put(location, parameterType); } } + if (constructor.isVarArgs()) { + int lastPosition = parameterTypes.length - 1; + Class varArgComponentType = parameterTypes[lastPosition] + .getComponentType(); + hasPrimitiveVarArgComponentType = varArgComponentType + .isPrimitive(); + if (hasPrimitiveVarArgComponentType) { + primitiveLocations.put(lastPosition, varArgComponentType); + } + } else { + hasPrimitiveVarArgComponentType = false; + } } @Override @@ -247,14 +253,25 @@ public class ConstructorUtils { @Override public Object[] apply(Object[] args) { checkNotNull(args); - - for (Map.Entry> primitiveEntry : primitiveLocations.entrySet()) { - Integer location = primitiveEntry.getKey(); + Iterator>> entryIterator = primitiveLocations.entrySet().iterator(); + Map.Entry> lastEntry = null; + while (entryIterator.hasNext()) { + lastEntry = entryIterator.next(); + Integer location = lastEntry.getKey(); if (args[location] == null) { - Class primitiveClass = primitiveEntry.getValue(); + Class primitiveClass = lastEntry.getValue(); args[location] = defaultPrimitives.getInstance(primitiveClass); } } + if (hasPrimitiveVarArgComponentType + && lastEntry != null) {// just an assertion + for (int location = lastEntry.getKey(); location < args.length; location++) { + if (args[location] == null) { + Class primitiveClass = lastEntry.getValue(); + args[location] = defaultPrimitives.getInstance(primitiveClass); + } + } + } return args; }