Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- import java.util.*;
- import java.nio.file.*;
- import java.util.regex.*;
- public class CBFIni {
- public ArrayList<SectionFields> sectionData = new ArrayList<SectionFields>();
- Pattern patternParam = Pattern.compile("^[A-Za-z_0-9]+");
- Matcher matchParam, matchValue;
- String nameParam;
- public CBFIni() {
- try {
- List<String> lines = Files.readAllLines(Paths.get("backups.conf"));
- boolean checkSection = false;
- int i = -1;
- for(String line: lines){
- if (line.length() > 0 && line.charAt(0) != '#') {
- if (!checkSection) {
- checkSection = true; // Отметка, что начались параметры секции
- sectionData.add(new SectionFields()); // Создаем новый элемент-объект списка
- }
- i++;
- matchParam = patternParam.matcher(line); // Выуживаем имя параметра
- if (matchParam.matches()) {
- nameParam = line.substring(matchParam.start(),matchParam.end()); // Имя параметра
- sectionData.get(sectionData.size()-1).(nameParam) = line.substring(matchParam.end()+1);
- }
- } else {
- checkSection = false;
- i = -1;
- }
- }
- } catch (IOException e) {
- }
- }
- public class SectionFields {
- String backup, description, type, server, folder;
- int days, weeks, monthes, years, masterDay;
- }
- }
- sectionData.get(sectionData.size()-1).(nameParam) = line.substring(matchParam.end()+1);
- private static void setValue(Class<?> type,
- Object object,
- String paramName,
- String value) throws Exception {
- Field field;
- do {
- field = type.getDeclaredField(paramName);
- type = type.getSuperclass();
- } while (field != null && type != null);
- if (field == null)
- throw new IllegalArgumentException(
- "can't find the field with name: " + paramName);
- field.setAccessible(true);
- field.set(object, value);
- }
- private static Map<String, MethodHandle> getSetters(Class<?> type) throws Exception {
- Map<String, MethodHandle> setters = new HashMap<>();
- while (type != null) {
- for (Field field : type.getDeclaredFields()) {
- field.setAccessible(true);
- setters.put(field.getName(), MethodHandles
- .lookup()
- .unreflectSetter(field));
- }
- type = type.getSuperclass();
- }
- return setters;
- }
- Map<String, MethodHandle> setters = getSetters(SectionFields.class);
- SectionFields obj = new SectionFields();
- setters.get("backup").invoke(obj, "hello world");
Add Comment
Please, Sign In to add comment