jeudi 31 octobre 2019

Accessing field in Java based on condition

I have to write a utility method that can get the particular variable value from an Object, on evaluating some conditions.

Below are the requirements.

  1. if "name" is coming as type call "getName" to get the Value.
  2. If "subtype" is coming as type call "getSubType" to get the Subtype Value.

Current Solution:

       public Object getFieldValue(String type, IDTO dto){
        Method method = dto.getClass().getMethod("get"+createMethodName(type));
        Object returnedObject = method.invoke(dto, null);
        return returnedObject;
     }

However, the operations are quite heavy and it is giving performance issues. Is there any alternative that can be chosen or any better way to do this.

Other points:

  1. Interface IDTO has all the getter setters declared only. No fields declared in that.




Aucun commentaire:

Enregistrer un commentaire