addIntoAssociationModelCollection(className, fieldTypeClass.getName(), fieldTypeClass.getName(), Const.Model.ONE_TO_ONE); } else if (action == GET_ASSOCIATION_INFO_ACTION) { addIntoAssociationInfoCollection(className, fieldTypeClass.getName(), fieldTypeClass.getName(), field, reverseField, Const.Model.ONE_TO_ONE); else if (isCollection(reverseFieldTypeClass)) { String genericTypeName = getGenericTypeName(reverseField); if (className.equals(genericTypeName)) { if (action == GET_ASSOCIATIONS_ACTION) { addIntoAssociationModelCollection(className, fieldTypeClass.getName(), className, Const.Model.MANY_TO_ONE); } else if (action == GET_ASSOCIATION_INFO_ACTION) { addIntoAssociationInfoCollection(className, fieldTypeClass.getName(), className, field, reverseField, Const.Model.MANY_TO_ONE); addIntoAssociationModelCollection(className, fieldTypeClass.getName(), fieldTypeClass.getName(), Const.Model.ONE_TO_ONE); } else if (action == GET_ASSOCIATION_INFO_ACTION) { addIntoAssociationInfoCollection(className, fieldTypeClass.getName(), fieldTypeClass.getName(), field, null, Const.Model.ONE_TO_ONE);
/** * Get the association info model by the class name. * * @param className * The class name to introspection. * @return Collection of association info. */ protected Collection<AssociationsInfo> getAssociationInfo(String className) { if (mAssociationInfos == null) { mAssociationInfos = new HashSet<AssociationsInfo>(); } mAssociationInfos.clear(); analyzeClassFields(className, GET_ASSOCIATION_INFO_ACTION); return mAssociationInfos; }
private void recursiveSupportedGenericFields(Class<?> clazz, List<Field> supportedGenericFields) { if (clazz == LitePalSupport.class || clazz == Object.class) { return; } Field[] fields = clazz.getDeclaredFields(); if (fields != null && fields.length > 0) { for (Field field : fields) { Column annotation = field.getAnnotation(Column.class); if (annotation != null && annotation.ignore()) { continue; } int modifiers = field.getModifiers(); if (!Modifier.isStatic(modifiers) && isCollection(field.getType())) { String genericTypeName = getGenericTypeName(field); if (BaseUtility.isGenericTypeSupported(genericTypeName) || clazz.getName().equalsIgnoreCase(genericTypeName)) { supportedGenericFields.add(field); } } } } recursiveSupportedGenericFields(clazz.getSuperclass(), supportedGenericFields); }
/** * Introspection of the passed in class. Analyze the fields of current class * and find out the associations of it. * * @param className * The class name to introspection. * @param action * Between {@link org.litepal.LitePalBase#GET_ASSOCIATIONS_ACTION} and * {@link org.litepal.LitePalBase#GET_ASSOCIATION_INFO_ACTION} */ private void analyzeClassFields(String className, int action) { try { Class<?> dynamicClass = Class.forName(className); Field[] fields = dynamicClass.getDeclaredFields(); for (Field field : fields) { if (isNonPrimitive(field)) { Column annotation = field.getAnnotation(Column.class); if (annotation != null && annotation.ignore()) { continue; } oneToAnyConditions(className, field, action); manyToAnyConditions(className, field, action); } } } catch (ClassNotFoundException ex) { ex.printStackTrace(); throw new DatabaseGenerateException(DatabaseGenerateException.CLASS_NOT_FOUND + className); } }
/** * This method is used to get the table model by the class name passed * in. The principle to generate table model is that each field in the class * with non-static modifier and has a type among int/Integer, long/Long, * short/Short, float/Float, double/Double, char/Character, boolean/Boolean * or String, would generate a column with same name as corresponding field. * If users don't want some of the fields map a column, declare an ignore * annotation with {@link Column#ignore()}. * * @param className * The full name of the class to map in database. * @return A table model with table name, class name and the map of column * name and column type. */ protected TableModel getTableModel(String className) { String tableName = DBUtility.getTableNameByClassName(className); TableModel tableModel = new TableModel(); tableModel.setTableName(tableName); tableModel.setClassName(className); List<Field> supportedFields = getSupportedFields(className); for (Field field : supportedFields) { ColumnModel columnModel = convertFieldToColumnModel(field); tableModel.addColumnModel(columnModel); } return tableModel; }
/** * Introspection of the passed in class. Analyze the fields of current class * and find out the associations of it. * * @param className * The class name to introspection. * @param action * Between {@link org.litepal.LitePalBase#GET_ASSOCIATIONS_ACTION} and * {@link org.litepal.LitePalBase#GET_ASSOCIATION_INFO_ACTION} */ private void analyzeClassFields(String className, int action) { try { Class<?> dynamicClass = Class.forName(className); Field[] fields = dynamicClass.getDeclaredFields(); for (Field field : fields) { if (isPrivateAndNonPrimitive(field)) { oneToAnyConditions(className, field, action); manyToAnyConditions(className, field, action); } } } catch (ClassNotFoundException ex) { ex.printStackTrace(); throw new DatabaseGenerateException(DatabaseGenerateException.CLASS_NOT_FOUND + className); } }
/** * This method is used to get the table model by the class name passed * in. The principle to generate table model is that each field in the class * with non-static modifier and has a type among int/Integer, long/Long, * short/Short, float/Float, double/Double, char/Character, boolean/Boolean * or String, would generate a column with same name as corresponding field. * If users don't want some of the fields map a column, declare an ignore * annotation with {@link Column#ignore()}. * * @param className * The full name of the class to map in database. * @return A table model with table name, class name and the map of column * name and column type. */ protected TableModel getTableModel(String className) { String tableName = DBUtility.getTableNameByClassName(className); TableModel tableModel = new TableModel(); tableModel.setTableName(tableName); tableModel.setClassName(className); List<Field> supportedFields = getSupportedFields(className); for (Field field : supportedFields) { ColumnModel columnModel = convertFieldToColumnModel(field); tableModel.addColumnModel(columnModel); } return tableModel; }
if (isCollection(field.getType())) { String genericTypeName = getGenericTypeName(field); addIntoAssociationModelCollection(className, genericTypeName, genericTypeName, Const.Model.MANY_TO_ONE); } else if (action == GET_ASSOCIATION_INFO_ACTION) { addIntoAssociationInfoCollection(className, genericTypeName, genericTypeName, field, reverseField, Const.Model.MANY_TO_ONE); else if (isCollection(reverseFieldTypeClass)) { String reverseGenericTypeName = getGenericTypeName(reverseField); if (className.equals(reverseGenericTypeName)) { if (action == GET_ASSOCIATIONS_ACTION) { mGenericModels.add(genericModel); } else { addIntoAssociationModelCollection(className, genericTypeName, null, Const.Model.MANY_TO_MANY); addIntoAssociationInfoCollection(className, genericTypeName, null, field, reverseField, Const.Model.MANY_TO_MANY); addIntoAssociationModelCollection(className, genericTypeName, genericTypeName, Const.Model.MANY_TO_ONE); } else if (action == GET_ASSOCIATION_INFO_ACTION) { addIntoAssociationInfoCollection(className, genericTypeName, genericTypeName, field, null, Const.Model.MANY_TO_ONE); genericModel.setTableName(DBUtility.getGenericTableName(className, field.getName()));
/** * This method is used to get association models depends on the given class * name list. * * @param classNames * The names of the classes that want to get their associations. * @return Collection of association models. */ protected Collection<AssociationsModel> getAssociations(List<String> classNames) { if (mAssociationModels == null) { mAssociationModels = new HashSet<AssociationsModel>(); } if (mGenericModels == null) { mGenericModels = new HashSet<GenericModel>(); } mAssociationModels.clear(); mGenericModels.clear(); for (String className : classNames) { analyzeClassFields(className, GET_ASSOCIATIONS_ACTION); } return mAssociationModels; }
if (isCollection(field.getType())) { String genericTypeName = getGenericTypeName(field); addIntoAssociationModelCollection(className, genericTypeName, genericTypeName, Const.Model.MANY_TO_ONE); } else if (action == GET_ASSOCIATION_INFO_ACTION) { addIntoAssociationInfoCollection(className, genericTypeName, genericTypeName, field, reverseField, Const.Model.MANY_TO_ONE); else if (isCollection(reverseFieldTypeClass)) { String reverseGenericTypeName = getGenericTypeName(reverseField); if (className.equals(reverseGenericTypeName)) { if (action == GET_ASSOCIATIONS_ACTION) { addIntoAssociationModelCollection(className, genericTypeName, null, Const.Model.MANY_TO_MANY); } else if (action == GET_ASSOCIATION_INFO_ACTION) { addIntoAssociationInfoCollection(className, genericTypeName, null, field, reverseField, Const.Model.MANY_TO_MANY); addIntoAssociationModelCollection(className, genericTypeName, genericTypeName, Const.Model.MANY_TO_ONE); } else if (action == GET_ASSOCIATION_INFO_ACTION) { addIntoAssociationInfoCollection(className, genericTypeName, genericTypeName, field, null, Const.Model.MANY_TO_ONE);
/** * Get the association info model by the class name. * * @param className * The class name to introspection. * @return Collection of association info. */ protected Collection<AssociationsInfo> getAssociationInfo(String className) { if (mAssociationInfos == null) { mAssociationInfos = new HashSet<AssociationsInfo>(); } mAssociationInfos.clear(); analyzeClassFields(className, GET_ASSOCIATION_INFO_ACTION); return mAssociationInfos; }
addIntoAssociationModelCollection(className, fieldTypeClass.getName(), fieldTypeClass.getName(), Const.Model.ONE_TO_ONE); } else if (action == GET_ASSOCIATION_INFO_ACTION) { addIntoAssociationInfoCollection(className, fieldTypeClass.getName(), fieldTypeClass.getName(), field, reverseField, Const.Model.ONE_TO_ONE); else if (isCollection(reverseFieldTypeClass)) { String genericTypeName = getGenericTypeName(reverseField); if (className.equals(genericTypeName)) { if (action == GET_ASSOCIATIONS_ACTION) { addIntoAssociationModelCollection(className, fieldTypeClass.getName(), className, Const.Model.MANY_TO_ONE); } else if (action == GET_ASSOCIATION_INFO_ACTION) { addIntoAssociationInfoCollection(className, fieldTypeClass.getName(), className, field, reverseField, Const.Model.MANY_TO_ONE); addIntoAssociationModelCollection(className, fieldTypeClass.getName(), fieldTypeClass.getName(), Const.Model.ONE_TO_ONE); } else if (action == GET_ASSOCIATION_INFO_ACTION) { addIntoAssociationInfoCollection(className, fieldTypeClass.getName(), fieldTypeClass.getName(), field, null, Const.Model.ONE_TO_ONE);
/** * This method is used to get association models depends on the given class * name list. * * @param classNames * The names of the classes that want to get their associations. * @return Collection of association models. */ protected Collection<AssociationsModel> getAssociations(List<String> classNames) { if (mAssociationModels == null) { mAssociationModels = new HashSet<AssociationsModel>(); } mAssociationModels.clear(); for (String className : classNames) { analyzeClassFields(className, GET_ASSOCIATIONS_ACTION); } return mAssociationModels; }