Over a million developers have joined DZone.

Groovy Goodness: Where Is My Class? [Snippet]

DZone's Guide to

Groovy Goodness: Where Is My Class? [Snippet]

Wondering where your class is in Groovy? Look through this post to find out how to get the location of your Class file.

· Java Zone ·
Free Resource

Download Microservices for Java Developers: A hands-on introduction to frameworks and containers. Brought to you in partnership with Red Hat.

Groovy 2.5.0 makes it possible to get the location of a Class file by adding the method getLocation to 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.

Download Building Reactive Microservices in Java: Asynchronous and Event-Based Application Design. Brought to you in partnership with Red Hat

java ,groovy ,class ,code ,location ,tutorial

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}