Search in sources :

Example 6 with Method

use of com.badlogic.gdx.utils.reflect.Method in project libgdx by libgdx.

the class AnnotationTest method create.

@Override
public void create() {
    font = new BitmapFont();
    batch = new SpriteBatch();
    try {
        Annotation annotation = ClassReflection.getDeclaredAnnotation(AnnotatedClass.class, TestAnnotation.class);
        if (annotation != null) {
            TestAnnotation annotationInstance = annotation.getAnnotation(TestAnnotation.class);
            println("Class annotation:\n name=" + annotationInstance.name() + ",\n values=" + Arrays.toString(annotationInstance.values()) + ",\n enum=" + annotationInstance.someEnum().toString());
        } else {
            println("ERROR: Class annotation not found.");
        }
        Field field = ClassReflection.getDeclaredField(AnnotatedClass.class, "annotatedValue");
        if (field != null) {
            Annotation[] annotations = field.getDeclaredAnnotations();
            for (Annotation a : annotations) {
                if (a.getAnnotationType().equals(TestAnnotation.class)) {
                    TestAnnotation annotationInstance = a.getAnnotation(TestAnnotation.class);
                    println("Field annotation:\n name=" + annotationInstance.name() + ",\n values=" + Arrays.toString(annotationInstance.values()) + ",\n enum=" + annotationInstance.someEnum().toString());
                    break;
                }
            }
        } else {
            println("ERROR: Field 'annotatedValue' not found.");
        }
        Method method = ClassReflection.getDeclaredMethod(AnnotatedClass.class, "annotatedMethod");
        if (method != null) {
            Annotation[] annotations = method.getDeclaredAnnotations();
            for (Annotation a : annotations) {
                if (a.getAnnotationType().equals(TestAnnotation.class)) {
                    TestAnnotation annotationInstance = a.getAnnotation(TestAnnotation.class);
                    println("Method annotation:\n name=" + annotationInstance.name() + ",\n values=" + Arrays.toString(annotationInstance.values()) + ",\n enum=" + annotationInstance.someEnum().toString());
                    break;
                }
            }
        } else {
            println("ERROR: Method 'annotatedMethod' not found.");
        }
        println("Class annotations w/@Inherit:");
        Annotation[] annotations = ClassReflection.getAnnotations(InheritClassB.class);
        for (Annotation a : annotations) {
            println(" name=" + a.getAnnotationType().getSimpleName());
        }
        if (!ClassReflection.isAnnotationPresent(InheritClassB.class, TestInheritAnnotation.class)) {
            println("ERROR: Inherited class annotation not found.");
        }
    } catch (Exception e) {
        println("FAILED: " + e.getMessage());
        message += e.getClass();
    }
}
Also used : Field(com.badlogic.gdx.utils.reflect.Field) Method(com.badlogic.gdx.utils.reflect.Method) BitmapFont(com.badlogic.gdx.graphics.g2d.BitmapFont) SpriteBatch(com.badlogic.gdx.graphics.g2d.SpriteBatch) Annotation(com.badlogic.gdx.utils.reflect.Annotation)

Example 7 with Method

use of com.badlogic.gdx.utils.reflect.Method in project libgdx-inGameConsole by StrongJoshua.

the class AbstractConsole method execCommand.

/* (non-Javadoc)
	 * @see com.strongjoshua.console.Console#execCommand(java.lang.String)
	 */
@Override
public void execCommand(String command) {
    if (disabled)
        return;
    log(command, LogLevel.COMMAND);
    String[] parts = command.split(" ");
    String methodName = parts[0];
    String[] sArgs = null;
    if (parts.length > 1) {
        sArgs = new String[parts.length - 1];
        for (int i = 1; i < parts.length; i++) {
            sArgs[i - 1] = parts[i];
        }
    }
    Class<? extends CommandExecutor> clazz = exec.getClass();
    Method[] methods = ClassReflection.getMethods(clazz);
    Array<Integer> possible = new Array<Integer>();
    for (int i = 0; i < methods.length; i++) {
        Method method = methods[i];
        if (method.getName().equalsIgnoreCase(methodName) && ConsoleUtils.canExecuteCommand(this, method)) {
            possible.add(i);
        }
    }
    if (possible.size <= 0) {
        log("No such method found.", LogLevel.ERROR);
        return;
    }
    int size = possible.size;
    int numArgs = sArgs == null ? 0 : sArgs.length;
    for (int i = 0; i < size; i++) {
        Method m = methods[possible.get(i)];
        Class<?>[] params = m.getParameterTypes();
        if (numArgs == params.length) {
            try {
                Object[] args = null;
                try {
                    if (sArgs != null) {
                        args = new Object[numArgs];
                        for (int j = 0; j < params.length; j++) {
                            Class<?> param = params[j];
                            final String value = sArgs[j];
                            if (param.equals(String.class)) {
                                args[j] = value;
                            } else if (param.equals(Boolean.class) || param.equals(boolean.class)) {
                                args[j] = Boolean.parseBoolean(value);
                            } else if (param.equals(Byte.class) || param.equals(byte.class)) {
                                args[j] = Byte.parseByte(value);
                            } else if (param.equals(Short.class) || param.equals(short.class)) {
                                args[j] = Short.parseShort(value);
                            } else if (param.equals(Integer.class) || param.equals(int.class)) {
                                args[j] = Integer.parseInt(value);
                            } else if (param.equals(Long.class) || param.equals(long.class)) {
                                args[j] = Long.parseLong(value);
                            } else if (param.equals(Float.class) || param.equals(float.class)) {
                                args[j] = Float.parseFloat(value);
                            } else if (param.equals(Double.class) || param.equals(double.class)) {
                                args[j] = Double.parseDouble(value);
                            }
                        }
                    }
                } catch (Exception e) {
                    // Error occurred trying to parse parameter, continue to next function
                    continue;
                }
                m.setAccessible(true);
                m.invoke(exec, args);
                return;
            } catch (ReflectionException e) {
                String msg = e.getMessage();
                if (msg == null || msg.length() <= 0 || msg.equals("")) {
                    msg = "Unknown Error";
                    e.printStackTrace();
                }
                log(msg, LogLevel.ERROR);
                return;
            }
        }
    }
    log("Bad parameters. Check your code.", LogLevel.ERROR);
}
Also used : ReflectionException(com.badlogic.gdx.utils.reflect.ReflectionException) Method(com.badlogic.gdx.utils.reflect.Method) ReflectionException(com.badlogic.gdx.utils.reflect.ReflectionException) Array(com.badlogic.gdx.utils.Array)

Example 8 with Method

use of com.badlogic.gdx.utils.reflect.Method in project libgdx-inGameConsole by StrongJoshua.

the class CommandCompleter method set.

public void set(CommandExecutor ce, String s) {
    reset();
    setString = s.toLowerCase();
    Array<Method> methods = getAllMethods(ce);
    for (Method m : methods) {
        String name = m.getName();
        if (name.toLowerCase().startsWith(setString) && ConsoleUtils.canDisplayCommand(ce.console, m)) {
            possibleCommands.add(name);
        }
    }
    iterator = new ObjectSetIterator<>(possibleCommands);
}
Also used : Method(com.badlogic.gdx.utils.reflect.Method)

Example 9 with Method

use of com.badlogic.gdx.utils.reflect.Method in project libgdx-inGameConsole by StrongJoshua.

the class CommandCompleter method getAllMethods.

private Array<Method> getAllMethods(CommandExecutor ce) {
    Array<Method> methods = new Array<>();
    Method[] ms = ClassReflection.getDeclaredMethods(ce.getClass());
    for (Method m : ms) {
        if (m.isPublic()) {
            methods.add(m);
        }
    }
    ms = ClassReflection.getDeclaredMethods(ce.getClass().getSuperclass());
    for (Method m : ms) {
        if (m.isPublic()) {
            methods.add(m);
        }
    }
    return methods;
}
Also used : Array(com.badlogic.gdx.utils.Array) Method(com.badlogic.gdx.utils.reflect.Method)

Aggregations

Method (com.badlogic.gdx.utils.reflect.Method)9 GdxRuntimeException (com.badlogic.gdx.utils.GdxRuntimeException)3 ReflectionException (com.badlogic.gdx.utils.reflect.ReflectionException)3 BitmapFont (com.badlogic.gdx.graphics.g2d.BitmapFont)2 SpriteBatch (com.badlogic.gdx.graphics.g2d.SpriteBatch)2 Array (com.badlogic.gdx.utils.Array)2 SerializationException (com.badlogic.gdx.utils.SerializationException)2 Field (com.badlogic.gdx.utils.reflect.Field)2 Vector2 (com.badlogic.gdx.math.Vector2)1 Json (com.badlogic.gdx.utils.Json)1 Annotation (com.badlogic.gdx.utils.reflect.Annotation)1 Constructor (com.badlogic.gdx.utils.reflect.Constructor)1