Get functions / variables / classes from processing and user

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

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
Classes solved in: post #8
Variables solved in: post #10

2 Likes

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

1 Like

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.

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

See Calling functions with variables - #6 by Schred

1 Like

Here is my approach:

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.

1 Like

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?

1 Like

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

Yep :stuck_out_tongue:

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;
  }
}
2 Likes

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

The resolution is analogous to the used for methods

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;
}
2 Likes

Thanks a ton for this!

1 Like

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

1 Like