3

I am trying to test my code though knowing using reflection is not a good way of testing. I have a outer class as public having private inner class with a public method as below,

public class Outer {

    private class Inner {
        private int var = 1;

        public Inner(int a) {
            System.out.println("___");
        }

        public void test(int a) {
            System.out.println("Hey");
        }
    }
}

My main java class looks like below

main() {
    Outer b = new Outer();
    System.out.println(b);
    Class<?> innerClass = Class.forName("car.Outer$Inner");

    Constructor<?> constructor = innerClass.getDeclaredConstructor(Outer.class, 1);

    constructor.setAccessible(true);
    Object c = constructor.newInstance(b,b);

    Method method = c.getClass().getDeclaredMethod("test");
    method.setAccessible(true);
    method.invoke(c, 1);
}

This is throwing

Exception in thread "main" java.lang.NoSuchMethodException: car.Outer$Inner.test() at java.lang.Class.getDeclaredMethod(Class.java:2130) at car.A.main(A.java:36)

How to invoke inner class method taking parameter using reflection?

5
  • can you check your method is in the class or not ? Commented Sep 27, 2018 at 6:46
  • That is inside the class only. The same works for the methods with no parameter. Commented Sep 27, 2018 at 6:48
  • 1
    You need to supply the argument class(es) in the call to getDeclaredMethod(). In this case, c.getClass().getDeclaredMethod("test", int.class) should work. Commented Sep 27, 2018 at 6:49
  • You are the life saver. Thanks Man:-) Commented Sep 27, 2018 at 6:56
  • Looks like such a question was already asked today: Java reflection: access private method inside inner class In this case OP got a step further. Commented Sep 27, 2018 at 7:26

1 Answer 1

1

You need to supply the argument class(es) in the call to getDeclaredMethod(). When you call getDeclaredMethod(), the first argument is the name of the method you want looked up and any remaining arguments are the classes of the argument(s) to the method you want. This is how getDeclaredMethod() distinguishes between overloaded method names to get one particular method. Since you have supplied no additional arguments, getDeclaredMethod() is looking for a method named test that takes no arguments. You're getting an exception because you have no such method in class Outer$Inner. The only test method you do have takes an int parameter`, so the following should do what you want:

Method method = c.getClass().getDeclaredMethod("test", int.class);

Here, int.class is the Class object corresponding to the primitive argument type int.

Sign up to request clarification or add additional context in comments.

Comments

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.