Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2019
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.10 KB | None | 0 0
  1. public class Util {
  2.  
  3.     public static <M> ArrayList<M> createModel(M model, Cursor cursor) throws IOException {
  4.  
  5.         try {
  6.             final Class<?> cls = model.getClass();
  7.  
  8.             final ArrayList<M> result = new ArrayList<>();
  9.             while (cursor.next()) {
  10.  
  11.                 final Class<?> item = Class.forName(cls.getName());
  12.                 final M obj = (M) item.newInstance();
  13.  
  14.                 final Field[] fields = item.getDeclaredFields();
  15.  
  16.                 for (Field field : fields) {
  17.                     field.setAccessible(true);
  18.  
  19.                     final Class<?> type = field.getType();
  20.  
  21.                     switch (type.toString()) {
  22.  
  23.                         case "int": {
  24.                             final int value = cursor.getInt(getFieldName(field), 0);
  25.                             field.setInt(obj, value);
  26.                             break;
  27.                         }
  28.  
  29.                         case "float": {
  30.                             final float value = cursor.getFloat(getFieldName(field), 0);
  31.                             field.setFloat(obj, value);
  32.                             break;
  33.                         }
  34.  
  35.                         case "long": {
  36.                             final long value = cursor.getLong(getFieldName(field), 0);
  37.                             field.setLong(obj, value);
  38.                             break;
  39.                         }
  40.  
  41.                         case "double": {
  42.                             final double value = cursor.getDouble(getFieldName(field), 0);
  43.                             field.setDouble(obj, value);
  44.                             break;
  45.                         }
  46.  
  47.                         case "boolean": {
  48.                             final boolean value = cursor.getBoolean(getFieldName(field), false);
  49.                             field.setBoolean(obj, value);
  50.                             break;
  51.                         }
  52.  
  53.                         case "class java.lang.String": {
  54.                             final String value = cursor.getString(getFieldName(field));
  55.                             field.set(obj, value);
  56.                             break;
  57.                         }
  58.  
  59.                         case "class java.sql.Timestamp": {
  60.                             final Timestamp value = cursor.getTimestamp(getFieldName(field), "1970-01-01 00:00:000");
  61.                             field.set(obj, value);
  62.                             break;
  63.                         }
  64.  
  65.                         default:
  66.                             //throw new IOException();
  67.                             continue;
  68.                     }
  69.                 }
  70.  
  71.                 result.add(obj);
  72.             }
  73.  
  74.             return result;
  75.         } catch (IllegalAccessException | InstantiationException | ClassNotFoundException e) {
  76.             e.printStackTrace();
  77.             throw new IOException();
  78.         }
  79.     }
  80.  
  81.     private static String getFieldName(Field field) {
  82.         final fieldName annotation = field.getDeclaredAnnotation(fieldName.class);
  83.         return annotation == null ? field.getName() : annotation.name();
  84.     }
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement