Get functions / variables / classes from Processing and user

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