Class to fill model objects with data from the database This is a helper class that helps fill models with data obtained from the database.

public class Util {

    public static &lt;M&gt; ArrayList&lt;M&gt; createModel(M model, Cursor cursor) throws IOException {

        try {
            final Class&lt;?&gt; cls = model.getClass();

            final ArrayList&lt;M&gt; result = new ArrayList&lt;&gt;();
            while (cursor.next()) {

                final Class&lt;?&gt; item = Class.forName(cls.getName());
                final M obj = (M) item.newInstance();

                final Field[] fields = item.getDeclaredFields();

                for (Field field : fields) {
                    field.setAccessible(true);

                    final Class&lt;?&gt; type = field.getType();

                    switch (type.toString()) {

                        case "int": {
                            final int value = cursor.getInt(getFieldName(field), 0);
                            field.setInt(obj, value);
                            break;
                        }

                        default:
                            //throw new IOException();
                            continue;
                    }
                }

                result.add(obj);
            }

            return result;
        } catch (IllegalAccessException | InstantiationException | ClassNotFoundException e) {
            e.printStackTrace();
            throw new IOException();
        }
    }

    private static String getFieldName(Field field) {
        final fieldName annotation = field.getDeclaredAnnotation(fieldName.class);
        return annotation == null ? field.getName() : annotation.name();
    }
}


All code here(pastebin.com)

I think my version is very bad, so I want to improve it, but I haven’t yet figured out how to do it.

Usage example:

String query = "SELECT * FROM table_name WHERE;";
Cursor cursor = helper.query(query);
List&lt;Test&gt; test = Util.createModel(new Test(), cursor);
Why do you think that your version in "very bad" ?

There are of course some little improvements that you can do. Like declaring List&lt;M&gt; instead of ArrayList&lt;M&gt; as the return type. Also you can pass Class&lt;M&gt; cls instead of an empty instance of your model.

Another think to improve the readability of your code is to split your main method into many little methods. Like one for the mapping of a class. 

Many dislike the switch and specially for a long one like that. If you want you can replace it with a Map&lt;String, BiConsumer&lt;Cursor, Field&gt;&gt; that associate a type and a consumer to set the value to a field. It is usually a wrong parctive to silently ignore an edge case. You may consider to throw an exception or at least log something instead of continuing in the default case.

Instead of testing the name of the type, you can compare the types and add the support for the wrapper types.

Finally for the exception. I am not sure that IOException is the best type, you can also use a logger instead of e.printStackTrace() because this one will pollute your output without any means to control it.

if ( Integer.class.equals(type) || Integer.TYPE.equals(type) ) {
  // ...
} else if ( .. ) {
  // ...  
} else {
  throw new UnsupportedTypeException("Type "+type.getName()+" cannot be mapped.");
}