Skip to main content
deleted 5 characters in body
Source Link
public List<List<Object>> void getData(Collection<T> data) {
    try {
        Field[] fields = data.iterator().next().getClass().getDeclaredFields();
        Set<String> realFields = new LinkedHashSet<String>();
        for(Field field : fields) {
            if (!(java.lang.reflect.Modifier.isStatic(field.getModifiers()) ||
                  java.lang.reflect.Modifier.isFinal(field.getModifiers()))) {
                realFields.add(field.getName());
            }
        }
        List<List<Object>> realData = new ArrayList<List<Object>>();
        for(T element : data) {
            List<Object> row = new ArrayList<Object>();
            for(String realField : realFields) {
                Field currentField = element.getClass().getDeclaredField(realField);
                currentField.setAccessible(true);
                row.add(currentField.get(element));
            }
            realData.add(row);
        }
        bulkInsert(table, realData, bulkSize);
    } catch (Exception e) {
        //basic error handling, this will be improved;
        throw new RuntimeException(e);
    }
}
public List<List<Object>> void getData(Collection<T> data) {
    try {
        Field[] fields = data.iterator().next().getClass().getDeclaredFields();
        Set<String> realFields = new LinkedHashSet<String>();
        for(Field field : fields) {
            if (!(java.lang.reflect.Modifier.isStatic(field.getModifiers()) ||
                  java.lang.reflect.Modifier.isFinal(field.getModifiers()))) {
                realFields.add(field.getName());
            }
        }
        List<List<Object>> realData = new ArrayList<List<Object>>();
        for(T element : data) {
            List<Object> row = new ArrayList<Object>();
            for(String realField : realFields) {
                Field currentField = element.getClass().getDeclaredField(realField);
                currentField.setAccessible(true);
                row.add(currentField.get(element));
            }
            realData.add(row);
        }
        bulkInsert(table, realData, bulkSize);
    } catch (Exception e) {
        //basic error handling, this will be improved;
        throw new RuntimeException(e);
    }
}
public List<List<Object>> getData(Collection<T> data) {
    try {
        Field[] fields = data.iterator().next().getClass().getDeclaredFields();
        Set<String> realFields = new LinkedHashSet<String>();
        for(Field field : fields) {
            if (!(java.lang.reflect.Modifier.isStatic(field.getModifiers()) ||
                  java.lang.reflect.Modifier.isFinal(field.getModifiers()))) {
                realFields.add(field.getName());
            }
        }
        List<List<Object>> realData = new ArrayList<List<Object>>();
        for(T element : data) {
            List<Object> row = new ArrayList<Object>();
            for(String realField : realFields) {
                Field currentField = element.getClass().getDeclaredField(realField);
                currentField.setAccessible(true);
                row.add(currentField.get(element));
            }
            realData.add(row);
        }
    } catch (Exception e) {
        //basic error handling, this will be improved;
        throw new RuntimeException(e);
    }
}
added 1062 characters in body
Source Link

EDIT: The method above was adapted from this one. I'm assuming Collection<T> data is a valid non-empty collection.

public List<List<Object>> void getData(Collection<T> data) {
    try {
        Field[] fields = data.iterator().next().getClass().getDeclaredFields();
        Set<String> realFields = new LinkedHashSet<String>();
        for(Field field : fields) {
            if (!(java.lang.reflect.Modifier.isStatic(field.getModifiers()) ||
                  java.lang.reflect.Modifier.isFinal(field.getModifiers()))) {
                realFields.add(field.getName());
            }
        }
        List<List<Object>> realData = new ArrayList<List<Object>>();
        for(T element : data) {
            List<Object> row = new ArrayList<Object>();
            for(String realField : realFields) {
                Field currentField = element.getClass().getDeclaredField(realField);
                currentField.setAccessible(true);
                row.add(currentField.get(element));
            }
            realData.add(row);
        }
        bulkInsert(table, realData, bulkSize);
    } catch (Exception e) {
        //basic error handling, this will be improved;
        throw new RuntimeException(e);
    }
}

EDIT: The method above was adapted from this one. I'm assuming Collection<T> data is a valid non-empty collection.

public List<List<Object>> void getData(Collection<T> data) {
    try {
        Field[] fields = data.iterator().next().getClass().getDeclaredFields();
        Set<String> realFields = new LinkedHashSet<String>();
        for(Field field : fields) {
            if (!(java.lang.reflect.Modifier.isStatic(field.getModifiers()) ||
                  java.lang.reflect.Modifier.isFinal(field.getModifiers()))) {
                realFields.add(field.getName());
            }
        }
        List<List<Object>> realData = new ArrayList<List<Object>>();
        for(T element : data) {
            List<Object> row = new ArrayList<Object>();
            for(String realField : realFields) {
                Field currentField = element.getClass().getDeclaredField(realField);
                currentField.setAccessible(true);
                row.add(currentField.get(element));
            }
            realData.add(row);
        }
        bulkInsert(table, realData, bulkSize);
    } catch (Exception e) {
        //basic error handling, this will be improved;
        throw new RuntimeException(e);
    }
}
Source Link

Is this the right way to retrieve all the fields from any Object?

I have a method that returns a List<Object> from any given Object containing its values. It doesn't get the values of any static or final field in the class.

public List<Object> objectData(Object object) {
    Field[] fields = object.getClass().getDeclaredFields();
    Set<String> realFields = new LinkedHashSet<String>();
    for(Field field : fields) {
        if (!(java.lang.reflect.Modifier.isStatic(field.getModifiers()) ||
              java.lang.reflect.Modifier.isFinal(field.getModifiers()))) {
            realFields.add(field.getName());
        }
    }
    List<Object> row = new ArrayList<Object>();
    for(String realField : realFields) {
        Field currentField = object.getClass().getDeclaredField(realField);
        currentField.setAccessible(true);
        row.add(currentField.get(object));
    }
    return row;
}

Is this the right approach? Isn't something else I can be missing?