Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix incorrect @onKey method header check #61

Closed
github-actions bot opened this issue Feb 29, 2024 · 2 comments
Closed

Fix incorrect @onKey method header check #61

github-actions bot opened this issue Feb 29, 2024 · 2 comments
Assignees
Labels

Comments

@github-actions
Copy link

        defaultResourceBundle = resourceBundle;
    }

    /**
     * Creates an event handler for the given method and instance that will be called when the specified key event occurs.
     *
     * @param method     The method to call
     * @param instance   The instance to call the method on
     * @param annotation The annotation with the key event information
     * @return An event handler for the given method and instance
     */
    private EventHandler<KeyEvent> createKeyEventHandler(Method method, Object instance, onKey annotation) {
        boolean hasEventParameter = method.getParameterCount() == 1 && method.getParameterTypes()[0].isAssignableFrom(KeyEvent.class);
        method.setAccessible(true);

        return event -> {
            // TODO: Utility method
            if ((annotation.code() == KeyCode.UNDEFINED || event.getCode() == annotation.code()) &&
                    (annotation.character().isEmpty() || event.getCharacter().equals(annotation.character())) &&
                    (annotation.text().isEmpty() || event.getText().equals(annotation.text())) &&
                    (event.isShiftDown() || !annotation.shift()) &&
                    (event.isControlDown() || !annotation.control()) &&
                    (event.isAltDown() || !annotation.alt()) &&
                    (event.isMetaDown() || !annotation.meta())
            ) {
                try {
                    if (hasEventParameter) {
                        method.invoke(instance, event);
                    } else {
                        method.invoke(instance);
                    }
                } catch (IllegalAccessException | InvocationTargetException e) {
                    throw new RuntimeException("Couldn't call method '" + method.getName() + "' in class '" + instance.getClass().getName() + "' on key event.", e);
                }
            }
        };
    }

    /**
     * Clears all key handlers registered for the given instance.
     *
     * @param instance The instance to clear the key handlers for
     */
    private void cleanUpListeners(Object instance) {
        Collection<Pair<onKey.Target, EventHandler<KeyEvent>>> handlers = keyEventHandlers.get(instance);
        if (handlers != null) {
            for (Pair<onKey.Target, EventHandler<KeyEvent>> handler : handlers) {
                switch (handler.getKey()) {
                    case SCENE -> app.get().stage().getScene().removeEventFilter(KeyEvent.ANY, handler.getValue());
                    case STAGE -> app.get().stage().removeEventFilter(KeyEvent.ANY, handler.getValue());
                }
            }
            keyEventHandlers.remove(instance);
        }
    }


    /**
     * Returns the title of the given controller instance if it has one.
     * If the title is a key, the title will be looked up in the resource bundle of the controller.
@Clashsoft
Copy link
Member

I just noticed there is a bug:

        boolean hasEventParameter = method.getParameterCount() == 1 && method.getParameterTypes()[0].isAssignableFrom(KeyEvent.class);

If the second condition is false, e.g. because the method is declared as

@onKey(...)
void wrongKey(String key) {
}

Then it will call method.invoke(instance); and error with Couldn't call method ....

So either the second condition can be removed, or there needs to be separate error. Possibly even an annotation processor check which should be trivial.

@LeStegii LeStegii changed the title Utility method Fix incorrect @onKey method header check Mar 4, 2024
@LeStegii
Copy link
Collaborator

LeStegii commented Mar 5, 2024

Fixed in #64

@LeStegii LeStegii closed this as completed Mar 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants