0

I have an object of type java.lang.reflect.Method and i need to set it to the fallowing variable BiConsumer<_notaFiscalServicoGeraServicosMov, Double> metodoSetBC.

Is it possible to obtain the reference from the java.lang.reflect.Method instance?

6
  • is it possible to obtain the reference from the java.lang.reflect.Method instance?
    – F. Souza
    Commented Jan 15, 2019 at 15:25
  • 2
    Do you want your BiConsumer to call the method represented by your Method object? What's the signature of that Method?
    – ernest_k
    Commented Jan 15, 2019 at 15:27
  • Its a setter public void setValueBC(Double valueBC) { this.valueBC= valueBC; }
    – F. Souza
    Commented Jan 15, 2019 at 15:31
  • And you effectively want to call _notaFiscalServicoGeraServicosMovObject.setValueBC(doubleValue)?
    – ernest_k
    Commented Jan 15, 2019 at 15:32
  • that method is an example. I have a list with methods all with the same signature, the user choose the method and assign a value to it. I need to be able to call _notaFiscalServicoGeraServicosMovObject.setValueBC(doubleValue) or any other method that the user choose
    – F. Souza
    Commented Jan 15, 2019 at 15:36

1 Answer 1

2
BiConsumer<_notaFiscalServicoGeraServicosMov, Double> consumer = (instance, param) -> {
    try {
        methodInstance.invoke(instance, new Object[] {param});
    } catch (LotsOfExceptionsHere e) {
        throw new RuntimeException(e);
    }
};

that should do it. There's no other way, really.

2
  • Yup, my bad. Edited the mistake. Commented Jan 15, 2019 at 15:38
  • i've found that answer, but netbeans says: incompatible types: bad return type in lambda expression unexpected return value
    – F. Souza
    Commented Jan 15, 2019 at 15:41

Not the answer you're looking for? Browse other questions tagged or ask your own question.