lcazarre
lcazarre

Reputation: 743

How to list methods that are not inherited?

I have the following two Java classes (Command and Player). I would like to be able to list from the Command class all the public methods from the Player class, without having to hard-code them. I tried several ways, including an interface, or the command pattern, but I am not fluent in Java and I did not manage to make them work.

It seems to me that using Java's reflection API would be easier. However, when I use it to print the public methods from the Player class, I get extraneous methods, which I assume were inherited from the Object class.

Is there a way to only include those methods I defined myself (i.e. those starting with "public method: public void Player.")?

Thank you,

LC

Here is the output I get:

public method: public void Player.search(Command)
public method: public Room Player.getCurrentRoom()
public method: public void Player.engage()
public method: public void Player.trade(Command)
public method: public void Player.goRoom(Command)
public method: public void Player.takeItem(Command)
public method: public void Player.dropItem(Command)
public method: public void Player.lock(Command)
public method: public void Player.unlock(Command)
public method: public final void java.lang.Object.wait(long,int) throws java.lang.InterruptedException
public method: public final native void java.lang.Object.wait(long) throws java.lang.InterruptedException
public method: public final void java.lang.Object.wait() throws java.lang.InterruptedException
public method: public boolean java.lang.Object.equals(java.lang.Object)
public method: public java.lang.String java.lang.Object.toString()
public method: public native int java.lang.Object.hashCode()
public method: public final native java.lang.Class java.lang.Object.getClass()
public method: public final native void java.lang.Object.notify()
public method: public final native void java.lang.Object.notifyAll()

Here is the Command class:

import java.lang.reflect.Method;

public class Command
{

    // a constant array that holds all valid command words
    private static String[] validCommands;      

    private String commandWord;
    private String secondWord;

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

        [...] //some code omitted

    public boolean process(Player player) 
    {

        Class pClass = player.getClass();
        Method[] methods = pClass.getMethods();
        for (int i = 0; i < methods.length; i++) {
            System.out.println("public method: " + methods[i]);
        }

        boolean wantToQuit = false;

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

        if (commandWord.equals("help")) {
            printHelp();
        }
        else if (commandWord.equals("go")) {
            player.goRoom(this);
        }
        else if (commandWord.equals("quit")) {
            wantToQuit = quit();
        }
        else if (commandWord.equals("take")) {
            player.takeItem(this);
        }
        else if (commandWord.equals("drop")) {
            player.dropItem(this);
        }
        else if (commandWord.equals("search")) {
            player.search(this);
        }
        else if (commandWord.equals("engage")) {
            player.engage();
        }
        else if (commandWord.equals("trade")) {
            player.trade(this);
        }
        else if (commandWord.equals("lock")) {
            player.lock(this);
        }
        else if (commandWord.equals("unlock")) {
            player.unlock(this);
        }
        else {
            System.out.println("Invalid command. Type 'help' if you forgot the list of available commands.");
        }
        // else command not recognised.
        return wantToQuit;
    }
}

Here is the outline of the Player class:

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)
    {   
        [...]
    }

}

Upvotes: 0

Views: 244

Answers (1)

the baldheadedguy
the baldheadedguy

Reputation: 183

You want to use the getDeclaredMethods() class from the Java reflection API.

http://docs.oracle.com/javase/7/docs/api/java/lang/Class.html#getDeclaredMethods()

That gives you only the methods declared on the class in question, ignoring superclasses and interfaces.

Upvotes: 2

Related Questions