Java getMethod()导致NoSuchMethodException错误

时间:2014-04-02 18:23:58

标签: java reflection

我试图在用户输入字符串命令时使用反射来启动适当的方法。例如,如果用户输入"去"在终端中,Player类的go()方法将由Command类的process()方法调用。

然而,我无法使我的代码工作,并且我得到NoSuchMethodException错误,我不知道如何修复。问题根源的行是Command类的一半(完整的类在底部复制):

        try {
            Method method = pClass.getMethod(commandWord);
            method.invoke(player, this);
        }
        catch (NoSuchMethodException err1) {
            System.out.println("No such method");
        }

有人可以指导我吗?我提前谢谢你。

LC

命令类:

import java.util.ArrayList;
import java.lang.reflect.*;

public class Command
{

    private String commandWord;
    private String secondWord;

    public Command(String firstWord, String secondWord)
    {
        commandWord = firstWord;
        this.secondWord = secondWord;
    }

    public boolean process(Player player) 
    {
        ArrayList<String> validCommands = new ArrayList<String>();
        String methodName;
        int index;

        Class pClass = player.getClass();
        Method[] methods = pClass.getDeclaredMethods();
        for (int i = 0; i < methods.length; i++) {
            if (Modifier.isPublic(methods[i].getModifiers())) {
                validCommands.add(methods[i].getName());
            }
        }

        boolean wantToQuit = false;

        if(commandWord == null) {
            System.out.println("I don't know what you mean...");
            return false;
        }

        if (commandWord.equals("help")) {
            System.out.println("You are lost. You are alone. You wander");
            System.out.println("around at the university.");
            System.out.println();
            System.out.println("Your command words are:");

            for(String command: validCommands) {
                System.out.print(command + "  ");
            }
            System.out.println();
        }
        else if (commandWord.equals("quit")) {
            wantToQuit = quit();
        }

        //THIS IS WHERE I GET THE ERROR
        try {
            Method method = pClass.getMethod(commandWord);
            method.invoke(player, this);
        }
        catch (NoSuchMethodException err1) {
            System.out.println("No such method");
        }
        catch (IllegalAccessException err2) {
            System.out.println("Illegal access");
        }
        catch (InvocationTargetException err3) {
            System.out.println("Illegal access");
        }

        return wantToQuit;
    }

    [...] //some methods omitted
}

玩家类:

public class Player
{
    private String name;
    private Room currentRoom;
    private ArrayList<Item> items;

    Player (String name, Room startingRoom)
    {
        this.name = name;
        items = new ArrayList<Item>();
        this.currentRoom = startingRoom;
        printWelcome();
    }

    public void engage()
    {
        [...]
    }

    public void trade(Command command) 
    {
        [...]       }

    public void goRoom(Command command) 
    {   
        [...]       }

    public void search(Command command) 
    {
        [...]       }

    public void takeItem(Command command) 
    {
        [...]       }

    public void dropItem(Command command) 
    {
        [...]       }

    public void lock(Command command)
    {   
        [...]       }

    public void unlock(Command command)
    {   
        [...]
    }

}

1 个答案:

答案 0 :(得分:2)

试试这个。

pClass.getMethod(commandWord, Command.class)

在我看来,问题在于你正在寻找没有任何方法的方法 参数虽然这些方法似乎都有Command类型的参数。

有关详细信息,请参阅此处:

Class.getMethod JavaDoc