I try to intercept some OpenGL calls for testing my rendering classes. Reflection is used for replacing the OpenGL backend.
I feel this class is badly written and I need advices for refactoring it.
package fr.meuns.opengl;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.nio.FloatBuffer;
public class GL
{
private static class MethodSignature
{
String name;
Class< ? >[] parameters;
public MethodSignature( String name, Class< ? >... parameters )
{
this.name = name;
this.parameters = parameters;
}
}
private final static MethodSignature[] METHOD_SIGNATURES = new MethodSignature[] {
new MethodSignature( "glGenBuffers" ),
new MethodSignature( "glDeleteBuffers", int.class ),
new MethodSignature( "glBindBuffer", int.class, int.class ),
new MethodSignature( "glBufferData", int.class, FloatBuffer.class, int.class ),
new MethodSignature( "glGetBufferSubData", int.class, long.class, FloatBuffer.class )
...
};
private final static String[] CONSTANT_NAMES = new String[] {
"GL_ARRAY_BUFFER",
"GL_ELEMENT_ARRAY_BUFFER",
"GL_STATIC_DRAW"
...
};
public static int GL_ARRAY_BUFFER;
public static int GL_ELEMENT_ARRAY_BUFFER;
public static int GL_STATIC_DRAW;
...
public static Method _glGenBuffers;
public static Method _glDeleteBuffers;
public static Method _glBindBuffer;
public static Method _glBufferData;
public static Method _glGetBufferSubData;
...
public static int glGenBuffers()
{
try
{
return (int)_glGenBuffers.invoke( null );
}
catch( IllegalAccessException | IllegalArgumentException | InvocationTargetException e )
{
e.printStackTrace();
return 0;
}
}
public static void glDeleteBuffers( int buffer )
{
try
{
_glDeleteBuffers.invoke( null, buffer );
}
catch( IllegalAccessException | IllegalArgumentException | InvocationTargetException e )
{
e.printStackTrace();
}
}
public static void glBindBuffer( int target, int buffer )
{
try
{
_glBindBuffer.invoke( null, target, buffer );
}
catch( IllegalAccessException | IllegalArgumentException | InvocationTargetException e )
{
e.printStackTrace();
}
}
public static void glBufferData( int target, FloatBuffer data, int usage )
{
try
{
_glBufferData.invoke( null, target, data, usage );
}
catch( IllegalAccessException | IllegalArgumentException | InvocationTargetException e )
{
e.printStackTrace();
}
}
public static void glGetBufferSubData( int target, long offset, FloatBuffer data )
{
try
{
_glGetBufferSubData.invoke( null, target, offset, data );
}
catch( IllegalAccessException | IllegalArgumentException | InvocationTargetException e )
{
e.printStackTrace();
}
}
public ...
public static void useClass( Class< ? > glClass )
{
for( String name : CONSTANT_NAMES )
try
{
GL.class
.getField( name )
.set( null, glClass.getField( name ).get( null ) );
}
catch( IllegalArgumentException | IllegalAccessException | NoSuchFieldException | SecurityException e1 )
{
e1.printStackTrace();
}
for( MethodSignature signature : METHOD_SIGNATURES )
try
{
GL.class
.getField( "_" + signature.name )
.set( null, glClass.getDeclaredMethod( signature.name, signature.parameters ) );
}
catch( IllegalArgumentException | IllegalAccessException | NoSuchFieldException | SecurityException | NoSuchMethodException e )
{
e.printStackTrace();
}
}
}
Initialization for rendering using LWJGL :
import org.lwjgl.opengl.GL11;
import org.lwjgl.opengl.GL15;
import org.lwjgl.opengl.GL20;
...
GL.useClass( GL11.class );
GL.useClass( GL15.class );
GL.useClass( GL20.class );
...
Initialization for testing :
import fr.meuns.opengl.GLMock;
...
GL.useClass( GLMock.class );
...