in core/src/main/java/com/amplifyframework/core/model/ModelSchema.java [110:181]
public static ModelSchema fromModelClass(@NonNull Class<? extends Model> clazz) throws AmplifyException {
try {
final List<Field> classFields = FieldFinder.findModelFieldsIn(clazz);
final TreeMap<String, ModelField> fields = new TreeMap<>();
final TreeMap<String, ModelAssociation> associations = new TreeMap<>();
final TreeMap<String, ModelIndex> indexes = new TreeMap<>();
final List<AuthRule> authRules = new ArrayList<>();
// Set the model name and plural name (null if not provided)
ModelConfig modelConfig = clazz.getAnnotation(ModelConfig.class);
final String modelName = clazz.getSimpleName();
final String modelPluralName = modelConfig != null && !modelConfig.pluralName().isEmpty()
? modelConfig.pluralName()
: null;
final String listPluralName = modelConfig != null && !modelConfig.listPluralName().isEmpty()
? modelConfig.listPluralName()
: null;
final String syncPluralName = modelConfig != null && !modelConfig.syncPluralName().isEmpty()
? modelConfig.syncPluralName()
: null;
if (modelConfig != null) {
for (com.amplifyframework.core.model.annotations.AuthRule ruleAnnotation : modelConfig.authRules()) {
authRules.add(new AuthRule(ruleAnnotation));
}
}
for (Annotation annotation : clazz.getAnnotations()) {
if (annotation.annotationType().isAssignableFrom(Indexes.class)) {
Indexes indexesAnnotation = (Indexes) annotation;
for (Index indexAnnotation : indexesAnnotation.value()) {
ModelIndex modelIndex = createModelIndex(indexAnnotation);
indexes.put(modelIndex.getIndexName(), modelIndex);
}
} else if (annotation.annotationType().isAssignableFrom(Index.class)) {
ModelIndex modelIndex = createModelIndex((Index) annotation);
indexes.put(modelIndex.getIndexName(), modelIndex);
}
}
for (Field field : classFields) {
final ModelField modelField = createModelField(field);
if (modelField != null) {
fields.put(field.getName(), modelField);
}
final ModelAssociation modelAssociation = createModelAssociation(field);
if (modelAssociation != null) {
associations.put(field.getName(), modelAssociation);
}
}
return ModelSchema.builder()
.name(modelName)
.pluralName(modelPluralName)
.listPluralName(listPluralName)
.syncPluralName(syncPluralName)
.authRules(authRules)
.fields(fields)
.associations(associations)
.indexes(indexes)
.modelClass(clazz)
.build();
} catch (Exception exception) {
throw new AmplifyException(
"Error in constructing a ModelSchema.",
exception,
AmplifyException.TODO_RECOVERY_SUGGESTION
);
}
}