Search in sources :

Example 1 with Method

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

the class AbstractConsole method printCommands.

@Override
public void printCommands() {
    Method[] methods = ClassReflection.getDeclaredMethods(exec.getClass());
    for (int j = 0; j < methods.length; j++) {
        Method m = methods[j];
        if (m.isPublic() && ConsoleUtils.canDisplayCommand(this, m)) {
            String s = "";
            s += m.getName();
            s += " : ";
            Class<?>[] params = m.getParameterTypes();
            for (int i = 0; i < params.length; i++) {
                s += params[i].getSimpleName();
                if (i < params.length - 1) {
                    s += ", ";
                }
            }
            log(s);
        }
    }
}
Also used : Method(com.badlogic.gdx.utils.reflect.Method)

Example 2 with Method

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

the class CollectionsTest method invoke.

private void invoke(String methodName, Object object, Object... args) {
    try {
        Method theMethod = null;
        for (Method method : ClassReflection.getMethods(object.getClass())) {
            if (methodName.equals(method.getName()) && method.getParameterTypes().length == args.length) {
                theMethod = method;
                break;
            }
        }
        theMethod.invoke(object, args);
    } catch (Throwable ex) {
        throw new GdxRuntimeException(ex);
    }
}
Also used : GdxRuntimeException(com.badlogic.gdx.utils.GdxRuntimeException) Method(com.badlogic.gdx.utils.reflect.Method)

Example 3 with Method

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

the class Skin method setEnabled.

/** Sets the style on the actor to disabled or enabled. This is done by appending "-disabled" to the style name when enabled is
	 * false, and removing "-disabled" from the style name when enabled is true. A method named "getStyle" is called the actor via
	 * reflection and the name of that style is found in the skin. If the actor doesn't have a "getStyle" method or the style was
	 * not found in the skin, no exception is thrown and the actor is left unchanged. */
public void setEnabled(Actor actor, boolean enabled) {
    // Get current style.
    Method method = findMethod(actor.getClass(), "getStyle");
    if (method == null)
        return;
    Object style;
    try {
        style = method.invoke(actor);
    } catch (Exception ignored) {
        return;
    }
    // Determine new style.
    String name = find(style);
    if (name == null)
        return;
    name = name.replace("-disabled", "") + (enabled ? "" : "-disabled");
    style = get(name, style.getClass());
    // Set new style.
    method = findMethod(actor.getClass(), "setStyle");
    if (method == null)
        return;
    try {
        method.invoke(actor, style);
    } catch (Exception ignored) {
    }
}
Also used : Method(com.badlogic.gdx.utils.reflect.Method) GdxRuntimeException(com.badlogic.gdx.utils.GdxRuntimeException) SerializationException(com.badlogic.gdx.utils.SerializationException) ReflectionException(com.badlogic.gdx.utils.reflect.ReflectionException)

Example 4 with Method

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

the class ReflectionTest method create.

@Override
public void create() {
    font = new BitmapFont();
    batch = new SpriteBatch();
    try {
        Vector2 fromDefaultConstructor = ClassReflection.newInstance(Vector2.class);
        println("From default constructor: " + fromDefaultConstructor);
        Method mSet = ClassReflection.getMethod(Vector2.class, "set", float.class, float.class);
        mSet.invoke(fromDefaultConstructor, 10, 11);
        println("Set to 10/11: " + fromDefaultConstructor);
        Constructor copyConstroctor = ClassReflection.getConstructor(Vector2.class, Vector2.class);
        Vector2 fromCopyConstructor = (Vector2) copyConstroctor.newInstance(fromDefaultConstructor);
        println("From copy constructor: " + fromCopyConstructor);
        Method mMul = ClassReflection.getMethod(Vector2.class, "scl", float.class);
        println("Multiplied by 2; " + mMul.invoke(fromCopyConstructor, 2));
        Method mNor = ClassReflection.getMethod(Vector2.class, "nor");
        println("Normalized: " + mNor.invoke(fromCopyConstructor));
        Vector2 fieldCopy = new Vector2();
        Field fx = ClassReflection.getField(Vector2.class, "x");
        Field fy = ClassReflection.getField(Vector2.class, "y");
        fx.set(fieldCopy, fx.get(fromCopyConstructor));
        fy.set(fieldCopy, fy.get(fromCopyConstructor));
        println("Copied field by field: " + fieldCopy);
        Json json = new Json();
        String jsonString = json.toJson(fromCopyConstructor);
        Vector2 fromJson = json.fromJson(Vector2.class, jsonString);
        println("JSON serialized: " + jsonString);
        println("JSON deserialized: " + fromJson);
        fromJson.x += 1;
        fromJson.y += 1;
        println("JSON deserialized + 1/1: " + fromJson);
        Object array = ArrayReflection.newInstance(int.class, 5);
        ArrayReflection.set(array, 0, 42);
        println("Array int: length=" + ArrayReflection.getLength(array) + ", access=" + ArrayReflection.get(array, 0));
        array = ArrayReflection.newInstance(String.class, 5);
        ArrayReflection.set(array, 0, "test string");
        println("Array String: length=" + ArrayReflection.getLength(array) + ", access=" + ArrayReflection.get(array, 0));
    } catch (Exception e) {
        message = "FAILED: " + e.getMessage() + "\n";
        message += e.getClass();
    }
}
Also used : Field(com.badlogic.gdx.utils.reflect.Field) Vector2(com.badlogic.gdx.math.Vector2) Constructor(com.badlogic.gdx.utils.reflect.Constructor) Method(com.badlogic.gdx.utils.reflect.Method) Json(com.badlogic.gdx.utils.Json) BitmapFont(com.badlogic.gdx.graphics.g2d.BitmapFont) SpriteBatch(com.badlogic.gdx.graphics.g2d.SpriteBatch)

Example 5 with Method

use of com.badlogic.gdx.utils.reflect.Method in project gdx-skineditor by cobolfoo.

the class Skin method setEnabled.

/**
	 * Sets the style on the actor to disabled or enabled. This is done by
	 * appending "-disabled" to the style name when enabled is false, and
	 * removing "-disabled" from the style name when enabled is true. A method
	 * named "getStyle" is called the actor via reflection and the name of that
	 * style is found in the skin. If the actor doesn't have a "getStyle" method
	 * or the style was not found in the skin, no exception is thrown and the
	 * actor is left unchanged.
	 */
public void setEnabled(Actor actor, boolean enabled) {
    // Get current style.
    Method method = findMethod(actor.getClass(), "getStyle");
    if (method == null)
        return;
    Object style;
    try {
        style = method.invoke(actor);
    } catch (Exception ignored) {
        return;
    }
    // Determine new style.
    String name = find(style);
    if (name == null)
        return;
    name = name.replace("-disabled", "") + (enabled ? "" : "-disabled");
    style = get(name, style.getClass());
    // Set new style.
    method = findMethod(actor.getClass(), "setStyle");
    if (method == null)
        return;
    try {
        method.invoke(actor, style);
    } catch (Exception ignored) {
    }
}
Also used : Method(com.badlogic.gdx.utils.reflect.Method) GdxRuntimeException(com.badlogic.gdx.utils.GdxRuntimeException) SerializationException(com.badlogic.gdx.utils.SerializationException) ReflectionException(com.badlogic.gdx.utils.reflect.ReflectionException)

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