java.nio.Buffer not loading clear() method on runtime

不问归期 提交于 2020-08-02 07:32:27

问题


So I am a developer for a project that uses a java agent to inject. It should be noted though that this error occurs after main is called.

Everything is going fine for most users, but a few are having an issue where java.nio.IntBuffer isn't loading clear() (inherited from Buffer)

Error:

java.lang.NoSuchMethodError: java.nio.IntBuffer.clear()Ljava/nio/IntBuffer;

Then the stacktrace, which simply gives the first time clear() is called in our code.

What is the cause of this (besides the fact that Java isn't loading at runtime) and how do I fix it?


回答1:


Thanks to the comment from Janez Kuhar and doing some digging this is caused by JDK9 breaking compatibility. In our code we call IntBuffer.clear() and expect it to return Buffer, but in JDK9 they made all Buffer methods return the child type (i.e. ByteBuffer or IntBuffer as opposed to just Buffer), therefore people running JRE8 (most of our userbase) experience NoSuchMethodError's because the return type is incompatible and must be casted like this

someMethod(((Buffer)intBuffer).clear());


来源:https://stackoverflow.com/questions/48693695/java-nio-buffer-not-loading-clear-method-on-runtime

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!