@Override @SuppressWarnings("unchecked") public <K> K or(K alternative) { U.notNull(alternative, "alternative"); T value = getOrNull(); return value != null ? (K) Cls.convert(value, alternative.getClass()) : alternative; }
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; }
private <T> void invokePostConstruct(T target) { List<Method> methods = Cls.getMethodsAnnotated(target.getClass(), PostConstruct.class); for (Method method : methods) { Cls.invoke(method, target); } }
public static void watchForChanges(String path, Operation<String> changeListener) { Class<?> watch = Cls.getClassIfExists("org.rapidoid.io.watch.Watch"); if (watch != null) { Method dir = Cls.getMethod(watch, "dir", String.class, Operation.class); Cls.invokeStatic(dir, path, changeListener); } }
private static IReqInfo createInfo() { if (Cls.exists(RAPIDOID_CTX)) { return (IReqInfo) Cls.newInstance(Cls.get("org.rapidoid.http.impl.RapidoidReqInfo")); } return NO_REQ_INFO; }
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); }
if (Cls.isAppBeanType(type)) { return new BeanParamRetriever(customization, type, name, valid != null); } else if (Cls.kindOf(type).isConcrete()) { return new DataParamRetriever(type, name, required != null); } else if (Cls.isDataStructure(type) || Cls.isJREClass(type.getCanonicalName())) { throw U.rte("Unsupported parameter type: %s", type);
public MapProp(String name, Object key, Class<?> type) { this.name = name; this.key = key; this.type = type; this.typeKind = Cls.kindOf(type); }
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; }
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)); } } } }
static IGoodies getGoodies() { Class<?> goodiesClass; if (Msc.isPlatform()) { goodiesClass = Cls.get("org.rapidoid.goodies.RapidoidPlatformGoodies"); } else { goodiesClass = Cls.getClassIfExists("org.rapidoid.goodies.RapidoidGoodies"); U.must(goodiesClass != null, "Cannot find the Goodies, is module 'rapidoid-web' missing?"); } return (IGoodies) Cls.newInstance(goodiesClass); }
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); }
@Override public R processUnknown(T context, Object value) { throw U.rte("Unsupported type: " + Cls.of(value).getName()); }
@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 boolean areAssignable(Class<?>[] types, Object[] values) { if (types.length != values.length) { return false; } for (int i = 0; i < values.length; i++) { Object val = values[i]; if (val != null && !instanceOf(val, types[i])) { return false; } } return true; }
public static void bind(Object destBean, Map<String, ?> src) { Method bind = Cls.findMethod(destBean.getClass(), "bind", Map.class); if (bind != null) { Cls.invoke(bind, destBean, src); } else { update(destBean, src, false, false); } }