Search code examples
javacglibluaj

How can I call the original method with CGLib?


I'm using CGLib to dynamicly change a TileEntity (Minecraft). In the InvocationHandler I call the Lua functions for the interface, but I want the methods that already exist to be called in Java. This is my invoke method:

    public Object invoke(Object arg0, Method arg1, Object[] arg2)
            throws Throwable {
        for (Method m : BaseTileEntity.class.getMethods()) {
            if (m.equals(arg1)) {
                return m.invoke(arg0, arg2);
            }
        }
        return ((BaseTileEntity)arg0).file.call(arg1.getName(), arg2).arg(1);
    }

The problem is that invoking the method will call the same method and not the original method. Is there a way to call the original method?


Solution

  • Found out how to call the original method: implement MethodInterceptor instead of InvocationHandler. The method of MethodInterceptor has an extra argument that can call the original method using .invokeSuper(arg0, arg2).