lundi 18 décembre 2017

Nullable Annotation not appears in field

I'm using Java Reflections for get model's fields and some fields are Nullables with the @Nullable annotation.

public class Category implements Serializable {

    private String id;
    private String name;
    @Nullable
    private String description;
    @Nullable
    private String urlIcon;
    private ArrayList<String> sounds;

    //Getters and setters
}

And when I try to get the annotations of fields, any field has annotations :( I don't know why.

Can you help me please.

public static <T> String getCreateSentence(Class<T> clazz) {
    StringBuilder sentence = new StringBuilder("CREATE TABLE " + clazz.getSimpleName() + " (");
    //Loop for ever field in Model
    for (Field field : clazz.getDeclaredFields()) {
        if (field.getType().isPrimitive() && !field.getName().equals("serialVersionUID")) {
            if (field.getType().equals(Integer.TYPE)
                    || field.getType().equals(Boolean.TYPE)
                    || field.getType().equals(Byte.TYPE)
                    || field.getType().equals(Long.TYPE)
                    || field.getType().equals(Short.TYPE)) {
                //field.isAnnotation is false ever and field.getDeclaredAnnotations has 0 items ever
                if (field.isAnnotationPresent(Nullable.class)) {
                    sentence.append(field.getName()).append(" INT NULL, ");
                } else {
                    sentence.append(field.getName()).append(" INT NOT NULL, ");
                }
            } else {
                //I evaluate another field types
            }
        }
    }

    return sentence.substring(0, sentence.lastIndexOf(",")) + ");";
}





Aucun commentaire:

Enregistrer un commentaire