Byte-Budy Static Method Redefine

Viewed 100

I'm trying to intercep static methods with byte-budy. The following is supposed to redefine the class and load it. It works with no error but my interceptor ain't being called.

public abstract class GLObject extends External<Integer> {

    static {
        ByteBuddyAgent.install();

        new ByteBuddy()
                .redefine(
                        TypePool.Default.ofSystemLoader().describe("org.lwjgl.opengl.GL46C").resolve(),
                        ClassFileLocator.ForClassLoader.ofSystemLoader())
                .method(
                        nameStartsWith("gl"))
                .intercept(
                        MethodDelegation.to(GLInterceptor.class))
                .make()
                .load(
                        Thread.currentThread().getContextClassLoader(),
                        ClassReloadingStrategy.fromInstalledAgent());
    }

}

The interceptor to be called is

public class GLInterceptor {
    private static final Logger LOGGER = LoggerFactory.getLogger(GLInterceptor.class);
    private static final GLCapabilities capabilities = GL.getCapabilities();

    @RuntimeType
    public static Object intercept(@Origin Method method, @AllArguments final Object[] args) throws Exception {
        LOGGER.info("%s", method.getName());
        final var result = method.invoke(GL46C.class, method, args);

        if (capabilities.OpenGL43) {
            final var builder = new StringBuilder();

            var code = 0;
            while (( code = GL11C.glGetError() ) != GL11C.GL_NO_ERROR) {
                builder.append("  - ");
                builder.append(switch (code) {
                    case GL11C.GL_INVALID_ENUM -> "Invalid Enum";
                    case GL11C.GL_INVALID_OPERATION -> "Invalid Operation";
                    case GL11C.GL_INVALID_VALUE -> "Invalid Value";
                    case GL11C.GL_STACK_OVERFLOW -> "Stack Overflow";
                    case GL11C.GL_STACK_UNDERFLOW -> "Stack Underflow";
                    case GL11C.GL_OUT_OF_MEMORY -> "Out Of Memory";
                    case GL30C.GL_INVALID_FRAMEBUFFER_OPERATION -> "Invalid Framebuffer Operation";
                    default -> "???";
                });

                builder.append("\n");
            }

            if (builder.length() > 0)
                throw new RendererProviderException("OpenGL ERRORS:\n\n%s".formatted(builder.toString()));
        }

        return result;
    }
}

I've tryed to use diferent intercept method signature with @Super and @SuperCall but those resulted in exception similar to:

None of [public static java.lang.Object br.toe.engine.utils.GLInterceptor.intercept(java.lang.Object,java.lang.reflect.Method,java.lang.Object[]) throws java.lang.Exception] allows for delegation from public static void org.lwjgl.opengl.GL46C.glMultiDrawArraysIndirectCount(int,java.nio.ByteBuffer,long,int,int)

What am i doing wrong?

1 Answers

The signature in the exception:

public static 
java.lang.Object 
br.toe.engine.utils.GLInterceptor.intercept(
  java.lang.Object,
  java.lang.reflect.Method,
  java.lang.Object[])

ressembles the method's signature in your copied interceptor. Are you sure that there's no first argument, for example annotated with @This? You can set an attribute on the latter to be null for static methods if that's what you intend to do.

Related