// Path: onam/src/main/java/com/basilgregory/onam/android/DbUtil.java
// static String getColumnName(Field field){
//     return getColumnName(getMethod("get", field));
// }
// 
// Path: onam/src/main/java/com/basilgregory/onam/android/DbUtil.java
// static String getMappingForeignColumnNameClass(Class aClass){
//     return aClass.getSimpleName().toLowerCase()+"_id";
// }
// 
// Path: onam/src/main/java/com/basilgregory/onam/android/DbUtil.java
// static  Method getMethod (String prefix, Field field) {
//     if (field == null) return null;
//     String name = prefix + field.getName();
//     try {
//         switch (prefix.toLowerCase()) {
//             case "set":
//                 return findSetterMethod(name,field.getDeclaringClass(), field.getType());
//             case "get" :
//                 return findGetterMethod(name,field.getDeclaringClass());
//         }
//     } catch (Exception e) {
//         e.printStackTrace();
//     }
//     L.w("No "+prefix +" method found for "+field.getName());
// 
//     return null;
// }
// 
// Path: onam/src/main/java/com/basilgregory/onam/android/Entity.java
// public static <E extends Entity> E find(Class entityClass,long id){
//     return (E) DBExecutor.getInstance().findById(entityClass,id);
// 
// }


import android.database.Cursor;
import com.basilgregory.onam.annotations.Table;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.List;
import static com.basilgregory.onam.android.DbUtil.getColumnName;
import static com.basilgregory.onam.android.DbUtil.getMappingForeignColumnNameClass;
import static com.basilgregory.onam.android.DbUtil.getMethod;
import static com.basilgregory.onam.android.Entity.find;

                String firstParameterType = field.getType().getSimpleName().toUpperCase();
                entity = entity == null ? cls.newInstance() : entity;
                Method setter= getMethod("set", field);
                if (setter == null || setter.getParameterTypes()[0].getAnnotation(Table.class) != null) continue;
                Object valueFromDatabase = FieldType.getValueFromCursor(cursor,
                        firstParameterType,columnName);
                setter.invoke(entity,valueFromDatabase);
            } catch (IllegalStateException e) {
                L.vi("Field ("+field.getName()
                        +") of type ("+field.getType().getSimpleName().toUpperCase()
                        +") is not a match with the database types (possibly a LIST)");
                L.vi(e.getLocalizedMessage());
            }catch (Exception e) {
                e.printStackTrace();
            }
        }
        try {
            entity.setId((Long) FieldType.getValueFromCursor(cursor,
                    FieldType.LONG.realName().toUpperCase(), DB.PRIMARY_KEY_ID));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return entity;
    }

    static Entity convertToEntity(Entity entity,Cursor cursor){
        if (cursor == null || cursor.getCount() < 1) return null;
        cursor.moveToFirst();
        Field[] fields = entity.getClass().getDeclaredFields();
// Path: onam/src/main/java/com/basilgregory/onam/android/DbUtil.java
// static String getColumnName(Field field){
//     return getColumnName(getMethod("get", field));
// }
// 
// Path: onam/src/main/java/com/basilgregory/onam/android/DbUtil.java
// static String getMappingForeignColumnNameClass(Class aClass){
//     return aClass.getSimpleName().toLowerCase()+"_id";
// }
// 
// Path: onam/src/main/java/com/basilgregory/onam/android/DbUtil.java
// static  Method getMethod (String prefix, Field field) {
//     if (field == null) return null;
//     String name = prefix + field.getName();
//     try {
//         switch (prefix.toLowerCase()) {
//             case "set":
//                 return findSetterMethod(name,field.getDeclaringClass(), field.getType());
//             case "get" :
//                 return findGetterMethod(name,field.getDeclaringClass());
//         }
//     } catch (Exception e) {
//         e.printStackTrace();
//     }
//     L.w("No "+prefix +" method found for "+field.getName());
// 
//     return null;
// }
// 
// Path: onam/src/main/java/com/basilgregory/onam/android/Entity.java
// public static <E extends Entity> E find(Class entityClass,long id){
//     return (E) DBExecutor.getInstance().findById(entityClass,id);
// 
// }


// Path: onam/src/main/java/com/basilgregory/onam/android/EntityBuilder.java
import android.database.Cursor;
import com.basilgregory.onam.annotations.Table;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.List;
import static com.basilgregory.onam.android.DbUtil.getColumnName;
import static com.basilgregory.onam.android.DbUtil.getMappingForeignColumnNameClass;
import static com.basilgregory.onam.android.DbUtil.getMethod;
import static com.basilgregory.onam.android.Entity.find;


                String firstParameterType = field.getType().getSimpleName().toUpperCase();
                entity = entity == null ? cls.newInstance() : entity;
                Method setter= getMethod("set", field);
                if (setter == null || setter.getParameterTypes()[0].getAnnotation(Table.class) != null) continue;
                Object valueFromDatabase = FieldType.getValueFromCursor(cursor,
                        firstParameterType,columnName);
                setter.invoke(entity,valueFromDatabase);
            } catch (IllegalStateException e) {
                L.vi("Field ("+field.getName()
                        +") of type ("+field.getType().getSimpleName().toUpperCase()
                        +") is not a match with the database types (possibly a LIST)");
                L.vi(e.getLocalizedMessage());
            }catch (Exception e) {
                e.printStackTrace();
            }
        }
        try {
            entity.setId((Long) FieldType.getValueFromCursor(cursor,
                    FieldType.LONG.realName().toUpperCase(), DB.PRIMARY_KEY_ID));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return entity;
    }

    static Entity convertToEntity(Entity entity,Cursor cursor){
        if (cursor == null || cursor.getCount() < 1) return null;
        cursor.moveToFirst();
        Field[] fields = entity.getClass().getDeclaredFields();
        return convertToEntity((Class<Entity>) entity.getClass(),cursor,fields,entity);