java自定义注释初始化局部变量

时间:2016-12-09 09:24:21

标签: java annotations

下面的代码有很多String类型的变量需要初始化为默认值。我想创建自定义注释来初始化它们。示例代码是

private static final PseudoClass PRESSED = PseudoClass.getPseudoClass("pressed");

@Override
public void start(Stage primaryStage) {
    Spinner<Integer> spinner = new Spinner(Integer.MIN_VALUE, Integer.MAX_VALUE, 0);

    class IncrementHandler implements EventHandler<MouseEvent> {

        private Spinner spinner;
        private boolean increment;
        private long startTimestamp;

        private static final long DELAY = 1000l * 1000L * 750L; // 0.75 sec
        private Node button;

        private final AnimationTimer timer = new AnimationTimer() {

            @Override
            public void handle(long now) {
                if (now - startTimestamp >= DELAY) {
                    // trigger updates every frame once the initial delay is over
                    if (increment) {
                        spinner.increment();
                    } else {
                        spinner.decrement();
                    }
                }
            }
        };

        @Override
        public void handle(MouseEvent event) {
            if (event.getButton() == MouseButton.PRIMARY) {
                Spinner source = (Spinner) event.getSource();
                Node node = event.getPickResult().getIntersectedNode();

                Boolean increment = null;
                // find which kind of button was pressed and if one was pressed
                while (increment == null && node != source) {
                    if (node.getStyleClass().contains("increment-arrow-button")) {
                        increment = Boolean.TRUE;
                    } else if (node.getStyleClass().contains("decrement-arrow-button")) {
                        increment = Boolean.FALSE;
                    } else {
                        node = node.getParent();
                    }
                }
                if (increment != null) {
                    event.consume();
                    source.requestFocus();
                    spinner = source;
                    this.increment = increment;

                    // timestamp to calculate the delay
                    startTimestamp = System.nanoTime();

                    button = node;

                    // update for css styling
                    node.pseudoClassStateChanged(PRESSED, true);

                    // first value update
                    timer.handle(startTimestamp + DELAY);

                    // trigger timer for more updates later
                    timer.start();
                }
            }
        }

        public void stop() {
            timer.stop();
            button.pseudoClassStateChanged(PRESSED, false);
            button = null;
            spinner = null;
        }
    }

    IncrementHandler handler = new IncrementHandler();
    spinner.addEventFilter(MouseEvent.MOUSE_PRESSED, handler);
    spinner.addEventFilter(MouseEvent.MOUSE_RELEASED, evt -> {
        if (evt.getButton() == MouseButton.PRIMARY) {
            handler.stop();
        }
    });

    Scene scene = new Scene(spinner);

    primaryStage.setScene(scene);
    primaryStage.show();
}

然而,正如你可以假设的那样,它不会编译。如何使用注释来实现此要求。

0 个答案:

没有答案
相关问题