Author: fschumacher Date: Sun Dec 2 11:33:14 2018 New Revision: 1847987 URL: http://svn.apache.org/viewvc?rev=1847987&view=rev Log: Simplify code that tries to find the correct constructor
Part of #435 and Bugzilla Id: 62972 Modified: jmeter/trunk/src/core/org/apache/jmeter/gui/util/PowerTableModel.java Modified: jmeter/trunk/src/core/org/apache/jmeter/gui/util/PowerTableModel.java URL: http://svn.apache.org/viewvc/jmeter/trunk/src/core/org/apache/jmeter/gui/util/PowerTableModel.java?rev=1847987&r1=1847986&r2=1847987&view=diff ============================================================================== --- jmeter/trunk/src/core/org/apache/jmeter/gui/util/PowerTableModel.java (original) +++ jmeter/trunk/src/core/org/apache/jmeter/gui/util/PowerTableModel.java Sun Dec 2 11:33:14 2018 @@ -19,9 +19,9 @@ package org.apache.jmeter.gui.util; import java.lang.reflect.Constructor; -import java.lang.reflect.InvocationTargetException; import java.util.ArrayList; import java.util.Arrays; +import java.util.Collections; import java.util.List; import javax.swing.event.TableModelEvent; @@ -40,6 +40,10 @@ public class PowerTableModel extends Def private Class<?>[] columnClasses; + private static final List<Object> DEFAULT_ARGS = Collections.unmodifiableList(Arrays.asList("", Integer.valueOf(0), + Long.valueOf(0L), Boolean.FALSE, Float.valueOf(0F), Double.valueOf(0D), Character.valueOf(' '), + Byte.valueOf(Byte.MIN_VALUE), Short.valueOf(Short.MIN_VALUE))); + public PowerTableModel(String[] headers, Class<?>[] classes) { if (headers.length != classes.length){ throw new IllegalArgumentException("Header and column array sizes differ"); @@ -159,50 +163,13 @@ public class PowerTableModel extends Def try { return colClass.getDeclaredConstructor().newInstance(); } catch (Exception e) { - try { - Constructor<?> constr = colClass.getConstructor(String.class); - return constr.newInstance(""); - } catch (NoSuchMethodException | InstantiationException | IllegalAccessException | InvocationTargetException ignored) { - } - try { - Constructor<?> constr = colClass.getConstructor(Integer.TYPE); - return constr.newInstance(Integer.valueOf(0)); - } catch (NoSuchMethodException | InstantiationException | IllegalAccessException | InvocationTargetException ignored) { - } - try { - Constructor<?> constr = colClass.getConstructor(Long.TYPE); - return constr.newInstance(Long.valueOf(0L)); - } catch (NoSuchMethodException | InstantiationException | IllegalAccessException | InvocationTargetException ignored) { - } - try { - Constructor<?> constr = colClass.getConstructor(Boolean.TYPE); - return constr.newInstance(Boolean.FALSE); - } catch (NoSuchMethodException | InstantiationException | IllegalAccessException | InvocationTargetException ignored) { - } - try { - Constructor<?> constr = colClass.getConstructor(Float.TYPE); - return constr.newInstance(Float.valueOf(0F)); - } catch (NoSuchMethodException | InstantiationException | IllegalAccessException | InvocationTargetException ignored) { - } - try { - Constructor<?> constr = colClass.getConstructor(Double.TYPE); - return constr.newInstance(Double.valueOf(0D)); - } catch (NoSuchMethodException | InstantiationException | IllegalAccessException | InvocationTargetException ignored) { - } - try { - Constructor<?> constr = colClass.getConstructor(Character.TYPE); - return constr.newInstance(Character.valueOf(' ')); - } catch (NoSuchMethodException | InstantiationException | IllegalAccessException | InvocationTargetException ignored) { - } - try { - Constructor<?> constr = colClass.getConstructor(Byte.TYPE); - return constr.newInstance(Byte.valueOf(Byte.MIN_VALUE)); - } catch (NoSuchMethodException | InstantiationException | IllegalAccessException | InvocationTargetException ignored) { - } - try { - Constructor<?> constr = colClass.getConstructor(Short.TYPE); - return constr.newInstance(Short.valueOf(Short.MIN_VALUE)); - } catch (NoSuchMethodException | InstantiationException | IllegalAccessException | InvocationTargetException ignored) { + for (Object initArg: DEFAULT_ARGS) { + try { + Constructor<?> constr = colClass.getConstructor(initArg.getClass()); + return constr.newInstance(initArg); + } catch (ReflectiveOperationException ignored) { + // no need to log this, as we are just trying out all available default args + } } } return "";