1

I'm making a command-line based tool in Java, and I was thinking I might be able to make it all a bit easier on my self if I could take user input and automatically find the needed functions based on the users input.

What I want to do, is, that if a user types:

User create newUser

The java code looks for a class called user, then looks for a function called create, and inputs newUser as the first argument of the function. Meaning typing the command "User create newUser" would make the java code trigger this line of code:

User.create("newUser");

And of cause, return errors if the class or function was not found, or so.

So far I've been playing with

Class.forName(cmdArg[0])

Where cmdArg[0] is the first word given by the Scanner, found by splitting where there's spaces.

Anyway, is it possible to achieve this in Java? Any help would be much appreciated.


My solution:

Okay, i got it working, my final solution was a combination of duffymo's and user978548's answer, my solution looks like this:

Class comArgs[] = new Class[1];
comArgs[0] = String.class;

String[] args = new String[1];
args[0] = commandArray[2];

Class.forName("code."+commandArray[0])
    .getDeclaredMethod(commandArray[1], comArgs)
    .invoke(null, args);

This is surrounded by a try/catch with allot of exceptions, but it works.

I also have a HashMap for which commands i want to receive.

The args variable can should be as long as the number of arguments needed for the called method.

3
  • And user create newUser eat food would evaluate to User.create("newUser").eat ("food"); or to User.create("newUser", "eat", "food"); or to User.create("newUser", "eat").food();? Commented Mar 26, 2012 at 12:40
  • @user unknown: Preferably it could just ignore everything bot the first three words. Commented Mar 26, 2012 at 12:46
  • What about int, float, Boolean, Date, JFrames? What about imports? I don't know any User.class in the JDK. Commented Mar 26, 2012 at 12:48

4 Answers 4

3

You can use the features built into the java.lang.Class class:

Class.forName(args[0]).newInstance();

Have your users input the fully-resolved class name and you don't have to worry about all those shenanigans. You'll need them anyway, because the short name might not be unique.

Another approach is to put the Class instances that you want users to be able to create in a Map and have them input the key.

Sign up to request clarification or add additional context in comments.

Comments

2

as duffymo said, Class.forName(args[0]).newInstance(); for the class, and as Chin Boon said, you have all that you want in reflections method. Like, to run your method: Object.class.getMethods()[find your method here].invoke(obj, args)

Comments

1

What you are looking for is Java reflection.

Comments

0

duffymo is correct - reflection is what you are prob talking about.

However, I would maybe suggest looking at a combination of Builder/Factory design patterns to make this a little nicer rather than using reflection to attempt to find the class/methods you want (although obviously this depends on the context of the problem and I am making some assumptions here!).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.