GLDebug: Use a method handle instead of a string comparison to increase the performance

master
MeFisto94 5 years ago
parent 12481c08f6
commit 312fb5bb57
  1. 19
      jme3-core/src/main/java/com/jme3/renderer/opengl/GLDebug.java

@ -36,6 +36,8 @@ import com.jme3.renderer.RendererException;
import java.lang.reflect.InvocationHandler; import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.lang.reflect.Proxy; import java.lang.reflect.Proxy;
import java.util.logging.Level;
import java.util.logging.Logger;
/** /**
* This class uses Reflection to intercept method calls to the Proxy Object ({@link #createProxy(GL, Object, Class[])} * This class uses Reflection to intercept method calls to the Proxy Object ({@link #createProxy(GL, Object, Class[])}
@ -51,10 +53,15 @@ import java.lang.reflect.Proxy;
public class GLDebug implements InvocationHandler { public class GLDebug implements InvocationHandler {
protected Object obj; protected Object obj;
protected GL gl; protected GL gl;
protected Method methodGlGetError;
private static final Logger LOG = Logger.getLogger(GLDebug.class.getName());
private GLDebug(GL gl, Object obj) { private GLDebug(GL gl, Object obj) throws NoSuchMethodException {
this.gl = gl; this.gl = gl;
this.obj = obj; this.obj = obj;
methodGlGetError = GL.class.getMethod("glGetError");
/* The NoSuchMethodException shouldn't be thrown, but since we're in a constructor and cannot fail safe
* otherwise, we throw it. */
} }
protected String decodeError(int err) { protected String decodeError(int err) {
@ -102,7 +109,7 @@ public class GLDebug implements InvocationHandler {
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
Object result = method.invoke(obj, args); Object result = method.invoke(obj, args);
if (method.getName().equals("glGetError")) { if (method.equals(methodGlGetError)) {
return result; return result;
} }
@ -117,13 +124,19 @@ public class GLDebug implements InvocationHandler {
* @param gl The GL Context, required to call {@link GL#glGetError()} * @param gl The GL Context, required to call {@link GL#glGetError()}
* @param obj The object which methods will be proxied * @param obj The object which methods will be proxied
* @param implementedInterfaces The interfaces/class this object implements * @param implementedInterfaces The interfaces/class this object implements
* @return The Proxy object * @return The Proxy object (or null if an error occured)
*/ */
public static Object createProxy(GL gl, Object obj, Class<?>... implementedInterfaces) { public static Object createProxy(GL gl, Object obj, Class<?>... implementedInterfaces) {
try {
return Proxy.newProxyInstance( return Proxy.newProxyInstance(
GLDebug.class.getClassLoader(), GLDebug.class.getClassLoader(),
implementedInterfaces, implementedInterfaces,
new GLDebug(gl, obj) new GLDebug(gl, obj)
); );
} catch (NoSuchMethodException nsme) {
LOG.log(Level.SEVERE, "Could not initialize the proxy because the glGetError method wasn't found!",
nsme);
return null;
}
} }
} }

Loading…
Cancel
Save