0

Today, when I deal with code. I meet with this problem. For example:

public void method(Entity entity){
    if(entity.getA()  != 5){
        entity.setA(2);
    }
    if(entity.getB()  != 5){
        entity.setB(2);
    }
    ....// next is other field like C,D,E...
}

My question is that there are so many fields in our object. If I do the same logic in different fields I will input so much code. Is there any idea to deal with this problem? My idea is use Reflection.

My work is to do data-clean. Like the example above if the field is not 5, I will change it to 2. Also I will do this operation to every field.

Sorry my English is not so good, if you can't understand what I say,you could leave a message. Thank you!

XiaoXin
  • 37
  • 5
  • Yes, Reflection is the best option. – Esterlinkof Jul 12 '21 at 07:51
  • If there are so many fields that you don't want to access them manually, wouldn't it be better to store them as an array/map/... and have something like `entity.get(key)` in the first place? – Socowi Jul 12 '21 at 07:53
  • Make a list of objects, then put it in the stream and apply a filter. – Vishwa Ratna Jul 12 '21 at 07:53
  • Does this answer your question? [Copy all values from fields in one class to another through reflection](https://stackoverflow.com/questions/1667854/copy-all-values-from-fields-in-one-class-to-another-through-reflection) – Progman Jul 12 '21 at 07:53
  • what do you mean by `so many`? Do not use reflection - it's not for such a purpose. Just write the method, let it have 100 lines of code if necessary and that's all – m.antkowicz Jul 12 '21 at 07:57
  • My work is to do data-clean. Like the example above if the field is not 5, I will change it to 2. Also I will do this operation to every field. – XiaoXin Jul 12 '21 at 08:03
  • 2
    It sounds a lot like your design is broken – Software Engineer Jul 12 '21 at 08:06
  • design? I can't understand what you point for. Counld you give me some tips? Thank you. – XiaoXin Jul 12 '21 at 08:11

1 Answers1

2

You can use something like this one.

    public void method(Entity entity){
        String [] methodAppendixes = ....
        for (methodAppendix : methodAppendixes){
            Method setMethod = Entity.class.getMethod("set" + methodAppendix);
            Method getMethod = Entity.class.getMethod("get" + methodAppendix, int.class);

            if( setMethod.invoke(entity) != 5){
                getMethod.invoke(entity, 2);
            }
        }
    }

Better option is to put your fields in the Map and loop over them.

    // add the following field and get method to Entity class
    class Entity {
        private Map<String, Integer> fields = new HashMap();
        public Map<String, Integer> getFields(){
            return fields;
        }
    ...
    }

    public void method(Entity entity){
        entity.getFields.keySet().stream()
                .filter(e -> entity.getFields.get(e) != 5)
                .forEach(e -> entity.getFields.put(e, 2));
    }
Esterlinkof
  • 1,444
  • 3
  • 22
  • 27
  • Yes, My idea is to use reflection. – XiaoXin Jul 12 '21 at 08:04
  • You can put your method appendixes at `methodAppendixes` array and move forward. – Esterlinkof Jul 12 '21 at 08:19
  • technically this is a good answer but just think how much drawbacks it will have - when you'll not provide setter, when setter will have another name than field, when you will add a field that should not be updated like this but it will 'automagically' be etc – m.antkowicz Jul 12 '21 at 08:34
  • @m.antkowicz This is a terrible design :D I tried to answer his/her question about reflection method call . Better option is, (as mentioned by others) using java map. – Esterlinkof Jul 12 '21 at 08:45
  • @Esterlinkof , design is bad. But if i use java map, what should I do? Access ervery private filed ? Also, I will do get and set method manually. Thank you for answer. Could you give me some code to show your map idea? – XiaoXin Jul 12 '21 at 08:54
  • @XiaoXin I edited my answer and added Map option. Let me know if you have any question. – Esterlinkof Jul 12 '21 at 09:02
  • @XiaoXin If it works, could you please mark my question as approved? – Esterlinkof Jul 13 '21 at 07:50