@Test public void testLongestConstructor() throws Exception { ConstructorTestClass obj = GobblinConstructorUtils .invokeLongestConstructor(ConstructorTestClass.class, Integer.valueOf(1), "String1", "String2"); Assert.assertEquals(obj.id.intValue(), 1); Assert.assertEquals(obj.str, "String1"); obj = GobblinConstructorUtils.invokeLongestConstructor(ConstructorTestClass.class, Integer.valueOf(1), "String1"); Assert.assertEquals(obj.id.intValue(), 1); Assert.assertEquals(obj.str, "String1"); try { obj = GobblinConstructorUtils.invokeLongestConstructor(ConstructorTestClass.class, Integer.valueOf(1)); Assert.fail(); } catch (NoSuchMethodException nsme) { } obj = GobblinConstructorUtils.invokeLongestConstructor(ConstructorTestClassWithNoArgs.class); Assert.assertEquals(obj.id.intValue(), 0); Assert.assertEquals(obj.str, "noArgs"); Assert.assertNull(obj.props); obj = GobblinConstructorUtils.invokeLongestConstructor(ConstructorTestClassWithNoArgs.class, Integer.valueOf(1)); Assert.assertEquals(obj.id.intValue(), 0); Assert.assertEquals(obj.str, "noArgs"); Assert.assertNull(obj.props); }
public static <T> T invokeLongestConstructor(Class<T> cls, Object... args) throws ReflectiveOperationException { Class<?>[] parameterTypes = new Class[args.length]; for (int i = 0; i < args.length; i++) { parameterTypes[i] = args[i].getClass(); } for (int i = args.length; i >= 0; i--) { if (ConstructorUtils.getMatchingAccessibleConstructor(cls, Arrays.copyOfRange(parameterTypes, 0, i)) != null) { log.debug( String.format("Found accessible constructor for class %s with parameter types %s.", cls, Arrays.toString(Arrays.copyOfRange(parameterTypes, 0, i)))); return ConstructorUtils.invokeConstructor(cls, Arrays.copyOfRange(args, 0, i)); } } throw new NoSuchMethodException(String.format("No accessible constructor for class %s with parameters a subset of %s.", cls, Arrays.toString(parameterTypes))); }
GobblinConstructorUtils { public static <T> T invokeLongestConstructor(Class<T> cls, Object... args) throws ReflectiveOperationException { Class<?>[] parameterTypes = new Class[args.length]; for (int i = 0; i < args.length; i++) { parameterTypes[i] = args[i].getClass(); } for (int i = args.length; i >= 0; i--) { if (ConstructorUtils.getMatchingAccessibleConstructor(cls, Arrays.copyOfRange(parameterTypes, 0, i)) != null) { log.debug( String.format("Found accessible constructor for class %s with parameter types %s.", cls, Arrays.toString(Arrays.copyOfRange(parameterTypes, 0, i)))); return ConstructorUtils.invokeConstructor(cls, Arrays.copyOfRange(args, 0, i)); } } throw new NoSuchMethodException(String.format("No accessible constructor for class %s with parameters a subset of %s.", cls, Arrays.toString(parameterTypes))); } }
GobblinConstructorUtils { public static <T> T invokeLongestConstructor(Class<T> cls, Object... args) throws ReflectiveOperationException { Class<?>[] parameterTypes = new Class[args.length]; for (int i = 0; i < args.length; i++) { parameterTypes[i] = args[i].getClass(); } for (int i = args.length; i >= 0; i--) { if (ConstructorUtils.getMatchingAccessibleConstructor(cls, Arrays.copyOfRange(parameterTypes, 0, i)) != null) { log.debug( String.format("Found accessible constructor for class %s with parameter types %s.", cls, Arrays.toString(Arrays.copyOfRange(parameterTypes, 0, i)))); return ConstructorUtils.invokeConstructor(cls, Arrays.copyOfRange(args, 0, i)); } } throw new NoSuchMethodException(String.format("No accessible constructor for class %s with parameters a subset of %s.", cls, Arrays.toString(parameterTypes))); }  }
GobblinConstructorUtils { public static <T> T invokeLongestConstructor(Class<T> cls, Object... args) throws ReflectiveOperationException { Class<?>[] parameterTypes = new Class[args.length]; for (int i = 0; i < args.length; i++) { parameterTypes[i] = args[i].getClass(); } for (int i = args.length; i >= 0; i--) { if (ConstructorUtils.getMatchingAccessibleConstructor(cls, Arrays.copyOfRange(parameterTypes, 0, i)) != null) { log.debug( String.format("Found accessible constructor for class %s with parameter types %s.", cls, Arrays.toString(Arrays.copyOfRange(parameterTypes, 0, i)))); return ConstructorUtils.invokeConstructor(cls, Arrays.copyOfRange(args, 0, i)); } } throw new NoSuchMethodException(String.format("No accessible constructor for class %s with parameters a subset of %s.", cls, Arrays.toString(parameterTypes))); }  @SafeVarargs static T invokeFirstConstructor(Class<T> cls, List<Object>... constructorArgs); static T invokeLongestConstructor(Class<T> cls, Object... args); @SuppressWarnings("unchecked") static T invokeConstructor(final Class<T> superType, final String clsName, Object... args); }
GobblinConstructorUtils { public static <T> T invokeLongestConstructor(Class<T> cls, Object... args) throws ReflectiveOperationException { Class<?>[] parameterTypes = new Class[args.length]; for (int i = 0; i < args.length; i++) { parameterTypes[i] = args[i].getClass(); } for (int i = args.length; i >= 0; i--) { if (ConstructorUtils.getMatchingAccessibleConstructor(cls, Arrays.copyOfRange(parameterTypes, 0, i)) != null) { log.debug( String.format("Found accessible constructor for class %s with parameter types %s.", cls, Arrays.toString(Arrays.copyOfRange(parameterTypes, 0, i)))); return ConstructorUtils.invokeConstructor(cls, Arrays.copyOfRange(args, 0, i)); } } throw new NoSuchMethodException(String.format("No accessible constructor for class %s with parameters a subset of %s.", cls, Arrays.toString(parameterTypes))); }  @SafeVarargs static T invokeFirstConstructor(Class<T> cls, List<Object>... constructorArgs); static T invokeLongestConstructor(Class<T> cls, Object... args); @SuppressWarnings("unchecked") static T invokeConstructor(final Class<T> superType, final String clsName, Object... args);  }