0

I have imported the fully qualified name of the class and created an instance of the class. I then proceeded to acquire the private method name of the class:

InvokeCallWebservice invokeCallWebservice = new InvokeCallWebservice();

Method method = null;

try {
     method = invokeCallWebservice.getClass().getDeclaredMethod("Fully Qualified Class Name.getURL", String.class);
} 
catch (SecurityException e) {
     System.out.println(e.getCause());
} 
catch (NoSuchMethodException e) {
     System.out.println(e.getCause());
}

    System.out.println(method.getName());

An exception is thrown because method is null. I am not sure the reason which could be the class exists in a different project and package or because I need to specify the second argument as many times as there are parameters in the method. Can I actually invoke this on a private method?

Here is the stack trace:

java.lang.NoSuchMethodException: InvokeCallWebservice.getURL(java.lang.String)
at java.lang.Class.getDeclaredMethod(Class.java:1937)
at com.geico.debug.Debug.main(Debug.java:39)
6
  • Fully Qualified Class Name.getURL. No, just the name of the method. Commented Apr 14, 2014 at 16:37
  • Also print the stack trace and post it here. Commented Apr 14, 2014 at 16:38
  • You might want to post the method in question as well. Does it have a String parameter? Commented Apr 14, 2014 at 18:11
  • The method in question has 5 Class.String parameters whose definition within the same project but a different package is: private void getURL(String IPNumber, String ClaimNumber, String EventID, String UserEmail, String logId) Commented Apr 14, 2014 at 18:14
  • Then you need to use getDeclaredMethod("getURL", String.class, String.class, String.class, String.class, String.class); to match its parameter types. I've updated my answer. Commented Apr 14, 2014 at 18:15

3 Answers 3

2

You have to get the class first, and with that class, get the method you want to get, only using the method name and parameters.

Class clazz = Class.forName("package.ClassIWant");

Method myMethod = clazz.getDeclaredMethod("getURL", String.class);
Sign up to request clarification or add additional context in comments.

2 Comments

Tried this using the fully qualified name. The class is in the same Project but a different package than where I am setting my Method reference. I got the class from the fully qualified name but am still unable to get the private method.
It works now. I needed to Class clazz = Class.getForName("Class Name"); and then Method m = clazz.getDeclaredMethod("getURL", String.class, String.class, String.class, String.class, String.class) and then finally m.setAccessible(true) & Object o = m.invoke(instance, arguments). I did not have all parameter arguments listed out when trying to get the method name.
2

The Class#getDeclaredMethod(String, Object...) method javadoc states

The name parameter is a String that specifies the simple name of the desired method

The simple name being just the name of the method as it appears in the source code, not qualified with the class it belongs to.

getDeclaredMethod() will throw a NoSuchMethodException if no such (named) method exists. In your case, you just print the cause, but still try to use the method variable even if it wasn't assigned. So it remains null and you get a NPE.


If your method takes 5 String arguments, then you need to use

getDeclaredMethod("getURL", String.class, String.class, String.class, String.class, String.class);

to match all its parameter types.

Comments

1

The correct syntax is:

Method method = ClassName.getClass().getDeclaredMethod(methodName, methodParameters);

Here methodName is the name of the method and methodParameters is the the parameter array. Change it to:

method = invokeCallWebservice.getClass().getDeclaredMethod("methodName", String.class);

Where methodName is your method name.

For more details please read this doc.

1 Comment

Tried this already. The class whose private method I want direct access to through Reflection is in a different package than where I am setting my Method reference. I am still receiving an exception for no such method found.

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.