Example usage for com.badlogic.gdx.utils.reflect Method invoke

List of usage examples for com.badlogic.gdx.utils.reflect Method invoke

Introduction

In this page you can find the example usage for com.badlogic.gdx.utils.reflect Method invoke.

Prototype

public Object invoke(Object obj, Object... args) throws ReflectionException 

Source Link

Document

Invokes the underlying method on the supplied object with the supplied parameters.

Usage

From source file:com.github.ykrasik.jaci.util.reflection.ReflectionUtils.java

License:Apache License

/**
 * Invokes the method, using the provided instance as 'this'.
 * Method must be no-args and have a return value of type {@code T}.
 * If the method is private, it will be made accessible outside of it's class.
 *
 * @param instance Instance to use as 'this' for invocation.
 * @param method Method to invoke./*from www. j a v  a  2 s  . c o  m*/
 * @param <T> Return type.
 * @return Result of invoking the no-args method.
 * @throws RuntimeException If an error occurred invoking the method.
 */
@SuppressWarnings("unchecked")
public static <T> T invokeNoArgs(Object instance, Method method) {
    try {
        if (!method.isAccessible()) {
            method.setAccessible(true);
        }
        return (T) method.invoke(instance, NO_ARGS);
    } catch (final java.lang.Throwable $ex) {
        throw new RuntimeException($ex);
    }
}

From source file:es.eucm.ead.engine.components.behaviors.BehaviorComponent.java

License:Open Source License

/**
 * Default implementation relies in reflection to set the attributes
 *//*from w  w w .  j  a  va  2 s  . co m*/
protected void initializeBehavior(S event, Class eventClass, T runtimeBehavior) {
    Method[] methods = ClassReflection.getDeclaredMethods(eventClass);
    for (Method get : methods) {
        String getPrefix = get.getName().startsWith("get") ? "get" : "is";
        if (get.getName().startsWith("get") || get.getName().startsWith("is")) {
            // Search equivalent set method in runtimeBehavior
            String setMethodName = get.getName().replace(getPrefix, "set");
            Class returningType = get.getReturnType();
            try {
                Method set = ClassReflection.getDeclaredMethod(runtimeBehavior.getClass(), setMethodName,
                        returningType);
                if (set != null) {
                    set.invoke(runtimeBehavior, get.invoke(event));
                }
            } catch (ReflectionException e) {
                Gdx.app.error("BehaviorComponent", "Error initializing behavior", e);
            }
        }
    }

    if (eventClass.getSuperclass() != null && eventClass.getSuperclass() != Object.class) {
        initializeBehavior(event, eventClass.getSuperclass(), runtimeBehavior);
    }
}