Query available toolchains progammatically from the script

I’d like to achieve the following:

  1. If a valid version of the JDK 6 can be found pre-installed use that for compilation
  2. Otherwise, if a valid version of the JDK7 can be found pre-installed, warn and use that for compilation
  3. Otherwise, fall back to an auto-provisioned version of the JDK 8 and warn.

Is there any way I can do so from within the build script?
I do not want to parse the output of gradle -q javaToolchains, I’d like access to that information programmatically. Thanks.