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

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

Introduction

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

Prototype

public String getName() 

Source Link

Document

Returns the name of the method.

Usage

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

License:Apache License

/**
 * Find the first encountered method with the provided name. Doesn't take parameters into account.
 * Includes inherited methods.//ww w .  j a v  a 2  s  . c om
 *
 * @param clazz Class to search.
 * @param methodName Method name.
 * @return First encountered method with the provided name.
 * @throws IllegalArgumentException If the class doesn't contain a method with the provided name.
 */
public static Method lookupMethod(Class<?> clazz, String methodName) {
    for (Method method : ClassReflection.getMethods(clazz)) {
        if (method.getName().equals(methodName)) {
            return method;
        }
    }
    throw new IllegalArgumentException(
            ("Class \'" + clazz + "\' doesn\'t have method: \'" + methodName + "\'"));
}

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

License:Apache License

/**
 * Assert that the given method takes no parameters.
 *
 * @param method Method to assert./*from   ww  w. ja  va 2 s . com*/
 * @throws IllegalArgumentException If the method takes any parameters.
 */
public static void assertNoParameters(Method method) {
    final Class<?>[] parameterTypes = method.getParameterTypes();
    if (parameterTypes.length > 0) {
        final String message = ("Class=\'" + method.getDeclaringClass() + "\', method=\'" + method.getName()
                + "\': Must take no parameters!");
        throw new IllegalArgumentException(message);
    }
}

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.//  w w w  .  j a  va  2  s . c  o m
 * @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 ww .j  av a 2 s .  c  o 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);
    }
}

From source file:com.github.ykrasik.jaci.reflection.method.ReflectionMethodProcessor.java

License:Apache License

/**
 * Process the method and create a {@link CommandDef} out of it, if it is accepted by one of the {@link MethodCommandFactory}s.
 *
 * @param instance Instance of a class to which this method belongs.
 * @param method Method to be processed.
 * @return A {@code present} {@link CommandDef} if any of the factories managed to process the method.
 *///from  www .  j a v  a  2s . co m
public Opt<CommandDef> process(Object instance, Method method) {
    try {
        return doCreateCommand(instance, method);
    } catch (Exception e) {
        final String message = ("Error creating command: class=" + method.getDeclaringClass() + ", method="
                + method.getName());
        throw new IllegalArgumentException(message, e);
    }
}

From source file:com.github.ykrasik.jaci.reflection.method.factory.DefaultAnnotationMethodCommandFactory.java

License:Apache License

@Override
protected CommandDef doCreate(Object instance, Method method, Command annotation) throws Exception {
    // Reflect method params.
    final List<ReflectionParameter> params = ReflectionUtils.reflectMethodParameters(method);

    final String name = getNonEmptyString(annotation.value()).getOrElse(method.getName());
    final CommandDef.Builder builder = new CommandDef.Builder(name,
            new ReflectionCommandExecutor(outputPromise, instance, method));

    final Opt<String> description = getNonEmptyString(annotation.description());
    if (description.isPresent()) {
        builder.setDescription(description.get());
    }//from  w  w  w .j a v  a2 s .c o  m

    for (ReflectionParameter param : params) {
        final ParamDef<?> paramDef = paramProcessor.createParam(instance, param);
        builder.addParam(paramDef);
    }

    return builder.build();
}

From source file:com.github.ykrasik.jaci.reflection.method.factory.ToggleAnnotationMethodCommandFactory.java

License:Apache License

@Override
protected CommandDef doCreate(Object instance, Method method, ToggleCommand annotation) throws Exception {
    ReflectionUtils.assertReturnValue(method, ToggleCommandStateAccessor.class);
    ReflectionUtils.assertNoParameters(method);
    final ToggleCommandStateAccessor accessor = ReflectionUtils.invokeNoArgs(instance, method);

    final String name = getNonEmptyString(annotation.value()).getOrElse(method.getName());
    final ToggleCommandDefBuilder builder = new ToggleCommandDefBuilder(name, accessor);

    final Opt<String> description = getNonEmptyString(annotation.description());
    if (description.isPresent()) {
        builder.setDescription(description.get());
    }// w  w  w.  j  a v  a 2s  . c om

    final Opt<String> paramName = getNonEmptyString(annotation.paramName());
    if (paramName.isPresent()) {
        builder.setParamName(paramName.get());
    }

    final Opt<String> paramDescription = getNonEmptyString(annotation.paramDescription());
    if (paramDescription.isPresent()) {
        builder.setParamDescription(paramDescription.get());
    }

    return builder.build();
}