samedi 19 mars 2016

how can I make the following code better?

I want to make the following code better, but cannot get a good idea. Is there any way to solve this?

I just create a Android project and use greenDAO greendao to create tables by Class.

for (Field field : fields) {
    fieldName = field.getName();
    // we don't need this.
    if ("serialVersionUID".equals(fieldName)) {
        continue;
    }
    type = field.getType();
    // primary key, just auto increment.
    if ("id".equals(fieldName)) {
        entity.addIdProperty().autoincrement();
        continue;
    }
    // other fields
    /*
     * this is the problem what I want to solve.
     * I thought it's too bad to read and have a bad looking.
     */
    if (type.equals(String.class)) {
        entity.addStringProperty(fieldName);
    }else if (type.equals(Integer.class)) {
        entity.addIntProperty(fieldName);
    }else if (type.equals(Double.class)) {
        entity.addDoubleProperty(fieldName);
    }else if (type.equals(Float.class)) {
        entity.addFloatProperty(fieldName);
    }else if (type.equals(Long.class)) {
        entity.addLongProperty(fieldName);
    }else if (type.equals(Byte.class)) {
        entity.addByteProperty(fieldName);
    }else if (type.equals(Short.class)) {
        entity.addShortProperty(fieldName);
    }else if (type.equals(Boolean.class)) {
        entity.addBooleanProperty(fieldName);
    }else if (type.equals(Character.class)) {
        entity.addStringProperty(fieldName);
    }else if (type.equals(Date.class)) {
        entity.addDateProperty(fieldName);
    }
}





Aucun commentaire:

Enregistrer un commentaire