/** * Validates a field given field name as string. * * @param fieldName provided as a string * @param conf map of confs * @param configs config class */ public static void validateField(String fieldName, Map<String, Object> conf, List<Class<?>> configs) { Field field = null; for (Class<?> clazz : configs) { try { field = clazz.getField(fieldName); } catch (NoSuchFieldException e) { //Ignored } } if (field == null) { throw new RuntimeException("Could not find " + fieldName + " in any of " + configs); } validateField(field, conf); }
/** * Validates a field given field name as string uses Config.java as the default config class * * @param fieldName provided as a string * @param conf map of confs */ public static void validateField(String fieldName, Map<String, Object> conf) { validateField(fieldName, conf, getConfigClasses()); }
/** * Validate all confs in map. * * @param conf map of configs * @param classes config class */ public static void validateFields(Map<String, Object> conf, List<Class<?>> classes) { for (Class<?> clazz : classes) { for (Field field : clazz.getDeclaredFields()) { if (!isFieldAllowed(field)) { continue; } Object keyObj = null; try { keyObj = field.get(null); } catch (IllegalAccessException e) { //This should not happen because we checked for PUBLIC in isFieldAllowed throw new RuntimeException(e); } //make sure that defined key is string in case wrong stuff got put into Config.java if (keyObj instanceof String) { String confKey = (String) keyObj; if (conf.containsKey(confKey)) { validateField(field, conf); } } } } }
/** * Validates a field given field name as string uses Config.java as the default config class * * @param fieldName provided as a string * @param conf map of confs */ public static void validateField(String fieldName, Map conf) { validateField(fieldName, conf, CONFIG_CLASS); }
/** * Validates a field given field name as string * * @param fieldName provided as a string * @param conf map of confs * @param configClass config class */ public static void validateField(String fieldName, Map conf, Class configClass) { Field field = null; try { field = configClass.getField(fieldName); } catch (NoSuchFieldException e) { throw new RuntimeException(e); } validateField(field, conf); }
/** * Validate all confs in map * * @param conf map of configs * @param configClass config class */ public static void validateFields(Map conf, Class configClass) { for (Field field : configClass.getFields()) { Object keyObj = null; try { keyObj = field.get(null); } catch (IllegalAccessException e) { throw new RuntimeException(e); } //make sure that defined key is string in case wrong stuff got put into Config.java if (keyObj instanceof String) { String confKey = (String) keyObj; if (conf.containsKey(confKey)) { validateField(field, conf); } } } }