package com.test.myapplication.model.validator;

import android.support.annotation.NonNull;

import java.lang.reflect.Field;

/**
 * Created by Indigo on 3/22/17.
 */

public class ModelValidator {

    private Object model;

    public ModelValidator(@NonNull Object model)  {
        this.model = model;
    }

    public void validate() throws IllegalArgumentException {

        if (model == null) {
            throw new IllegalArgumentException("Model cannot be null");
        }

        final Field[] modelFields = model.getClass().getFields();

        for (Field modelField : modelFields) {

            modelField.setAccessible(true);

            if (modelField.isAnnotationPresent(IsDefined.class)) {

                try {
                    Object modelAttribute = modelField.get(model);

                    if (modelAttribute == null) {
                        throw new IllegalArgumentException(modelField + " is required");
                    }
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
            }

        }
    }

}

@@ -656,18 +656,130 @@
     
-if (modelF
+validateIsDefinedField(modelField);%0A        %7D%0A    %7D%0A%0A    private void validateIsDefinedField(Field field) %7B%0A%0A        if (f
 ield
@@ -824,38 +824,8 @@
  %7B%0A%0A
-                try %7B%0A        
     
@@ -860,29 +860,12 @@
 e = 
-modelField.get(model)
+null
 ;%0A%0A 
@@ -875,24 +875,30 @@
         
+try %7B%0A
         
 if (mode
@@ -885,28 +885,32 @@
 y %7B%0A        
-if (
+        
 modelAttribu
@@ -917,60 +917,56 @@
 te =
-= null) %7B%0A                        throw new 
+ field.get(model);%0A            %7D catch (
 IllegalA
 rgum
@@ -953,39 +953,37 @@
  catch (IllegalA
-rgument
+ccess
 Exception(modelF
@@ -979,65 +979,66 @@
 tion
-(modelField + %22 is required%22);%0A             
+ e) %7B%0A                e.printStackTrace();%0A
        
-%7D%0A
     
+ %7D%0A%0A
     
@@ -1049,103 +1049,113 @@
     
-%7D catch (IllegalAccessException e) %7B%0A                    e.printStackTrace();%0A                %7D
+if (modelAttribute == null) %7B%0A                throw new IllegalArgumentException(field + %22 is required%22);
 %0A   
@@ -1182,11 +1182,10 @@
 %7D%0A    %7D%0A
-%0A
 %7D%0A

Refactor validator