/** * Convert a field instance into A ColumnModel instance. ColumnModel can provide information when * creating table. * * @param field A supported field to map into column. * @return ColumnModel instance contains column information. */ private ColumnModel convertFieldToColumnModel(Field field) { String columnType = null; String fieldType = field.getType().getName(); for (OrmChange ormChange : typeChangeRules) { columnType = ormChange.object2Relation(fieldType); if (columnType != null) { break; } } boolean nullable = true; boolean unique = false; String defaultValue = ""; Column annotation = field.getAnnotation(Column.class); if (annotation != null) { nullable = annotation.nullable(); unique = annotation.unique(); defaultValue = annotation.defaultValue(); } ColumnModel columnModel = new ColumnModel(); columnModel.setColumnName(field.getName()); columnModel.setColumnType(columnType); columnModel.setIsNullable(nullable); columnModel.setIsUnique(unique); columnModel.setDefaultValue(defaultValue); return columnModel; }
/** * Find all the fields in the class. But not each field is supported to add a column to the table. * Only the basic data types and String are supported. This method will intercept all the types * which are not supported and return a new list of supported fields. * * @param className The full name of the class. * @return A list of supported fields */ protected List<Field> getSupportedFields(String className) { List<Field> fieldList = classFieldsMap.get(className); if (fieldList == null) { List<Field> supportedFields = new ArrayList<Field>(); Class<?> dynamicClass; try { dynamicClass = Class.forName(className); } catch (ClassNotFoundException e) { throw new DatabaseGenerateException(DatabaseGenerateException.CLASS_NOT_FOUND + className); } Field[] fields = dynamicClass.getDeclaredFields(); for (Field field : fields) { Column annotation = field.getAnnotation(Column.class); if (annotation != null && annotation.ignore()) { continue; } int modifiers = field.getModifiers(); if (!Modifier.isStatic(modifiers)) { Class<?> fieldTypeClass = field.getType(); String fieldType = fieldTypeClass.getName(); if (BaseUtility.isFieldTypeSupported(fieldType)) { supportedFields.add(field); } } } classFieldsMap.put(className, supportedFields); return supportedFields; } return fieldList; }