mercredi 10 juin 2020

Invoking a deprecated/overloaded function using reflection in Java

I have a class called Contract.class which contains the following overloaded functions(among many):

@Deprecated
public static RemoteCall<Contract> deploy(Web3j web3j, Credentials credentials, BigInteger gasPrice, BigInteger gasLimit) {
    return deployRemoteCall(Contract.class, web3j, credentials, gasPrice, gasLimit, BINARY, "");
}

This java class is compiled during the execution of my program so I am using the URLClassLoader to load this class.

Class<?> cls = urlcl.loadClass("<package_name>.Contract");

And then I try to invoke the deploy function like this:

Method deployMethod = cls
                .getMethod("deploy", Web3j.class, Credentials.class, BigInteger.class, BigInteger.class);

RemoteCall<?> deployCall = (RemoteCall<?>) deployMethod
                .invoke(cls, web3j, credentials, BigInteger.valueOf(22000000000L), BigInteger.valueOf(4300000L));

Contract contract = (Contract) deployCall.send();

However, the code throws this error:

java.lang.NoSuchMethodException: <package_name>.Contract.deploy(org.web3j.protocol.Web3j, org.web3j.crypto.Credentials, java.math.BigInteger, java.math.BigInteger

Although when I try to list the functions of the class with the type of its arguments, I do get this following entry:

deploy
arg0 org.web3j.protocol.Web3j
arg1 org.web3j.crypto.Credentials
arg2 java.math.BigInteger
arg3 java.math.BigInteger

Also I tried putting another function in the Contract class, randomFunction with System.out.println("random text"); and when I invoke this randomFunction I do get the intended output.

So what am I missing here? Can this issue be to the fact that I am accessing an overloaded or deprecated function?

EDIT

I tried removing the other overloaded functions and the deprecated annotation on top of this function and I still get the same error. So it is not a problem due to either of them.





Aucun commentaire:

Enregistrer un commentaire