|
| 1 | +/* |
| 2 | + * Copyright (c) 2009-2020 jMonkeyEngine |
| 3 | + * All rights reserved. |
| 4 | + * |
| 5 | + * Redistribution and use in source and binary forms, with or without |
| 6 | + * modification, are permitted provided that the following conditions are |
| 7 | + * met: |
| 8 | + * |
| 9 | + * * Redistributions of source code must retain the above copyright |
| 10 | + * notice, this list of conditions and the following disclaimer. |
| 11 | + * |
| 12 | + * * Redistributions in binary form must reproduce the above copyright |
| 13 | + * notice, this list of conditions and the following disclaimer in the |
| 14 | + * documentation and/or other materials provided with the distribution. |
| 15 | + * |
| 16 | + * * Neither the name of 'jMonkeyEngine' nor the names of its contributors |
| 17 | + * may be used to endorse or promote products derived from this software |
| 18 | + * without specific prior written permission. |
| 19 | + * |
| 20 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 21 | + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED |
| 22 | + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 23 | + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
| 24 | + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 25 | + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 26 | + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 27 | + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
| 28 | + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 29 | + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 30 | + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 31 | + */ |
1 | 32 | package com.jme3.renderer.opengl; |
2 | 33 |
|
3 | 34 | import com.jme3.renderer.RendererException; |
4 | 35 |
|
5 | | -public abstract class GLDebug { |
| 36 | +import java.lang.reflect.InvocationHandler; |
| 37 | +import java.lang.reflect.Method; |
| 38 | +import java.lang.reflect.Proxy; |
6 | 39 |
|
| 40 | +/** |
| 41 | + * This class uses Reflection to intercept method calls to the Proxy Object ({@link #createProxy(GL, Object, Class[])} |
| 42 | + * and extends them with the Error Checking in {@link #checkError()}.<br/> |
| 43 | + * This means we don't have to generate a class with overrides for every possible method just to add a |
| 44 | + * {@link #checkError()} call.<br/> |
| 45 | + * Note that we should not call {@link #checkError()} for {@link GL#glGetError()}, it doesn't make sense.<br /> |
| 46 | + * Note that this class is general purpose and as such every class instance (every object) can be guarded as long as |
| 47 | + * the passed gl instance is valid.<br/> |
| 48 | + * |
| 49 | + * @author MeFisto94 |
| 50 | + */ |
| 51 | +public class GLDebug implements InvocationHandler { |
| 52 | + protected Object obj; |
7 | 53 | protected GL gl; |
8 | | - |
| 54 | + protected Method methodGlGetError; |
| 55 | + |
| 56 | + private GLDebug(GL gl, Object obj) throws NoSuchMethodException { |
| 57 | + this.gl = gl; |
| 58 | + this.obj = obj; |
| 59 | + methodGlGetError = GL.class.getMethod("glGetError"); |
| 60 | + /* The NoSuchMethodException shouldn't be thrown, but since we're in a constructor and cannot fail safe |
| 61 | + * otherwise, we throw it. */ |
| 62 | + } |
| 63 | + |
9 | 64 | protected String decodeError(int err) { |
10 | 65 | String errMsg; |
11 | 66 | switch (err) { |
@@ -46,4 +101,38 @@ protected void checkError() { |
46 | 101 | throw new RendererException("An OpenGL error occurred - " + decodeError(err)); |
47 | 102 | } |
48 | 103 | } |
| 104 | + |
| 105 | + @Override |
| 106 | + public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { |
| 107 | + Object result = method.invoke(obj, args); |
| 108 | + |
| 109 | + if (method.equals(methodGlGetError)) { |
| 110 | + return result; |
| 111 | + } |
| 112 | + |
| 113 | + checkError(); |
| 114 | + return result; |
| 115 | + } |
| 116 | + |
| 117 | + /** |
| 118 | + * Creates a debug-proxied object, which will call {@link GL#glGetError()} after every method invocation and throw |
| 119 | + * a {@link com.jme3.renderer.RendererException} if there was a GL Error. |
| 120 | + * |
| 121 | + * @param gl The GL Context, required to call {@link GL#glGetError()} |
| 122 | + * @param obj The object which methods will be proxied |
| 123 | + * @param implementedInterfaces The interfaces/class this object implements |
| 124 | + * @return The Proxy object (or null if an error occured) |
| 125 | + */ |
| 126 | + public static Object createProxy(GL gl, Object obj, Class<?>... implementedInterfaces) { |
| 127 | + try { |
| 128 | + return Proxy.newProxyInstance( |
| 129 | + GLDebug.class.getClassLoader(), |
| 130 | + implementedInterfaces, |
| 131 | + new GLDebug(gl, obj) |
| 132 | + ); |
| 133 | + } catch (NoSuchMethodException nsme) { |
| 134 | + throw new IllegalArgumentException ("Could not initialize the proxy because the glGetError method wasn't " + |
| 135 | + "found!", nsme); |
| 136 | + } |
| 137 | + } |
49 | 138 | } |
0 commit comments