# Reflections and console

**URL:** <https://discourse.processing.org/t/reflections-and-console/2525>\
**Category:** Coding Questions\
**Created:** [August 10, 2018, 8:16am UTC](https://discourse.processing.org/t/reflections-and-console/2525 "2018-08-10T08:16:13Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![FurryNightShade](https://avatars.discourse-cdn.com/v4/letter/f/e5b9ba/32.png) [@FurryNightShade](https://discourse.processing.org/u/FurryNightShade)\
**Post date:** [August 10, 2018, 8:16am UTC](https://discourse.processing.org/t/reflections-and-console/2525/1 "2018-08-10T08:16:14Z")

</div>

Hello,  
I’ve made this in c# before and I’d like to know, how to do this in Java.  
I have a set of commands with arguments i.e.

```auto
//Command: "set var <string:name> <int:value> <bool:log>"
string[] commandName = new string[]{"set", "var"};
Type className = AriaVar.getType();
String methodName = "setVar";
string[] argumentTypes = new string[]{"string", "int", "bool"};

AriaCommandBlueprint acb = new AriaCommandBlueprint(commandName, argumentTypes, className, methodName);

```

A console will take a user input and run a method `methodName` in class `type` with arguments `argumentTypes`. I’d like to do this in Java, possibly inside Processing’s PApplet.

---

<div class="post-metadata">

**Author:** ![shoaibmirza](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/shoaibmirza/32/923_2.png) [@shoaibmirza](https://discourse.processing.org/u/shoaibmirza)\
**Post date:** [August 10, 2018, 8:58am UTC](https://discourse.processing.org/t/reflections-and-console/2525/2 "2018-08-10T08:58:31Z")

</div>

Hi. i hope this well work.

```auto
`String[] commandName = {"set", "var"};
String methodName = "setVar";
String [] argumentTypes = {"string", "int", "bool"};

String className = this.getClass().getSimpleName();

AriaCommandBlueprint acb = new AriaCommandBlueprint(commandName, argumentTypes, className, methodName);

```

---

<div class="post-metadata">

**Author:** ![lqdev](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/lqdev/32/29_2.png) [@lqdev](https://discourse.processing.org/u/lqdev)\
**Post date:** [August 10, 2018, 8:59am UTC](https://discourse.processing.org/t/reflections-and-console/2525/3 "2018-08-10T08:59:15Z")

</div>

You can use Java reflection.

```java
Method method = getClass().getDeclaredMethod("name", String.class); // gets method "name" with a String argument
method.invoke(this, "hello"); // invokes the method on this object with the argument "hello"

```

Using this knowledge you may have a console command like `set hello 2`. We can use it in our command parser:

```java
String cmd = "set hello 2";
String[] args = cmd.split("\\s+"); // we use a regular expression that can detect more than one space
Method cmdMethod = getClass().getDeclaredMethod("cmd_" + args[0], String[].class); // I don't remember the exceptions this method throws, you'll have to find out yourself
cmdMethod.invoke(this, args); // I don't remember the exceptions of this either

// then we can have a method like
void cmd_set(String[] args) {
  // do something
}

```

That method will be then called each time you use the command `set`.

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [August 10, 2018, 9:25am UTC](https://discourse.processing.org/t/reflections-and-console/2525/4 "2018-08-10T09:25:51Z")

</div>

Processing provides methods **method()** & **thread()** too. 🕶  
Plus the _args[]_ array w/ the strings passed when running the sketch. 🏃‍♂️
