2


我想将某些字符路由到方法,以便在命令行中键入字符时执行该方法。

基于如何调用存储在 HashMap 中的方法的答案,我使用“命令”设计模式将这些字符映射到方法。

但是我想一般地实现这一点,所以似乎我需要实现反射才能将Method类用作参数。我的尝试是在我的匿名课堂 上获得一个NullPointerException现场...private Method method


这是我的代码:

import java.lang.reflect.Method;

public interface InvokesMethod {

    public void invokeMethod() throws Exception;
    public void setMethod(Method method);
} // end of interface


import java.util.HashMap;
import java.lang.reflect.Method;

public class Terminal {

    public HashMap<Character, InvokesMethod> commands;

    public Terminal() {
        this.commands = new HashMap<Character, InvokesMethod>();

        try {
            this.setCommand('p',
                 this.getClass().getDeclaredMethod("printHelloWorld"));

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void printHelloWorld() {
        System.out.println("Hello World!");
    }

    private void setCommand(char letter, Method method) {
        this.commands.put(letter, new InvokesMethod() {

            // NullPointerException starts here in the stack-trace:
            private Method method;

            @Override
            public void invokeMethod() throws Exception {
                method.invoke(null);
            }

            @Override
            public void setMethod(Method method) {
                this.method = method;
            }
        }).setMethod(method);
    }

    public void executeCommand(char letter) throws Exception {
        this.commands.get(letter).invokeMethod();
    }
} // end of class


public class Main() {

    public static void main(String[] args) {
        Terminal commandLine = new Terminal();

        try {
            commandLine.executeCommand('p');

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
} // end of class
4

2 回答 2

1

关于您没有启动的代码method。请记住,executenull必须调用公共静态方法:

您的另一个问题,您没有正确启动界面。这是工作示例:

调用MethodItf

public interface InvokesMethodItf {

public void invokeMethod() throws Exception;
public void setMethod(Method method);
} 

调用方法

public class InvokesMethod implements InvokesMethodItf{

private Method method;

@Override
public void invokeMethod() throws Exception {
     method.invoke(null);
}

@Override
public void setMethod(Method method) {
    this.method = method;
}

}

终端

public class Terminal {

public HashMap<Character, InvokesMethodItf> commands;

public Terminal() {
    this.commands = new HashMap<Character, InvokesMethodItf>();

    try {
        this.setCommand('p',
             this.getClass().getDeclaredMethod("printHelloWorld"));

    } catch (Exception e) {
        e.printStackTrace();
    }
}

public static void printHelloWorld() {// method.invoke(null) looking for "static" method
    System.out.println("Hello World!");
}


private void setCommand(char letter, Method method) {

    InvokesMethodItf inv = new InvokesMethod();

    inv.setMethod(method);

    this.commands.put(letter, inv);
}

public void executeCommand(char letter) throws Exception {
    this.commands.get(letter).invokeMethod();
 }
}

主要的

public class Main {
public static void main(String[] args) {
    Terminal commandLine = new Terminal();

    try {
        commandLine.executeCommand('p');

    } catch (Exception e) {
        e.printStackTrace();
    }
  }
}

输出:

Hello World!
于 2013-09-28T05:14:03.967 回答
0


感谢@Maxim 的原始建议,我有一个替代解决方案,方法是将方法设置为 HashMap 中的字符串——

import java.util.HashMap;
import java.lang.reflect.Method;

public class Terminal {

    private HashMap<Character, String> commands;

    public Terminal() {
        this.commands = new HashMap<Character, String>();
        this.commands.put('p', "printHelloWorld");
    }

    private void printHelloWorld() {
        System.out.println("Hello World!");
    }

    public void executeCommand(char letter) throws Exception {
        Method method = getClass().getDeclaredMethod(this.commands.get(letter));
        method.invoke(this);
    }


public class Main {
    public static void main(String[] args) {
        Terminal commandLine = new Terminal();

        try {
            commandLine.executeCommand('p');

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
} // end of class


输出:

Hello World!


现在要弄清楚如何将参数传递给反射方法......

于 2013-09-28T05:46:10.230 回答