Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import java.lang.reflect.Field;

import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;

public class Main {
    public static <T> ArrayList<HashMap<String, String>> prepareDataToSave(

            ArrayList<T> source, Class<T> classType)
            throws NoSuchMethodException, SecurityException, IllegalArgumentException, IllegalAccessException {

        ArrayList<HashMap<String, String>> destination = new ArrayList<HashMap<String, String>>();
        ArrayList<Field> savedFieds = new ArrayList<Field>();
        Field[] fields;
        Object value = null;
        HashMap<String, String> aux;
        Field auxField;

        fields = classType.getDeclaredFields();

        for (int j = 0; j < fields.length; j++) {

            Class<?> type = fields[j].getType();

            if (!(Modifier.isStatic(fields[j].getModifiers()) || Modifier.isFinal(fields[j].getModifiers())
                    || type.isArray() || Collection.class.isAssignableFrom(type))) {

                savedFieds.add(fields[j]);

            }
        }

        if (classType == null || fields == null) {
            return null;
        }

        for (int i = 0; i < source.size(); i++) {
            aux = new HashMap<String, String>();

            for (int j = 0; j < savedFieds.size(); j++) {

                auxField = savedFieds.get(j);
                auxField.setAccessible(true);
                value = auxField.get(source.get(i));
                aux.put(auxField.getName(), value.toString());

            }

            destination.add(aux);
        }

        return destination;

    }
}