0

I just read about reflection and decided to give it a try, but I seem to run into an error I can not find the cause for.

I got the following code in a class:

String hashType = "md5";
Method method = DigestUtils.class.getDeclaredMethod(hashType + "Hex");
String hash = (String) method.invoke("hello");

This piece of code that should store the hashed string into the variable hash, throws the following error at runtime:

java.lang.NoSuchMethodException: org.apache.commons.codec.digest.DigestUtils.md5Hex()
        at java.lang.Class.getDeclaredMethod(Unknown Source)
        at stringHasher.stringHasher.hashString(stringHasher.java:37)

According to the API documentation the method does exist: https://commons.apache.org/proper/commons-codec/apidocs/org/apache/commons/codec/digest/DigestUtils.html

Beside not understanding what is the cause of this error I also don't understand why I need to cast the returned value by the method to a String as the API states it returns a string (should type safety not be in the hands of the programmer in this case instead of enforced by eclipse?).

5
  • 5
    There are 3 different methods called md5Hex but all of them have a parameter. You are not declaring any parameter in the getDeclaredMethod call so it looks for a method without parameters md5Hex() that doesn't exist. But may I ask why you are using reflection in this case at all? If you want type safety directly calling the method and not using reflection seems a better solution. Commented Aug 13, 2018 at 14:51
  • 1
    As @OHGODSPIDERS said, there are 3 methods called md5Hex. So try this one: DigestUtils.class.getDeclaredMethod(hashType + "Hex", String.class); Commented Aug 13, 2018 at 14:53
  • 1
    I think this is a duplicate of stackoverflow.com/questions/15465407/… Commented Aug 13, 2018 at 14:53
  • I am using it because I would like to try it out and see if I can get it to work. I might use it to reduce code at a later time. How would declare parameters in the getDeclaredMethod call? Commented Aug 13, 2018 at 14:55
  • @BilalELCHAMI thanks for that. Commented Aug 13, 2018 at 14:56

1 Answer 1

3

You should add an argument type as a second argument in getDeclaredMethod. And you should pass something (better null) as a first argument to invoke a static method.

String hashType = "md5";
Method method = DigestUtils.class.getDeclaredMethod(hashType + "Hex", String.class);
String hash = (String) method.invoke(null, "hello");

And for a non-static methods you can do that:

DigestUtils instance = new DigestUtils();
String hash = (String) method.invoke(instance, "hello");
Sign up to request clarification or add additional context in comments.

1 Comment

for static methods you should use null - but argument is just ignored so it is not a big issue and you can use anything. But using any arguments looks like maybe it is target of this method.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.