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

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

Introduction

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

Prototype

public Class getReturnType() 

Source Link

Document

Returns a Class object that represents the formal return type of the method.

Usage

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

License:Apache License

/**
 * Assert that the given method returns the expected return type.
 *
 * @param method Method to assert.//from   w  w w .j av a  2s. com
 * @param expectedReturnType Expected return type of the method.
 * @throws IllegalArgumentException If the method's return type doesn't match the expected type.
 */
public static void assertReturnValue(Method method, Class<?> expectedReturnType) {
    final Class<?> returnType = method.getReturnType();
    if (returnType != expectedReturnType) {
        final String message = ("Class=\'" + method.getDeclaringClass() + "\', method=\'" + method.getName()
                + "\': Must return a value of type \'" + expectedReturnType + "\'!");
        throw new IllegalArgumentException(message);
    }
}

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 av a  2 s.c  om*/
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);
    }
}