protected static final void initDatabase( Class<?>[] classes, Map<String, String> options, Map<String, Field> optionFields) { if (outputDB == null) { return; } // --Init Database outputDB.connect(); DBResultLogger logger = new DBResultLogger(outputDB, runName); Execution.logger = logger; outputDB.beginTransaction(); // --Add Options for (String key : optionFields.keySet()) { Field f = optionFields.get(key.toLowerCase()); // (try to save the declared option) String value = options.get(key); if (value == null) { // (if no declared option, get field value) try { boolean accessSave = true; if (!f.isAccessible()) { accessSave = false; f.setAccessible(true); } Object v = f.get(null); if (v == null) { value = "<null>"; } else if (v.getClass().isArray()) { value = Arrays.toString((Object[]) v); } else { value = v.toString(); } if (!accessSave) { f.setAccessible(false); } } catch (IllegalArgumentException e) { throw log.fail(e); } catch (IllegalAccessException e) { throw log.fail(e); } } logger.logOption(key, value, f.getDeclaringClass().getName() + "." + f.getName()); } // --Commit outputDB.endTransaction(); }
private static void fillField(Field f, String value) { try { // --Permissions boolean accessState = true; if (Modifier.isFinal(f.getModifiers())) { log.err(LOG_TAG, "Option cannot be final: " + f); System.exit(ExitCode.BAD_OPTION.code); } if (!f.isAccessible()) { accessState = false; f.setAccessible(true); } // --Set Value Object objVal = Utils.cast(value, f.getGenericType()); if (objVal != null) { if (objVal.getClass().isArray()) { // (case: array) Object[] array = (Object[]) objVal; // error check if (!f.getType().isArray()) { log.err( LOG_TAG, "Setting an array to a non-array field. field: " + f + " value: " + Arrays.toString(array) + " src: " + value); System.exit(ExitCode.BAD_OPTION.code); } // create specific array Object toSet = Array.newInstance(f.getType().getComponentType(), array.length); for (int i = 0; i < array.length; i++) { Array.set(toSet, i, array[i]); } // set value f.set(null, toSet); } else { // case: not array f.set(null, objVal); } } else { log.err( LOG_TAG, "Cannot assign option field: " + f + " value: " + value + "; invalid type"); System.exit(ExitCode.BAD_OPTION.code); } // --Permissions if (!accessState) { f.setAccessible(false); } } catch (IllegalArgumentException e) { log.err( LOG_TAG, "Cannot assign option field: " + f.getDeclaringClass().getCanonicalName() + "." + f.getName() + " value: " + value + " cause: " + e.getMessage()); System.exit(ExitCode.BAD_OPTION.code); } catch (IllegalAccessException e) { log.err( LOG_TAG, "Cannot access option field: " + f.getDeclaringClass().getCanonicalName() + "." + f.getName()); System.exit(ExitCode.BAD_OPTION.code); } catch (Exception e) { log.err( LOG_TAG, "Cannot assign option field: " + f.getDeclaringClass().getCanonicalName() + "." + f.getName() + " value: " + value + " cause: " + e.getMessage()); System.exit(ExitCode.BAD_OPTION.code); } }