Accessing column names in Processing.org Tables
For a project I am currently working on using the Processing programming language, I use the provided Table class to read in data from a flat CSV file.
The column names in this file have to match parameters specified elsewhere in the code. As a consquence, when validating the data from the file, I need access to the column names.
There is an undocumented private field in all instances of the Table object which contains the column names (when the flag to treat the first line of the file as column headers is flipped in the Table constructor).
However, in the words of Processing:
The field Table.columnTitles is not visible
Using Java reflection, we can access the contents of that field.
class DataHandler {
String filename;
Table table;
String[] column_titles;
DataHandler(String filename_) {
filename = filename_;
table = loadTable(filename, "header");
try {
java.lang.reflect.Field f = table.getClass().getDeclaredField("columnTitles");
f.setAccessible(true);
column_titles = (String[]) f.get(table);
}
catch (Exception exc) {
exc.printStackTrace();
}
}
}