3636import java .lang .reflect .InvocationHandler ;
3737import java .lang .reflect .Method ;
3838import java .lang .reflect .Proxy ;
39+ import java .util .logging .Level ;
40+ import java .util .logging .Logger ;
3941
4042/**
4143 * This class uses Reflection to intercept method calls to the Proxy Object ({@link #createProxy(GL, Object, Class[])}
5153public class GLDebug implements InvocationHandler {
5254 protected Object obj ;
5355 protected GL gl ;
56+ protected Method methodGlGetError ;
57+ private static final Logger LOG = Logger .getLogger (GLDebug .class .getName ());
5458
55- private GLDebug (GL gl , Object obj ) {
59+ private GLDebug (GL gl , Object obj ) throws NoSuchMethodException {
5660 this .gl = gl ;
5761 this .obj = obj ;
62+ methodGlGetError = GL .class .getMethod ("glGetError" );
63+ /* The NoSuchMethodException shouldn't be thrown, but since we're in a constructor and cannot fail safe
64+ * otherwise, we throw it. */
5865 }
5966
6067 protected String decodeError (int err ) {
@@ -102,7 +109,7 @@ protected void checkError() {
102109 public Object invoke (Object proxy , Method method , Object [] args ) throws Throwable {
103110 Object result = method .invoke (obj , args );
104111
105- if (method .getName (). equals ("glGetError" )) {
112+ if (method .equals (methodGlGetError )) {
106113 return result ;
107114 }
108115
@@ -117,13 +124,19 @@ public Object invoke(Object proxy, Method method, Object[] args) throws Throwabl
117124 * @param gl The GL Context, required to call {@link GL#glGetError()}
118125 * @param obj The object which methods will be proxied
119126 * @param implementedInterfaces The interfaces/class this object implements
120- * @return The Proxy object
127+ * @return The Proxy object (or null if an error occured)
121128 */
122129 public static Object createProxy (GL gl , Object obj , Class <?>... implementedInterfaces ) {
123- return Proxy .newProxyInstance (
124- GLDebug .class .getClassLoader (),
125- implementedInterfaces ,
126- new GLDebug (gl , obj )
127- );
130+ try {
131+ return Proxy .newProxyInstance (
132+ GLDebug .class .getClassLoader (),
133+ implementedInterfaces ,
134+ new GLDebug (gl , obj )
135+ );
136+ } catch (NoSuchMethodException nsme ) {
137+ LOG .log (Level .SEVERE , "Could not initialize the proxy because the glGetError method wasn't found!" ,
138+ nsme );
139+ return null ;
140+ }
128141 }
129142}
0 commit comments