In the Java function, the interface type is really only a type declaration,
and nothing prevents you from casting the object with that interface type
declaration back to its implementing class, thus making all methods
accessible. Now, as you pass the object to Lua, there is no interface type
declaration as Lua is dynamically typed, and all methods of the object are
thus accessible by default. To phrase this another way, when you pass an
object to Lua, you are passing that object with its dynamic type information
only, and all static type information, such as an interface type declaration
in the formal parameter list of a method, is gone.
How to solve this? For most cases, you may simply be able to live with it.
Any Lua code that uses more than the interface you are promising on the
object is simply bound the break at some point. If for some reason you
really have to restrict the access, one way to achieve this is to pass a
wrapper object that only exposes the interface methods you want to expose.
This could be implemented with an anonymous interface instantiation like
this:
final Object o;
luaState.pushObject(new Interface() {
void foo (int a) {
o.foo(a);
}
int bar() {
return o.bar();
}
});