This Script of the Day gives some help when writing a script, but not knowing exactly what methods are available.

Problem

The easiest way I know of to explore the QuPath source code while scripting is through IntelliJ.

Having an IDE to give easy access to the QuPath source code and auto-complete methods is invaluable.

But scripts are then generally run in QuPath through the Script Editor. The help it offers is minimal; the closest thing is has to auto-complete is the ability to press Ctrl + space to automatically complete the name of any built-in methods that you might have started to type.

Press Ctrl + space repeatedly to cycle through multiple compatible options.

Often, it would be useful to have some way to see quickly what methods are available without needing to hunt it down in the source code or open an IDE.

Solution

Java Reflection can help here.

Combined with Groovy, here’s a very quick way to print the available methods for any Java object:

// Get an object... but what can we do with it?!
def pathObject = getSelectedObject()

// Check available methods
pathObject.class.methods.each {
    println it
}

This can work on classes too; the following will print all the ‘built-in’ methods of QPEx.

import qupath.lib.scripting.QPEx
QPEx.methods.each {
    println it
}

And if you want to see the fields instead, try this:

import qupath.lib.scripting.QPEx
QPEx.fields.each {
    println it
}

If the ‘INFO’ bit that appears in the QuPath script editor when printing bothers you, here’s a workaround (for PathClass this time):

// Get an object & its classification
def pathObject = getSelectedObject()
def pathClass = pathObject.getPathClass()

def sb = new StringBuilder('\n')
pathClass?.class?.methods.each {
  sb << it << '\n'
}
println sb

This example has another bit of Groovy goodness, where the ?. syntax avoids errors if there is no PathClass (i.e. it is null).

All in all this is not a tremendously elegant approach to interrogate objects… but it can come in useful sometimes.