mardi 27 septembre 2022

Use java operator :: dynamically

I have the following implementation:

private SomeWritter someWritter(String someArgument) {
    SomeWritter.Builder builder = SomeWritter.builder(someArguments);
    builder = builder.addColumn("colName1", TargetClass::getArg1)
    builder = builder.addColumn("colName2", TargetClass::getArg2)
    return builder.build();
}

private Builder<T> addColumn(String colName, ToDoubleFunction<T> getter){
   //implementation goes here
}

my issue is that I need to iterate over the addColumns call, something among these lines:

private void SomeWritter(String someArgument) {
    SomeWritter.Builder builder = SomeWritter.builder(someArguments);
    for (Field field : getFilteredFieldsFromClass(TargetClass.class)) {
        builder = builder.addColumn(field.getName(), [SOMEHOW GET THE REF TO GETTER HERE])
    }
    return builder.build();
}

in order to get the refference to the getter, I tryed to do TargetClass.class.getMethod("getArg1", ...);

this works, but I have a Method, not a ToDoubleFunction.

I need to somehow get that ToDoDoubleFunction, programatically, I want to do the same that the TargetClass:: does, dinamically, not harcoded. any ideas ?





Aucun commentaire:

Enregistrer un commentaire