简体   繁体   English

未调用基于Java接口的多态性

[英]Java Interface-Based Polymorphism Not Being Called

I'm working on a program that takes in commands as string data and passes it to the correct handler, as defined by a HashMap . 我正在开发一个将命令作为字符串数据接收并将其传递给HashMap定义的正确处理程序的程序。

The code that passes the command to the correct handler is the following: 将命令传递给正确的处理程序的代码如下:

//This is run in its own thread, hence the run() method
@Override
public void run() {
    try {
        //Socket is a Socket object containing the client's connection
        InputStreamReader is = new InputStreamReader(socket.getInputStream());
        //MAX_MESSAGE_SIZE is an integer, specifically 1024
        char[] data = new char[MAX_MESSAGE_SIZE];
        is.read(data, 0, MAX_MESSAGE_SIZE);
        String strData = new String(data);
        //Split on UNIX or Windows type newline
        String[] lines = strData.split("\\r?\\n");
        //First verb determines the command
        String command = (lines[0].split(": "))[0];
        //Re-add the ": " because the HashMap will look for it
        if (handlerMap.containsKey((command + ": "))) {
            System.err.println("Got command: " + command);
            AbstractHandler handler = handlerMap.get(command);
            System.err.println("Passing connection + data to handler...");
            handler.handleData(socket, lines);
            System.err.println("Handler is done!");
        } else {
            System.err.println("Didn't understand command: " + command);
            DataOutputStream outputStream = new DataOutputStream(socket.getOutputStream());
            outputStream.writeBytes(Protocol.UNKNOWN_ERROR);
            outputStream.flush();
            socket.close();
        }
    } catch (IOException e) {
        System.err.println("Caught IOException: " + e.getMessage());
    }
}

The value part of the HashMap is an object that implements the interface AbstractHandler . HashMap的值部分是实现接口AbstractHandler的对象。 AbstractHandler defines the one method: handleData(Socket s, String[] lines) . AbstractHandler定义了一个方法: handleData(Socket s, String[] lines) For reference, here is where the map is initialized: 供参考,这是地图的初始化位置:

     public RequestManager(Socket socket) {
    this.socket = socket;
    handlerMap = new HashMap<>();
    handlerMap.put(Protocol.USERNAME, new AuthenticationHandler());
    //Set arg to true b/c it is a set request, not a change request
    handlerMap.put(Protocol.SET_PASSWORD, new ChangePasswordHandler(true));
    handlerMap.put(Protocol.CHANGE_PASSWORD, new ChangePasswordHandler());
    handlerMap.put(Protocol.FORGOT_PASSWORD, new ForgotPasswordHandler());
}

and all of the handleData methods in the objects just contain the following code: 并且对象中的所有handleData方法仅包含以下代码:

@Override
public void handleData(Socket s, String[] lines) {
     clientSocket = s; //clientSocket field in class
     System.err.println("Made it into handler");
}

What's strange is after "Passing connection + data to handler" is shown, nothing happens. 奇怪的是,在显示“将连接+数据传递给处理程序”后,什么都没有发生。 I don't see anything about getting into the handler, nor do I see exceptions or the message that the handler is done. 我没有看到有关进入处理程序的任何信息,也没有看到处理程序完成的异常或消息。 What could cause this? 是什么原因造成的?

You test to see if a handler exists with 您测试以查看是否存在处理程序

if (handlerMap.containsKey((command + ": "))) {

but you try and get a handler with 但是你尝试得到一个处理程序

AbstractHandler handler = handlerMap.get(command);

So if it exists as for the key CommandName: , you presumably won't get it with the key CommandName . 因此,如果它与键CommandName:一样存在,则可能不会通过键CommandName获得它。 Therefore you'll have an unchecked NullPointerException on the call handler.handleData(socket, lines); 因此,您将在调用handler.handleData(socket, lines);上有一个未经检查的NullPointerException handler.handleData(socket, lines); and your runnable will die a horrid death. 你的可跑者会死得很惨。

It looks like you need to change either the first, or the second. 看来您需要更改第一个或第二个。 Given you say you get to see it print "Passing connection + data to handler..." I think you need to change it to: 假设您说可以看到它显示“正在将连接+数据传递给处理程序...”,我认为您需要将其更改为:

AbstractHandler handler = handlerMap.get(command + ": ");

A small stylistic change when you deal with maps would prevent this biting you now and in the future. 在处理地图时,如果进行小的样式更改,就可以防止这种情况在现在和将来对您造成影响。 Map.get returns null if the key is not found , so you can just do: 如果找不到键,则 Map.get 返回null ,因此您可以执行以下操作:

AbstractHandler handler = handlerMap.get(command + ": ");
if (handler != null) {
    /* ... */
    handler.handleData(socket, lines);
    System.err.println("Handler is done!");
} else {
    /* ... */
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM