/** * Instantiate a Kotlin class using the provided constructor. * @param ctor the constructor of the Kotlin class to instantiate * @param args the constructor arguments to apply * (use {@code null} for unspecified parameter if needed) */ public static <T> T instantiateClass(Constructor<T> ctor, Object... args) throws IllegalAccessException, InvocationTargetException, InstantiationException { KFunction<T> kotlinConstructor = ReflectJvmMapping.getKotlinFunction(ctor); if (kotlinConstructor == null) { return ctor.newInstance(args); } List<KParameter> parameters = kotlinConstructor.getParameters(); Map<KParameter, Object> argParameters = new HashMap<>(parameters.size()); Assert.isTrue(args.length <= parameters.size(), "Number of provided arguments should be less of equals than number of constructor parameters"); for (int i = 0 ; i < args.length ; i++) { if (!(parameters.get(i).isOptional() && args[i] == null)) { argParameters.put(parameters.get(i), args[i]); } } return kotlinConstructor.callBy(argParameters); }
/** * Instantiate a Kotlin class using the provided constructor. * @param ctor the constructor of the Kotlin class to instantiate * @param args the constructor arguments to apply * (use {@code null} for unspecified parameter if needed) */ public static <T> T instantiateClass(Constructor<T> ctor, Object... args) throws IllegalAccessException, InvocationTargetException, InstantiationException { KFunction<T> kotlinConstructor = ReflectJvmMapping.getKotlinFunction(ctor); if (kotlinConstructor == null) { return ctor.newInstance(args); } List<KParameter> parameters = kotlinConstructor.getParameters(); Map<KParameter, Object> argParameters = new HashMap<>(parameters.size()); Assert.isTrue(args.length <= parameters.size(), "Number of provided arguments should be less of equals than number of constructor parameters"); for (int i = 0 ; i < args.length ; i++) { if (!(parameters.get(i).isOptional() && args[i] == null)) { argParameters.put(parameters.get(i), args[i]); } } return kotlinConstructor.callBy(argParameters); }