Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Parse multiple classes from groovy file

Tags:

groovy

Is there a way to parse all all the classes in a groovy script? To Parse ONE class right now:

java.lang.Class clazz = groovyClassLoader.parseClass(new File("MainApp.groovy"))

MainApp.groovy:

class MainApp {
   def doIt() {}
}

class OtherMainApp {
  def doTheRest() {}
}

This will return only MainApp.

I would like something like this:

java.lang.Class[] clazz = groovyClassLoader.parseClass(new File("MainApp.groovy"))

where clazz contains will contain both MainApp class and OtherMainApp class

Basically I want to be able to extract all the declared classes in a script.

Because of the nature of the app that I'm building groovyc command won't help

Thanks,

Federico

like image 336
Federico Avatar asked May 17 '26 02:05

Federico


1 Answers

No can do:

https://issues.apache.org/jira/browse/GROOVY-3793

You could do it yourself though: you could parse the class yourself (just count the {} pairs), dump it out to a new file, and away you go. Ugly? yes. Painful? Very. Possible? Maybe. Better solution? Not until Groovy fixes the bug.

like image 80
corsiKa Avatar answered May 18 '26 14:05

corsiKa