(Quick Reference)

5.8 Artifact API - Reference Documentation

Authors: Andres Almiray

Version: 1.2.0

5.8 Artifact API

The Artifact API provides introspection capabilities on the conventions set on each artifact type. The following sections explain further what you can do with this API.

5.8.1 Evaluating Conventions

Every Griffon application exposes all information about its artifacts and addons via a pair of helper classes
  • AddonManager - used for all installed addons
  • ArtifactManager - used for all remaining artifacts

ArtifactManager

The ArtifactManager class provides methods to evaluate the conventions within the project and internally stores references to all classes within a GriffonApplication using subclasses of GriffonClass class.

A GriffonClass represents a physical Griffon resources such as a controller or a service. For example to get all GriffonClass instances you can call:

app.artifactManager.allClasses.each { println it.name }

There are a few "magic" properties that the ArtifactManager instance possesses that allow you to narrow the type of artifact you are interested in. For example if you only need to deal with controllers you can do:

app.artifactManager.controllerClasses.each { println it.name }

Dynamic method conventions are as follows:

  • get*Classes - Retrieves all the classes for a particular artifact type. Example app.artifactManager.getControllerClasses().
  • *Classes - Retrieves all the classes for a particular artifact type. Example app.artifactManager.controllerClasses.
  • is*Class - Returns true if the given class is of the given artifact type. Example app.artifactManager.isControllerClass(ExampleController)

The GriffonClass interface itself provides a number of useful methods that allow you to further evaluate and work with the conventions. These include:

  • newInstance - Creates a new instance of the enclosed class.
  • getName - Returns the logical name of the class in the application without the trailing convention part if applicable
  • getClazz - Returns the artifact class
  • getType - Returns the type of the artifact, i.e "controller"
  • getTrailing - Returns the suffix (if any) of the artifact, i.e "Controller"

For a full reference refer to the javadoc API.

AddonManager

The AddonManager class is responsible for holding references to all addons (which are of type griffon.core.GriffonAddon), as well as providing metainformation on each addon via an addon descriptor. The latter can be used to know at runtime the name and version of a particular addon, useful for building a dynamic About dialog for example.

All addons have the same behavior which is explained in detail in the Addons section.

5.8.2 Adding Dynamic Methods at Runtime

For Griffon managed classes like controllers, models and so forth you can add methods, constructors etc. using the ExpandoMetaClass mechanism by accessing each controller's MetaClass:

class ExampleAddon {
    def addonPostInit(app) {
        app.artifactManager.controllerClasses.each { controllerClass ->
             controllerClass.metaClass.myNewMethod = {-> println "hello world" }
        }
    }
}

In this case we use the app.artifactManager object to get a reference to all of the controller classes' MetaClass instances and then add a new method called myNewMethod to each controller. Alternatively, if you know before hand the class you wish add a method to you can simple reference that classes metaClass property:

class ExampleAddon {
    def addonPostInit(app) {
        String.metaClass.swapCase = {->
            def sb = new StringBuffer()
            delegate.each {
                sb << (Character.isUpperCase(it as char) ? 
                       Character.toLowerCase(it as char) : 
                       Character.toUpperCase(it as char))
            }
            sb.toString()
        }

assert "UpAndDown" == "uPaNDdOWN".swapCase() } }

In this example we add a new method swapCase to java.lang.String directly by accessing its metaClass.

5.8.3 Artifact Types

All Griffon artifacts share common behavior. This behavior is captured by an interface named griffon.core.GriffonArtifact. Additional interfaces with more explicit behavior exist per each artifact type. The following is a list of the basic types and their corresponding interface

Starting with Griffon 0.9.1 the compiler will make sure that each artifact implements its corresponding interface via AST injection. This feature can be very useful when accessing artifacts from languages other than Groovy (see Dealing with Non-Groovy Artifacts to learn more about this kind of artifacts).

AST injection is always enabled unless you disable it as explained in the Disable AST Injection section.

Additionally to each artifact type you will find a companion GriffonClass that is specialized for each type. These specialized classes can be used to discover metadata about a particular artifact. The following is a list of the companion GriffonClass for each of the basic artifacts found in core

Be aware that additional artifacts provided by plugins (such as Charts and Wizards) may provide their own interface and companion GriffonClass. These too will be available when querying the ArtifactManager.