Search in sources :

Example 1 with SpecificAccessibleObject

use of org.terasology.utilities.reflection.SpecificAccessibleObject in project Terasology by MovingBlocks.

the class MethodCommand method registerAvailable.

/**
 * Registers all available command methods annotated with {@link org.terasology.logic.console.commandSystem.annotations.Command}.
 */
public static void registerAvailable(Object provider, Console console, Context context) {
    Predicate<? super Method> predicate = Predicates.<Method>and(ReflectionUtils.withModifier(Modifier.PUBLIC), ReflectionUtils.withAnnotation(Command.class));
    Set<Method> commandMethods = ReflectionUtils.getAllMethods(provider.getClass(), predicate);
    for (Method method : commandMethods) {
        if (!hasSenderAnnotation(method)) {
            logger.error("Command {} provided by {} contains a EntityRef without @Sender annotation, may cause a NullPointerException", method.getName(), provider.getClass().getSimpleName());
        }
        logger.debug("Registering command method {} in class {}", method.getName(), method.getDeclaringClass().getCanonicalName());
        try {
            SpecificAccessibleObject<Method> specificMethod = new SpecificAccessibleObject<>(method, provider);
            MethodCommand command = referringTo(specificMethod, context);
            console.registerCommand(command);
            logger.debug("Registered command method {} in class {}", method.getName(), method.getDeclaringClass().getCanonicalName());
        } catch (RuntimeException t) {
            logger.error("Failed to load command method {} in class {}", method.getName(), method.getDeclaringClass().getCanonicalName(), t);
        }
    }
}
Also used : SpecificAccessibleObject(org.terasology.utilities.reflection.SpecificAccessibleObject) Command(org.terasology.logic.console.commandSystem.annotations.Command) Method(java.lang.reflect.Method)

Aggregations

Method (java.lang.reflect.Method)1 Command (org.terasology.logic.console.commandSystem.annotations.Command)1 SpecificAccessibleObject (org.terasology.utilities.reflection.SpecificAccessibleObject)1