private void setContentValues(T entity, ContentValues cv, String type) throws IllegalAccessException { Field[] fields = this.clazz.getDeclaredFields(); for (Field field : fields) { if (!field.isAnnotationPresent(Column.class)) { continue; } Column column = field.getAnnotation(Column.class); // Log.d("BaseDAO",column.name()+" : "+column.type()); field.setAccessible(true); Object fieldValue = field.get(entity); if (fieldValue == null) continue; if (("create".equals(type)) && (field.isAnnotationPresent(Id.class)) && fieldValue.toString().equals("0")) { continue; } cv.put(column.name(), String.valueOf(fieldValue)); } }
public BaseDAO(SQLiteOpenHelper dbHelper) { this.dbHelper = dbHelper; this.clazz = ((Class) ((java.lang.reflect.ParameterizedType) getClass().getGenericSuperclass()) .getActualTypeArguments()[0]); Log.d("BaseDAO", "clazz:" + this.clazz); if (this.clazz.isAnnotationPresent(Table.class)) { Table table = this.clazz.getAnnotation(Table.class); this.tableName = table.name(); Log.d("BaseDAO", "tableName:" + this.tableName); } Field[] fields = this.clazz.getDeclaredFields(); for (Field field : fields) if (field.isAnnotationPresent(Id.class)) { Log.d("BaseDAO", "### get idColumn"); Column column = field.getAnnotation(Column.class); this.idColumn = column.name(); break; } }
private void getListFromCursor(List<T> list, Cursor cursor) throws IllegalAccessException, InstantiationException { while (cursor.moveToNext()) { Object entity = this.clazz.newInstance(); for (Field field : this.clazz.getDeclaredFields()) { Column column = null; if (field.isAnnotationPresent(Column.class)) { column = field.getAnnotation(Column.class); field.setAccessible(true); Class<?> fieldType = field.getType(); if ((Integer.TYPE == fieldType) || (Integer.class == fieldType)) { int fieldValue = cursor.getInt(cursor.getColumnIndex(column.name())); field.set(entity, Integer.valueOf(fieldValue)); } else if (String.class == fieldType) { String fieldValue = cursor.getString(cursor.getColumnIndex(column.name())); field.set(entity, fieldValue); } else if ((Long.TYPE == fieldType) || (Long.class == fieldType)) { long fieldValue = cursor.getLong(cursor.getColumnIndex(column.name())); field.set(entity, Long.valueOf(fieldValue)); } else if ((Float.TYPE == fieldType) || (Float.class == fieldType)) { float fieldValue = cursor.getFloat(cursor.getColumnIndex(column.name())); field.set(entity, Float.valueOf(fieldValue)); } else if ((Short.TYPE == fieldType) || (Short.class == fieldType)) { short fieldValue = cursor.getShort(cursor.getColumnIndex(column.name())); field.set(entity, Short.valueOf(fieldValue)); } else if ((Double.TYPE == fieldType) || (Double.class == fieldType)) { double fieldValue = cursor.getDouble(cursor.getColumnIndex(column.name())); field.set(entity, Double.valueOf(fieldValue)); } else if ((Boolean.TYPE == fieldType) || (Boolean.class == fieldType)) { String fieldValue = cursor.getString(cursor.getColumnIndex(column.name())); field.set(entity, Boolean.valueOf(fieldValue)); } // else if (MsgType.class == fieldType) {// add for event // String fieldValue = cursor.getString(cursor.getColumnIndex(column.name())); // field.set(entity, MsgType.valueOf(fieldValue)); // // } // else if (MsgState.class == fieldType) {// add for event // String fieldValue = cursor.getString(cursor.getColumnIndex(column.name())); // field.set(entity, MsgState.valueOf(fieldValue)); // } // else if (EventState.class == fieldType) {// add for event // String fieldValue = cursor.getString(cursor.getColumnIndex(column.name())); // field.set(entity, EventState.valueOf(fieldValue)); // } // // else if (SendRecv.class == fieldType) {// add for event // String fieldValue = cursor.getString(cursor.getColumnIndex(column.name())); // field.set(entity, SendRecv.valueOf(fieldValue)); // } // // else if (CityType.class == fieldType) {// add for event // String fieldValue = cursor.getString(cursor.getColumnIndex(column.name())); // field.set(entity, CityType.valueOf(fieldValue)); // } // // else if (BoardType.class == fieldType) {// add for BBS // String fieldValue = cursor.getString(cursor.getColumnIndex(column.name())); // field.set(entity, BoardType.valueOf(fieldValue)); // } else if (Blob.class == fieldType) { byte[] fieldValue = cursor.getBlob(cursor.getColumnIndex(column.name())); field.set(entity, fieldValue); } else if (Character.TYPE == fieldType) { String fieldValue = cursor.getString(cursor.getColumnIndex(column.name())); if ((fieldValue != null) && (fieldValue.length() > 0)) { field.set(entity, Character.valueOf(fieldValue.charAt(0))); } } } } Log.d("BaseDAO", "----------------------"); list.add((T) entity); } }