4

I'm having issues using a private method inside a private class inside a public class using the reflections api. Here's a simplified code example:

public class Outer {
    private class Inner {
        private Integer value;

        private Inner() {
            this(0);
        }
        private Inner(Integer y) {
            value = y;
        }

        private Integer someMethod(Integer x) {
            return x * x;
        }
    }
}

Again, I want to be able to instantiate an Outer class object then call someMethod from the private Inner class. I've been trying to do this with reflections, but I can't seem to get past 1 level in. Also, the inner class may or may not have a constructor which most code seems to use. The above code is just the framework.

My current code:

Outer outerObject = new Outer();
Constructor<?> constructor = innerNodeClass.getDeclaredConstructor(Outer.class);
constructor.setAccessible(true);
Object innerObject = constructor.newInstance(outerObject);
innerObject.someMethod(5)

I looked up various ways to get to an inner class or private method, but can't find a way to get an outer object to an inner object without using the constructor. I'm only interested in using the private method in the inner class on an element in the outer object.

4
  • It seems like you stopped reading documentation and inventing your own things when it comes to invoking your method. Reflection doesn't give the ability at compile time to do such things. It is runtime only and based in identifying methods via their plain name as string. Commented Sep 27, 2018 at 5:08
  • very confused: "instanciate Outer and call someMehtod", "to get to an inner class", "to get an outer object to an inner object without using constructor", "using private method of inner class on an element in the outer". BTW "inner class may not have a constructor" is wrong, there always will be one for every object: the declared ones or a default one Commented Sep 27, 2018 at 8:10
  • 1
    can't find a way to get an outer object to an inner object without using the constructor - Since Inner is an inner class thus not a static nested class there is no way to get instance of Inner than creating first an Outer instance. Hence you have to create two instances (outer, inner) to call inner.someMethod() and this implies to use the constructors of Outer and Inner. Commented Sep 27, 2018 at 8:21
  • maybe the problem is how to get the Class instance for the inner class, like in Class<?> innerNodeClass = Arrays.stream( Outer.class.getDeclaredClasses() ).filter( c -> c.getSimpleName().equals("Inner") ).findFirst().get(); - Alternative, if you can change the Outer class: add a static method returning this class (or something like that - depends on project/...) - LuCio's comment still applies! Commented Sep 27, 2018 at 9:04

1 Answer 1

3

Once you have an instance of the inner object, you can use reflection to invoke the method itself:

Method someMethod = innerObject.getClass().getDeclaredMethod("someMethod", Integer.TYPE);
someMethod.setAccessible(true);
someMethod.invoke(innerObject, 5);
Sign up to request clarification or add additional context in comments.

1 Comment

note: the argument for the method should be Integer.class not Integer.TYPE - it is declared as Integer (why ever?)

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.