自定义 EventListener 检查类覆盖的抽象方法

你好,所以我得到了以下代码:


事件处理程序.java


package me.xenopyax.edla.watcher;


import static java.nio.file.StandardWatchEventKinds.*;


import java.io.IOException;

import java.nio.file.FileSystem;

import java.nio.file.Path;

import java.nio.file.Paths;

import java.nio.file.WatchEvent;

import java.nio.file.WatchEvent.Kind;

import java.util.ArrayList;

import java.util.List;

import java.nio.file.WatchKey;

import java.nio.file.WatchService;


public class EventHandler {


private Path folderPath = Paths.get(System.getProperty("user.dir") + "/Saved Games/Frontier Developments/Elite Dangerous");

private String watchFile;

private List<EventListener> listeners = new ArrayList<>();


  public EventHandler()  {


    // We obtain the file system of the Path

    FileSystem fileSystem = folderPath.getFileSystem();


    // We create the new WatchService using the try-with-resources block

    try (WatchService service = fileSystem.newWatchService()) {

        // We watch for modification events

        folderPath.register(service, ENTRY_MODIFY);


        // Start the infinite polling loop

        while (true) {

            // Wait for the next event

            WatchKey watchKey = service.take();


            for (WatchEvent<?> watchEvent : watchKey.pollEvents()) {

                // Get the type of the event

                Kind<?> kind = watchEvent.kind();


                if (kind == ENTRY_MODIFY) {

                    Path watchEventPath = (Path) watchEvent.context();


                    // Call this if the right file is involved

                    if (watchEventPath.toString().equals(watchFile)) {

                        //File has been modified, call event registered


                    }

                }

            }


            if (!watchKey.reset()) {

                // Exit if no longer valid

                break;

            }


        }


现在我的问题是如何从EventHandler.java中的EventListener.java调用抽象方法以及如何在ArrayList中检查哪些方法被覆盖?我正在尝试创建一个监听文件的 EventHandler,当发生更改时,它会查找更改的内容并从EventListener.java触发适当的抽象方法。


慕桂英4014372
浏览 118回答 1
1回答

泛舟湖上清波郎朗

如果它不是您的抽象类,则可以检查方法的声明类,然后该方法被覆盖。import sun.reflect.generics.reflectiveObjects.NotImplementedException;import java.lang.reflect.Method;import java.util.ArrayList;import java.util.List;class Main {&nbsp; &nbsp; static abstract class EventListener {&nbsp; &nbsp; &nbsp; &nbsp; public void onFileChanged() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; throw new NotImplementedException();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; static class EventListenerNotImpl extends EventListener {&nbsp; &nbsp; }&nbsp; &nbsp; static class EventListenerImpl extends EventListener {&nbsp; &nbsp; &nbsp; &nbsp; private String id;&nbsp; &nbsp; &nbsp; &nbsp; public EventListenerImpl(String id) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.id = id;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; public void onFileChanged() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println(id + ":" + EventListenerImpl.class.getCanonicalName() + ".onFileChanged() was called");&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; static class EventHandler {&nbsp; &nbsp; &nbsp; &nbsp; private List<EventListener> listeners = new ArrayList<>();&nbsp; &nbsp; &nbsp; &nbsp; public void addListener(EventListener listener) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; listeners.add(listener);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; private void propagateOnFileChangedEvent() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; listeners.forEach(l -> {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Method m = l.getClass().getMethod("onFileChanged");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (!m.getDeclaringClass().equals(EventListener.class)) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; l.onFileChanged();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } catch (NoSuchMethodException e) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; e.printStackTrace();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; public static void main(String[] args) {&nbsp; &nbsp; &nbsp; &nbsp; EventHandler handler = new EventHandler();&nbsp; &nbsp; &nbsp; &nbsp; handler.addListener(new EventListenerImpl("listener-1"));&nbsp; &nbsp; &nbsp; &nbsp; handler.addListener(new EventListenerNotImpl()); // Not will be invoked onFileChangedEvent&nbsp; &nbsp; &nbsp; &nbsp; handler.addListener(new EventListenerImpl("listener-3"));&nbsp; &nbsp; &nbsp; &nbsp; handler.propagateOnFileChangedEvent();&nbsp; &nbsp; }}输出:listener-1:Main.EventListenerImpl.onFileChanged() was calledlistener-3:Main.EventListenerImpl.onFileChanged() was called
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java