I’m trying to use reflection to invoke a method whose name and arguments are known at runtime, and I’m failing with an IllegalAccessException.
This is on an object that is an instance of a nonpublic class which implements a public interface, and I’ve got a brain cramp trying to remember the right way to invoke such a method.
public interface Foo
{
public int getFooValue();
}
class FooImpl implements Foo
{
@Override public int getFooValue() { return 42; }
}
Object foo = new FooImpl();
Given the foo object, how would I call foo.getFooValue() reflectively?
If I look through the results of foo.getClass().getMethods(), this should work but I think it causes the IllegalAccessException Is this a case where I have to call getDeclaredMethods()? Or do I have to walk through the public interfaces/superclasses and call getDeclaredMethods there?
This works: