Annotationsklasse

Adriano10

Bekanntes Mitglied
Java:
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)

public @interface Callable {

}

 public class ContextMenu extends javafx.scene.control.ContextMenu {
        public ContextMenu(Automaton automaton) {
            List<Method> methods = getMethods(automaton);
            for (Method method : methods) {
                MenuItem item = new MenuItem(method.getName());
                item.setOnAction(ev -> {
                    try {
                        method.invoke(automaton);
                    } catch (IllegalAccessException | InvocationTargetException e) {
                        Alert alert =
                                new Alert(Alert.AlertType.ERROR, "Beim Ausfuehren der Methode ist ein Fehler aufgetreten!", ButtonType.OK);
                        alert.showAndWait();
                    }
                });
                getItems().add(item);
            }
        }

        private List<Method> getMethods(Automaton automaton) {
            List<Method> res = new ArrayList<>();
            for (Method method : automaton.getClass().getDeclaredMethods()) {
                if (Modifier.isPublic(method.getModifiers()) && method.getParameterCount() == 0 &&
                    method.isAnnotationPresent(Callable.class)) {
                    res.add(method);
                }
            }
            return res;
        }
    }

public class Test{
        @Callable
        public void test(int i, int j) {
            System.out.println("huhu " + i + " :" + j );
        }
}

Moin, ich wünsche euch ein gesundes und erfolgreiches Jahr.

Also die Methode test(int i, int j) von der Klasse Test erscheint im ContextMenu nicht. Aber wenn ich die Methode ohne Parameter schreibe z.B test(), dann funktioniert.

Ganzen Tag versuche ich herauszufinden, woran das liegen könnte. Falls jemand schon damit Erfahrung gemacht hat, würde mir sehr geholfen
 

Neue Themen


Oben