public static Object[] instantiateAll(Class<?>... classes) { Object[] instances = new Object[classes.length]; for (int i = 0; i < instances.length; i++) { instances[i] = newInstance(classes[i]); } return instances; }
public static Object[] instantiateAll(Collection<Class<?>> classes) { if (classes.isEmpty()) { return EMPTY_ARRAY; } Object[] instances = new Object[classes.size()]; int i = 0; for (Class<?> clazz : classes) { instances[i++] = newInstance(clazz); } return instances; }
private static <T2> T2 createFromEntry(Class<T2> clazz2, Entry<?, ?> e, Map<String, Object> extra) { String key = Cls.str(e.getKey()); T2 item = Cls.newInstance(clazz2, key, e.getValue(), extra); return item; }
@SuppressWarnings("unchecked") private static <T> T newInstance(Object source) { if (source instanceof AutoExpandingMap) { AutoExpandingMap autoExpandingMap = (AutoExpandingMap) source; return (T) autoExpandingMap.copy(); } return (T) Cls.newInstance(source.getClass()); }
@SuppressWarnings("unchecked") private void mapSet(Object target, Object value) { U.must(value instanceof Map, "Expected a map, but found: %s", value); Map<Object, Object> map = get(target); if (map == null) { map = (Map<Object, Object>) Cls.newInstance(type); } map.clear(); map.putAll((Map<Object, Object>) value); }
private static void addBuiltInModules(List<RapidoidModule> modules) { for (String clsName : Cls.getRapidoidClasses()) { if (clsName.endsWith("Module")) { Class<?> cls = Cls.getClassIfExists(clsName); boolean isModule = cls != null && !cls.isInterface() && RapidoidModule.class.isAssignableFrom(cls) && !Modifier.isAbstract(cls.getModifiers()); if (isModule) { modules.add((RapidoidModule) Cls.newInstance(cls)); } } } }
@SuppressWarnings("unchecked") private void collSet(Object target, Object value) { U.must(value instanceof Collection<?>, "Expected a collection, but found: %s", value); Collection<Object> coll = get(target); if (coll == null) { coll = (Collection<Object>) Cls.newInstance(type); } coll.clear(); coll.addAll((Collection<Object>) value); }
@SuppressWarnings("unchecked") public static <T> Items mockBeanItems(int size, Class<T> beanType) { T[] beans = (T[]) new Object[size]; for (int i = 0; i < beans.length; i++) { beans[i] = Cls.newInstance(beanType); Beany.setId(beans[i], i); } return beanItems(beanType, beans); }
public static <T> Map<String, T> toBeanMap(Map<String, Object> data, Class<T> type) { Map<String, T> map = U.map(); for (Map.Entry<String, Object> e : data.entrySet()) { T bean; Object value = e.getValue(); if (value instanceof Map) { bean = JSON.MAPPER.convertValue(value, type); } else if (value instanceof String) { bean = Cls.newInstance(type, value); } else { throw U.rte("Unsupported configuration type: %s", Cls.of(value)); } map.put(e.getKey(), bean); } return Collections.unmodifiableMap(map); }
public static <T> T customizable(Class<T> clazz, Object... args) { String customClassName = "Customized" + clazz.getSimpleName(); Class<T> customClass = getClassIfExists(customClassName); if (customClass == null) { customClass = getClassIfExists("custom." + customClassName); } if (customClass != null && !clazz.isAssignableFrom(customClass)) { customClass = null; } return newInstance(U.or(customClass, clazz), args); }
private static IReqInfo createInfo() { if (Cls.exists(RAPIDOID_CTX)) { return (IReqInfo) Cls.newInstance(Cls.get("org.rapidoid.http.impl.RapidoidReqInfo")); } return NO_REQ_INFO; }
@Override public <K, V> Map<K, V> autoExpandingInjectingMap(final Class<V> clazz) { return Coll.autoExpandingMap(src -> inject(Cls.newInstance(clazz))); }
@SuppressWarnings("unchecked") public static <T> T newInstance(Class<T> clazz, Map<String, Object> properties) { if (U.isEmpty(properties)) { return newInstance(clazz); } Collection<Object> values = properties.values(); for (Constructor<?> constr : clazz.getConstructors()) { Class<?>[] paramTypes = constr.getParameterTypes(); Object[] args = getAssignableArgs(paramTypes, values); if (args != null) { try { return (T) constr.newInstance(args); } catch (Exception e) { throw U.rte(e); } } } throw U.rte("Cannot find appropriate constructor for %s with args %s!", clazz, values); }
private void openSockets() { ioWorkers = new ExtendedWorker[net.workers()]; for (int i = 0; i < ioWorkers.length; i++) { RapidoidHelper helper = Cls.newInstance(net.helperClass(), net.exchangeClass()); String workerName = "client" + (i + 1); ioWorkers[i] = new ExtendedWorker(workerName, helper, net, sslContext); new Thread(ioWorkers[i], workerName).start(); } for (int i = 0; i < ioWorkers.length; i++) { ioWorkers[i].waitToStart(); } }
items.add(Cls.newInstance(clazz2, Cls.str(o), null, null)); items.add(Cls.newInstance(clazz2, Cls.str(obj), null, null)); return Cls.newInstance(clazz1, items);
private static Object wrapGuiContent(Object content) { if (MscOpts.hasRapidoidGUI()) { Object[] items = null; if (content instanceof Collection<?>) { items = U.array((Collection<?>) content); } else if (content instanceof Object[]) { items = (Object[]) content; } if (items != null) { return Cls.newInstance(Cls.get("org.rapidoid.html.ElementGroup"), new Object[]{items}); } } return content; }
@Override public void run() { RapidoidHelper helper = Cls.newInstance(net.helperClass(), net.exchangeClass()); helper.requestIdGen = workerIndex; // to generate UNIQUE request ID (+= MAX_IO_WORKERS) worker = new RapidoidWorker("server" + (workerIndex + 1), helper, net, sslContext); worker.run(); }
exchange = Cls.newInstance(exchangeClass); pool = Pools.create("exchanges", (Callable) () -> Cls.newInstance(exchangeClass), 1000); } else { exchange = null;
@Override public void process(Channel ctx) { T exchange = useExchangePool ? pool(ctx.helper()).get() : Cls.newInstance(exchangeType); assert Cls.instanceOf(exchange, exchangeType); exchange.reset(); exchange.setConnection(ctx); process(ctx, exchange); }
public static ReqRespHandler add(final Class<?> entityType, final String baseUri) { return (ReqRespHandler) (req, resp) -> { final Object entity = Cls.newInstance(entityType); if (resp.screen().title() == null) { resp.screen().title("Add " + name(entityType)); } Btn save = btnSave(entity).go(baseUri + "/manage"); Btn cancel = GUI.btn("Cancel").go(baseUri + "/manage"); return GUI.create(entity).buttons(save, cancel); }; }