Groovy 2.5.0 makes it possible to get the location of a Class file by adding the method getLocation to the Class class. If the Class is part of the JDK the location returned is null, but otherwise we get the location of the JAR file or source file (if available) with the Class file.

In the following example we get the location for the internal JDK String class and the Groovy utility class ConfigSlurper:

// Internal JDK class location is null.
assert String.location == null


// Import ConfigSlurper with alias.
import groovy.util.ConfigSlurper as ConfigReader

// Location of Groovy JAR file.
def groovyJarFile = 'file:/Users/mrhaki/.sdkman/candidates/groovy/2.5.0/lib/groovy-2.5.0.jar'.toURL()

// ConfigSlurper is located in the Groovy JAR file.
assert ConfigSlurper.location == groovyJarFile

// Works also for aliased class.
assert ConfigReader.location == groovyJarFile

Written with Groovy 2.5.0.

shadow-left