[groovy-user] Add a folder to groovy class loader

148 views
Skip to first unread message

Jorge Franco Leza

unread,
Nov 23, 2012, 7:25:12 PM11/23/12
to us...@groovy.codehaus.org
Hi!

Trying to add a folder to ClassLoader, but failing.

Executing code with groovy -cp path/to/dir - Ok
Adding in execution time - No Ok

So this is the code:

**************************************************
def GroovyClassLoader gcl = new GroovyClassLoader(this.class.getClassLoader())
def clz = gcl.loadClass("org.codehaus.groovy.ast.builder.AstBuilder")
ast = (AstBuilder)clz.newInstance()

list = ast.buildFromString(CompilePhase.SEMANTIC_ANALYSIS,script)
**************************************************

Always get fail when compiling (ast.buildFromString), script has a reference to another groovy class file.
Tryed gcl.addClasspath but ignores it.
Tryed create GroovyClassLoader with CompilerConfiguration, ignores too.
I suppose when compiling not using ast classloader. But I don't know the way for adding folder to compiler class loader.

Do anyone got it? Any code that works?

Thank you!



---------------------------------------------------------------------
To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email


Jochen Theodorou

unread,
Nov 26, 2012, 5:29:31 AM11/26/12
to us...@groovy.codehaus.org
Am 24.11.2012 01:25, schrieb Jorge Franco Leza:
> Hi!
>
> Trying to add a folder to ClassLoader, but failing.
>
> Executing code with groovy -cp path/to/dir - Ok
> Adding in execution time - No Ok
>
> So this is the code:
>
> **************************************************
> def GroovyClassLoader gcl = new GroovyClassLoader(this.class.getClassLoader())
> def clz = gcl.loadClass("org.codehaus.groovy.ast.builder.AstBuilder")
> ast = (AstBuilder)clz.newInstance()
>
> list = ast.buildFromString(CompilePhase.SEMANTIC_ANALYSIS,script)
> **************************************************
> Always get fail when compiling (ast.buildFromString), script has a reference to another groovy class file.
> Tryed gcl.addClasspath but ignores it.
> Tryed create GroovyClassLoader with CompilerConfiguration, ignores too.
> I suppose when compiling not using ast classloader. But I don't know the way for adding folder to compiler class loader.

I tihnk the astbuilder does not support this. It takes a vanilla
GroovyClassLoader, thus it will ignore anything you do with your gcl in
the code above. Can you explain why you need that?

bye blacdrag


--
Jochen "blackdrag" Theodorou - Groovy Project Tech Lead
blog: http://blackdragsview.blogspot.com/
german groovy discussion newsgroup: de.comp.lang.misc
For Groovy programming sources visit http://groovy-lang.org

Jorge Franco

unread,
Nov 26, 2012, 6:17:58 AM11/26/12
to us...@groovy.codehaus.org

In grooscript, people can have groovy files to be converted in a random directory. Inside, can have some diferent folder/packages with different classes. For convert a file or code fragment, grooscript compiles that file, and that file can have dependencies to other files. If the classpath not contain that random directory, then compilation fails. I thought about add a property, to people can set a directory for grooscript compilation. And add that directory to classpath in execution time.

Can be boring if want to automate conversion task, or using files in a war or something. At the moment can only add directory to classpath before execute it, looking for offer other way.

Thanks!

2012/11/26 Jochen Theodorou <blac...@gmx.org>

Jochen Theodorou

unread,
Nov 26, 2012, 6:55:50 AM11/26/12
to us...@groovy.codehaus.org
Am 26.11.2012 12:17, schrieb Jorge Franco:
> In grooscript, people can have groovy files to be converted in a random
> directory. Inside, can have some diferent folder/packages with different
> classes. For convert a file or code fragment, grooscript compiles that
> file, and that file can have dependencies to other files.

converted in what way?

> If the
> classpath not contain that random directory, then compilation fails. I
> thought about add a property, to people can set a directory for
> grooscript compilation. And add that directory to classpath in execution
> time.
>
> Can be boring if want to automate conversion task, or using files in a
> war or something. At the moment can only add directory to classpath
> before execute it, looking for offer other way.

maybe AstBuilder needs to be exteneded for your purpose.

bye blackdrag

Jorge Franco

unread,
Nov 26, 2012, 9:47:26 AM11/26/12
to us...@groovy.codehaus.org
Convert groovy files to javascript files. From groovy code, get the AST tree, visit it and generate javascript code. Need AST tree from groovy code.

Thank you for help.

2012/11/26 Jochen Theodorou <blac...@gmx.org>

Jochen Theodorou

unread,
Nov 26, 2012, 10:29:39 AM11/26/12
to us...@groovy.codehaus.org
Am 26.11.2012 15:47, schrieb Jorge Franco:
> Convert groovy files to javascript files. From groovy code, get the AST
> tree, visit it and generate javascript code. Need AST tree from groovy code.

ah I see... do you need to use AstBuilder for that, or can you change to
CompilationUnit? Because there you can set those things easily.
buildFromSource/String is really only a tiny wrapper around that.

Jorge Franco Leza

unread,
Nov 26, 2012, 1:58:30 PM11/26/12
to us...@groovy.codehaus.org
Cheers, got it with CompilationUnit, thank you vey much.
Reply all
Reply to author
Forward
0 new messages