///////////////////////////////////////////////////////////////
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
///////////////////////////////////////////////////////////////

[[lang-groovy, Groovy Scripting Library]]
= Groovy Scripting =

[devstatus]
--------------
source=libraries/lang-groovy/dev-status.xml
--------------

Groovy Scripting Library

include::../../build/docs/buildinfo/artifact.txt[]

The Groovy Scripting Library is a Generic mixin that implements interfaces by delegating to Groovy code from classpath resources.

But before looking at how to use the Scripting library we'll see that *you don't need this library at all if you simply need to use Groovy classes as Fragments*.
Then we'll see how to use Groovy class scripts and method scripts.

All examples below are based on this type:

[snippet,java]
----
source=libraries/lang-groovy/src/test/groovy/org/qi4j/lang/groovy/HelloSpeaker.java
tag=speaker
----


== Using compiled Groovy classes ==

To directly use compiled Groovy classes as Fragments you don't need this library at all.

Having this Groovy class compiled ;

.HelloSpeakerMixin.groovy
[snippet,groovy]
----
source=libraries/lang-groovy/src/test/groovy/org/qi4j/lang/groovy/HelloSpeakerMixin.groovy
tag=direct
----

assemble it directly:

[snippet,java]
----
source=libraries/lang-groovy/src/test/groovy/org/qi4j/lang/groovy/HelloSpeakerTest.java
tag=direct
----


== Using Groovy class scripts ==

Declare a Groovy class in a classpath resource file located in the same package as the implemented type with the name +<type>.groovy+:

.HelloSpeaker.groovy
[snippet,groovy]
----
source=libraries/lang-groovy/src/test/resources/org/qi4j/lang/groovy/HelloSpeaker.groovy
tag=script
----

Then assemble it with the +GroovyMixin+:

[snippet,java]
----
source=libraries/lang-groovy/src/test/groovy/org/qi4j/lang/groovy/HelloSpeakerTest.java
tag=script
----

Groovy class scripts get +@This+ injection in a +This+ property if present.


== Using Groovy method scripts ==

Implement single composites methods, each in a classpath resource file located in the same package as the implemented type with the name +<type>.<method>.groovy+:

.HelloSpeaker.sayHello.groovy
[snippet,groovy]
----
source=libraries/lang-groovy/src/test/resources/org/qi4j/lang/groovy/HelloSpeaker.sayHello.groovy
tag=script
----

Then assemble it with the +GroovyMixin+:

[snippet,java]
----
source=libraries/lang-groovy/src/test/groovy/org/qi4j/lang/groovy/HelloSpeakerTest.java
tag=script
----

Groovy method scripts get +@This+ injection as +This+ property and the methods arguments array as +args+.
