@SuppressWarnings("ConstantConditions") @Test public void instantiate() { TestClass testClass = ReflectionHelper.instantiate(TestClass.class); Assert.assertEquals(0, testClass.getA()); Assert.assertEquals("", testClass.getS()); Assert.assertEquals(1, ReflectionHelper.instantiate(TestClass.class, 1).getA()); Assert.assertEquals("A", ReflectionHelper.instantiate(TestClass.class, 1, "A").getS()); }
static <T> T instantiate(Class<T> type, Object... args) { try { if (args.length == 0) { return type.getDeclaredConstructor().newInstance(); } else { Class<?>[] classes = toClasses(args); return type.getDeclaredConstructor(classes).newInstance(args); } } catch (InstantiationException | IllegalAccessException | NoSuchMethodException | InvocationTargetException e) { e.printStackTrace(); } return null; }
ReflectionHelper { static <T> T instantiate(Class<T> type, Object... args) { try { if (args.length == 0) { return type.getDeclaredConstructor().newInstance(); } else { Class<?>[] classes = toClasses(args); return type.getDeclaredConstructor(classes).newInstance(args); } } catch (InstantiationException | IllegalAccessException | NoSuchMethodException | InvocationTargetException e) { e.printStackTrace(); } return null; } }
ReflectionHelper { static <T> T instantiate(Class<T> type, Object... args) { try { if (args.length == 0) { return type.getDeclaredConstructor().newInstance(); } else { Class<?>[] classes = toClasses(args); return type.getDeclaredConstructor(classes).newInstance(args); } } catch (InstantiationException | IllegalAccessException | NoSuchMethodException | InvocationTargetException e) { e.printStackTrace(); } return null; } private  ReflectionHelper(); }
ReflectionHelper { static <T> T instantiate(Class<T> type, Object... args) { try { if (args.length == 0) { return type.getDeclaredConstructor().newInstance(); } else { Class<?>[] classes = toClasses(args); return type.getDeclaredConstructor(classes).newInstance(args); } } catch (InstantiationException | IllegalAccessException | NoSuchMethodException | InvocationTargetException e) { e.printStackTrace(); } return null; } private  ReflectionHelper();  }
ReflectionHelper { static <T> T instantiate(Class<T> type, Object... args) { try { if (args.length == 0) { return type.getDeclaredConstructor().newInstance(); } else { Class<?>[] classes = toClasses(args); return type.getDeclaredConstructor(classes).newInstance(args); } } catch (InstantiationException | IllegalAccessException | NoSuchMethodException | InvocationTargetException e) { e.printStackTrace(); } return null; } private  ReflectionHelper();   }