thebreadcat
thebreadcat

Reputation: 11

reading imports from text file in java/groovy

i would like to know if it is possible to somehow read data from a text file and use it to tell java what libaries to import. I dont think its possible in java but what about groovy?

Upvotes: 1

Views: 363

Answers (3)

Jacek Cz
Jacek Cz

Reputation: 1906

Groovy has a "import handler", class (interface to be true) GroovyResourceLoader, a kind of event is fired for every import required by "main" source. Is works in cooperation with GroovyClassLoader and maybe not with GroovyShell. I understand that my answer is in different area that "automatically add some hidden imports" - I speak about "classic imports".

Upvotes: 0

ataylor
ataylor

Reputation: 66109

With groovy 1.8, it is possible to dynamically add imports to a script executed with GroovyShell. In particular, take a look at org.codehaus.groovy.control.customizers.ImportCustomizer. There's a good example of how to use this here: http://mrhaki.blogspot.com/2011/06/groovy-goodness-add-imports.html.

Upvotes: 1

jhocking
jhocking

Reputation: 5577

You can't totally avoid having 'import' statements, but it is an important part of many implementations of dependency injection that the actual dependencies are defined in a config file (ie. external text) and this information is loaded at runtime.

Upvotes: 0

Related Questions