71

Groovy Goodness: Where Is My Class? [Snippet]

 6 years ago
source link: https://www.tuicool.com/articles/hit/ErauiiN
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

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.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK