Hi Christophe, Speaking first to your example of invoking GDK classes from Java: Yes, this can be done. Although sometimes it can be less usable (for example if you want to call a method that takes a closure as an argument). I hadn't heard of GroovyScriptLoader, but I've seen the JSR223 and GroovyShell.evaluate() techniques used. But a fair amount of the time it isn't necessary.
To your second point on invoking your own Groovy classes from Java: Yes! Tons of folks have done this. I do find it helpful though to make my Groovy classes a it easier for Java to consume by explicitly typing the entry points Java will access so that you can avoid casting. The only times I'd suggest going the script route (besides for actual scripts) is if there were lots of cumbersome constructs (for example, the closure arg I mentioned, or a DSL) that made it too painful to use Java directly. In general though, you can mix & match pretty painlessly. Groovy's classes are Java bytecode (most of them are even written in Java), and can be invoked from any other language that compiles to Java bytecode, including Java. Oh, and for the future, you may want to address questions like this on the user list ([email protected]). This list is meant for discussing the development of Groovy itself. -Keegan On Wed, Aug 12, 2015 at 11:55 AM, Christophe Henry <[email protected]> wrote: > Hello, > > I was wondering how much Groovy classes could be called from Java. > > I am aware that solutions to call Groovy scripts are available like > GroovyScriptLoader but I was thinking about simpler solutions. > > This afternoon, while working on a classic Maven project, I noticed > something weird : some Groovy classes were in the auto-completion list that > IntelliJ was displaying me. So I decided to do a test and try calling a > Groovy class from Java. > > So I tried the following code : > > ``` > String[] strings = { "Hello world"} ; > System.out.println(new GStringImpl(new Object[0], strings) ; > ``` > > And it worked. So I suppose this is possible, in a way, that, if I wrote a > Groovy class, I could instantiate it and call it's methods from Java code, > right? > -- > > *Cordialement, Christophe Henry* >
