Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

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

Verify, standardize, and correct the Big 4 + more– name, email, phone and global addresses – try our Data Quality APIs now at Melissa Developer Portal!

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.

Developers! Quickly and easily gain access to the tools and information you need! Explore, test and combine our data quality APIs at Melissa Developer Portal – home to tools that save time and boost revenue. Our APIs verify, standardize, and correct the Big 4 + more – name, email, phone and global addresses – to ensure accurate delivery, prevent blacklisting and identify risks in real-time.

Topics:
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 }}