package com.peterhi.ui.gl;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import javax.media.opengl.GL;
import javax.media.opengl.GLException;
public final class GLUtil {
public static Object glInvoke(GL gl, String name, Class<?>[] types, Object[] args) {
if (gl == null) {
throw new NullPointerException();
}
if (name == null) {
throw new NullPointerException();
}
if (types == null) {
throw new NullPointerException();
}
if (args == null) {
throw new NullPointerException();
}
if (name.isEmpty()) {
throw new IllegalArgumentException();
}
if (types.length != args.length) {
throw new IllegalArgumentException();
}
try {
Class<?> type = gl.getClass();
Method method = type.getMethod(name, types);
return method.invoke(gl, args);
} catch (RuntimeException ex) {
throw ex;
} catch (Exception ex) {
throw new GLException(ex);
}
}
public static Object glInvoke(GL gl, String name, Object...typesAndArgs) {
if (gl == null) {
throw new NullPointerException();
}
if (name == null) {
throw new NullPointerException();
}
if (name.isEmpty()) {
throw new IllegalArgumentException();
}
if (typesAndArgs.length % 2 != 0) {
throw new IllegalArgumentException();
}
List<Class<?>> typeList = new ArrayList<Class<?>>();
List<Object> argList = new ArrayList<Object>();
for (int i = 0; i < typesAndArgs.length; i++) {
Object typeOrArg = typesAndArgs[i];
if (i % 2 == 0) {
if (!(typeOrArg instanceof Class)) {
throw new IllegalArgumentException();
} else {
typeList.add((Class<?> )typeOrArg);
}
} else {
argList.add(typeOrArg);
}
}
Class<?>[] types = typeList.toArray(new Class<?>[typeList.size()]);
Object[] args = argList.toArray(new Object[argList.size()]);
return GLUtil.glInvoke(gl, name, types, args);
}
}