Get functions / variables / classes from Processing and user

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