# Get functions / variables / classes from Processing and user

**URL:** <https://discourse.processing.org/t/get-functions-variables-classes-from-processing-and-user/30100>\
**Category:** Development\
**Created:** [May 14, 2021, 3:25pm UTC](https://discourse.processing.org/t/get-functions-variables-classes-from-processing-and-user/30100 "2021-05-14T15:25:15Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![Flolo](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/flolo/32/13874_2.png) [@Flolo](https://discourse.processing.org/u/Flolo)\
**Post date:** [May 14, 2021, 3:25pm UTC](https://discourse.processing.org/t/get-functions-variables-classes-from-processing-and-user/30100/1 "2021-05-14T15:25:15Z")

</div>

Hey, I want to write a visual scripting tool (with eclipse) and for that I need a function with which I can find all functions in the sketch and get the parameters.

Processing does the same, if you press CTRL and space bar, then variables and functions are suggested (with parameters).

Here’s a picture:  
 ![Processing](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/d/dde06f53f17be8cf391c75712360b3ee3fdb6b35.png)

For example, I call getFunctions () in the tool script and then it should give me an array with all the functions that are open in processing.

Thanks

**Functions solved in:** [post #5](https://discourse.processing.org/t/get-functions-variables-classes-from-processing-and-user/30100/5)  
**Classes solved in:** [post #8](https://discourse.processing.org/t/find-functions-with-parameters-like-processing/30100/8)  
**Variables solved in:** [post #10](https://discourse.processing.org/t/get-functions-variables-classes-from-processing-and-user/30100/10)

---

<div class="post-metadata">

**Author:** ![Ov3rM1nD](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/ov3rm1nd/32/1756_2.png) [@Ov3rM1nD](https://discourse.processing.org/u/Ov3rM1nD)\
**Post date:** [May 14, 2021, 4:00pm UTC](https://discourse.processing.org/t/get-functions-variables-classes-from-processing-and-user/30100/2 "2021-05-14T16:00:03Z")

</div>

Do you only want the default Processing methods, or do you want to include methods written by users as well?

---

<div class="post-metadata">

**Author:** ![Flolo](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/flolo/32/13874_2.png) [@Flolo](https://discourse.processing.org/u/Flolo)\
**Post date:** [May 14, 2021, 5:12pm UTC](https://discourse.processing.org/t/get-functions-variables-classes-from-processing-and-user/30100/3 "2021-05-14T17:12:04Z")

</div>

I want to include methods written by users as well.  
But if that is not possible, then the normal functions of processing would also be ok.

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [May 14, 2021, 5:41pm UTC](https://discourse.processing.org/t/get-functions-variables-classes-from-processing-and-user/30100/4 "2021-05-14T17:41:31Z")

</div>

Here Method[] all = getClass().getDeclaredMethods();

See [Calling functions with variables - #6 by Schred](https://discourse.processing.org/t/calling-functions-with-variables/30001/6)

---

<div class="post-metadata">

**Author:** ![Ov3rM1nD](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/ov3rm1nd/32/1756_2.png) [@Ov3rM1nD](https://discourse.processing.org/u/Ov3rM1nD)\
**Post date:** [May 14, 2021, 5:47pm UTC](https://discourse.processing.org/t/get-functions-variables-classes-from-processing-and-user/30100/5 "2021-05-14T17:47:17Z")

</div>

Here is my approach:

```auto
import java.lang.reflect.Method;
import java.util.Collections;

void setup() {
  printMethods(getClass().getMethods()); // all default processing methods
  printMethods(getClass().getDeclaredMethods()); // user methods
  exit();
}

String clean(String str) {
  str = str.substring(str.lastIndexOf('.') + 1);
  return str.replaceAll("[^\\w]", "");
}

void printMethods(Method[] list) {
  ArrayList<String> result = new ArrayList();

  for (Method m : list) {
    String methodName = clean(m.getName());  
    String returnType = clean(m.getReturnType().toString());
    String params = "";
    for (Class p : m.getParameterTypes()) {
      params += clean(p.getName()) + " ";
    }

    result.add(methodName + "(" + params.trim() + ") : " + returnType);
  }

  Collections.sort(result);

  for (String s : result) println(s);
}

```

in short, you can do this through Java Reflections.

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [May 14, 2021, 6:10pm UTC](https://discourse.processing.org/t/get-functions-variables-classes-from-processing-and-user/30100/6 "2021-05-14T18:10:44Z")

</div>

Besides getting the methods, is there a way to get all the classes that I‘ve written in a sketch and for each class its methods?

---

<div class="post-metadata">

**Author:** ![Ov3rM1nD](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/ov3rm1nd/32/1756_2.png) [@Ov3rM1nD](https://discourse.processing.org/u/Ov3rM1nD)\
**Post date:** [May 14, 2021, 6:46pm UTC](https://discourse.processing.org/t/get-functions-variables-classes-from-processing-and-user/30100/8 "2021-05-14T18:46:51Z")

</div>

> is there a way to get all the classes that I‘ve written in a sketch and for each class its methods?

Yep 😛

```auto
import java.lang.reflect.Method;
import java.util.Collections;

void setup() {
  println("\n *****PAPPLET METHODS:*****");
  printMethods(getClass().getMethods());
  
  println("\n *****USER METHODS:*****");
  printMethods(getClass().getDeclaredMethods());

  println("\n *****USER CLASS METHODS:*****");
  for (Class c : getClass().getDeclaredClasses()) printMethods(c.getDeclaredMethods());
  
  exit();
}

String clean(String str) {
  str = str.substring(str.lastIndexOf('.') + 1);
  if (str.contains("$")) str = str.substring(str.indexOf('$') + 1); // get rid of the sketch class name and keep only the child class name
  return str.replaceAll("[^\\w]", "");
}

void printMethods(Method[] list) {
  ArrayList<String> result = new ArrayList();

  for (Method m : list) {
    String methodName = clean(m.getName());  
    String returnType = clean(m.getReturnType().toString());
    String params = "";
    for (Class p : m.getParameterTypes()) {
      params += clean(p.getName()) + " ";
    }

    result.add(methodName + "(" + params.trim() + ") : " + returnType);
  }

  Collections.sort(result);

  for (String s : result) println(s);
}

class Foo {
  void fooMeth1() { }
  
  Foo fooMeth2(Foo f, String s, Integer i) {
    return f;
  }
}

class Bar {
  void barMeth1() { }
  
  String[] barMeth2(String[] s) {
    return s;
  }
}

```

---

<div class="post-metadata">

**Author:** ![Flolo](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/flolo/32/13874_2.png) [@Flolo](https://discourse.processing.org/u/Flolo)\
**Post date:** [May 14, 2021, 7:01pm UTC](https://discourse.processing.org/t/get-functions-variables-classes-from-processing-and-user/30100/9 "2021-05-14T19:01:36Z")

</div>

And is there a way to get all the variables from Processing and the user variables?

---

<div class="post-metadata">

**Author:** ![Ov3rM1nD](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/ov3rm1nd/32/1756_2.png) [@Ov3rM1nD](https://discourse.processing.org/u/Ov3rM1nD)\
**Post date:** [May 14, 2021, 7:29pm UTC](https://discourse.processing.org/t/get-functions-variables-classes-from-processing-and-user/30100/10 "2021-05-14T19:29:18Z")

</div>

The resolution is analogous to the used for methods

```auto
import java.lang.reflect.Field;
import java.util.Collections;

Foo foo;
String something;
int somthingElse;

void setup() {
  println("\n *****PAPPLET FIELDS:*****");
  printFields(getClass().getFields());

  println("\n *****USER FIELDS:*****");
  printFields(getClass().getDeclaredFields());

  println("\n *****USER CLASS FIELDS:*****");
  for (Class c : getClass().getDeclaredClasses()) printFields(c.getDeclaredFields());

  exit();
}

String clean(String str) {
  str = str.substring(str.lastIndexOf('.') + 1);
  if (str.contains("$")) str = str.substring(str.indexOf('$') + 1); // get rid of the sketch class name and keep only the child class name
  return str.replaceAll("[^\\w]", "");
}

void printFields(Field[] list) {
  ArrayList<String> result = new ArrayList();

  for (Field f : list) {
    String name = clean(f.getName());
    String type = clean(f.getType().toString());

    result.add(name + " : " + type);
  }
  Collections.sort(result);
  for (String s : result) println(s);
}

class Foo {
  int fooInt;
  boolean fooState;
  Foo fooInstance;
}

```

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [May 14, 2021, 8:23pm UTC](https://discourse.processing.org/t/get-functions-variables-classes-from-processing-and-user/30100/11 "2021-05-14T20:23:19Z")

</div>

> [@Ov3rM1nD](#):
>
> Yep

Thanks a ton for this!

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [May 15, 2021, 6:44pm UTC](https://discourse.processing.org/t/get-functions-variables-classes-from-processing-and-user/30100/12 "2021-05-15T18:44:43Z")

</div>

How far does this go, can I inquire

- which functions are used by a class (that I wrote) outside of itself (in my Sketch, but outside any classes) and
- which functions are used by a class (that I wrote) outside of itself (the class) in other classes that I wrote

That would be great!

Is there an overview for that?

Chrisir

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [March 28, 2023, 12:43pm UTC](https://discourse.processing.org/t/get-functions-variables-classes-from-processing-and-user/30100/13 "2023-03-28T12:43:22Z")

</div>

I am sorry, how can I get a list of the functions of a Sketgch and a list of the methods of a class therein?

Is there a documentation for this?

Can I analyze another Sketch B by giving the path of B in the Analyzing Sketch A? (when the Sketch A contains  
getClass().getDeclaredMethods() etc.)

Thank you all!

Also, can I get the functions that are called by a certain class?
