1

I have the following problem in Java and I am not sure if exist a solution (maybe using reflection but I am absolutly not sure).

I have a method like this:

@Override
public int insertTrend(String trendTableName, Map<String, Object> jsonTrendInfo) {

    for (Map.Entry<String,Object> entry : jsonTrendInfo.entrySet())  
        System.out.println("Key = " + entry.getKey() + 
                         ", Value = " + entry.getValue());

    return 0;
}

as you can see I am iterating over a Map extracting the key and the related value.

Basically, at the moment, I have an output like this:

Key = Sensor0001, Value = 39.2
Key = Sensor0002, Value = 32.5
Key = Sensor0003, Value = 21.7
Key = Sensor0004, Value = 47.3
Key = Sensor0005, Value = 33.4
..............................
..............................
..............................

Basically the key will contains the name of a sensor and the value contains the value retrieved by this sensor.

These sensor are a lot (in the hundreds). In my code I have a DTO class containing these sensors as fields, the DTO class will contain something like:

private Double sensor001;
private Double sensor002;
private Double sensor003;
private Double sensor004;
private Double sensor005;
.........................
.........................
.........................

my DTO class will contain also the related getter and setters method.

For example:

public void setSensor001(Double sensor001) {
    sensor001 = sensor001;
}

I know that to initialize all the fileds of my DTO object I can use a parametrized constructor but I am asking if there is a way to automatically build and call the setter method of the field related to the current element in my Map.

For example:

Iterating on the map the first element have key=Sensor0001 and value=39.2

So the setSensor001() method will be call on my DTO object taking the value 39.2 as parameter.

In this way iterating on my Map I will fully initizialize my DTO object avoiding to write a huge constructor.

Can it be done in some way? In case it is possible is it a smart solution or there are some drawbacks using this approach? (maybe in perfromance)

ivan_pozdeev
  • 33,874
  • 19
  • 107
  • 152
AndreaNobili
  • 40,955
  • 107
  • 324
  • 596
  • If I were you, I would just use Jackson and run something like `DtoClass converted = objectMapper.convertValue(jsonTrendInfo);` as this already hints that you're doing JSON. I'm sure there are plenty of alternative tools (like mapstruct, I guess, or some other bean utilities) for this kind of job. – ernest_k Jun 13 '20 at 15:52
  • final ObjectMapper mapper = new ObjectMapper(); // jackson's objectmapper final MyPojo pojo = mapper.convertValue(map, MyPojo.class); https://stackoverflow.com/questions/16428817/convert-a-mapstring-string-to-a-pojo – subinksoman Jun 13 '20 at 15:59
  • @ernest_k you save my day, if you put it as response I will accept it – AndreaNobili Jun 13 '20 at 16:37

1 Answers1

1

Try to use reflection, like this:

@Override
public int insertTrend(String trendTableName, Map<String, Object> jsonTrendInfo) {

    for (Map.Entry<String,Object> entry : jsonTrendInfo.entrySet())
        invoke(entry.getValue(), entry.getKey());

    return 0;
}
    public void invoke(Object obj, String name, Double value) throws Exception {
        name = "setS" + name.substring(1);
        Method method = obj.getClass().getDeclaredMethod(name, Double.class);
        method.setAccessible(true);
        method.invoke(obj, value);
    }
0xh3xa
  • 4,801
  • 2
  • 14
  • 28