vendredi 8 janvier 2021

JAVA Reflection invoke method with casting argument failed

I want to call method of a reflection of okhttp3.Request. The method to be called is writeTo(okio.BufferedSink).

private String getBody(final Object body){
    try {
        final BufferedSink buffer = Okio.buffer((Sink) new Buffer());

        Method methodWriteTo = findMethod(body.getClass(), "writeTo");

        # Not working 
        methodWriteTo.invoke(body, BufferedSink.class.cast(buffer));

        # Not working 
        methodWriteTo.invoke(body, (BufferedSink)buffer);

        String body = buffer.buffer().readUtf8();
        return body;
    } catch (Exception e) {
       ...
    }

    return "";
}

I always got http3.RequestBody$Companion$toRequestBody$1.writeTo argument 1 has type okio.BufferedSink, got okio.RealBufferedSink.

The okio.BufferedSink is interface class, so how can invoke this method with okio.RealBufferedSink ?





Aucun commentaire:

Enregistrer un commentaire